bash_completions.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. // Copyright 2013-2023 The Cobra Authors
  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 at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // 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 and
  13. // limitations under the License.
  14. package cobra
  15. import (
  16. "bytes"
  17. "fmt"
  18. "io"
  19. "os"
  20. "sort"
  21. "strings"
  22. "github.com/spf13/pflag"
  23. )
  24. // Annotations for Bash completion.
  25. const (
  26. BashCompFilenameExt = "cobra_annotation_bash_completion_filename_extensions"
  27. BashCompCustom = "cobra_annotation_bash_completion_custom"
  28. BashCompOneRequiredFlag = "cobra_annotation_bash_completion_one_required_flag"
  29. BashCompSubdirsInDir = "cobra_annotation_bash_completion_subdirs_in_dir"
  30. )
  31. func writePreamble(buf io.StringWriter, name string) {
  32. WriteStringAndCheck(buf, fmt.Sprintf("# bash completion for %-36s -*- shell-script -*-\n", name))
  33. WriteStringAndCheck(buf, fmt.Sprintf(`
  34. __%[1]s_debug()
  35. {
  36. if [[ -n ${BASH_COMP_DEBUG_FILE:-} ]]; then
  37. echo "$*" >> "${BASH_COMP_DEBUG_FILE}"
  38. fi
  39. }
  40. # Homebrew on Macs have version 1.3 of bash-completion which doesn't include
  41. # _init_completion. This is a very minimal version of that function.
  42. __%[1]s_init_completion()
  43. {
  44. COMPREPLY=()
  45. _get_comp_words_by_ref "$@" cur prev words cword
  46. }
  47. __%[1]s_index_of_word()
  48. {
  49. local w word=$1
  50. shift
  51. index=0
  52. for w in "$@"; do
  53. [[ $w = "$word" ]] && return
  54. index=$((index+1))
  55. done
  56. index=-1
  57. }
  58. __%[1]s_contains_word()
  59. {
  60. local w word=$1; shift
  61. for w in "$@"; do
  62. [[ $w = "$word" ]] && return
  63. done
  64. return 1
  65. }
  66. __%[1]s_handle_go_custom_completion()
  67. {
  68. __%[1]s_debug "${FUNCNAME[0]}: cur is ${cur}, words[*] is ${words[*]}, #words[@] is ${#words[@]}"
  69. local shellCompDirectiveError=%[3]d
  70. local shellCompDirectiveNoSpace=%[4]d
  71. local shellCompDirectiveNoFileComp=%[5]d
  72. local shellCompDirectiveFilterFileExt=%[6]d
  73. local shellCompDirectiveFilterDirs=%[7]d
  74. local out requestComp lastParam lastChar comp directive args
  75. # Prepare the command to request completions for the program.
  76. # Calling ${words[0]} instead of directly %[1]s allows handling aliases
  77. args=("${words[@]:1}")
  78. # Disable ActiveHelp which is not supported for bash completion v1
  79. requestComp="%[8]s=0 ${words[0]} %[2]s ${args[*]}"
  80. lastParam=${words[$((${#words[@]}-1))]}
  81. lastChar=${lastParam:$((${#lastParam}-1)):1}
  82. __%[1]s_debug "${FUNCNAME[0]}: lastParam ${lastParam}, lastChar ${lastChar}"
  83. if [ -z "${cur}" ] && [ "${lastChar}" != "=" ]; then
  84. # If the last parameter is complete (there is a space following it)
  85. # We add an extra empty parameter so we can indicate this to the go method.
  86. __%[1]s_debug "${FUNCNAME[0]}: Adding extra empty parameter"
  87. requestComp="${requestComp} \"\""
  88. fi
  89. __%[1]s_debug "${FUNCNAME[0]}: calling ${requestComp}"
  90. # Use eval to handle any environment variables and such
  91. out=$(eval "${requestComp}" 2>/dev/null)
  92. # Extract the directive integer at the very end of the output following a colon (:)
  93. directive=${out##*:}
  94. # Remove the directive
  95. out=${out%%:*}
  96. if [ "${directive}" = "${out}" ]; then
  97. # There is not directive specified
  98. directive=0
  99. fi
  100. __%[1]s_debug "${FUNCNAME[0]}: the completion directive is: ${directive}"
  101. __%[1]s_debug "${FUNCNAME[0]}: the completions are: ${out}"
  102. if [ $((directive & shellCompDirectiveError)) -ne 0 ]; then
  103. # Error code. No completion.
  104. __%[1]s_debug "${FUNCNAME[0]}: received error from custom completion go code"
  105. return
  106. else
  107. if [ $((directive & shellCompDirectiveNoSpace)) -ne 0 ]; then
  108. if [[ $(type -t compopt) = "builtin" ]]; then
  109. __%[1]s_debug "${FUNCNAME[0]}: activating no space"
  110. compopt -o nospace
  111. fi
  112. fi
  113. if [ $((directive & shellCompDirectiveNoFileComp)) -ne 0 ]; then
  114. if [[ $(type -t compopt) = "builtin" ]]; then
  115. __%[1]s_debug "${FUNCNAME[0]}: activating no file completion"
  116. compopt +o default
  117. fi
  118. fi
  119. fi
  120. if [ $((directive & shellCompDirectiveFilterFileExt)) -ne 0 ]; then
  121. # File extension filtering
  122. local fullFilter filter filteringCmd
  123. # Do not use quotes around the $out variable or else newline
  124. # characters will be kept.
  125. for filter in ${out}; do
  126. fullFilter+="$filter|"
  127. done
  128. filteringCmd="_filedir $fullFilter"
  129. __%[1]s_debug "File filtering command: $filteringCmd"
  130. $filteringCmd
  131. elif [ $((directive & shellCompDirectiveFilterDirs)) -ne 0 ]; then
  132. # File completion for directories only
  133. local subdir
  134. # Use printf to strip any trailing newline
  135. subdir=$(printf "%%s" "${out}")
  136. if [ -n "$subdir" ]; then
  137. __%[1]s_debug "Listing directories in $subdir"
  138. __%[1]s_handle_subdirs_in_dir_flag "$subdir"
  139. else
  140. __%[1]s_debug "Listing directories in ."
  141. _filedir -d
  142. fi
  143. else
  144. while IFS='' read -r comp; do
  145. COMPREPLY+=("$comp")
  146. done < <(compgen -W "${out}" -- "$cur")
  147. fi
  148. }
  149. __%[1]s_handle_reply()
  150. {
  151. __%[1]s_debug "${FUNCNAME[0]}"
  152. local comp
  153. case $cur in
  154. -*)
  155. if [[ $(type -t compopt) = "builtin" ]]; then
  156. compopt -o nospace
  157. fi
  158. local allflags
  159. if [ ${#must_have_one_flag[@]} -ne 0 ]; then
  160. allflags=("${must_have_one_flag[@]}")
  161. else
  162. allflags=("${flags[*]} ${two_word_flags[*]}")
  163. fi
  164. while IFS='' read -r comp; do
  165. COMPREPLY+=("$comp")
  166. done < <(compgen -W "${allflags[*]}" -- "$cur")
  167. if [[ $(type -t compopt) = "builtin" ]]; then
  168. [[ "${COMPREPLY[0]}" == *= ]] || compopt +o nospace
  169. fi
  170. # complete after --flag=abc
  171. if [[ $cur == *=* ]]; then
  172. if [[ $(type -t compopt) = "builtin" ]]; then
  173. compopt +o nospace
  174. fi
  175. local index flag
  176. flag="${cur%%=*}"
  177. __%[1]s_index_of_word "${flag}" "${flags_with_completion[@]}"
  178. COMPREPLY=()
  179. if [[ ${index} -ge 0 ]]; then
  180. PREFIX=""
  181. cur="${cur#*=}"
  182. ${flags_completion[${index}]}
  183. if [ -n "${ZSH_VERSION:-}" ]; then
  184. # zsh completion needs --flag= prefix
  185. eval "COMPREPLY=( \"\${COMPREPLY[@]/#/${flag}=}\" )"
  186. fi
  187. fi
  188. fi
  189. if [[ -z "${flag_parsing_disabled}" ]]; then
  190. # If flag parsing is enabled, we have completed the flags and can return.
  191. # If flag parsing is disabled, we may not know all (or any) of the flags, so we fallthrough
  192. # to possibly call handle_go_custom_completion.
  193. return 0;
  194. fi
  195. ;;
  196. esac
  197. # check if we are handling a flag with special work handling
  198. local index
  199. __%[1]s_index_of_word "${prev}" "${flags_with_completion[@]}"
  200. if [[ ${index} -ge 0 ]]; then
  201. ${flags_completion[${index}]}
  202. return
  203. fi
  204. # we are parsing a flag and don't have a special handler, no completion
  205. if [[ ${cur} != "${words[cword]}" ]]; then
  206. return
  207. fi
  208. local completions
  209. completions=("${commands[@]}")
  210. if [[ ${#must_have_one_noun[@]} -ne 0 ]]; then
  211. completions+=("${must_have_one_noun[@]}")
  212. elif [[ -n "${has_completion_function}" ]]; then
  213. # if a go completion function is provided, defer to that function
  214. __%[1]s_handle_go_custom_completion
  215. fi
  216. if [[ ${#must_have_one_flag[@]} -ne 0 ]]; then
  217. completions+=("${must_have_one_flag[@]}")
  218. fi
  219. while IFS='' read -r comp; do
  220. COMPREPLY+=("$comp")
  221. done < <(compgen -W "${completions[*]}" -- "$cur")
  222. if [[ ${#COMPREPLY[@]} -eq 0 && ${#noun_aliases[@]} -gt 0 && ${#must_have_one_noun[@]} -ne 0 ]]; then
  223. while IFS='' read -r comp; do
  224. COMPREPLY+=("$comp")
  225. done < <(compgen -W "${noun_aliases[*]}" -- "$cur")
  226. fi
  227. if [[ ${#COMPREPLY[@]} -eq 0 ]]; then
  228. if declare -F __%[1]s_custom_func >/dev/null; then
  229. # try command name qualified custom func
  230. __%[1]s_custom_func
  231. else
  232. # otherwise fall back to unqualified for compatibility
  233. declare -F __custom_func >/dev/null && __custom_func
  234. fi
  235. fi
  236. # available in bash-completion >= 2, not always present on macOS
  237. if declare -F __ltrim_colon_completions >/dev/null; then
  238. __ltrim_colon_completions "$cur"
  239. fi
  240. # If there is only 1 completion and it is a flag with an = it will be completed
  241. # but we don't want a space after the =
  242. if [[ "${#COMPREPLY[@]}" -eq "1" ]] && [[ $(type -t compopt) = "builtin" ]] && [[ "${COMPREPLY[0]}" == --*= ]]; then
  243. compopt -o nospace
  244. fi
  245. }
  246. # The arguments should be in the form "ext1|ext2|extn"
  247. __%[1]s_handle_filename_extension_flag()
  248. {
  249. local ext="$1"
  250. _filedir "@(${ext})"
  251. }
  252. __%[1]s_handle_subdirs_in_dir_flag()
  253. {
  254. local dir="$1"
  255. pushd "${dir}" >/dev/null 2>&1 && _filedir -d && popd >/dev/null 2>&1 || return
  256. }
  257. __%[1]s_handle_flag()
  258. {
  259. __%[1]s_debug "${FUNCNAME[0]}: c is $c words[c] is ${words[c]}"
  260. # if a command required a flag, and we found it, unset must_have_one_flag()
  261. local flagname=${words[c]}
  262. local flagvalue=""
  263. # if the word contained an =
  264. if [[ ${words[c]} == *"="* ]]; then
  265. flagvalue=${flagname#*=} # take in as flagvalue after the =
  266. flagname=${flagname%%=*} # strip everything after the =
  267. flagname="${flagname}=" # but put the = back
  268. fi
  269. __%[1]s_debug "${FUNCNAME[0]}: looking for ${flagname}"
  270. if __%[1]s_contains_word "${flagname}" "${must_have_one_flag[@]}"; then
  271. must_have_one_flag=()
  272. fi
  273. # if you set a flag which only applies to this command, don't show subcommands
  274. if __%[1]s_contains_word "${flagname}" "${local_nonpersistent_flags[@]}"; then
  275. commands=()
  276. fi
  277. # keep flag value with flagname as flaghash
  278. # flaghash variable is an associative array which is only supported in bash > 3.
  279. if [[ -z "${BASH_VERSION:-}" || "${BASH_VERSINFO[0]:-}" -gt 3 ]]; then
  280. if [ -n "${flagvalue}" ] ; then
  281. flaghash[${flagname}]=${flagvalue}
  282. elif [ -n "${words[ $((c+1)) ]}" ] ; then
  283. flaghash[${flagname}]=${words[ $((c+1)) ]}
  284. else
  285. flaghash[${flagname}]="true" # pad "true" for bool flag
  286. fi
  287. fi
  288. # skip the argument to a two word flag
  289. if [[ ${words[c]} != *"="* ]] && __%[1]s_contains_word "${words[c]}" "${two_word_flags[@]}"; then
  290. __%[1]s_debug "${FUNCNAME[0]}: found a flag ${words[c]}, skip the next argument"
  291. c=$((c+1))
  292. # if we are looking for a flags value, don't show commands
  293. if [[ $c -eq $cword ]]; then
  294. commands=()
  295. fi
  296. fi
  297. c=$((c+1))
  298. }
  299. __%[1]s_handle_noun()
  300. {
  301. __%[1]s_debug "${FUNCNAME[0]}: c is $c words[c] is ${words[c]}"
  302. if __%[1]s_contains_word "${words[c]}" "${must_have_one_noun[@]}"; then
  303. must_have_one_noun=()
  304. elif __%[1]s_contains_word "${words[c]}" "${noun_aliases[@]}"; then
  305. must_have_one_noun=()
  306. fi
  307. nouns+=("${words[c]}")
  308. c=$((c+1))
  309. }
  310. __%[1]s_handle_command()
  311. {
  312. __%[1]s_debug "${FUNCNAME[0]}: c is $c words[c] is ${words[c]}"
  313. local next_command
  314. if [[ -n ${last_command} ]]; then
  315. next_command="_${last_command}_${words[c]//:/__}"
  316. else
  317. if [[ $c -eq 0 ]]; then
  318. next_command="_%[1]s_root_command"
  319. else
  320. next_command="_${words[c]//:/__}"
  321. fi
  322. fi
  323. c=$((c+1))
  324. __%[1]s_debug "${FUNCNAME[0]}: looking for ${next_command}"
  325. declare -F "$next_command" >/dev/null && $next_command
  326. }
  327. __%[1]s_handle_word()
  328. {
  329. if [[ $c -ge $cword ]]; then
  330. __%[1]s_handle_reply
  331. return
  332. fi
  333. __%[1]s_debug "${FUNCNAME[0]}: c is $c words[c] is ${words[c]}"
  334. if [[ "${words[c]}" == -* ]]; then
  335. __%[1]s_handle_flag
  336. elif __%[1]s_contains_word "${words[c]}" "${commands[@]}"; then
  337. __%[1]s_handle_command
  338. elif [[ $c -eq 0 ]]; then
  339. __%[1]s_handle_command
  340. elif __%[1]s_contains_word "${words[c]}" "${command_aliases[@]}"; then
  341. # aliashash variable is an associative array which is only supported in bash > 3.
  342. if [[ -z "${BASH_VERSION:-}" || "${BASH_VERSINFO[0]:-}" -gt 3 ]]; then
  343. words[c]=${aliashash[${words[c]}]}
  344. __%[1]s_handle_command
  345. else
  346. __%[1]s_handle_noun
  347. fi
  348. else
  349. __%[1]s_handle_noun
  350. fi
  351. __%[1]s_handle_word
  352. }
  353. `, name, ShellCompNoDescRequestCmd,
  354. ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp,
  355. ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs, activeHelpEnvVar(name)))
  356. }
  357. func writePostscript(buf io.StringWriter, name string) {
  358. name = strings.ReplaceAll(name, ":", "__")
  359. WriteStringAndCheck(buf, fmt.Sprintf("__start_%s()\n", name))
  360. WriteStringAndCheck(buf, fmt.Sprintf(`{
  361. local cur prev words cword split
  362. declare -A flaghash 2>/dev/null || :
  363. declare -A aliashash 2>/dev/null || :
  364. if declare -F _init_completion >/dev/null 2>&1; then
  365. _init_completion -s || return
  366. else
  367. __%[1]s_init_completion -n "=" || return
  368. fi
  369. local c=0
  370. local flag_parsing_disabled=
  371. local flags=()
  372. local two_word_flags=()
  373. local local_nonpersistent_flags=()
  374. local flags_with_completion=()
  375. local flags_completion=()
  376. local commands=("%[1]s")
  377. local command_aliases=()
  378. local must_have_one_flag=()
  379. local must_have_one_noun=()
  380. local has_completion_function=""
  381. local last_command=""
  382. local nouns=()
  383. local noun_aliases=()
  384. __%[1]s_handle_word
  385. }
  386. `, name))
  387. WriteStringAndCheck(buf, fmt.Sprintf(`if [[ $(type -t compopt) = "builtin" ]]; then
  388. complete -o default -F __start_%s %s
  389. else
  390. complete -o default -o nospace -F __start_%s %s
  391. fi
  392. `, name, name, name, name))
  393. WriteStringAndCheck(buf, "# ex: ts=4 sw=4 et filetype=sh\n")
  394. }
  395. func writeCommands(buf io.StringWriter, cmd *Command) {
  396. WriteStringAndCheck(buf, " commands=()\n")
  397. for _, c := range cmd.Commands() {
  398. if !c.IsAvailableCommand() && c != cmd.helpCommand {
  399. continue
  400. }
  401. WriteStringAndCheck(buf, fmt.Sprintf(" commands+=(%q)\n", c.Name()))
  402. writeCmdAliases(buf, c)
  403. }
  404. WriteStringAndCheck(buf, "\n")
  405. }
  406. func writeFlagHandler(buf io.StringWriter, name string, annotations map[string][]string, cmd *Command) {
  407. for key, value := range annotations {
  408. switch key {
  409. case BashCompFilenameExt:
  410. WriteStringAndCheck(buf, fmt.Sprintf(" flags_with_completion+=(%q)\n", name))
  411. var ext string
  412. if len(value) > 0 {
  413. ext = fmt.Sprintf("__%s_handle_filename_extension_flag ", cmd.Root().Name()) + strings.Join(value, "|")
  414. } else {
  415. ext = "_filedir"
  416. }
  417. WriteStringAndCheck(buf, fmt.Sprintf(" flags_completion+=(%q)\n", ext))
  418. case BashCompCustom:
  419. WriteStringAndCheck(buf, fmt.Sprintf(" flags_with_completion+=(%q)\n", name))
  420. if len(value) > 0 {
  421. handlers := strings.Join(value, "; ")
  422. WriteStringAndCheck(buf, fmt.Sprintf(" flags_completion+=(%q)\n", handlers))
  423. } else {
  424. WriteStringAndCheck(buf, " flags_completion+=(:)\n")
  425. }
  426. case BashCompSubdirsInDir:
  427. WriteStringAndCheck(buf, fmt.Sprintf(" flags_with_completion+=(%q)\n", name))
  428. var ext string
  429. if len(value) == 1 {
  430. ext = fmt.Sprintf("__%s_handle_subdirs_in_dir_flag ", cmd.Root().Name()) + value[0]
  431. } else {
  432. ext = "_filedir -d"
  433. }
  434. WriteStringAndCheck(buf, fmt.Sprintf(" flags_completion+=(%q)\n", ext))
  435. }
  436. }
  437. }
  438. const cbn = "\")\n"
  439. func writeShortFlag(buf io.StringWriter, flag *pflag.Flag, cmd *Command) {
  440. name := flag.Shorthand
  441. format := " "
  442. if len(flag.NoOptDefVal) == 0 {
  443. format += "two_word_"
  444. }
  445. format += "flags+=(\"-%s" + cbn
  446. WriteStringAndCheck(buf, fmt.Sprintf(format, name))
  447. writeFlagHandler(buf, "-"+name, flag.Annotations, cmd)
  448. }
  449. func writeFlag(buf io.StringWriter, flag *pflag.Flag, cmd *Command) {
  450. name := flag.Name
  451. format := " flags+=(\"--%s"
  452. if len(flag.NoOptDefVal) == 0 {
  453. format += "="
  454. }
  455. format += cbn
  456. WriteStringAndCheck(buf, fmt.Sprintf(format, name))
  457. if len(flag.NoOptDefVal) == 0 {
  458. format = " two_word_flags+=(\"--%s" + cbn
  459. WriteStringAndCheck(buf, fmt.Sprintf(format, name))
  460. }
  461. writeFlagHandler(buf, "--"+name, flag.Annotations, cmd)
  462. }
  463. func writeLocalNonPersistentFlag(buf io.StringWriter, flag *pflag.Flag) {
  464. name := flag.Name
  465. format := " local_nonpersistent_flags+=(\"--%[1]s" + cbn
  466. if len(flag.NoOptDefVal) == 0 {
  467. format += " local_nonpersistent_flags+=(\"--%[1]s=" + cbn
  468. }
  469. WriteStringAndCheck(buf, fmt.Sprintf(format, name))
  470. if len(flag.Shorthand) > 0 {
  471. WriteStringAndCheck(buf, fmt.Sprintf(" local_nonpersistent_flags+=(\"-%s\")\n", flag.Shorthand))
  472. }
  473. }
  474. // prepareCustomAnnotationsForFlags setup annotations for go completions for registered flags
  475. func prepareCustomAnnotationsForFlags(cmd *Command) {
  476. flagCompletionMutex.RLock()
  477. defer flagCompletionMutex.RUnlock()
  478. for flag := range flagCompletionFunctions {
  479. // Make sure the completion script calls the __*_go_custom_completion function for
  480. // every registered flag. We need to do this here (and not when the flag was registered
  481. // for completion) so that we can know the root command name for the prefix
  482. // of __<prefix>_go_custom_completion
  483. if flag.Annotations == nil {
  484. flag.Annotations = map[string][]string{}
  485. }
  486. flag.Annotations[BashCompCustom] = []string{fmt.Sprintf("__%[1]s_handle_go_custom_completion", cmd.Root().Name())}
  487. }
  488. }
  489. func writeFlags(buf io.StringWriter, cmd *Command) {
  490. prepareCustomAnnotationsForFlags(cmd)
  491. WriteStringAndCheck(buf, ` flags=()
  492. two_word_flags=()
  493. local_nonpersistent_flags=()
  494. flags_with_completion=()
  495. flags_completion=()
  496. `)
  497. if cmd.DisableFlagParsing {
  498. WriteStringAndCheck(buf, " flag_parsing_disabled=1\n")
  499. }
  500. localNonPersistentFlags := cmd.LocalNonPersistentFlags()
  501. cmd.NonInheritedFlags().VisitAll(func(flag *pflag.Flag) {
  502. if nonCompletableFlag(flag) {
  503. return
  504. }
  505. writeFlag(buf, flag, cmd)
  506. if len(flag.Shorthand) > 0 {
  507. writeShortFlag(buf, flag, cmd)
  508. }
  509. // localNonPersistentFlags are used to stop the completion of subcommands when one is set
  510. // if TraverseChildren is true we should allow to complete subcommands
  511. if localNonPersistentFlags.Lookup(flag.Name) != nil && !cmd.Root().TraverseChildren {
  512. writeLocalNonPersistentFlag(buf, flag)
  513. }
  514. })
  515. cmd.InheritedFlags().VisitAll(func(flag *pflag.Flag) {
  516. if nonCompletableFlag(flag) {
  517. return
  518. }
  519. writeFlag(buf, flag, cmd)
  520. if len(flag.Shorthand) > 0 {
  521. writeShortFlag(buf, flag, cmd)
  522. }
  523. })
  524. WriteStringAndCheck(buf, "\n")
  525. }
  526. func writeRequiredFlag(buf io.StringWriter, cmd *Command) {
  527. WriteStringAndCheck(buf, " must_have_one_flag=()\n")
  528. flags := cmd.NonInheritedFlags()
  529. flags.VisitAll(func(flag *pflag.Flag) {
  530. if nonCompletableFlag(flag) {
  531. return
  532. }
  533. for key := range flag.Annotations {
  534. switch key {
  535. case BashCompOneRequiredFlag:
  536. format := " must_have_one_flag+=(\"--%s"
  537. if flag.Value.Type() != "bool" {
  538. format += "="
  539. }
  540. format += cbn
  541. WriteStringAndCheck(buf, fmt.Sprintf(format, flag.Name))
  542. if len(flag.Shorthand) > 0 {
  543. WriteStringAndCheck(buf, fmt.Sprintf(" must_have_one_flag+=(\"-%s"+cbn, flag.Shorthand))
  544. }
  545. }
  546. }
  547. })
  548. }
  549. func writeRequiredNouns(buf io.StringWriter, cmd *Command) {
  550. WriteStringAndCheck(buf, " must_have_one_noun=()\n")
  551. sort.Strings(cmd.ValidArgs)
  552. for _, value := range cmd.ValidArgs {
  553. // Remove any description that may be included following a tab character.
  554. // Descriptions are not supported by bash completion.
  555. value = strings.Split(value, "\t")[0]
  556. WriteStringAndCheck(buf, fmt.Sprintf(" must_have_one_noun+=(%q)\n", value))
  557. }
  558. if cmd.ValidArgsFunction != nil {
  559. WriteStringAndCheck(buf, " has_completion_function=1\n")
  560. }
  561. }
  562. func writeCmdAliases(buf io.StringWriter, cmd *Command) {
  563. if len(cmd.Aliases) == 0 {
  564. return
  565. }
  566. sort.Strings(cmd.Aliases)
  567. WriteStringAndCheck(buf, fmt.Sprint(` if [[ -z "${BASH_VERSION:-}" || "${BASH_VERSINFO[0]:-}" -gt 3 ]]; then`, "\n"))
  568. for _, value := range cmd.Aliases {
  569. WriteStringAndCheck(buf, fmt.Sprintf(" command_aliases+=(%q)\n", value))
  570. WriteStringAndCheck(buf, fmt.Sprintf(" aliashash[%q]=%q\n", value, cmd.Name()))
  571. }
  572. WriteStringAndCheck(buf, ` fi`)
  573. WriteStringAndCheck(buf, "\n")
  574. }
  575. func writeArgAliases(buf io.StringWriter, cmd *Command) {
  576. WriteStringAndCheck(buf, " noun_aliases=()\n")
  577. sort.Strings(cmd.ArgAliases)
  578. for _, value := range cmd.ArgAliases {
  579. WriteStringAndCheck(buf, fmt.Sprintf(" noun_aliases+=(%q)\n", value))
  580. }
  581. }
  582. func gen(buf io.StringWriter, cmd *Command) {
  583. for _, c := range cmd.Commands() {
  584. if !c.IsAvailableCommand() && c != cmd.helpCommand {
  585. continue
  586. }
  587. gen(buf, c)
  588. }
  589. commandName := cmd.CommandPath()
  590. commandName = strings.ReplaceAll(commandName, " ", "_")
  591. commandName = strings.ReplaceAll(commandName, ":", "__")
  592. if cmd.Root() == cmd {
  593. WriteStringAndCheck(buf, fmt.Sprintf("_%s_root_command()\n{\n", commandName))
  594. } else {
  595. WriteStringAndCheck(buf, fmt.Sprintf("_%s()\n{\n", commandName))
  596. }
  597. WriteStringAndCheck(buf, fmt.Sprintf(" last_command=%q\n", commandName))
  598. WriteStringAndCheck(buf, "\n")
  599. WriteStringAndCheck(buf, " command_aliases=()\n")
  600. WriteStringAndCheck(buf, "\n")
  601. writeCommands(buf, cmd)
  602. writeFlags(buf, cmd)
  603. writeRequiredFlag(buf, cmd)
  604. writeRequiredNouns(buf, cmd)
  605. writeArgAliases(buf, cmd)
  606. WriteStringAndCheck(buf, "}\n\n")
  607. }
  608. // GenBashCompletion generates bash completion file and writes to the passed writer.
  609. func (c *Command) GenBashCompletion(w io.Writer) error {
  610. buf := new(bytes.Buffer)
  611. writePreamble(buf, c.Name())
  612. if len(c.BashCompletionFunction) > 0 {
  613. buf.WriteString(c.BashCompletionFunction + "\n")
  614. }
  615. gen(buf, c)
  616. writePostscript(buf, c.Name())
  617. _, err := buf.WriteTo(w)
  618. return err
  619. }
  620. func nonCompletableFlag(flag *pflag.Flag) bool {
  621. return flag.Hidden || len(flag.Deprecated) > 0
  622. }
  623. // GenBashCompletionFile generates bash completion file.
  624. func (c *Command) GenBashCompletionFile(filename string) error {
  625. outFile, err := os.Create(filename)
  626. if err != nil {
  627. return err
  628. }
  629. defer outFile.Close()
  630. return c.GenBashCompletion(outFile)
  631. }