Skip to content
Myr

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

3 files+60 added−7 removed

modified:.gitignore+12 added
17 unmodified lines
18_cgo_export.*1920_testmain.go2122# 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# session27Session.vim28# temporary29.netrwhist30*~31# auto-generated tag files32tags3334*.exe3536cobra.test
modified:bash_completions.go+35 added-7 removed
114 unmodified lines
115 return116 fi117118 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") )127128 if [[ ${#COMPREPLY[@]} -eq 0 && ${#noun_aliases[@]} -gt 0 && ${#must_have_one_noun[@]} -ne 0 ]]; then129 COMPREPLY=( $(compgen -W "${noun_aliases[*]}" -- "$cur") )
36 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
1030 unmodified lines
1031 }1032 return c.flags1033}10341035// LocalNonPersistentFlags are flags specific to this command which will NOT persist to subcommands1036func (c *Command) LocalNonPersistentFlags() *flag.FlagSet {1037 persistentFlags := c.PersistentFlags()10381039 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}10471048// Get the local FlagSet specifically set in the current command1049func (c *Command) LocalFlags() *flag.FlagSet {1050 c.mergePersistentFlags()1051
189 unmodified lines