1
mirror of https://github.com/spf13/cobra.git synced 2024-12-27 22:11:11 -07:00

Compare commits

...

9 Commits

Author SHA1 Message Date
Jeffrey Faer
850374ab3b
Merge 7a9725b587 into 9f9056765c 2024-12-18 11:58:59 -08:00
Sebastiaan van Stijn
9f9056765c
build(deps): bump github.com/cpuguy83/go-md2man/v2 from 2.0.5 to 2.0.6 ()
fix compatibility with go versions before go1.17

diff: https://github.com/cpuguy83/go-md2man/compare/v2.0.5...v2.0.6

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2024-12-16 21:26:34 -05:00
Jeffrey Faer
7a9725b587 Only quote the last arg.
The other ones should already have been quoted by the completion script.

Signed-off-by: Jeffrey Faer <jeffrey.faer@gmail.com>
2024-12-05 22:03:34 -07:00
Jeffrey Faer
df206d9f38 remove line
Signed-off-by: Jeffrey Faer <jeffrey.faer@gmail.com>
2024-12-04 23:04:36 -07:00
Jeffrey Faer
718f1d85e5 Handle wordbreaks better.
Signed-off-by: Jeffrey Faer <jeffrey.faer@gmail.com>
2024-12-04 22:55:38 -07:00
Jeffrey Faer
9771cb0949 address review comments
Signed-off-by: Jeffrey Faer <jeffrey.faer@gmail.com>
2024-12-04 21:39:34 -07:00
Jeffrey Faer
76d900bc72 Don't quote different completion types.
They're not going through compgen so they don't need it.

Signed-off-by: Jeffrey Faer <jeffrey.faer@gmail.com>
2024-12-04 21:39:34 -07:00
Jeffrey Faer
497d36cfc1 fix: We don't need an extra empty character when we're doing proper quoting.
Signed-off-by: Jeffrey Faer <jeffrey.faer@gmail.com>
2024-12-04 21:39:34 -07:00
Jeffrey Faer
d131fa6e88 fix(bash): Fix bash completion for suggestions that contain special characters.
Special characters include whitepace, so this is more general than
 . This should also fix .

I added some test cases to cobra-completion-testing. This PR makes them
pass. It also doesn't trip any of the performance regression tests. I'm
happy to submit those tests as a PR as well.
  - https://github.com/JeffFaer/cobra-completion-testing/tree/special_characters
  - 52254c1a25

Signed-off-by: Jeffrey Faer <jeffrey.faer@gmail.com>
2024-12-04 21:39:34 -07:00
3 changed files with 62 additions and 25 deletions

View File

@ -59,19 +59,18 @@ __%[1]s_get_completion_results() {
# Prepare the command to request completions for the program. # Prepare the command to request completions for the program.
# Calling ${words[0]} instead of directly %[1]s allows handling aliases # Calling ${words[0]} instead of directly %[1]s allows handling aliases
args=("${words[@]:1}") args=("${words[@]:1}")
requestComp="${words[0]} %[2]s ${args[*]}" requestComp="${words[0]} %[2]s"
if [[ "${#args[@]}" -gt 0 ]]; then
# Previous args should already be escaped...
requestComp+=" ${args[*]::${#args[@]}-1}"
# ...but the current arg might not yet be escaped.
requestComp+=" $(printf "%%q" "${args[${#args[@]}-1]}")"
fi
lastParam=${words[$((${#words[@]}-1))]} lastParam=${words[$((${#words[@]}-1))]}
lastChar=${lastParam:$((${#lastParam}-1)):1} lastChar=${lastParam:$((${#lastParam}-1)):1}
__%[1]s_debug "lastParam ${lastParam}, lastChar ${lastChar}" __%[1]s_debug "lastParam ${lastParam}, lastChar ${lastChar}"
if [[ -z ${cur} && ${lastChar} != = ]]; then
# If the last parameter is complete (there is a space following it)
# We add an extra empty parameter so we can indicate this to the go method.
__%[1]s_debug "Adding extra empty parameter"
requestComp="${requestComp} ''"
fi
# When completing a flag with an = (e.g., %[1]s -n=<TAB>) # When completing a flag with an = (e.g., %[1]s -n=<TAB>)
# bash focuses on the part after the =, so we need to remove # bash focuses on the part after the =, so we need to remove
# the flag part from $cur # the flag part from $cur
@ -173,8 +172,9 @@ __%[1]s_process_completion_results() {
__%[1]s_handle_completion_types __%[1]s_handle_completion_types
fi fi
__%[1]s_handle_special_char "$cur" : __%[1]s_handle_wordbreaks "$cur"
__%[1]s_handle_special_char "$cur" =
__%[1]s_debug "The final COMPREPLY: $(printf "%%s\n" "${COMPREPLY[@]}")"
# Print the activeHelp statements before we finish # Print the activeHelp statements before we finish
if ((${#activeHelp[*]} != 0)); then if ((${#activeHelp[*]} != 0)); then
@ -224,20 +224,28 @@ __%[1]s_handle_completion_types() {
# completions at once on the command-line we must remove the descriptions. # completions at once on the command-line we must remove the descriptions.
# https://github.com/spf13/cobra/issues/1508 # https://github.com/spf13/cobra/issues/1508
local tab=$'\t' comp local tab=$'\t' comp
while IFS='' read -r comp; do for comp in "${completions[@]}"; do
[[ -z $comp ]] && continue [[ -z $comp ]] && continue
# Strip any description # Strip any description
comp=${comp%%%%$tab*} comp=${comp%%%%$tab*}
# Only consider the completions that match # Only consider the completions that match
if [[ $comp == "$cur"* ]]; then if [[ $comp == "$cur"* ]]; then
COMPREPLY+=("$comp") COMPREPLY+=("$comp")
fi fi
done < <(printf "%%s\n" "${completions[@]}") done
IFS=$'\n' read -ra COMPREPLY -d '' < <(printf "%%q\n" "${COMPREPLY[@]}")
;; ;;
*) *)
# Type: complete (normal completion) # Type: complete (normal completion)
__%[1]s_handle_standard_completion_case __%[1]s_handle_standard_completion_case
# If there is a single completion left, escape the completion
if ((${#COMPREPLY[@]} == 1)); then
COMPREPLY[0]="$(printf "%%q" "${COMPREPLY[0]}")"
fi
;; ;;
esac esac
} }
@ -247,7 +255,12 @@ __%[1]s_handle_standard_completion_case() {
# Short circuit to optimize if we don't have descriptions # Short circuit to optimize if we don't have descriptions
if [[ "${completions[*]}" != *$tab* ]]; then if [[ "${completions[*]}" != *$tab* ]]; then
IFS=$'\n' read -ra COMPREPLY -d '' < <(compgen -W "${completions[*]}" -- "$cur") # compgen's -W option respects shell quoting, so we need to escape.
local compgen_words="$(printf "%%q\n" "${completions[@]}")"
# compgen appears to respect shell quoting _after_ checking whether
# they have the right prefix, so we also need to quote cur.
local compgen_cur="$(printf "%%q" "${cur}")"
IFS=$'\n' read -ra COMPREPLY -d '' < <(IFS=$'\n'; compgen -W "${compgen_words}" -- "${compgen_cur}")
return 0 return 0
fi fi
@ -271,21 +284,30 @@ __%[1]s_handle_standard_completion_case() {
__%[1]s_debug "COMPREPLY[0]: ${COMPREPLY[0]}" __%[1]s_debug "COMPREPLY[0]: ${COMPREPLY[0]}"
comp="${COMPREPLY[0]%%%%$tab*}" comp="${COMPREPLY[0]%%%%$tab*}"
__%[1]s_debug "Removed description from single completion, which is now: ${comp}" __%[1]s_debug "Removed description from single completion, which is now: ${comp}"
COMPREPLY[0]=$comp COMPREPLY[0]="${comp}"
else # Format the descriptions else # Format the descriptions
__%[1]s_format_comp_descriptions $longest __%[1]s_format_comp_descriptions $longest
fi fi
} }
__%[1]s_handle_special_char() __%[1]s_handle_wordbreaks()
{ {
if ((${#COMPREPLY[@]} == 0)); then
return;
fi
local comp="$1" local comp="$1"
local char=$2 local i prefix
if [[ "$comp" == *${char}* && "$COMP_WORDBREAKS" == *${char}* ]]; then for ((i=0; i < ${#comp}; i++)); do
local word=${comp%%"${comp##*${char}}"} local char="${comp:$i:1}"
local idx=${#COMPREPLY[*]} if [[ "${COMP_WORDBREAKS}" == *"${char}"* ]]; then
while ((--idx >= 0)); do prefix="${comp::$i+1}"
COMPREPLY[idx]=${COMPREPLY[idx]#"$word"} fi
done
if [[ -n "${prefix}" ]]; then
for ((i=0; i < ${#COMPREPLY[@]}; i++)); do
COMPREPLY[i]=${COMPREPLY[i]#$prefix}
done done
fi fi
} }
@ -342,6 +364,21 @@ __start_%[1]s()
COMPREPLY=() COMPREPLY=()
# Omit wordbreaks that would need to be escaped.
local wordbreaks i
for ((i=0; i < ${#COMP_WORDBREAKS}; i++)); do
local char="${COMP_WORDBREAKS:$i:1}"
if [[ $'\n\t ' == *"${char}"* ]]; then
wordbreaks+="${char}"
continue
fi
if [[ "${char}" == "$(printf "%%q" "${char}")" ]]; then
wordbreaks+="${char}"
continue
fi
done
COMP_WORDBREAKS="${wordbreaks}"
# Call _init_completion from the bash-completion package # Call _init_completion from the bash-completion package
# to prepare the arguments properly # to prepare the arguments properly
if declare -F _init_completion >/dev/null 2>&1; then if declare -F _init_completion >/dev/null 2>&1; then

2
go.mod
View File

@ -3,7 +3,7 @@ module github.com/spf13/cobra
go 1.15 go 1.15
require ( require (
github.com/cpuguy83/go-md2man/v2 v2.0.5 github.com/cpuguy83/go-md2man/v2 v2.0.6
github.com/inconshreveable/mousetrap v1.1.0 github.com/inconshreveable/mousetrap v1.1.0
github.com/spf13/pflag v1.0.5 github.com/spf13/pflag v1.0.5
gopkg.in/yaml.v3 v3.0.1 gopkg.in/yaml.v3 v3.0.1

4
go.sum
View File

@ -1,5 +1,5 @@
github.com/cpuguy83/go-md2man/v2 v2.0.5 h1:ZtcqGrnekaHpVLArFSe4HK5DoKx1T0rq2DwVB0alcyc= github.com/cpuguy83/go-md2man/v2 v2.0.6 h1:XJtiaUW6dEEqVuZiMTn1ldk455QWwEIsMIJlo5vtkx0=
github.com/cpuguy83/go-md2man/v2 v2.0.5/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=