Improve consistency of flags when using SetGlobalNormalizationFunc (#522)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Fix #521
authored by Diego Becciolini · committed by Albert Nigmatzianov · parent e5f66de · Browse files · 2 files·+88 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+88 added−2 removed
- other:+88 added−2 removed
Provenance
- Commit
0dacccf- Tree
3b4b039- Author
Diego Becciolini <diego.becciolini@gmail.com>- Committer
Albert Nigmatzianov <albertnigma@gmail.com>- Parent
e5f66de
2 files changed+88 added−2 removed
modified:cobra_test.go+72 added-2 removed
188 unmodified lines189 cmdTimes.Flags().BoolVarP(&flagb2, "booltwo", "c", false, "help message for flag booltwo")190 cmdTimes.Flags().IntVarP(&flagi2, "inttwo", "j", 234, "help message for flag inttwo")191 cmdTimes.Flags().StringVarP(&flags2b, "strtwo", "t", "2", strtwoChildHelp)192 cmdTimes.PersistentFlags().StringVarP(&flags2b, "strtwo", "t", "2", strtwoChildHelp)193+ cmdTimes.LocalFlags() // populate lflags before parent is set194 cmdPrint.Flags().BoolVarP(&flagb3, "boolthree", "b", true, "help message for flag boolthree")195 cmdPrint.PersistentFlags().StringVarP(&flags3, "strthree", "s", "three", "help message for flag strthree")196 }197 12 unmodified lines210 tt, tp, te = nil, nil, nil211 rootPersPre, echoPre, echoPersPre, timesPersPre = nil, nil, nil, nil212 213 var c = cmdRootNoRun213- flagInit()214 commandInit()215+ flagInit()216 return c217 }218 219 func initializeWithSameName() *Command {220 tt, tp, te = nil, nil, nil221 rootPersPre, echoPre, echoPersPre, timesPersPre = nil, nil, nil, nil222 var c = cmdRootSameName222- flagInit()223 commandInit()224+ flagInit()225 return c226 }227 228 func initializeWithRootCmd() *Command {681 unmodified lines910 911 func TestFlagAccess(t *testing.T) {912 initialize()913 914+ cmdEcho.AddCommand(cmdTimes)915 local := cmdTimes.LocalFlags()916 inherited := cmdTimes.InheritedFlags()917 918 for _, f := range []string{"inttwo", "strtwo", "booltwo"} {247 unmodified lines1166 return pflag.NormalizedName(name)1167 }1168 1169 rootCmd := initialize()1170+ rootCmd.AddCommand(cmdEcho)1171+1172 rootCmd.SetGlobalNormalizationFunc(normFunc)1173 if reflect.ValueOf(normFunc).Pointer() != reflect.ValueOf(rootCmd.GlobalNormalizationFunc()).Pointer() {1174 t.Error("rootCmd seems to have a wrong normalization function")1175 }1176 1177+ // Also check it propagates retroactively1178+ if reflect.ValueOf(normFunc).Pointer() != reflect.ValueOf(cmdEcho.GlobalNormalizationFunc()).Pointer() {1179+ t.Error("cmdEcho should have had the normalization function of rootCmd")1180+ }1181+1182 // First add the cmdEchoSub to cmdPrint1183 cmdPrint.AddCommand(cmdEchoSub)1184 if cmdPrint.GlobalNormalizationFunc() != nil && cmdEchoSub.GlobalNormalizationFunc() != nil {1185 t.Error("cmdPrint and cmdEchoSub should had no normalization functions")6 unmodified lines1192 t.Error("cmdPrint and cmdEchoSub should had the normalization function of rootCmd")1193 }1194 }1195 1196+func TestNormPassedOnLocal(t *testing.T) {1197+ n := func(f *pflag.FlagSet, name string) pflag.NormalizedName {1198+ return pflag.NormalizedName(strings.ToUpper(name))1199+ }1200+1201+ cmd := &Command{}1202+ flagVal := false1203+1204+ cmd.Flags().BoolVar(&flagVal, "flagname", true, "this is a dummy flag")1205+ cmd.SetGlobalNormalizationFunc(n)1206+ if cmd.LocalFlags().Lookup("flagname") != cmd.LocalFlags().Lookup("FLAGNAME") {1207+ t.Error("Normalization function should be passed on to Local flag set")1208+ }1209+}1210+1211+func TestNormPassedOnInherited(t *testing.T) {1212+ n := func(f *pflag.FlagSet, name string) pflag.NormalizedName {1213+ return pflag.NormalizedName(strings.ToUpper(name))1214+ }1215+1216+ cmd, childBefore, childAfter := &Command{}, &Command{}, &Command{}1217+ flagVal := false1218+ cmd.AddCommand(childBefore)1219+1220+ cmd.PersistentFlags().BoolVar(&flagVal, "flagname", true, "this is a dummy flag")1221+ cmd.SetGlobalNormalizationFunc(n)1222+1223+ cmd.AddCommand(childAfter)1224+1225+ if f := childBefore.InheritedFlags(); f.Lookup("flagname") == nil || f.Lookup("flagname") != f.Lookup("FLAGNAME") {1226+ t.Error("Normalization function should be passed on to inherited flag set in command added before flag")1227+ }1228+ if f := childAfter.InheritedFlags(); f.Lookup("flagname") == nil || f.Lookup("flagname") != f.Lookup("FLAGNAME") {1229+ t.Error("Normalization function should be passed on to inherited flag set in command added after flag")1230+ }1231+}1232+1233+// Related to https://github.com/spf13/cobra/issues/521.1234+func TestNormConsistent(t *testing.T) {1235+ n := func(f *pflag.FlagSet, name string) pflag.NormalizedName {1236+ return pflag.NormalizedName(strings.ToUpper(name))1237+ }1238+ id := func(f *pflag.FlagSet, name string) pflag.NormalizedName {1239+ return pflag.NormalizedName(name)1240+ }1241+1242+ cmd := &Command{}1243+ flagVal := false1244+1245+ cmd.Flags().BoolVar(&flagVal, "flagname", true, "this is a dummy flag")1246+ // Build local flag set1247+ cmd.LocalFlags()1248+1249+ cmd.SetGlobalNormalizationFunc(n)1250+ cmd.SetGlobalNormalizationFunc(id)1251+1252+ if cmd.LocalFlags().Lookup("flagname") == cmd.LocalFlags().Lookup("FLAGNAME") {1253+ t.Error("Normalizing flag names should not result in duplicate flags")1254+ }1255+}1256+1257 func TestFlagOnPflagCommandLine(t *testing.T) {1258 flagName := "flagOnCommandLine"1259 pflag.String(flagName, "", "about my flag")1260 r := fullSetupTest("--help")54 unmodified linesmodified:command.go+16 added
804 unmodified lines805 }806 807 // ResetCommands used for testing.808 func (c *Command) ResetCommands() {809+ c.parent = nil810 c.commands = nil811 c.helpCommand = nil812 c.parentsPflags = nil813 }318 unmodified lines1132 }1133 c.lflags.SetOutput(c.flagErrorBuf)1134 }1135 c.lflags.SortFlags = c.Flags().SortFlags1136+ if c.globNormFunc != nil {1137+ c.lflags.SetNormalizeFunc(c.globNormFunc)1138+ }1139 1140 addToLocal := func(f *flag.Flag) {1141 if c.lflags.Lookup(f.Name) == nil && c.parentsPflags.Lookup(f.Name) == nil {1142 c.lflags.AddFlag(f)16 unmodified lines1159 c.iflags.SetOutput(c.flagErrorBuf)1160 }1161 1162 local := c.LocalFlags()1163+ if c.globNormFunc != nil {1164+ c.iflags.SetNormalizeFunc(c.globNormFunc)1165+ }1166+1167 c.parentsPflags.VisitAll(func(f *flag.Flag) {1168 if c.iflags.Lookup(f.Name) == nil && local.Lookup(f.Name) == nil {1169 c.iflags.AddFlag(f)1170 }25 unmodified lines1196 c.flags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)1197 c.flags.SetOutput(c.flagErrorBuf)1198 c.pflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)1199 c.pflags.SetOutput(c.flagErrorBuf)1200+1201+ c.lflags = nil1202+ c.iflags = nil1203+ c.parentsPflags = nil1204 }1205 1206 // HasFlags checks if the command contains any flags (local plus persistent from the entire structure).1207 func (c *Command) HasFlags() bool {101 unmodified lines1309 c.parentsPflags.SetOutput(c.flagErrorBuf)1310 c.parentsPflags.SortFlags = false1311 }1312 1313+ if c.globNormFunc != nil {1314+ c.parentsPflags.SetNormalizeFunc(c.globNormFunc)1315+ }1316+1317 c.Root().PersistentFlags().AddFlagSet(flag.CommandLine)1318 1319 c.VisitParents(func(parent *Command) {1320 c.parentsPflags.AddFlagSet(parent.PersistentFlags())2 unmodified lines