Skip to content
Myr

Add only missing persistent flags of parents in mergePersistentFlags

Before adoption: this commit came in with the history the project adopted, so no landing records it.

As persistent flags of parents can only be added, we don't need to always check them every time, so make updateParentsPflags return only added flags. Performance improvement: benchmark old ns/op new ns/op delta BenchmarkInheritedFlags-4 5595 4412 -21.14% BenchmarkLocalFlags-4 3235 2667 -17.56% benchmark old allocs new allocs delta BenchmarkInheritedFlags-4 39 24 -38.46% BenchmarkLocalFlags-4 21 15 -28.57% benchmark old bytes new bytes delta BenchmarkInheritedFlags-4 1000 600 -40.00% BenchmarkLocalFlags-4 544 408 -25.00%

authored by Albert Nigmatzianov · committed by Albert Nigmatzianov · parent 6202b59 · Browse files · 2 files+32 added−19 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+32 added−19 removed

  • other:+32 added−19 removed

2 files+32 added−19 removed

modified:cobra_test.go+1 added-1 removed
673 unmodified lines
674 // persistentFlag should act like normal flag on its own command675 fullSetupTest("echo times -s again -c -p test here")676677 if strings.Join(tt, " ") != "test here" {678 t.Errorf("flags didn't leave proper args remaining..%s given", tt)678 t.Errorf("flags didn't leave proper args remaining. %s given", tt)679 }680681 if flags1 != "again" {682 t.Errorf("string flag didn't get correct value, had %v", flags1)
514 unmodified lines
modified:command.go+31 added-18 removed
770 unmodified lines
771// ResetCommands used for testing.772func (c *Command) ResetCommands() {773 c.commands = nil774 c.helpCommand = nil775 c.parentsPflags = nil776}777778// Sorts commands by their names.779type commandSorterByName []*Command
303 unmodified lines
10831084// LocalFlags returns the local FlagSet specifically set in the current command.1085func (c *Command) LocalFlags() *flag.FlagSet {1086 c.mergePersistentFlags()1086 c.updateParentsPersistentFlags()10871088 if c.lflags == nil {1089 c.lflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)1090 c.lflags.SetOutput(c.OutOrStderr())
14 unmodified lines
11051106// InheritedFlags returns all flags which were inherited from parents commands.1107func (c *Command) InheritedFlags() *flag.FlagSet {1108 c.mergePersistentFlags()1109 c.updateParentsPersistentFlags()11091110 if c.iflags == nil {1111 c.iflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)1112 }
111 unmodified lines
1224func (c *Command) Parent() *Command {1225 return c.parent1226}12271228// mergePersistentFlags merges c.PersistentFlags() to c.Flags()1229// and adds missing persistent flags of all parents.1229func (c *Command) updateParentsPersistentFlags() {1230func (c *Command) mergePersistentFlags() {1231 if c.HasPersistentFlags() {1232 c.PersistentFlags().VisitAll(func(f *flag.Flag) {1233 if c.Flags().Lookup(f.Name) == nil {1234 c.Flags().AddFlag(f)1235 }1236 })1237 }12381239 added := c.updateParentsPflags()1240 if len(added) > 0 {1241 for _, f := range added {1242 if c.Flags().Lookup(f.Name) == nil {1243 c.Flags().AddFlag(f)1244 }1245 }1246 }1247}12481249// updateParentsPflags updates c.parentsPflags by adding1250// new persistent flags of all parents and returns added flags.1251// If c.parentsPflags == nil, it makes new.1252//1253// This function must be used ONLY in mergePersistentFlags.1254func (c *Command) updateParentsPflags() (added []*flag.Flag) {1255 if c.parentsPflags == nil {1256 c.parentsPflags = flag.NewFlagSet(c.Name(), flag.ContinueOnError)1257 c.parentsPflags.SetOutput(c.OutOrStderr())1258 c.parentsPflags.SortFlags = false1259 }12601261 c.VisitParents(func(x *Command) {1262 if x.HasPersistentFlags() {1263 x.PersistentFlags().VisitAll(func(f *flag.Flag) {1264 if c.parentsPflags.Lookup(f.Name) == nil {1265 c.parentsPflags.AddFlag(f)1266 added = append(added, f)1267 }1268 })1269 }1270 })1244}12711246func (c *Command) mergePersistentFlags() {1247 flags := c.Flags()12481249 merge := func(x *Command) {1250 if x.HasPersistentFlags() {1251 x.PersistentFlags().VisitAll(func(f *flag.Flag) {1252 if flags.Lookup(f.Name) == nil {1253 flags.AddFlag(f)1254 }1255 })1256 }1257 }1258 merge(c)1259 c.VisitParents(merge)1272 return added1273}