Skip to content

Commit 1e4315a

Browse files
authored
Merge pull request #1033 from akinomyoga/refactor-api-2
refactor: rename `{ => _comp}{_get_first_arg,_count_args}`
2 parents b9fb7b8 + 874c503 commit 1e4315a

29 files changed

+411
-203
lines changed

bash_completion

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2157,39 +2157,49 @@ _comp_realcommand()
21572157
}
21582158

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

2167-
arg=
2168-
for ((i = 1; i < COMP_CWORD; i++)); do
2169-
if [[ ${COMP_WORDS[i]} != -* ]]; then
2170-
arg=${COMP_WORDS[i]}
2168+
ret=
2169+
for ((i = 1; i < cword; i++)); do
2170+
if [[ ${words[i]} != -?* ]]; then
2171+
ret=${words[i]}
2172+
return 0
2173+
elif [[ ${words[i]} == -- ]]; then
2174+
((i + 1 < cword)) && ret=${words[i + 1]} && return 0
21712175
break
21722176
fi
21732177
done
2178+
return 1
21742179
}
21752180

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

2187-
args=1
2193+
ret=1
21882194
for ((i = 1; i < cword; i++)); do
21892195
# shellcheck disable=SC2053
2190-
if [[ ${words[i]} != -* && ${words[i - 1]} != ${2-} ||
2191-
${words[i]} == ${3-} ]]; then
2192-
((args++))
2196+
if [[ ${2-} && ${words[i]} == ${2-} ]]; then
2197+
((i++))
2198+
elif [[ ${words[i]} != -?* || ${3-} && ${words[i]} == ${3-} ]]; then
2199+
((ret++))
2200+
elif [[ ${words[i]} == -- ]]; then
2201+
((ret += cword - i - 1))
2202+
break
21932203
fi
21942204
done
21952205
}

bash_completion.d/000_bash_completion_compat.bash

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,4 +398,49 @@ _fstypes()
398398
_comp_compgen -a fstypes
399399
}
400400

401+
# This function returns the first argument, excluding options
402+
# @deprecated 2.12 Use `_comp_get_first_arg`. Note that the new function
403+
# `_comp_get_first_arg` operates on `words` and `cword` instead of `COMP_WORDS`
404+
# and `COMP_CWORD`. The new function considers a command-line argument after
405+
# `--` as an argument. The new function returns the result in variable `ret`
406+
# instead of `arg`.
407+
_get_first_arg()
408+
{
409+
local i
410+
411+
arg=
412+
for ((i = 1; i < COMP_CWORD; i++)); do
413+
if [[ ${COMP_WORDS[i]} != -* ]]; then
414+
arg=${COMP_WORDS[i]}
415+
break
416+
fi
417+
done
418+
}
419+
420+
# This function counts the number of args, excluding options
421+
# @param $1 chars Characters out of $COMP_WORDBREAKS which should
422+
# NOT be considered word breaks. See _comp__reassemble_words.
423+
# @param $2 glob Options whose following argument should not be counted
424+
# @param $3 glob Options that should be counted as args
425+
# @var[out] args Return the number of arguments
426+
# @deprecated 2.12 Use `_comp_count_args`. Note that the new function
427+
# `_comp_count_args` returns the result in variable `ret` instead of `args`.
428+
# In the new function, `-` is also counted as an argument. The new function
429+
# counts all the arguments after `--`.
430+
# shellcheck disable=SC2178 # assignments are not intended for global "args"
431+
_count_args()
432+
{
433+
local i cword words
434+
_comp__reassemble_words "${1-}" words cword
435+
436+
args=1
437+
for ((i = 1; i < cword; i++)); do
438+
# shellcheck disable=SC2053
439+
if [[ ${words[i]} != -* && ${words[i - 1]} != ${2-} ||
440+
${words[i]} == ${3-} ]]; then
441+
((args++))
442+
fi
443+
done
444+
}
445+
401446
# ex: filetype=sh

completions/7z

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ _comp_cmd_7z()
8484
return
8585
fi
8686

87-
local args
88-
_count_args "="
89-
if ((args == 2)); then
87+
local ret
88+
_comp_count_args "="
89+
if ((ret == 2)); then
9090
_filedir_xspec unzip "${@:2}"
9191
# TODO: parsing 7z i output?
9292
# - how to figure out if the format is input or output?

completions/_cal

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ _comp_cmd_cal()
2828
return
2929
fi
3030

31-
local args
32-
_count_args
33-
((args == 1)) && _comp_compgen -- -W '{1..12}'
31+
local ret
32+
_comp_count_args
33+
((ret == 1)) && _comp_compgen -- -W '{1..12}'
3434
} &&
3535
complete -F _comp_cmd_cal cal ncal
3636

completions/_xm

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ _comp_cmd_xm()
1717

1818
# TODO: split longopt
1919

20-
local args command commands options
20+
local ret command commands options
2121

2222
commands='console vncviewer create new delete destroy domid domname
2323
dump-core list mem-max mem-set migrate pause reboot rename reset
@@ -79,16 +79,16 @@ _comp_cmd_xm()
7979
mem-max | pause | reboot | rename | shutdown | unpause | \
8080
vcpu-list | vcpu-pin | vcpu-set | block-list | \
8181
network-list | vtpm-list)
82-
_count_args
83-
case $args in
82+
_comp_count_args
83+
case $ret in
8484
2)
8585
_comp_cmd_xm__domain_names
8686
;;
8787
esac
8888
;;
8989
migrate)
90-
_count_args
91-
case $args in
90+
_comp_count_args
91+
case $ret in
9292
2)
9393
_comp_cmd_xm__domain_names
9494
;;
@@ -101,8 +101,8 @@ _comp_cmd_xm()
101101
_comp_compgen_filedir
102102
;;
103103
save)
104-
_count_args
105-
case $args in
104+
_comp_count_args
105+
case $ret in
106106
2)
107107
_comp_cmd_xm__domain_names
108108
;;
@@ -112,8 +112,8 @@ _comp_cmd_xm()
112112
esac
113113
;;
114114
sysrq)
115-
_count_args
116-
case $args in
115+
_comp_count_args
116+
case $ret in
117117
2)
118118
_comp_cmd_xm__domain_names
119119
;;
@@ -123,8 +123,8 @@ _comp_cmd_xm()
123123
esac
124124
;;
125125
block-attach)
126-
_count_args
127-
case $args in
126+
_comp_count_args
127+
case $ret in
128128
2)
129129
_comp_cmd_xm__domain_names
130130
;;
@@ -140,8 +140,8 @@ _comp_cmd_xm()
140140
esac
141141
;;
142142
block-detach)
143-
_count_args
144-
case $args in
143+
_comp_count_args
144+
case $ret in
145145
2)
146146
_comp_cmd_xm__domain_names
147147
;;
@@ -152,8 +152,8 @@ _comp_cmd_xm()
152152
esac
153153
;;
154154
network-attach)
155-
_count_args
156-
case $args in
155+
_comp_count_args
156+
case $ret in
157157
2)
158158
_comp_cmd_xm__domain_names
159159
;;
@@ -164,8 +164,8 @@ _comp_cmd_xm()
164164
esac
165165
;;
166166
network-detach)
167-
_count_args
168-
case $args in
167+
_comp_count_args
168+
case $ret in
169169
2)
170170
_comp_cmd_xm__domain_names
171171
;;
@@ -201,8 +201,8 @@ _comp_cmd_xm()
201201
;;
202202
esac
203203

204-
_count_args
205-
case $args in
204+
_comp_count_args
205+
case $ret in
206206
2)
207207
_comp_cmd_xm__domain_names
208208
;;

completions/arp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ _comp_cmd_arp()
3333
return
3434
fi
3535

36-
local args
37-
_count_args "" "@(--device|--protocol|--file|--hw-type|-${noargopts}[iApfHt])"
38-
case $args in
36+
local ret
37+
_comp_count_args "" "@(--device|--protocol|--file|--hw-type|-${noargopts}[iApfHt])"
38+
case $ret in
3939
1)
4040
local ips=$("$1" -an | command sed -ne \
4141
's/.*(\([0-9]\{1,3\}\(\.[0-9]\{1,3\}\)\{3\}\)).*/\1/p')

completions/avctrl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ _comp_cmd_avctrl()
88
if [[ $cur == -* ]]; then
99
_comp_compgen -- -W '--help --quiet'
1010
else
11-
local args
12-
_count_args
13-
if ((args == 1)); then
11+
local ret
12+
_comp_count_args
13+
if ((ret == 1)); then
1414
_comp_compgen -- -W 'discover switch'
1515
fi
1616
fi

completions/chmod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ _comp_cmd_chmod()
2727
return
2828
fi
2929

30-
local args
31-
_count_args "" "" "$modearg"
30+
local ret
31+
_comp_count_args "" "" "$modearg"
3232

33-
case $args in
33+
case $ret in
3434
1) ;; # mode
3535
*) _comp_compgen_filedir ;;
3636
esac

completions/chown

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ _comp_cmd_chown()
2929
--no-dereference --from --silent --quiet --reference --recursive
3030
--verbose --help --version $opts'
3131
else
32-
local args
32+
local ret
3333

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

37-
if ((args == 1)); then
37+
if ((ret == 1)); then
3838
_comp_compgen_usergroup -u
3939
else
4040
_comp_compgen_filedir

completions/cryptsetup

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,11 @@ _comp_cmd_cryptsetup()
3434

3535
[[ $was_split ]] && return
3636

37-
local arg
38-
_get_first_arg
39-
if [[ ! $arg ]]; then
40-
if [[ $cur == -* ]]; then
41-
_comp_compgen_help
42-
[[ ${COMPREPLY-} == *= ]] && compopt -o nospace
43-
else
44-
_comp_compgen -- -W 'open close resize status benchmark repair
45-
erase luksFormat luksAddKey luksRemoveKey luksChangeKey
46-
luksKillSlot luksUUID isLuks luksDump tcryptDump luksSuspend
47-
luksResume luksHeaderBackup luksHeaderRestore'
48-
fi
49-
else
50-
local args
51-
_count_args "" "-${noargopts}[chslSbopitTdM]"
37+
local ret
38+
if _comp_get_first_arg; then
39+
local arg=$ret
40+
_comp_count_args "" "-${noargopts}[chslSbopitTdM]"
41+
local args=$ret
5242
case $arg in
5343
open | create | luksOpen | loopaesOpen | tcryptOpen)
5444
case $args in
@@ -96,6 +86,16 @@ _comp_cmd_cryptsetup()
9686
esac
9787
;;
9888
esac
89+
else
90+
if [[ $cur == -* ]]; then
91+
_comp_compgen_help
92+
[[ ${COMPREPLY-} == *= ]] && compopt -o nospace
93+
else
94+
_comp_compgen -- -W 'open close resize status benchmark repair
95+
erase luksFormat luksAddKey luksRemoveKey luksChangeKey
96+
luksKillSlot luksUUID isLuks luksDump tcryptDump luksSuspend
97+
luksResume luksHeaderBackup luksHeaderRestore'
98+
fi
9999
fi
100100

101101
} &&

completions/gpgv

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ _comp_cmd_gpgv()
1919
;;
2020
esac
2121

22-
local args
23-
_count_args "" "--@(weak-digest|*-fd|keyring|homedir)"
22+
local ret
23+
_comp_count_args "" "--@(weak-digest|*-fd|keyring|homedir)"
24+
local args=$ret
2425

2526
if [[ $cur == -* && $args -eq 1 ]]; then
2627
_comp_compgen_help

0 commit comments

Comments
 (0)