Skip to content
Myr

bash_completions.go

This file

Path

bash_completions.go

709 lines · 22.5 KiB · Go

Last changed by

More linting (#2099)Ville Skyttäbefore adoption6b5f577

History of this file

Take it

The walk stopped after 200 commits: lines older than 89d7b83 point at it.

update copyright year (#1927)Unai Martinez-Corralbefore adoption9e6b58a

line 1why, one step back
1// Copyright 2013-2023 The Cobra Authors

add missing license headers (#1809)Unai Martinez-Corralbefore adoption6d978a9

lines 2–14why, one step back
2//
3// Licensed under the Apache License, Version 2.0 (the "License");4// you may not use this file except in compliance with the License.5// You may obtain a copy of the License at6//7// http://www.apache.org/licenses/LICENSE-2.08//9// Unless required by applicable law or agreed to in writing, software10// distributed under the License is distributed on an "AS IS" BASIS,11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12// See the License for the specific language governing permissions and13// limitations under the License.14

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

lines 15–87why, one step back
15package cobra
1617import (18 "bytes"19 "fmt"20 "io"21 "os"22 "sort"23 "strings"2425 "github.com/spf13/pflag"26)2728// Annotations for Bash completion.29const (30 BashCompFilenameExt = "cobra_annotation_bash_completion_filename_extensions"31 BashCompCustom = "cobra_annotation_bash_completion_custom"32 BashCompOneRequiredFlag = "cobra_annotation_bash_completion_one_required_flag"33 BashCompSubdirsInDir = "cobra_annotation_bash_completion_subdirs_in_dir"34)3536func writePreamble(buf io.StringWriter, name string) {37 WriteStringAndCheck(buf, fmt.Sprintf("# bash completion for %-36s -*- shell-script -*-\n", name))38 WriteStringAndCheck(buf, fmt.Sprintf(`39__%[1]s_debug()40{41 if [[ -n ${BASH_COMP_DEBUG_FILE:-} ]]; then42 echo "$*" >> "${BASH_COMP_DEBUG_FILE}"43 fi44}4546# Homebrew on Macs have version 1.3 of bash-completion which doesn't include47# _init_completion. This is a very minimal version of that function.48__%[1]s_init_completion()49{50 COMPREPLY=()51 _get_comp_words_by_ref "$@" cur prev words cword52}5354__%[1]s_index_of_word()55{56 local w word=$157 shift58 index=059 for w in "$@"; do60 [[ $w = "$word" ]] && return61 index=$((index+1))62 done63 index=-164}6566__%[1]s_contains_word()67{68 local w word=$1; shift69 for w in "$@"; do70 [[ $w = "$word" ]] && return71 done72 return 173}7475__%[1]s_handle_go_custom_completion()76{77 __%[1]s_debug "${FUNCNAME[0]}: cur is ${cur}, words[*] is ${words[*]}, #words[@] is ${#words[@]}"7879 local shellCompDirectiveError=%[3]d80 local shellCompDirectiveNoSpace=%[4]d81 local shellCompDirectiveNoFileComp=%[5]d82 local shellCompDirectiveFilterFileExt=%[6]d83 local shellCompDirectiveFilterDirs=%[7]d8485 local out requestComp lastParam lastChar comp directive args8687 # Prepare the command to request completions for the program.

Fix grammar: 'allows to' (#1978)Taavi Väänänenbefore adoption2246fa8

line 88why, one step back
88 # Calling ${words[0]} instead of directly %[1]s allows handling aliases

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

line 89why, one step back
89 args=("${words[@]:1}")

Add Active Help support (#1482)Marc Khouzambefore adoptionf464d6c

lines 90–91why, one step back
90 # Disable ActiveHelp which is not supported for bash completion v1
91 requestComp="%[8]s=0 ${words[0]} %[2]s ${args[*]}"

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

lines 92–116why, one step back
92
93 lastParam=${words[$((${#words[@]}-1))]}94 lastChar=${lastParam:$((${#lastParam}-1)):1}95 __%[1]s_debug "${FUNCNAME[0]}: lastParam ${lastParam}, lastChar ${lastChar}"9697 if [ -z "${cur}" ] && [ "${lastChar}" != "=" ]; then98 # If the last parameter is complete (there is a space following it)99 # We add an extra empty parameter so we can indicate this to the go method.100 __%[1]s_debug "${FUNCNAME[0]}: Adding extra empty parameter"101 requestComp="${requestComp} \"\""102 fi103104 __%[1]s_debug "${FUNCNAME[0]}: calling ${requestComp}"105 # Use eval to handle any environment variables and such106 out=$(eval "${requestComp}" 2>/dev/null)107108 # Extract the directive integer at the very end of the output following a colon (:)109 directive=${out##*:}110 # Remove the directive111 out=${out%%:*}112 if [ "${directive}" = "${out}" ]; then113 # There is not directive specified114 directive=0115 fi116 __%[1]s_debug "${FUNCNAME[0]}: the completion directive is: ${directive}"

style(bash): out is not an array variable, do not refer to it as such (#1684)Ville Skyttäbefore adoptionf17e5a2

line 117why, one step back
117 __%[1]s_debug "${FUNCNAME[0]}: the completions are: ${out}"

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

lines 118–142why, one step back
118
119 if [ $((directive & shellCompDirectiveError)) -ne 0 ]; then120 # Error code. No completion.121 __%[1]s_debug "${FUNCNAME[0]}: received error from custom completion go code"122 return123 else124 if [ $((directive & shellCompDirectiveNoSpace)) -ne 0 ]; then125 if [[ $(type -t compopt) = "builtin" ]]; then126 __%[1]s_debug "${FUNCNAME[0]}: activating no space"127 compopt -o nospace128 fi129 fi130 if [ $((directive & shellCompDirectiveNoFileComp)) -ne 0 ]; then131 if [[ $(type -t compopt) = "builtin" ]]; then132 __%[1]s_debug "${FUNCNAME[0]}: activating no file completion"133 compopt +o default134 fi135 fi136 fi137138 if [ $((directive & shellCompDirectiveFilterFileExt)) -ne 0 ]; then139 # File extension filtering140 local fullFilter filter filteringCmd141 # Do not use quotes around the $out variable or else newline142 # characters will be kept.

style(bash): out is not an array variable, do not refer to it as such (#1684)Ville Skyttäbefore adoptionf17e5a2

line 143why, one step back
143 for filter in ${out}; do

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

lines 144–153why, one step back
144 fullFilter+="$filter|"
145 done146147 filteringCmd="_filedir $fullFilter"148 __%[1]s_debug "File filtering command: $filteringCmd"149 $filteringCmd150 elif [ $((directive & shellCompDirectiveFilterDirs)) -ne 0 ]; then151 # File completion for directories only152 local subdir153 # Use printf to strip any trailing newline

style(bash): out is not an array variable, do not refer to it as such (#1684)Ville Skyttäbefore adoptionf17e5a2

line 154why, one step back
154 subdir=$(printf "%%s" "${out}")

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

lines 155–164why, one step back
155 if [ -n "$subdir" ]; then
156 __%[1]s_debug "Listing directories in $subdir"157 __%[1]s_handle_subdirs_in_dir_flag "$subdir"158 else159 __%[1]s_debug "Listing directories in ."160 _filedir -d161 fi162 else163 while IFS='' read -r comp; do164 COMPREPLY+=("$comp")

style(bash): out is not an array variable, do not refer to it as such (#1684)Ville Skyttäbefore adoptionf17e5a2

line 165why, one step back
165 done < <(compgen -W "${out}" -- "$cur")

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

lines 166–400why, one step back
166 fi
167}168169__%[1]s_handle_reply()170{171 __%[1]s_debug "${FUNCNAME[0]}"172 local comp173 case $cur in174 -*)175 if [[ $(type -t compopt) = "builtin" ]]; then176 compopt -o nospace177 fi178 local allflags179 if [ ${#must_have_one_flag[@]} -ne 0 ]; then180 allflags=("${must_have_one_flag[@]}")181 else182 allflags=("${flags[*]} ${two_word_flags[*]}")183 fi184 while IFS='' read -r comp; do185 COMPREPLY+=("$comp")186 done < <(compgen -W "${allflags[*]}" -- "$cur")187 if [[ $(type -t compopt) = "builtin" ]]; then188 [[ "${COMPREPLY[0]}" == *= ]] || compopt +o nospace189 fi190191 # complete after --flag=abc192 if [[ $cur == *=* ]]; then193 if [[ $(type -t compopt) = "builtin" ]]; then194 compopt +o nospace195 fi196197 local index flag198 flag="${cur%%=*}"199 __%[1]s_index_of_word "${flag}" "${flags_with_completion[@]}"200 COMPREPLY=()201 if [[ ${index} -ge 0 ]]; then202 PREFIX=""203 cur="${cur#*=}"204 ${flags_completion[${index}]}205 if [ -n "${ZSH_VERSION:-}" ]; then206 # zsh completion needs --flag= prefix207 eval "COMPREPLY=( \"\${COMPREPLY[@]/#/${flag}=}\" )"208 fi209 fi210 fi211212 if [[ -z "${flag_parsing_disabled}" ]]; then213 # If flag parsing is enabled, we have completed the flags and can return.214 # If flag parsing is disabled, we may not know all (or any) of the flags, so we fallthrough215 # to possibly call handle_go_custom_completion.216 return 0;217 fi218 ;;219 esac220221 # check if we are handling a flag with special work handling222 local index223 __%[1]s_index_of_word "${prev}" "${flags_with_completion[@]}"224 if [[ ${index} -ge 0 ]]; then225 ${flags_completion[${index}]}226 return227 fi228229 # we are parsing a flag and don't have a special handler, no completion230 if [[ ${cur} != "${words[cword]}" ]]; then231 return232 fi233234 local completions235 completions=("${commands[@]}")236 if [[ ${#must_have_one_noun[@]} -ne 0 ]]; then237 completions+=("${must_have_one_noun[@]}")238 elif [[ -n "${has_completion_function}" ]]; then239 # if a go completion function is provided, defer to that function240 __%[1]s_handle_go_custom_completion241 fi242 if [[ ${#must_have_one_flag[@]} -ne 0 ]]; then243 completions+=("${must_have_one_flag[@]}")244 fi245 while IFS='' read -r comp; do246 COMPREPLY+=("$comp")247 done < <(compgen -W "${completions[*]}" -- "$cur")248249 if [[ ${#COMPREPLY[@]} -eq 0 && ${#noun_aliases[@]} -gt 0 && ${#must_have_one_noun[@]} -ne 0 ]]; then250 while IFS='' read -r comp; do251 COMPREPLY+=("$comp")252 done < <(compgen -W "${noun_aliases[*]}" -- "$cur")253 fi254255 if [[ ${#COMPREPLY[@]} -eq 0 ]]; then256 if declare -F __%[1]s_custom_func >/dev/null; then257 # try command name qualified custom func258 __%[1]s_custom_func259 else260 # otherwise fall back to unqualified for compatibility261 declare -F __custom_func >/dev/null && __custom_func262 fi263 fi264265 # available in bash-completion >= 2, not always present on macOS266 if declare -F __ltrim_colon_completions >/dev/null; then267 __ltrim_colon_completions "$cur"268 fi269270 # If there is only 1 completion and it is a flag with an = it will be completed271 # but we don't want a space after the =272 if [[ "${#COMPREPLY[@]}" -eq "1" ]] && [[ $(type -t compopt) = "builtin" ]] && [[ "${COMPREPLY[0]}" == --*= ]]; then273 compopt -o nospace274 fi275}276277# The arguments should be in the form "ext1|ext2|extn"278__%[1]s_handle_filename_extension_flag()279{280 local ext="$1"281 _filedir "@(${ext})"282}283284__%[1]s_handle_subdirs_in_dir_flag()285{286 local dir="$1"287 pushd "${dir}" >/dev/null 2>&1 && _filedir -d && popd >/dev/null 2>&1 || return288}289290__%[1]s_handle_flag()291{292 __%[1]s_debug "${FUNCNAME[0]}: c is $c words[c] is ${words[c]}"293294 # if a command required a flag, and we found it, unset must_have_one_flag()295 local flagname=${words[c]}296 local flagvalue=""297 # if the word contained an =298 if [[ ${words[c]} == *"="* ]]; then299 flagvalue=${flagname#*=} # take in as flagvalue after the =300 flagname=${flagname%%=*} # strip everything after the =301 flagname="${flagname}=" # but put the = back302 fi303 __%[1]s_debug "${FUNCNAME[0]}: looking for ${flagname}"304 if __%[1]s_contains_word "${flagname}" "${must_have_one_flag[@]}"; then305 must_have_one_flag=()306 fi307308 # if you set a flag which only applies to this command, don't show subcommands309 if __%[1]s_contains_word "${flagname}" "${local_nonpersistent_flags[@]}"; then310 commands=()311 fi312313 # keep flag value with flagname as flaghash314 # flaghash variable is an associative array which is only supported in bash > 3.315 if [[ -z "${BASH_VERSION:-}" || "${BASH_VERSINFO[0]:-}" -gt 3 ]]; then316 if [ -n "${flagvalue}" ] ; then317 flaghash[${flagname}]=${flagvalue}318 elif [ -n "${words[ $((c+1)) ]}" ] ; then319 flaghash[${flagname}]=${words[ $((c+1)) ]}320 else321 flaghash[${flagname}]="true" # pad "true" for bool flag322 fi323 fi324325 # skip the argument to a two word flag326 if [[ ${words[c]} != *"="* ]] && __%[1]s_contains_word "${words[c]}" "${two_word_flags[@]}"; then327 __%[1]s_debug "${FUNCNAME[0]}: found a flag ${words[c]}, skip the next argument"328 c=$((c+1))329 # if we are looking for a flags value, don't show commands330 if [[ $c -eq $cword ]]; then331 commands=()332 fi333 fi334335 c=$((c+1))336337}338339__%[1]s_handle_noun()340{341 __%[1]s_debug "${FUNCNAME[0]}: c is $c words[c] is ${words[c]}"342343 if __%[1]s_contains_word "${words[c]}" "${must_have_one_noun[@]}"; then344 must_have_one_noun=()345 elif __%[1]s_contains_word "${words[c]}" "${noun_aliases[@]}"; then346 must_have_one_noun=()347 fi348349 nouns+=("${words[c]}")350 c=$((c+1))351}352353__%[1]s_handle_command()354{355 __%[1]s_debug "${FUNCNAME[0]}: c is $c words[c] is ${words[c]}"356357 local next_command358 if [[ -n ${last_command} ]]; then359 next_command="_${last_command}_${words[c]//:/__}"360 else361 if [[ $c -eq 0 ]]; then362 next_command="_%[1]s_root_command"363 else364 next_command="_${words[c]//:/__}"365 fi366 fi367 c=$((c+1))368 __%[1]s_debug "${FUNCNAME[0]}: looking for ${next_command}"369 declare -F "$next_command" >/dev/null && $next_command370}371372__%[1]s_handle_word()373{374 if [[ $c -ge $cword ]]; then375 __%[1]s_handle_reply376 return377 fi378 __%[1]s_debug "${FUNCNAME[0]}: c is $c words[c] is ${words[c]}"379 if [[ "${words[c]}" == -* ]]; then380 __%[1]s_handle_flag381 elif __%[1]s_contains_word "${words[c]}" "${commands[@]}"; then382 __%[1]s_handle_command383 elif [[ $c -eq 0 ]]; then384 __%[1]s_handle_command385 elif __%[1]s_contains_word "${words[c]}" "${command_aliases[@]}"; then386 # aliashash variable is an associative array which is only supported in bash > 3.387 if [[ -z "${BASH_VERSION:-}" || "${BASH_VERSINFO[0]:-}" -gt 3 ]]; then388 words[c]=${aliashash[${words[c]}]}389 __%[1]s_handle_command390 else391 __%[1]s_handle_noun392 fi393 else394 __%[1]s_handle_noun395 fi396 __%[1]s_handle_word397}398399`, name, ShellCompNoDescRequestCmd,400 ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp,

Add Active Help support (#1482)Marc Khouzambefore adoptionf464d6c

line 401why, one step back
401 ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs, activeHelpEnvVar(name)))

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

lines 402–404why, one step back
402}
403404func writePostscript(buf io.StringWriter, name string) {

Prefer ReplaceAll instead of Replace(..., -1) (#1530)Hugobefore adoption25f5bb5

line 405why, one step back
405 name = strings.ReplaceAll(name, ":", "__")

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

lines 406–534why, one step back
406 WriteStringAndCheck(buf, fmt.Sprintf("__start_%s()\n", name))
407 WriteStringAndCheck(buf, fmt.Sprintf(`{408 local cur prev words cword split409 declare -A flaghash 2>/dev/null || :410 declare -A aliashash 2>/dev/null || :411 if declare -F _init_completion >/dev/null 2>&1; then412 _init_completion -s || return413 else414 __%[1]s_init_completion -n "=" || return415 fi416417 local c=0418 local flag_parsing_disabled=419 local flags=()420 local two_word_flags=()421 local local_nonpersistent_flags=()422 local flags_with_completion=()423 local flags_completion=()424 local commands=("%[1]s")425 local command_aliases=()426 local must_have_one_flag=()427 local must_have_one_noun=()428 local has_completion_function=""429 local last_command=""430 local nouns=()431 local noun_aliases=()432433 __%[1]s_handle_word434}435436`, name))437 WriteStringAndCheck(buf, fmt.Sprintf(`if [[ $(type -t compopt) = "builtin" ]]; then438 complete -o default -F __start_%s %s439else440 complete -o default -o nospace -F __start_%s %s441fi442443`, name, name, name, name))444 WriteStringAndCheck(buf, "# ex: ts=4 sw=4 et filetype=sh\n")445}446447func writeCommands(buf io.StringWriter, cmd *Command) {448 WriteStringAndCheck(buf, " commands=()\n")449 for _, c := range cmd.Commands() {450 if !c.IsAvailableCommand() && c != cmd.helpCommand {451 continue452 }453 WriteStringAndCheck(buf, fmt.Sprintf(" commands+=(%q)\n", c.Name()))454 writeCmdAliases(buf, c)455 }456 WriteStringAndCheck(buf, "\n")457}458459func writeFlagHandler(buf io.StringWriter, name string, annotations map[string][]string, cmd *Command) {460 for key, value := range annotations {461 switch key {462 case BashCompFilenameExt:463 WriteStringAndCheck(buf, fmt.Sprintf(" flags_with_completion+=(%q)\n", name))464465 var ext string466 if len(value) > 0 {467 ext = fmt.Sprintf("__%s_handle_filename_extension_flag ", cmd.Root().Name()) + strings.Join(value, "|")468 } else {469 ext = "_filedir"470 }471 WriteStringAndCheck(buf, fmt.Sprintf(" flags_completion+=(%q)\n", ext))472 case BashCompCustom:473 WriteStringAndCheck(buf, fmt.Sprintf(" flags_with_completion+=(%q)\n", name))474475 if len(value) > 0 {476 handlers := strings.Join(value, "; ")477 WriteStringAndCheck(buf, fmt.Sprintf(" flags_completion+=(%q)\n", handlers))478 } else {479 WriteStringAndCheck(buf, " flags_completion+=(:)\n")480 }481 case BashCompSubdirsInDir:482 WriteStringAndCheck(buf, fmt.Sprintf(" flags_with_completion+=(%q)\n", name))483484 var ext string485 if len(value) == 1 {486 ext = fmt.Sprintf("__%s_handle_subdirs_in_dir_flag ", cmd.Root().Name()) + value[0]487 } else {488 ext = "_filedir -d"489 }490 WriteStringAndCheck(buf, fmt.Sprintf(" flags_completion+=(%q)\n", ext))491 }492 }493}494495const cbn = "\")\n"496497func writeShortFlag(buf io.StringWriter, flag *pflag.Flag, cmd *Command) {498 name := flag.Shorthand499 format := " "500 if len(flag.NoOptDefVal) == 0 {501 format += "two_word_"502 }503 format += "flags+=(\"-%s" + cbn504 WriteStringAndCheck(buf, fmt.Sprintf(format, name))505 writeFlagHandler(buf, "-"+name, flag.Annotations, cmd)506}507508func writeFlag(buf io.StringWriter, flag *pflag.Flag, cmd *Command) {509 name := flag.Name510 format := " flags+=(\"--%s"511 if len(flag.NoOptDefVal) == 0 {512 format += "="513 }514 format += cbn515 WriteStringAndCheck(buf, fmt.Sprintf(format, name))516 if len(flag.NoOptDefVal) == 0 {517 format = " two_word_flags+=(\"--%s" + cbn518 WriteStringAndCheck(buf, fmt.Sprintf(format, name))519 }520 writeFlagHandler(buf, "--"+name, flag.Annotations, cmd)521}522523func writeLocalNonPersistentFlag(buf io.StringWriter, flag *pflag.Flag) {524 name := flag.Name525 format := " local_nonpersistent_flags+=(\"--%[1]s" + cbn526 if len(flag.NoOptDefVal) == 0 {527 format += " local_nonpersistent_flags+=(\"--%[1]s=" + cbn528 }529 WriteStringAndCheck(buf, fmt.Sprintf(format, name))530 if len(flag.Shorthand) > 0 {531 WriteStringAndCheck(buf, fmt.Sprintf(" local_nonpersistent_flags+=(\"-%s\")\n", flag.Shorthand))532 }533}534

fix: func name in doc strings (#1885)Yash Ladhabefore adoptionbf11ab6

line 535why, one step back
535// prepareCustomAnnotationsForFlags setup annotations for go completions for registered flags

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

lines 536–599why, one step back
536func prepareCustomAnnotationsForFlags(cmd *Command) {
537 flagCompletionMutex.RLock()538 defer flagCompletionMutex.RUnlock()539 for flag := range flagCompletionFunctions {540 // Make sure the completion script calls the __*_go_custom_completion function for541 // every registered flag. We need to do this here (and not when the flag was registered542 // for completion) so that we can know the root command name for the prefix543 // of __<prefix>_go_custom_completion544 if flag.Annotations == nil {545 flag.Annotations = map[string][]string{}546 }547 flag.Annotations[BashCompCustom] = []string{fmt.Sprintf("__%[1]s_handle_go_custom_completion", cmd.Root().Name())}548 }549}550551func writeFlags(buf io.StringWriter, cmd *Command) {552 prepareCustomAnnotationsForFlags(cmd)553 WriteStringAndCheck(buf, ` flags=()554 two_word_flags=()555 local_nonpersistent_flags=()556 flags_with_completion=()557 flags_completion=()558559`)560561 if cmd.DisableFlagParsing {562 WriteStringAndCheck(buf, " flag_parsing_disabled=1\n")563 }564565 localNonPersistentFlags := cmd.LocalNonPersistentFlags()566 cmd.NonInheritedFlags().VisitAll(func(flag *pflag.Flag) {567 if nonCompletableFlag(flag) {568 return569 }570 writeFlag(buf, flag, cmd)571 if len(flag.Shorthand) > 0 {572 writeShortFlag(buf, flag, cmd)573 }574 // localNonPersistentFlags are used to stop the completion of subcommands when one is set575 // if TraverseChildren is true we should allow to complete subcommands576 if localNonPersistentFlags.Lookup(flag.Name) != nil && !cmd.Root().TraverseChildren {577 writeLocalNonPersistentFlag(buf, flag)578 }579 })580 cmd.InheritedFlags().VisitAll(func(flag *pflag.Flag) {581 if nonCompletableFlag(flag) {582 return583 }584 writeFlag(buf, flag, cmd)585 if len(flag.Shorthand) > 0 {586 writeShortFlag(buf, flag, cmd)587 }588 })589590 WriteStringAndCheck(buf, "\n")591}592593func writeRequiredFlag(buf io.StringWriter, cmd *Command) {594 WriteStringAndCheck(buf, " must_have_one_flag=()\n")595 flags := cmd.NonInheritedFlags()596 flags.VisitAll(func(flag *pflag.Flag) {597 if nonCompletableFlag(flag) {598 return599 }

More linting (#2099)Ville Skyttäbefore adoption6b5f577

lines 600–606why, one step back
600 if _, ok := flag.Annotations[BashCompOneRequiredFlag]; ok {
601 format := " must_have_one_flag+=(\"--%s"602 if flag.Value.Type() != "bool" {603 format += "="604 }605 format += cbn606 WriteStringAndCheck(buf, fmt.Sprintf(format, flag.Name))

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

line 607why, one step back
607

More linting (#2099)Ville Skyttäbefore adoption6b5f577

lines 608–609why, one step back
608 if len(flag.Shorthand) > 0 {
609 WriteStringAndCheck(buf, fmt.Sprintf(" must_have_one_flag+=(\"-%s"+cbn, flag.Shorthand))

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

lines 610–620why, one step back
610 }
611 }612 })613}614615func writeRequiredNouns(buf io.StringWriter, cmd *Command) {616 WriteStringAndCheck(buf, " must_have_one_noun=()\n")617 sort.Strings(cmd.ValidArgs)618 for _, value := range cmd.ValidArgs {619 // Remove any description that may be included following a tab character.620 // Descriptions are not supported by bash completion.

Micro-optimizations (#1957)Ville Skyttäbefore adoption3d8ac43

line 621why, one step back
621 value = strings.SplitN(value, "\t", 2)[0]

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

lines 622–659why, one step back
622 WriteStringAndCheck(buf, fmt.Sprintf(" must_have_one_noun+=(%q)\n", value))
623 }624 if cmd.ValidArgsFunction != nil {625 WriteStringAndCheck(buf, " has_completion_function=1\n")626 }627}628629func writeCmdAliases(buf io.StringWriter, cmd *Command) {630 if len(cmd.Aliases) == 0 {631 return632 }633634 sort.Strings(cmd.Aliases)635636 WriteStringAndCheck(buf, fmt.Sprint(` if [[ -z "${BASH_VERSION:-}" || "${BASH_VERSINFO[0]:-}" -gt 3 ]]; then`, "\n"))637 for _, value := range cmd.Aliases {638 WriteStringAndCheck(buf, fmt.Sprintf(" command_aliases+=(%q)\n", value))639 WriteStringAndCheck(buf, fmt.Sprintf(" aliashash[%q]=%q\n", value, cmd.Name()))640 }641 WriteStringAndCheck(buf, ` fi`)642 WriteStringAndCheck(buf, "\n")643}644func writeArgAliases(buf io.StringWriter, cmd *Command) {645 WriteStringAndCheck(buf, " noun_aliases=()\n")646 sort.Strings(cmd.ArgAliases)647 for _, value := range cmd.ArgAliases {648 WriteStringAndCheck(buf, fmt.Sprintf(" noun_aliases+=(%q)\n", value))649 }650}651652func gen(buf io.StringWriter, cmd *Command) {653 for _, c := range cmd.Commands() {654 if !c.IsAvailableCommand() && c != cmd.helpCommand {655 continue656 }657 gen(buf, c)658 }659 commandName := cmd.CommandPath()

Prefer ReplaceAll instead of Replace(..., -1) (#1530)Hugobefore adoption25f5bb5

lines 660–661why, one step back
660 commandName = strings.ReplaceAll(commandName, " ", "_")
661 commandName = strings.ReplaceAll(commandName, ":", "__")

Add Polygon Edge as a project using Cobra (#1672)Miloš Živkovićbefore adoption89d7b83

lines 662–709why, one step back
662
663 if cmd.Root() == cmd {664 WriteStringAndCheck(buf, fmt.Sprintf("_%s_root_command()\n{\n", commandName))665 } else {666 WriteStringAndCheck(buf, fmt.Sprintf("_%s()\n{\n", commandName))667 }668669 WriteStringAndCheck(buf, fmt.Sprintf(" last_command=%q\n", commandName))670 WriteStringAndCheck(buf, "\n")671 WriteStringAndCheck(buf, " command_aliases=()\n")672 WriteStringAndCheck(buf, "\n")673674 writeCommands(buf, cmd)675 writeFlags(buf, cmd)676 writeRequiredFlag(buf, cmd)677 writeRequiredNouns(buf, cmd)678 writeArgAliases(buf, cmd)679 WriteStringAndCheck(buf, "}\n\n")680}681682// GenBashCompletion generates bash completion file and writes to the passed writer.683func (c *Command) GenBashCompletion(w io.Writer) error {684 buf := new(bytes.Buffer)685 writePreamble(buf, c.Name())686 if len(c.BashCompletionFunction) > 0 {687 buf.WriteString(c.BashCompletionFunction + "\n")688 }689 gen(buf, c)690 writePostscript(buf, c.Name())691692 _, err := buf.WriteTo(w)693 return err694}695696func nonCompletableFlag(flag *pflag.Flag) bool {697 return flag.Hidden || len(flag.Deprecated) > 0698}699700// GenBashCompletionFile generates bash completion file.701func (c *Command) GenBashCompletionFile(filename string) error {702 outFile, err := os.Create(filename)703 if err != nil {704 return err705 }706 defer outFile.Close()707708 return c.GenBashCompletion(outFile)709}