Merge pull request #288 from eparis/flags-vs-commands
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Do not display bash commands if local flag specified
authored by Eric Paris · committed by Eric Paris · parents f447048, 7bf964e · Browse files · 3 files·+60 added−7 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+60 added−7 removed
- other:+60 added−7 removed
Provenance
- Commit
c21ee9d- Tree
4768eb8- Author
Eric Paris <eparis@redhat.com>- Committer
Eric Paris <eparis@redhat.com>- Parent 1
f447048- Parent 2
7bf964e
3 files changed+60 added−7 removed
modified:.gitignore+12 added
17 unmodified lines18 _cgo_export.*19 20 _testmain.go21 22+# Vim files https://github.com/github/gitignore/blob/master/Global/Vim.gitignore23+# swap24+[._]*.s[a-w][a-z]25+[._]s[a-w][a-z]26+# session27+Session.vim28+# temporary29+.netrwhist30+*~31+# auto-generated tag files32+tags33+34 *.exe35 36 cobra.testmodified:bash_completions.go+35 added-7 removed
114 unmodified lines115 return116 fi117 118 local completions119- if [[ ${#must_have_one_flag[@]} -ne 0 ]]; then120- completions=("${must_have_one_flag[@]}")121- elif [[ ${#must_have_one_noun[@]} -ne 0 ]]; then119+ completions=("${commands[@]}")120+ if [[ ${#must_have_one_noun[@]} -ne 0 ]]; then121 completions=("${must_have_one_noun[@]}")123- else124- completions=("${commands[@]}")122 fi123+ if [[ ${#must_have_one_flag[@]} -ne 0 ]]; then124+ completions+=("${must_have_one_flag[@]}")125+ fi126 COMPREPLY=( $(compgen -W "${completions[*]}" -- "$cur") )127 128 if [[ ${#COMPREPLY[@]} -eq 0 && ${#noun_aliases[@]} -gt 0 && ${#must_have_one_noun[@]} -ne 0 ]]; then129 COMPREPLY=( $(compgen -W "${noun_aliases[*]}" -- "$cur") )36 unmodified lines166 if __contains_word "${flagname}" "${must_have_one_flag[@]}"; then167 must_have_one_flag=()168 fi169 170+ # 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+ fi174+175 # keep flag value with flagname as flaghash176 if [ -n "${flagvalue}" ] ; then177 flaghash[${flagname}]=${flagvalue}178 elif [ -n "${words[ $((c+1)) ]}" ] ; then88 unmodified lines267 268 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 lines365 return nil366 }367 368 func 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 lines379 return writeFlagHandler("-"+name, flag.Annotations, w)380 }381 382 func 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 lines392 }393 return writeFlagHandler("--"+name, flag.Annotations, w)394 }395 396+func 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+}409+410 func 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=()416 417 `)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 = err4 unmodified lines430 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 linesmodified:command.go+13 added
1030 unmodified lines1031 }1032 return c.flags1033 }1034 1035+// LocalNonPersistentFlags are flags specific to this command which will NOT persist to subcommands1036+func (c *Command) LocalNonPersistentFlags() *flag.FlagSet {1037+ persistentFlags := c.PersistentFlags()1038+1039+ out := flag.NewFlagSet(c.Name(), flag.ContinueOnError)1040+ c.LocalFlags().VisitAll(func(f *flag.Flag) {1041+ if persistentFlags.Lookup(f.Name) == nil {1042+ out.AddFlag(f)1043+ }1044+ })1045+ return out1046+}1047+1048 // Get the local FlagSet specifically set in the current command1049 func (c *Command) LocalFlags() *flag.FlagSet {1050 c.mergePersistentFlags()1051 189 unmodified lines