vendor/github.com/spf13/cobra/bash_completions.go
changeset 251 1c52a0eeb952
parent 242 2a9ec03fe5a1
child 256 6d9efbef00a9
equal deleted inserted replaced
250:c040f992052f 251:1c52a0eeb952
   127     if [[ ${#COMPREPLY[@]} -eq 0 && ${#noun_aliases[@]} -gt 0 && ${#must_have_one_noun[@]} -ne 0 ]]; then
   127     if [[ ${#COMPREPLY[@]} -eq 0 && ${#noun_aliases[@]} -gt 0 && ${#must_have_one_noun[@]} -ne 0 ]]; then
   128         COMPREPLY=( $(compgen -W "${noun_aliases[*]}" -- "$cur") )
   128         COMPREPLY=( $(compgen -W "${noun_aliases[*]}" -- "$cur") )
   129     fi
   129     fi
   130 
   130 
   131     if [[ ${#COMPREPLY[@]} -eq 0 ]]; then
   131     if [[ ${#COMPREPLY[@]} -eq 0 ]]; then
   132         declare -F __custom_func >/dev/null && __custom_func
   132 		if declare -F __%[1]s_custom_func >/dev/null; then
       
   133 			# try command name qualified custom func
       
   134 			__%[1]s_custom_func
       
   135 		else
       
   136 			# otherwise fall back to unqualified for compatibility
       
   137 			declare -F __custom_func >/dev/null && __custom_func
       
   138 		fi
   133     fi
   139     fi
   134 
   140 
   135     # available in bash-completion >= 2, not always present on macOS
   141     # available in bash-completion >= 2, not always present on macOS
   136     if declare -F __ltrim_colon_completions >/dev/null; then
   142     if declare -F __ltrim_colon_completions >/dev/null; then
   137         __ltrim_colon_completions "$cur"
   143         __ltrim_colon_completions "$cur"
   191             flaghash[${flagname}]="true" # pad "true" for bool flag
   197             flaghash[${flagname}]="true" # pad "true" for bool flag
   192         fi
   198         fi
   193     fi
   199     fi
   194 
   200 
   195     # skip the argument to a two word flag
   201     # skip the argument to a two word flag
   196     if __%[1]s_contains_word "${words[c]}" "${two_word_flags[@]}"; then
   202     if [[ ${words[c]} != *"="* ]] && __%[1]s_contains_word "${words[c]}" "${two_word_flags[@]}"; then
       
   203 			  __%[1]s_debug "${FUNCNAME[0]}: found a flag ${words[c]}, skip the next argument"
   197         c=$((c+1))
   204         c=$((c+1))
   198         # if we are looking for a flags value, don't show commands
   205         # if we are looking for a flags value, don't show commands
   199         if [[ $c -eq $cword ]]; then
   206         if [[ $c -eq $cword ]]; then
   200             commands=()
   207             commands=()
   201         fi
   208         fi
   371 	if len(flag.NoOptDefVal) == 0 {
   378 	if len(flag.NoOptDefVal) == 0 {
   372 		format += "="
   379 		format += "="
   373 	}
   380 	}
   374 	format += "\")\n"
   381 	format += "\")\n"
   375 	buf.WriteString(fmt.Sprintf(format, name))
   382 	buf.WriteString(fmt.Sprintf(format, name))
       
   383 	if len(flag.NoOptDefVal) == 0 {
       
   384 		format = "    two_word_flags+=(\"--%s\")\n"
       
   385 		buf.WriteString(fmt.Sprintf(format, name))
       
   386 	}
   376 	writeFlagHandler(buf, "--"+name, flag.Annotations, cmd)
   387 	writeFlagHandler(buf, "--"+name, flag.Annotations, cmd)
   377 }
   388 }
   378 
   389 
   379 func writeLocalNonPersistentFlag(buf *bytes.Buffer, flag *pflag.Flag) {
   390 func writeLocalNonPersistentFlag(buf *bytes.Buffer, flag *pflag.Flag) {
   380 	name := flag.Name
   391 	name := flag.Name
   532 	}
   543 	}
   533 	defer outFile.Close()
   544 	defer outFile.Close()
   534 
   545 
   535 	return c.GenBashCompletion(outFile)
   546 	return c.GenBashCompletion(outFile)
   536 }
   547 }
   537 
       
   538 // MarkFlagRequired adds the BashCompOneRequiredFlag annotation to the named flag if it exists,
       
   539 // and causes your command to report an error if invoked without the flag.
       
   540 func (c *Command) MarkFlagRequired(name string) error {
       
   541 	return MarkFlagRequired(c.Flags(), name)
       
   542 }
       
   543 
       
   544 // MarkPersistentFlagRequired adds the BashCompOneRequiredFlag annotation to the named persistent flag if it exists,
       
   545 // and causes your command to report an error if invoked without the flag.
       
   546 func (c *Command) MarkPersistentFlagRequired(name string) error {
       
   547 	return MarkFlagRequired(c.PersistentFlags(), name)
       
   548 }
       
   549 
       
   550 // MarkFlagRequired adds the BashCompOneRequiredFlag annotation to the named flag if it exists,
       
   551 // and causes your command to report an error if invoked without the flag.
       
   552 func MarkFlagRequired(flags *pflag.FlagSet, name string) error {
       
   553 	return flags.SetAnnotation(name, BashCompOneRequiredFlag, []string{"true"})
       
   554 }
       
   555 
       
   556 // MarkFlagFilename adds the BashCompFilenameExt annotation to the named flag, if it exists.
       
   557 // Generated bash autocompletion will select filenames for the flag, limiting to named extensions if provided.
       
   558 func (c *Command) MarkFlagFilename(name string, extensions ...string) error {
       
   559 	return MarkFlagFilename(c.Flags(), name, extensions...)
       
   560 }
       
   561 
       
   562 // MarkFlagCustom adds the BashCompCustom annotation to the named flag, if it exists.
       
   563 // Generated bash autocompletion will call the bash function f for the flag.
       
   564 func (c *Command) MarkFlagCustom(name string, f string) error {
       
   565 	return MarkFlagCustom(c.Flags(), name, f)
       
   566 }
       
   567 
       
   568 // MarkPersistentFlagFilename adds the BashCompFilenameExt annotation to the named persistent flag, if it exists.
       
   569 // Generated bash autocompletion will select filenames for the flag, limiting to named extensions if provided.
       
   570 func (c *Command) MarkPersistentFlagFilename(name string, extensions ...string) error {
       
   571 	return MarkFlagFilename(c.PersistentFlags(), name, extensions...)
       
   572 }
       
   573 
       
   574 // MarkFlagFilename adds the BashCompFilenameExt annotation to the named flag in the flag set, if it exists.
       
   575 // Generated bash autocompletion will select filenames for the flag, limiting to named extensions if provided.
       
   576 func MarkFlagFilename(flags *pflag.FlagSet, name string, extensions ...string) error {
       
   577 	return flags.SetAnnotation(name, BashCompFilenameExt, extensions)
       
   578 }
       
   579 
       
   580 // MarkFlagCustom adds the BashCompCustom annotation to the named flag in the flag set, if it exists.
       
   581 // Generated bash autocompletion will call the bash function f for the flag.
       
   582 func MarkFlagCustom(flags *pflag.FlagSet, name string, f string) error {
       
   583 	return flags.SetAnnotation(name, BashCompCustom, []string{f})
       
   584 }