Skip to content

Commit 9ec88a4

Browse files
committed
refactor(_comp_get_first_arg): return exit status
The current interface does not distinguish the case where no argument is found and the case where the first argument is an empty string. This patch returns whether the first argument is found through the exit status. This also modifies the callers so that they use the exit status to check if the first argument is specified.
1 parent 7e56c99 commit 9ec88a4

File tree

3 files changed

+58
-62
lines changed

3 files changed

+58
-62
lines changed

bash_completion

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2137,6 +2137,7 @@ _comp_realcommand()
21372137

21382138
# This function returns the first argument, excluding options
21392139
# @var[out] ret First argument if any, or otherwise an empty string
2140+
# @return True (0) if any argument is found, False (> 0) otherwise.
21402141
# @since 2.12
21412142
_comp_get_first_arg()
21422143
{
@@ -2146,12 +2147,13 @@ _comp_get_first_arg()
21462147
for ((i = 1; i < cword; i++)); do
21472148
if [[ ${words[i]} != -?* ]]; then
21482149
ret=${words[i]}
2149-
break
2150+
return 0
21502151
elif [[ ${words[i]} == -- ]]; then
2151-
((i + 1 < cword)) && ret=${words[i + 1]}
2152+
((i + 1 < cword)) && ret=${words[i + 1]} && return 0
21522153
break
21532154
fi
21542155
done
2156+
return 1
21552157
}
21562158

21572159
# This function counts the number of args, excluding options

completions/cryptsetup

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,8 @@ _comp_cmd_cryptsetup()
3535
[[ $was_split ]] && return
3636

3737
local ret
38-
_comp_get_first_arg
39-
local arg=$ret
40-
if [[ ! $arg ]]; then
41-
if [[ $cur == -* ]]; then
42-
_comp_compgen_help
43-
[[ ${COMPREPLY-} == *= ]] && compopt -o nospace
44-
else
45-
_comp_compgen -- -W 'open close resize status benchmark repair
46-
erase luksFormat luksAddKey luksRemoveKey luksChangeKey
47-
luksKillSlot luksUUID isLuks luksDump tcryptDump luksSuspend
48-
luksResume luksHeaderBackup luksHeaderRestore'
49-
fi
50-
else
38+
if _comp_get_first_arg; then
39+
local arg=$ret
5140
_comp_count_args "" "-${noargopts}[chslSbopitTdM]"
5241
local args=$ret
5342
case $arg in
@@ -97,6 +86,16 @@ _comp_cmd_cryptsetup()
9786
esac
9887
;;
9988
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
10099
fi
101100

102101
} &&

completions/hcitool

Lines changed: 42 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,7 @@ _comp_cmd_hcitool()
4646
[[ $was_split ]] && return
4747

4848
local ret
49-
_comp_get_first_arg
50-
if [[ ! $ret ]]; then
51-
if [[ $cur == -* ]]; then
52-
_comp_compgen_help
53-
else
54-
_comp_compgen -- -W 'dev inq scan name info spinq epinq cmd con cc
55-
dc sr cpt rssi lq tpl afh lst auth enc key clkoff clock'
56-
fi
57-
else
49+
if _comp_get_first_arg; then
5850
case $ret in
5951
name | info | dc | rssi | lq | afh | auth | key | clkoff | lst)
6052
_comp_count_args
@@ -97,6 +89,13 @@ _comp_cmd_hcitool()
9789
fi
9890
;;
9991
esac
92+
else
93+
if [[ $cur == -* ]]; then
94+
_comp_compgen_help
95+
else
96+
_comp_compgen -- -W 'dev inq scan name info spinq epinq cmd con cc
97+
dc sr cpt rssi lq tpl afh lst auth enc key clkoff clock'
98+
fi
10099
fi
101100
} &&
102101
complete -F _comp_cmd_hcitool hcitool
@@ -116,15 +115,7 @@ _comp_cmd_sdptool()
116115
[[ $was_split ]] && return
117116

118117
local ret
119-
_comp_get_first_arg
120-
if [[ ! $ret ]]; then
121-
if [[ $cur == -* ]]; then
122-
_comp_compgen_help
123-
else
124-
_comp_compgen -- -W 'search browse records add del get setattr
125-
setseq'
126-
fi
127-
else
118+
if _comp_get_first_arg; then
128119
case $ret in
129120
search)
130121
if [[ $cur == -* ]]; then
@@ -153,6 +144,13 @@ _comp_cmd_sdptool()
153144
fi
154145
;;
155146
esac
147+
else
148+
if [[ $cur == -* ]]; then
149+
_comp_compgen_help
150+
else
151+
_comp_compgen -- -W 'search browse records add del get setattr
152+
setseq'
153+
fi
156154
fi
157155
} &&
158156
complete -F _comp_cmd_sdptool sdptool
@@ -198,15 +196,8 @@ _comp_cmd_rfcomm()
198196
esac
199197

200198
local ret
201-
_comp_get_first_arg
202-
local arg=$ret
203-
if [[ ! $arg ]]; then
204-
if [[ $cur == -* ]]; then
205-
_comp_compgen_help
206-
else
207-
_comp_compgen -- -W 'show connect listen watch bind release'
208-
fi
209-
else
199+
if _comp_get_first_arg; then
200+
local arg=$ret
210201
_comp_count_args
211202
local args=$ret
212203
if ((args == 2)); then
@@ -220,6 +211,12 @@ _comp_cmd_rfcomm()
220211
;;
221212
esac
222213
fi
214+
else
215+
if [[ $cur == -* ]]; then
216+
_comp_compgen_help
217+
else
218+
_comp_compgen -- -W 'show connect listen watch bind release'
219+
fi
223220
fi
224221
} &&
225222
complete -F _comp_cmd_rfcomm rfcomm
@@ -238,14 +235,7 @@ _comp_cmd_ciptool()
238235
esac
239236

240237
local ret
241-
_comp_get_first_arg
242-
if [[ ! $ret ]]; then
243-
if [[ $cur == -* ]]; then
244-
_comp_compgen_help
245-
else
246-
_comp_compgen -- -W 'show search connect release loopback'
247-
fi
248-
else
238+
if _comp_get_first_arg; then
249239
case $ret in
250240
connect | release | loopback)
251241
local ret
@@ -255,6 +245,12 @@ _comp_cmd_ciptool()
255245
fi
256246
;;
257247
esac
248+
else
249+
if [[ $cur == -* ]]; then
250+
_comp_compgen_help
251+
else
252+
_comp_compgen -- -W 'show search connect release loopback'
253+
fi
258254
fi
259255
} &&
260256
complete -F _comp_cmd_ciptool ciptool
@@ -294,18 +290,7 @@ _comp_cmd_hciconfig()
294290
_comp_initialize -- "$@" || return
295291

296292
local ret
297-
_comp_get_first_arg
298-
if [[ ! $ret ]]; then
299-
if [[ $cur == -* ]]; then
300-
_comp_compgen -- -W '--help --all'
301-
else
302-
_comp_compgen -- -W 'up down reset rstat auth noauth encrypt
303-
noencrypt secmgr nosecmgr piscan noscan iscan pscan ptype name
304-
class voice iac inqmode inqdata inqtype inqparams pageparms
305-
pageto afhmode aclmtu scomtu putkey delkey commands features
306-
version revision lm'
307-
fi
308-
else
293+
if _comp_get_first_arg; then
309294
case $ret in
310295
putkey | delkey)
311296
_comp_count_args
@@ -326,6 +311,16 @@ _comp_cmd_hciconfig()
326311
fi
327312
;;
328313
esac
314+
else
315+
if [[ $cur == -* ]]; then
316+
_comp_compgen -- -W '--help --all'
317+
else
318+
_comp_compgen -- -W 'up down reset rstat auth noauth encrypt
319+
noencrypt secmgr nosecmgr piscan noscan iscan pscan ptype name
320+
class voice iac inqmode inqdata inqtype inqparams pageparms
321+
pageto afhmode aclmtu scomtu putkey delkey commands features
322+
version revision lm'
323+
fi
329324
fi
330325
} &&
331326
complete -F _comp_cmd_hciconfig hciconfig

0 commit comments

Comments
 (0)