Skip to content

Commit ad4472f

Browse files
committed
refactor(_comp_count_args): pass arguments through options
1 parent 521d2bb commit ad4472f

File tree

15 files changed

+62
-32
lines changed

15 files changed

+62
-32
lines changed

bash_completion

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2212,23 +2212,43 @@ _comp_get_first_arg()
22122212
}
22132213

22142214
# This function counts the number of args, excluding options
2215-
# @param $1 chars Characters out of $COMP_WORDBREAKS which should
2216-
# NOT be considered word breaks. See _comp__reassemble_words.
2217-
# @param $2 glob Options whose following argument should not be counted
2218-
# @param $3 glob Options that should be counted as args
2215+
#
2216+
# Options:
2217+
# -n CHARS Characters out of $COMP_WORDBREAKS which should
2218+
# NOT be considered word breaks. See
2219+
# _comp__reassemble_words.
2220+
# -a GLOB Options whose following argument should not be counted
2221+
# -i GLOB Options that should be counted as args
2222+
#
22192223
# @var[out] ret Return the number of arguments
22202224
# @since 2.12
22212225
_comp_count_args()
22222226
{
2223-
local i cword words
2224-
_comp__reassemble_words "${1-}<>&" words cword
2227+
local has_optarg="" has_exclude="" exclude="" glob_include=""
2228+
local OPTIND=1 OPTARG="" OPTERR=0 _opt
2229+
while getopts ':a:n:i:' _opt "$@"; do
2230+
case $_opt in
2231+
a) has_optarg=$OPTARG ;;
2232+
n) has_exclude=set exclude+=$OPTARG ;;
2233+
i) glob_include=$OPTARG ;;
2234+
*)
2235+
echo "bash_completion: $FUNCNAME: usage error" >&2
2236+
return 2
2237+
;;
2238+
esac
2239+
done
2240+
shift "$((OPTIND - 1))"
2241+
2242+
local cword words
2243+
_comp__reassemble_words "$exclude<>&" words cword
22252244

2245+
local i
22262246
ret=1
22272247
for ((i = 1; i < cword; i++)); do
22282248
# shellcheck disable=SC2053
2229-
if [[ ${2-} && ${words[i]} == ${2-} ]]; then
2249+
if [[ $has_optarg && ${words[i]} == $has_optarg ]]; then
22302250
((i++))
2231-
elif [[ ${words[i]} != -?* || ${3-} && ${words[i]} == ${3-} ]]; then
2251+
elif [[ ${words[i]} != -?* || $glob_include && ${words[i]} == $glob_include ]]; then
22322252
((ret++))
22332253
elif [[ ${words[i]} == -- ]]; then
22342254
((ret += cword - i - 1))

completions/7z

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ _comp_cmd_7z()
8585
fi
8686

8787
local ret
88-
_comp_count_args "="
88+
_comp_count_args -n "="
8989
if ((ret == 2)); then
9090
_filedir_xspec unzip "${@:2}"
9191
# TODO: parsing 7z i output?

completions/arp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ _comp_cmd_arp()
3434
fi
3535

3636
local ret
37-
_comp_count_args "" "@(--device|--protocol|--file|--hw-type|-${noargopts}[iApfHt])"
37+
_comp_count_args -a "@(--device|--protocol|--file|--hw-type|-${noargopts}[iApfHt])"
3838
case $ret in
3939
1)
4040
local ips=$("$1" -an | command sed -ne \

completions/chmod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ _comp_cmd_chmod()
2828
fi
2929

3030
local ret
31-
_comp_count_args "" "" "$modearg"
31+
_comp_count_args -i "$modearg"
3232

3333
case $ret in
3434
1) ;; # mode

completions/chown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ _comp_cmd_chown()
3232
local ret
3333

3434
# The first argument is a usergroup; the rest are filedir.
35-
_comp_count_args :
35+
_comp_count_args -n :
3636

3737
if ((ret == 1)); then
3838
_comp_compgen_usergroup -u

completions/cryptsetup

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ _comp_cmd_cryptsetup()
3737
local ret
3838
if _comp_get_first_arg; then
3939
local arg=$ret
40-
_comp_count_args "" "-${noargopts}[chslSbopitTdM]"
40+
_comp_count_args -a "-${noargopts}[chslSbopitTdM]"
4141
local args=$ret
4242
case $arg in
4343
open | create | luksOpen | loopaesOpen | tcryptOpen)

completions/gpgv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ _comp_cmd_gpgv()
2020
esac
2121

2222
local ret
23-
_comp_count_args "" "--@(weak-digest|*-fd|keyring|homedir)"
23+
_comp_count_args -a "--@(weak-digest|*-fd|keyring|homedir)"
2424
local args=$ret
2525

2626
if [[ $cur == -* && $args -eq 1 ]]; then

completions/ifup

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ _comp_cmd_ifupdown()
2929
fi
3030

3131
local ret
32-
_comp_count_args "" "@(--allow|-i|--interfaces|--state-dir|-X|--exclude|-o)"
32+
_comp_count_args -a "@(--allow|-i|--interfaces|--state-dir|-X|--exclude|-o)"
3333

3434
if ((ret == 1)); then
3535
_comp_compgen_configured_interfaces

completions/jq

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ _comp_cmd_jq()
6666
local ret
6767
# TODO: DTRT with args taking 2 options
6868
# -f|--from-file are not counted here because they supply the filter
69-
_comp_count_args "" "@(--arg|--arg?(json|file)|--slurpfile|--indent|--run-tests|-${noargopts}L)"
69+
_comp_count_args -a "@(--arg|--arg?(json|file)|--slurpfile|--indent|--run-tests|-${noargopts}L)"
7070

7171
# 1st arg is filter
7272
((ret == 1)) && return

completions/jsonschema

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ _comp_cmd_jsonschema()
2121
fi
2222

2323
local ret
24-
_comp_count_args "" "-*"
24+
_comp_count_args -a "-*"
2525
((ret == 1)) || return
2626
_comp_compgen_filedir '@(json|schema)'
2727
} &&

0 commit comments

Comments
 (0)