Skip to content
Myr

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

2 files+88 added−2 removed

modified:cobra_test.go+72 added-2 removed
188 unmodified lines
189 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 lines
210 tt, tp, te = nil, nil, nil211 rootPersPre, echoPre, echoPersPre, timesPersPre = nil, nil, nil, nil212213 var c = cmdRootNoRun213 flagInit()214 commandInit()215 flagInit()216 return c217}218219func 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}227228func initializeWithRootCmd() *Command {
681 unmodified lines
910911func TestFlagAccess(t *testing.T) {912 initialize()913914 cmdEcho.AddCommand(cmdTimes)915 local := cmdTimes.LocalFlags()916 inherited := cmdTimes.InheritedFlags()917918 for _, f := range []string{"inttwo", "strtwo", "booltwo"} {
247 unmodified lines
1166 return pflag.NormalizedName(name)1167 }11681169 rootCmd := initialize()1170 rootCmd.AddCommand(cmdEcho)11711172 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 }11761177 // 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 }11811182 // 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 lines
1192 t.Error("cmdPrint and cmdEchoSub should had the normalization function of rootCmd")1193 }1194}11951196func TestNormPassedOnLocal(t *testing.T) {1197 n := func(f *pflag.FlagSet, name string) pflag.NormalizedName {1198 return pflag.NormalizedName(strings.ToUpper(name))1199 }12001201 cmd := &Command{}1202 flagVal := false12031204 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}12101211func TestNormPassedOnInherited(t *testing.T) {1212 n := func(f *pflag.FlagSet, name string) pflag.NormalizedName {1213 return pflag.NormalizedName(strings.ToUpper(name))1214 }12151216 cmd, childBefore, childAfter := &Command{}, &Command{}, &Command{}1217 flagVal := false1218 cmd.AddCommand(childBefore)12191220 cmd.PersistentFlags().BoolVar(&flagVal, "flagname", true, "this is a dummy flag")1221 cmd.SetGlobalNormalizationFunc(n)12221223 cmd.AddCommand(childAfter)12241225 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}12321233// Related to https://github.com/spf13/cobra/issues/521.1234func 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 }12411242 cmd := &Command{}1243 flagVal := false12441245 cmd.Flags().BoolVar(&flagVal, "flagname", true, "this is a dummy flag")1246 // Build local flag set1247 cmd.LocalFlags()12481249 cmd.SetGlobalNormalizationFunc(n)1250 cmd.SetGlobalNormalizationFunc(id)12511252 if cmd.LocalFlags().Lookup("flagname") == cmd.LocalFlags().Lookup("FLAGNAME") {1253 t.Error("Normalizing flag names should not result in duplicate flags")1254 }1255}12561257func TestFlagOnPflagCommandLine(t *testing.T) {1258 flagName := "flagOnCommandLine"1259 pflag.String(flagName, "", "about my flag")1260 r := fullSetupTest("--help")
54 unmodified lines
modified:command.go+16 added
804 unmodified lines
805}806807// ResetCommands used for testing.808func (c *Command) ResetCommands() {809 c.parent = nil810 c.commands = nil811 c.helpCommand = nil812 c.parentsPflags = nil813}
318 unmodified lines
1132 }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 }11391140 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 lines
1159 c.iflags.SetOutput(c.flagErrorBuf)1160 }11611162 local := c.LocalFlags()1163 if c.globNormFunc != nil {1164 c.iflags.SetNormalizeFunc(c.globNormFunc)1165 }11661167 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 lines
1196 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)12001201 c.lflags = nil1202 c.iflags = nil1203 c.parentsPflags = nil1204}12051206// HasFlags checks if the command contains any flags (local plus persistent from the entire structure).1207func (c *Command) HasFlags() bool {
101 unmodified lines
1309 c.parentsPflags.SetOutput(c.flagErrorBuf)1310 c.parentsPflags.SortFlags = false1311 }13121313 if c.globNormFunc != nil {1314 c.parentsPflags.SetNormalizeFunc(c.globNormFunc)1315 }13161317 c.Root().PersistentFlags().AddFlagSet(flag.CommandLine)13181319 c.VisitParents(func(parent *Command) {1320 c.parentsPflags.AddFlagSet(parent.PersistentFlags())
2 unmodified lines