Skip to content

refactor: rename { => _comp}{_get_first_arg,_count_args} #1033

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 24 additions & 14 deletions bash_completion
Original file line number Diff line number Diff line change
Expand Up @@ -2157,39 +2157,49 @@ _comp_realcommand()
}

# This function returns the first argument, excluding options
# @param $1 chars Characters out of $COMP_WORDBREAKS which should
# NOT be considered word breaks. See _comp__reassemble_words.
# TODO:API: rename per conventions
_get_first_arg()
# @var[out] ret First argument before current being completed if any, or
# otherwise an empty string
# @return True (0) if any argument is found, False (> 0) otherwise.
# @since 2.12
_comp_get_first_arg()
{
local i

arg=
for ((i = 1; i < COMP_CWORD; i++)); do
if [[ ${COMP_WORDS[i]} != -* ]]; then
arg=${COMP_WORDS[i]}
ret=
for ((i = 1; i < cword; i++)); do
if [[ ${words[i]} != -?* ]]; then
ret=${words[i]}
return 0
elif [[ ${words[i]} == -- ]]; then
((i + 1 < cword)) && ret=${words[i + 1]} && return 0
break
fi
done
return 1
}

# This function counts the number of args, excluding options
# @param $1 chars Characters out of $COMP_WORDBREAKS which should
# NOT be considered word breaks. See _comp__reassemble_words.
# @param $2 glob Options whose following argument should not be counted
# @param $3 glob Options that should be counted as args
# TODO:API: rename per conventions
_count_args()
# @var[out] ret Return the number of arguments
# @since 2.12
_comp_count_args()
{
local i cword words
_comp__reassemble_words "${1-}" words cword

args=1
ret=1
for ((i = 1; i < cword; i++)); do
# shellcheck disable=SC2053
if [[ ${words[i]} != -* && ${words[i - 1]} != ${2-} ||
${words[i]} == ${3-} ]]; then
((args++))
if [[ ${2-} && ${words[i]} == ${2-} ]]; then
((i++))
elif [[ ${words[i]} != -?* || ${3-} && ${words[i]} == ${3-} ]]; then
((ret++))
elif [[ ${words[i]} == -- ]]; then
((ret += cword - i - 1))
break
fi
done
}
Expand Down
45 changes: 45 additions & 0 deletions bash_completion.d/000_bash_completion_compat.bash
Original file line number Diff line number Diff line change
Expand Up @@ -398,4 +398,49 @@ _fstypes()
_comp_compgen -a fstypes
}

# This function returns the first argument, excluding options
# @deprecated 2.12 Use `_comp_get_first_arg`. Note that the new function
# `_comp_get_first_arg` operates on `words` and `cword` instead of `COMP_WORDS`
# and `COMP_CWORD`. The new function considers a command-line argument after
# `--` as an argument. The new function returns the result in variable `ret`
# instead of `arg`.
_get_first_arg()
{
local i

arg=
for ((i = 1; i < COMP_CWORD; i++)); do
if [[ ${COMP_WORDS[i]} != -* ]]; then
arg=${COMP_WORDS[i]}
break
fi
done
}

# This function counts the number of args, excluding options
# @param $1 chars Characters out of $COMP_WORDBREAKS which should
# NOT be considered word breaks. See _comp__reassemble_words.
# @param $2 glob Options whose following argument should not be counted
# @param $3 glob Options that should be counted as args
# @var[out] args Return the number of arguments
# @deprecated 2.12 Use `_comp_count_args`. Note that the new function
# `_comp_count_args` returns the result in variable `ret` instead of `args`.
# In the new function, `-` is also counted as an argument. The new function
# counts all the arguments after `--`.
# shellcheck disable=SC2178 # assignments are not intended for global "args"
_count_args()
{
local i cword words
_comp__reassemble_words "${1-}" words cword

args=1
for ((i = 1; i < cword; i++)); do
# shellcheck disable=SC2053
if [[ ${words[i]} != -* && ${words[i - 1]} != ${2-} ||
${words[i]} == ${3-} ]]; then
((args++))
fi
done
}

# ex: filetype=sh
6 changes: 3 additions & 3 deletions completions/7z
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ _comp_cmd_7z()
return
fi

local args
_count_args "="
if ((args == 2)); then
local ret
_comp_count_args "="
if ((ret == 2)); then
_filedir_xspec unzip "${@:2}"
# TODO: parsing 7z i output?
# - how to figure out if the format is input or output?
Expand Down
6 changes: 3 additions & 3 deletions completions/_cal
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ _comp_cmd_cal()
return
fi

local args
_count_args
((args == 1)) && _comp_compgen -- -W '{1..12}'
local ret
_comp_count_args
((ret == 1)) && _comp_compgen -- -W '{1..12}'
} &&
complete -F _comp_cmd_cal cal ncal

Expand Down
38 changes: 19 additions & 19 deletions completions/_xm
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ _comp_cmd_xm()

# TODO: split longopt

local args command commands options
local ret command commands options

commands='console vncviewer create new delete destroy domid domname
dump-core list mem-max mem-set migrate pause reboot rename reset
Expand Down Expand Up @@ -79,16 +79,16 @@ _comp_cmd_xm()
mem-max | pause | reboot | rename | shutdown | unpause | \
vcpu-list | vcpu-pin | vcpu-set | block-list | \
network-list | vtpm-list)
_count_args
case $args in
_comp_count_args
case $ret in
2)
_comp_cmd_xm__domain_names
;;
esac
;;
migrate)
_count_args
case $args in
_comp_count_args
case $ret in
2)
_comp_cmd_xm__domain_names
;;
Expand All @@ -101,8 +101,8 @@ _comp_cmd_xm()
_comp_compgen_filedir
;;
save)
_count_args
case $args in
_comp_count_args
case $ret in
2)
_comp_cmd_xm__domain_names
;;
Expand All @@ -112,8 +112,8 @@ _comp_cmd_xm()
esac
;;
sysrq)
_count_args
case $args in
_comp_count_args
case $ret in
2)
_comp_cmd_xm__domain_names
;;
Expand All @@ -123,8 +123,8 @@ _comp_cmd_xm()
esac
;;
block-attach)
_count_args
case $args in
_comp_count_args
case $ret in
2)
_comp_cmd_xm__domain_names
;;
Expand All @@ -140,8 +140,8 @@ _comp_cmd_xm()
esac
;;
block-detach)
_count_args
case $args in
_comp_count_args
case $ret in
2)
_comp_cmd_xm__domain_names
;;
Expand All @@ -152,8 +152,8 @@ _comp_cmd_xm()
esac
;;
network-attach)
_count_args
case $args in
_comp_count_args
case $ret in
2)
_comp_cmd_xm__domain_names
;;
Expand All @@ -164,8 +164,8 @@ _comp_cmd_xm()
esac
;;
network-detach)
_count_args
case $args in
_comp_count_args
case $ret in
2)
_comp_cmd_xm__domain_names
;;
Expand Down Expand Up @@ -201,8 +201,8 @@ _comp_cmd_xm()
;;
esac

_count_args
case $args in
_comp_count_args
case $ret in
2)
_comp_cmd_xm__domain_names
;;
Expand Down
6 changes: 3 additions & 3 deletions completions/arp
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ _comp_cmd_arp()
return
fi

local args
_count_args "" "@(--device|--protocol|--file|--hw-type|-${noargopts}[iApfHt])"
case $args in
local ret
_comp_count_args "" "@(--device|--protocol|--file|--hw-type|-${noargopts}[iApfHt])"
case $ret in
1)
local ips=$("$1" -an | command sed -ne \
's/.*(\([0-9]\{1,3\}\(\.[0-9]\{1,3\}\)\{3\}\)).*/\1/p')
Expand Down
6 changes: 3 additions & 3 deletions completions/avctrl
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ _comp_cmd_avctrl()
if [[ $cur == -* ]]; then
_comp_compgen -- -W '--help --quiet'
else
local args
_count_args
if ((args == 1)); then
local ret
_comp_count_args
if ((ret == 1)); then
_comp_compgen -- -W 'discover switch'
fi
fi
Expand Down
6 changes: 3 additions & 3 deletions completions/chmod
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ _comp_cmd_chmod()
return
fi

local args
_count_args "" "" "$modearg"
local ret
_comp_count_args "" "" "$modearg"

case $args in
case $ret in
1) ;; # mode
*) _comp_compgen_filedir ;;
esac
Expand Down
6 changes: 3 additions & 3 deletions completions/chown
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ _comp_cmd_chown()
--no-dereference --from --silent --quiet --reference --recursive
--verbose --help --version $opts'
else
local args
local ret

# The first argument is a usergroup; the rest are filedir.
_count_args :
_comp_count_args :

if ((args == 1)); then
if ((ret == 1)); then
_comp_compgen_usergroup -u
else
_comp_compgen_filedir
Expand Down
30 changes: 15 additions & 15 deletions completions/cryptsetup
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,11 @@ _comp_cmd_cryptsetup()

[[ $was_split ]] && return

local arg
_get_first_arg
if [[ ! $arg ]]; then
if [[ $cur == -* ]]; then
_comp_compgen_help
[[ ${COMPREPLY-} == *= ]] && compopt -o nospace
else
_comp_compgen -- -W 'open close resize status benchmark repair
erase luksFormat luksAddKey luksRemoveKey luksChangeKey
luksKillSlot luksUUID isLuks luksDump tcryptDump luksSuspend
luksResume luksHeaderBackup luksHeaderRestore'
fi
else
local args
_count_args "" "-${noargopts}[chslSbopitTdM]"
local ret
if _comp_get_first_arg; then
local arg=$ret
_comp_count_args "" "-${noargopts}[chslSbopitTdM]"
local args=$ret
case $arg in
open | create | luksOpen | loopaesOpen | tcryptOpen)
case $args in
Expand Down Expand Up @@ -96,6 +86,16 @@ _comp_cmd_cryptsetup()
esac
;;
esac
else
if [[ $cur == -* ]]; then
_comp_compgen_help
[[ ${COMPREPLY-} == *= ]] && compopt -o nospace
else
_comp_compgen -- -W 'open close resize status benchmark repair
erase luksFormat luksAddKey luksRemoveKey luksChangeKey
luksKillSlot luksUUID isLuks luksDump tcryptDump luksSuspend
luksResume luksHeaderBackup luksHeaderRestore'
fi
fi

} &&
Expand Down
5 changes: 3 additions & 2 deletions completions/gpgv
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ _comp_cmd_gpgv()
;;
esac

local args
_count_args "" "--@(weak-digest|*-fd|keyring|homedir)"
local ret
_comp_count_args "" "--@(weak-digest|*-fd|keyring|homedir)"
local args=$ret

if [[ $cur == -* && $args -eq 1 ]]; then
_comp_compgen_help
Expand Down
Loading