Skip to content
Myr

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

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

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'

authored by Eric Paris · committed by Eric Paris · parent ecc7ea6 · Browse files · 2 files+43 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+43 added−2 removed

  • other:+43 added−2 removed

2 files+43 added−2 removed

modified:bash_completions.go+30 added-2 removed
165 unmodified lines
166 if __contains_word "${flagname}" "${must_have_one_flag[@]}"; then167 must_have_one_flag=()168 fi169170 # if you set a flag which only applies to this command, don't show subcommands171 if __contains_word "${flagname}" "${local_nonpersistent_flags[@]}"; then172 commands=()173 fi174175 # keep flag value with flagname as flaghash176 if [ -n "${flagvalue}" ] ; then177 flaghash[${flagname}]=${flagvalue}178 elif [ -n "${words[ $((c+1)) ]}" ] ; then
88 unmodified lines
267268 local c=0269 local flags=()270 local two_word_flags=()271 local local_nonpersistent_flags=()272 local flags_with_completion=()273 local flags_completion=()274 local commands=("%s")275 local must_have_one_flag=()
89 unmodified lines
365 return nil366}367368func writeShortFlag(flag *pflag.Flag, w io.Writer) error {363 b := (flag.Value.Type() == "bool")369 b := (len(flag.NoOptDefVal) > 0)370 name := flag.Shorthand371 format := " "372 if !b {373 format += "two_word_"
5 unmodified lines
379 return writeFlagHandler("-"+name, flag.Annotations, w)380}381382func writeFlag(flag *pflag.Flag, w io.Writer) error {377 b := (flag.Value.Type() == "bool")383 b := (len(flag.NoOptDefVal) > 0)384 name := flag.Name385 format := " flags+=(\"--%s"386 if !b {387 format += "="
4 unmodified lines
392 }393 return writeFlagHandler("--"+name, flag.Annotations, w)394}395396func writeLocalNonPersistentFlag(flag *pflag.Flag, w io.Writer) error {397 b := (len(flag.NoOptDefVal) > 0)398 name := flag.Name399 format := " local_nonpersistent_flags+=(\"--%s"400 if !b {401 format += "="402 }403 format += "\")\n"404 if _, err := fmt.Fprintf(w, format, name); err != nil {405 return err406 }407 return nil408}409410func writeFlags(cmd *Command, w io.Writer) error {411 _, err := fmt.Fprintf(w, ` flags=()412 two_word_flags=()413 local_nonpersistent_flags=()414 flags_with_completion=()415 flags_completion=()416417`)418 if err != nil {419 return err420 }421 localNonPersistentFlags := cmd.LocalNonPersistentFlags()422 var visitErr error423 cmd.NonInheritedFlags().VisitAll(func(flag *pflag.Flag) {424 if err := writeFlag(flag, w); err != nil {425 visitErr = err
4 unmodified lines
430 visitErr = err431 return432 }433 }434 if localNonPersistentFlags.Lookup(flag.Name) != nil {435 if err := writeLocalNonPersistentFlag(flag, w); err != nil {436 visitErr = err437 return438 }439 }440 })441 if visitErr != nil {442 return visitErr443 }
187 unmodified lines
modified:command.go+13 added
1023 unmodified lines
1024 }1025 return c.flags1026}10271028// LocalNonPersistentFlags are flags specific to this command which will NOT persist to subcommands1029func (c *Command) LocalNonPersistentFlags() *flag.FlagSet {1030 persistentFlags := c.PersistentFlags()10311032 out := flag.NewFlagSet(c.Name(), flag.ContinueOnError)1033 c.LocalFlags().VisitAll(func(f *flag.Flag) {1034 if persistentFlags.Lookup(f.Name) == nil {1035 out.AddFlag(f)1036 }1037 })1038 return out1039}10401041// Get the local FlagSet specifically set in the current command1042func (c *Command) LocalFlags() *flag.FlagSet {1043 c.mergePersistentFlags()1044
186 unmodified lines