Do not show subcommands in bash completion if a local flag was specified

If a user specifies a flag to a command which doesn't make sense to a
subcommand do not show subcommands as a suggestion.

This also changes things to show both 'required flags' and 'commands'
instead of only 'required flags'
This commit is contained in:
Eric Paris
2016-06-03 12:37:40 -04:00
parent ecc7ea6969
commit 7bf964e5b6
2 changed files with 43 additions and 2 deletions

View File

@ -1025,6 +1025,19 @@ func (c *Command) Flags() *flag.FlagSet {
return c.flags
}
// LocalNonPersistentFlags are flags specific to this command which will NOT persist to subcommands
func (c *Command) LocalNonPersistentFlags() *flag.FlagSet {
persistentFlags := c.PersistentFlags()
out := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
c.LocalFlags().VisitAll(func(f *flag.Flag) {
if persistentFlags.Lookup(f.Name) == nil {
out.AddFlag(f)
}
})
return out
}
// Get the local FlagSet specifically set in the current command
func (c *Command) LocalFlags() *flag.FlagSet {
c.mergePersistentFlags()