Skip to content

Commit 22a8e03

Browse files
committed
refactor(_comp_count_args): pass arguments through options
1 parent 554a8d0 commit 22a8e03

File tree

15 files changed

+45
-25
lines changed

15 files changed

+45
-25
lines changed

bash_completion

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2189,23 +2189,43 @@ _comp_get_first_arg()
21892189
}
21902190

21912191
# This function counts the number of args, excluding options
2192-
# @param $1 chars Characters out of $COMP_WORDBREAKS which should
2193-
# NOT be considered word breaks. See _comp__reassemble_words.
2194-
# @param $2 glob Options whose following argument should not be counted
2195-
# @param $3 glob Options that should be counted as args
2192+
#
2193+
# Options:
2194+
# -n CHARS Characters out of $COMP_WORDBREAKS which should
2195+
# NOT be considered word breaks. See
2196+
# _comp__reassemble_words.
2197+
# -a GLOB Options whose following argument should not be counted
2198+
# -i GLOB Options that should be counted as args
2199+
#
21962200
# @var[out] ret Return the number of arguments
21972201
# @since 2.12
21982202
_comp_count_args()
21992203
{
2200-
local i cword words
2201-
_comp__reassemble_words "${1-}<>&" words cword
2204+
local has_optarg="" has_exclude="" exclude="" glob_include=""
2205+
local OPTIND=1 OPTARG="" OPTERR=0 _opt
2206+
while getopts ':a:n:i:' _opt "$@"; do
2207+
case $_opt in
2208+
a) has_optarg=$OPTARG ;;
2209+
n) has_exclude=set exclude+=$OPTARG ;;
2210+
i) glob_include=$OPTARG ;;
2211+
*)
2212+
echo "bash_completion: $FUNCNAME: usage error" >&2
2213+
return 2
2214+
;;
2215+
esac
2216+
done
2217+
shift "$((OPTIND - 1))"
2218+
2219+
local cword words
2220+
_comp__reassemble_words "$exclude<>&" words cword
22022221

2222+
local i
22032223
ret=1
22042224
for ((i = 1; i < cword; i++)); do
22052225
# shellcheck disable=SC2053
2206-
if [[ ${2-} && ${words[i]} == ${2-} ]]; then
2226+
if [[ $has_optarg && ${words[i]} == $has_optarg ]]; then
22072227
((i++))
2208-
elif [[ ${words[i]} != -?* || ${3-} && ${words[i]} == ${3-} ]]; then
2228+
elif [[ ${words[i]} != -?* || $glob_include && ${words[i]} == $glob_include ]]; then
22092229
((ret++))
22102230
elif [[ ${words[i]} == -- ]]; then
22112231
((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)