Skip to content

Commit 443d0a2

Browse files
committed
fix(_comp_{first_arg,count_args}): count any arguments after --
1 parent 37f458d commit 443d0a2

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

bash_completion

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2168,6 +2168,9 @@ _comp_get_first_arg()
21682168
if [[ ${words[i]} != -?* ]]; then
21692169
arg=${words[i]}
21702170
break
2171+
elif [[ ${words[i]} == -- ]]; then
2172+
((i + 1 < cword)) && arg=${words[i + 1]}
2173+
break
21712174
fi
21722175
done
21732176
}
@@ -2190,6 +2193,9 @@ _comp_count_args()
21902193
if [[ ${words[i]} != -?* && ${words[i - 1]} != ${2-} ||
21912194
${words[i]} == ${3-} ]]; then
21922195
((ret++))
2196+
elif [[ ${words[i]} == -- ]]; then
2197+
((ret += cword - i - 1))
2198+
break
21932199
fi
21942200
done
21952201
}

bash_completion.d/000_bash_completion_compat.bash

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,8 @@ _fstypes()
401401
# This function returns the first argument, excluding options
402402
# @deprecated 2.12 Use `_comp_get_first_arg`. Note that the new function
403403
# `_comp_get_first_arg` operates on `words` and `cword` instead of `COMP_WORDS`
404-
# and `COMP_CWORD`.
404+
# and `COMP_CWORD`. The new function considers a command-line argument after
405+
# `--` as an argument.
405406
_get_first_arg()
406407
{
407408
local i
@@ -423,7 +424,8 @@ _get_first_arg()
423424
# @var[out] args Return the number of arguments
424425
# @deprecated 2.12 Use `_comp_count_args`. Note that the new function
425426
# `_comp_count_args` returns the result in variable `ret` instead of `args`.
426-
# In the new function, `-` is also counted as an argument.
427+
# In the new function, `-` is also counted as an argument. The new function
428+
# counts all the arguments after `--`.
427429
# shellcheck disable=SC2178 # assignments are not intended for global "args"
428430
_count_args()
429431
{

test/t/unit/test_unit_count_args.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,15 @@ def test_10_single_hyphen_2(self, bash):
7878
bash, "(a -b - c - e)", 5, "a -b - c - e", 11, arg='"" "-b"'
7979
)
8080
assert output == "3"
81+
82+
def test_11_double_hyphen_1(self, bash):
83+
"""all the words after -- should be counted"""
84+
output = self._test(
85+
bash, "(a -b -- -c -d e)", 5, "a -b -- -c -d e", 14
86+
)
87+
assert output == "3"
88+
89+
def test_11_double_hyphen_2(self, bash):
90+
"""all the words after -- should be counted"""
91+
output = self._test(bash, "(a b -- -c -d e)", 5, "a b -- -c -d e", 13)
92+
assert output == "4"

test/t/unit/test_unit_get_first_arg.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,17 @@ def test_7_single_hyphen(self, bash, functions):
5656
bash, '_comp__test_unit "(a -b - c -d e)" 5', want_output=None
5757
).strip()
5858
assert output == "-"
59+
60+
def test_8_double_hyphen_1(self, bash, functions):
61+
"""any word after -- should be picked"""
62+
output = assert_bash_exec(
63+
bash, '_comp__test_unit "(a -b -- -c -d e)" 5', want_output=None
64+
).strip()
65+
assert output == "-c"
66+
67+
def test_8_double_hyphen_2(self, bash, functions):
68+
"""any word after -- should be picked only without any preceding argument"""
69+
output = assert_bash_exec(
70+
bash, '_comp__test_unit "(a b -- -c -d e)" 5', want_output=None
71+
).strip()
72+
assert output == "b"

0 commit comments

Comments
 (0)