11 "github.com/spf13/pflag"
14 // Annotations for Bash completion.
16 BashCompFilenameExt = "cobra_annotation_bash_completion_filename_extensions"
17 BashCompCustom = "cobra_annotation_bash_completion_custom"
18 BashCompOneRequiredFlag = "cobra_annotation_bash_completion_one_required_flag"
19 BashCompSubdirsInDir = "cobra_annotation_bash_completion_subdirs_in_dir"
22 func writePreamble(buf *bytes.Buffer, name string) {
23 buf.WriteString(fmt.Sprintf("# bash completion for %-36s -*- shell-script -*-\n", name))
27 if [[ -n ${BASH_COMP_DEBUG_FILE} ]]; then
28 echo "$*" >> "${BASH_COMP_DEBUG_FILE}"
32 # Homebrew on Macs have version 1.3 of bash-completion which doesn't include
33 # _init_completion. This is a very minimal version of that function.
34 __my_init_completion()
37 _get_comp_words_by_ref "$@" cur prev words cword
46 [[ $w = "$word" ]] && return
54 local w word=$1; shift
56 [[ $w = "$word" ]] && return
63 __debug "${FUNCNAME[0]}"
66 if [[ $(type -t compopt) = "builtin" ]]; then
70 if [ ${#must_have_one_flag[@]} -ne 0 ]; then
71 allflags=("${must_have_one_flag[@]}")
73 allflags=("${flags[*]} ${two_word_flags[*]}")
75 COMPREPLY=( $(compgen -W "${allflags[*]}" -- "$cur") )
76 if [[ $(type -t compopt) = "builtin" ]]; then
77 [[ "${COMPREPLY[0]}" == *= ]] || compopt +o nospace
80 # complete after --flag=abc
81 if [[ $cur == *=* ]]; then
82 if [[ $(type -t compopt) = "builtin" ]]; then
88 __index_of_word "${flag}" "${flags_with_completion[@]}"
90 if [[ ${index} -ge 0 ]]; then
93 ${flags_completion[${index}]}
94 if [ -n "${ZSH_VERSION}" ]; then
95 # zsh completion needs --flag= prefix
96 eval "COMPREPLY=( \"\${COMPREPLY[@]/#/${flag}=}\" )"
104 # check if we are handling a flag with special work handling
106 __index_of_word "${prev}" "${flags_with_completion[@]}"
107 if [[ ${index} -ge 0 ]]; then
108 ${flags_completion[${index}]}
112 # we are parsing a flag and don't have a special handler, no completion
113 if [[ ${cur} != "${words[cword]}" ]]; then
118 completions=("${commands[@]}")
119 if [[ ${#must_have_one_noun[@]} -ne 0 ]]; then
120 completions=("${must_have_one_noun[@]}")
122 if [[ ${#must_have_one_flag[@]} -ne 0 ]]; then
123 completions+=("${must_have_one_flag[@]}")
125 COMPREPLY=( $(compgen -W "${completions[*]}" -- "$cur") )
127 if [[ ${#COMPREPLY[@]} -eq 0 && ${#noun_aliases[@]} -gt 0 && ${#must_have_one_noun[@]} -ne 0 ]]; then
128 COMPREPLY=( $(compgen -W "${noun_aliases[*]}" -- "$cur") )
131 if [[ ${#COMPREPLY[@]} -eq 0 ]]; then
132 declare -F __custom_func >/dev/null && __custom_func
135 # available in bash-completion >= 2, not always present on macOS
136 if declare -F __ltrim_colon_completions >/dev/null; then
137 __ltrim_colon_completions "$cur"
141 # The arguments should be in the form "ext1|ext2|extn"
142 __handle_filename_extension_flag()
148 __handle_subdirs_in_dir_flag()
151 pushd "${dir}" >/dev/null 2>&1 && _filedir -d && popd >/dev/null 2>&1
156 __debug "${FUNCNAME[0]}: c is $c words[c] is ${words[c]}"
158 # if a command required a flag, and we found it, unset must_have_one_flag()
159 local flagname=${words[c]}
161 # if the word contained an =
162 if [[ ${words[c]} == *"="* ]]; then
163 flagvalue=${flagname#*=} # take in as flagvalue after the =
164 flagname=${flagname%%=*} # strip everything after the =
165 flagname="${flagname}=" # but put the = back
167 __debug "${FUNCNAME[0]}: looking for ${flagname}"
168 if __contains_word "${flagname}" "${must_have_one_flag[@]}"; then
169 must_have_one_flag=()
172 # if you set a flag which only applies to this command, don't show subcommands
173 if __contains_word "${flagname}" "${local_nonpersistent_flags[@]}"; then
177 # keep flag value with flagname as flaghash
178 if [ -n "${flagvalue}" ] ; then
179 flaghash[${flagname}]=${flagvalue}
180 elif [ -n "${words[ $((c+1)) ]}" ] ; then
181 flaghash[${flagname}]=${words[ $((c+1)) ]}
183 flaghash[${flagname}]="true" # pad "true" for bool flag
186 # skip the argument to a two word flag
187 if __contains_word "${words[c]}" "${two_word_flags[@]}"; then
189 # if we are looking for a flags value, don't show commands
190 if [[ $c -eq $cword ]]; then
201 __debug "${FUNCNAME[0]}: c is $c words[c] is ${words[c]}"
203 if __contains_word "${words[c]}" "${must_have_one_noun[@]}"; then
204 must_have_one_noun=()
205 elif __contains_word "${words[c]}" "${noun_aliases[@]}"; then
206 must_have_one_noun=()
209 nouns+=("${words[c]}")
215 __debug "${FUNCNAME[0]}: c is $c words[c] is ${words[c]}"
218 if [[ -n ${last_command} ]]; then
219 next_command="_${last_command}_${words[c]//:/__}"
221 if [[ $c -eq 0 ]]; then
222 next_command="_$(basename "${words[c]//:/__}")"
224 next_command="_${words[c]//:/__}"
228 __debug "${FUNCNAME[0]}: looking for ${next_command}"
229 declare -F "$next_command" >/dev/null && $next_command
234 if [[ $c -ge $cword ]]; then
238 __debug "${FUNCNAME[0]}: c is $c words[c] is ${words[c]}"
239 if [[ "${words[c]}" == -* ]]; then
241 elif __contains_word "${words[c]}" "${commands[@]}"; then
243 elif [[ $c -eq 0 ]] && __contains_word "$(basename "${words[c]}")" "${commands[@]}"; then
254 func writePostscript(buf *bytes.Buffer, name string) {
255 name = strings.Replace(name, ":", "__", -1)
256 buf.WriteString(fmt.Sprintf("__start_%s()\n", name))
257 buf.WriteString(fmt.Sprintf(`{
258 local cur prev words cword
259 declare -A flaghash 2>/dev/null || :
260 if declare -F _init_completion >/dev/null 2>&1; then
261 _init_completion -s || return
263 __my_init_completion -n "=" || return
268 local two_word_flags=()
269 local local_nonpersistent_flags=()
270 local flags_with_completion=()
271 local flags_completion=()
272 local commands=("%s")
273 local must_have_one_flag=()
274 local must_have_one_noun=()
282 buf.WriteString(fmt.Sprintf(`if [[ $(type -t compopt) = "builtin" ]]; then
283 complete -o default -F __start_%s %s
285 complete -o default -o nospace -F __start_%s %s
288 `, name, name, name, name))
289 buf.WriteString("# ex: ts=4 sw=4 et filetype=sh\n")
292 func writeCommands(buf *bytes.Buffer, cmd *Command) {
293 buf.WriteString(" commands=()\n")
294 for _, c := range cmd.Commands() {
295 if !c.IsAvailableCommand() || c == cmd.helpCommand {
298 buf.WriteString(fmt.Sprintf(" commands+=(%q)\n", c.Name()))
300 buf.WriteString("\n")
303 func writeFlagHandler(buf *bytes.Buffer, name string, annotations map[string][]string) {
304 for key, value := range annotations {
306 case BashCompFilenameExt:
307 buf.WriteString(fmt.Sprintf(" flags_with_completion+=(%q)\n", name))
311 ext = "__handle_filename_extension_flag " + strings.Join(value, "|")
315 buf.WriteString(fmt.Sprintf(" flags_completion+=(%q)\n", ext))
317 buf.WriteString(fmt.Sprintf(" flags_with_completion+=(%q)\n", name))
319 handlers := strings.Join(value, "; ")
320 buf.WriteString(fmt.Sprintf(" flags_completion+=(%q)\n", handlers))
322 buf.WriteString(" flags_completion+=(:)\n")
324 case BashCompSubdirsInDir:
325 buf.WriteString(fmt.Sprintf(" flags_with_completion+=(%q)\n", name))
329 ext = "__handle_subdirs_in_dir_flag " + value[0]
333 buf.WriteString(fmt.Sprintf(" flags_completion+=(%q)\n", ext))
338 func writeShortFlag(buf *bytes.Buffer, flag *pflag.Flag) {
339 name := flag.Shorthand
341 if len(flag.NoOptDefVal) == 0 {
342 format += "two_word_"
344 format += "flags+=(\"-%s\")\n"
345 buf.WriteString(fmt.Sprintf(format, name))
346 writeFlagHandler(buf, "-"+name, flag.Annotations)
349 func writeFlag(buf *bytes.Buffer, flag *pflag.Flag) {
351 format := " flags+=(\"--%s"
352 if len(flag.NoOptDefVal) == 0 {
356 buf.WriteString(fmt.Sprintf(format, name))
357 writeFlagHandler(buf, "--"+name, flag.Annotations)
360 func writeLocalNonPersistentFlag(buf *bytes.Buffer, flag *pflag.Flag) {
362 format := " local_nonpersistent_flags+=(\"--%s"
363 if len(flag.NoOptDefVal) == 0 {
367 buf.WriteString(fmt.Sprintf(format, name))
370 func writeFlags(buf *bytes.Buffer, cmd *Command) {
371 buf.WriteString(` flags=()
373 local_nonpersistent_flags=()
374 flags_with_completion=()
378 localNonPersistentFlags := cmd.LocalNonPersistentFlags()
379 cmd.NonInheritedFlags().VisitAll(func(flag *pflag.Flag) {
380 if nonCompletableFlag(flag) {
384 if len(flag.Shorthand) > 0 {
385 writeShortFlag(buf, flag)
387 if localNonPersistentFlags.Lookup(flag.Name) != nil {
388 writeLocalNonPersistentFlag(buf, flag)
391 cmd.InheritedFlags().VisitAll(func(flag *pflag.Flag) {
392 if nonCompletableFlag(flag) {
396 if len(flag.Shorthand) > 0 {
397 writeShortFlag(buf, flag)
401 buf.WriteString("\n")
404 func writeRequiredFlag(buf *bytes.Buffer, cmd *Command) {
405 buf.WriteString(" must_have_one_flag=()\n")
406 flags := cmd.NonInheritedFlags()
407 flags.VisitAll(func(flag *pflag.Flag) {
408 if nonCompletableFlag(flag) {
411 for key := range flag.Annotations {
413 case BashCompOneRequiredFlag:
414 format := " must_have_one_flag+=(\"--%s"
415 if flag.Value.Type() != "bool" {
419 buf.WriteString(fmt.Sprintf(format, flag.Name))
421 if len(flag.Shorthand) > 0 {
422 buf.WriteString(fmt.Sprintf(" must_have_one_flag+=(\"-%s\")\n", flag.Shorthand))
429 func writeRequiredNouns(buf *bytes.Buffer, cmd *Command) {
430 buf.WriteString(" must_have_one_noun=()\n")
431 sort.Sort(sort.StringSlice(cmd.ValidArgs))
432 for _, value := range cmd.ValidArgs {
433 buf.WriteString(fmt.Sprintf(" must_have_one_noun+=(%q)\n", value))
437 func writeArgAliases(buf *bytes.Buffer, cmd *Command) {
438 buf.WriteString(" noun_aliases=()\n")
439 sort.Sort(sort.StringSlice(cmd.ArgAliases))
440 for _, value := range cmd.ArgAliases {
441 buf.WriteString(fmt.Sprintf(" noun_aliases+=(%q)\n", value))
445 func gen(buf *bytes.Buffer, cmd *Command) {
446 for _, c := range cmd.Commands() {
447 if !c.IsAvailableCommand() || c == cmd.helpCommand {
452 commandName := cmd.CommandPath()
453 commandName = strings.Replace(commandName, " ", "_", -1)
454 commandName = strings.Replace(commandName, ":", "__", -1)
455 buf.WriteString(fmt.Sprintf("_%s()\n{\n", commandName))
456 buf.WriteString(fmt.Sprintf(" last_command=%q\n", commandName))
457 writeCommands(buf, cmd)
459 writeRequiredFlag(buf, cmd)
460 writeRequiredNouns(buf, cmd)
461 writeArgAliases(buf, cmd)
462 buf.WriteString("}\n\n")
465 // GenBashCompletion generates bash completion file and writes to the passed writer.
466 func (c *Command) GenBashCompletion(w io.Writer) error {
467 buf := new(bytes.Buffer)
468 writePreamble(buf, c.Name())
469 if len(c.BashCompletionFunction) > 0 {
470 buf.WriteString(c.BashCompletionFunction + "\n")
473 writePostscript(buf, c.Name())
475 _, err := buf.WriteTo(w)
479 func nonCompletableFlag(flag *pflag.Flag) bool {
480 return flag.Hidden || len(flag.Deprecated) > 0
483 // GenBashCompletionFile generates bash completion file.
484 func (c *Command) GenBashCompletionFile(filename string) error {
485 outFile, err := os.Create(filename)
489 defer outFile.Close()
491 return c.GenBashCompletion(outFile)
494 // MarkFlagRequired adds the BashCompOneRequiredFlag annotation to the named flag, if it exists.
495 func (c *Command) MarkFlagRequired(name string) error {
496 return MarkFlagRequired(c.Flags(), name)
499 // MarkPersistentFlagRequired adds the BashCompOneRequiredFlag annotation to the named persistent flag, if it exists.
500 func (c *Command) MarkPersistentFlagRequired(name string) error {
501 return MarkFlagRequired(c.PersistentFlags(), name)
504 // MarkFlagRequired adds the BashCompOneRequiredFlag annotation to the named flag in the flag set, if it exists.
505 func MarkFlagRequired(flags *pflag.FlagSet, name string) error {
506 return flags.SetAnnotation(name, BashCompOneRequiredFlag, []string{"true"})
509 // MarkFlagFilename adds the BashCompFilenameExt annotation to the named flag, if it exists.
510 // Generated bash autocompletion will select filenames for the flag, limiting to named extensions if provided.
511 func (c *Command) MarkFlagFilename(name string, extensions ...string) error {
512 return MarkFlagFilename(c.Flags(), name, extensions...)
515 // MarkFlagCustom adds the BashCompCustom annotation to the named flag, if it exists.
516 // Generated bash autocompletion will call the bash function f for the flag.
517 func (c *Command) MarkFlagCustom(name string, f string) error {
518 return MarkFlagCustom(c.Flags(), name, f)
521 // MarkPersistentFlagFilename adds the BashCompFilenameExt annotation to the named persistent flag, if it exists.
522 // Generated bash autocompletion will select filenames for the flag, limiting to named extensions if provided.
523 func (c *Command) MarkPersistentFlagFilename(name string, extensions ...string) error {
524 return MarkFlagFilename(c.PersistentFlags(), name, extensions...)
527 // MarkFlagFilename adds the BashCompFilenameExt annotation to the named flag in the flag set, if it exists.
528 // Generated bash autocompletion will select filenames for the flag, limiting to named extensions if provided.
529 func MarkFlagFilename(flags *pflag.FlagSet, name string, extensions ...string) error {
530 return flags.SetAnnotation(name, BashCompFilenameExt, extensions)
533 // MarkFlagCustom adds the BashCompCustom annotation to the named flag in the flag set, if it exists.
534 // Generated bash autocompletion will call the bash function f for the flag.
535 func MarkFlagCustom(flags *pflag.FlagSet, name string, f string) error {
536 return flags.SetAnnotation(name, BashCompCustom, []string{f})