Skip to content

Commit

Permalink
fix(_comp_{first_arg,count_args}): count any arguments after --
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed Sep 1, 2023
1 parent e23a79e commit 9bfd760
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
6 changes: 6 additions & 0 deletions bash_completion
Original file line number Diff line number Diff line change
Expand Up @@ -2168,6 +2168,9 @@ _comp_get_first_arg()
if [[ ${words[i]} != -?* ]]; then
arg=${words[i]}
break
elif [[ ${words[i]} == -- ]]; then
((i + 1 < cword)) && arg=${words[i + 1]}
break
fi
done
}
Expand All @@ -2190,6 +2193,9 @@ _comp_count_args()
if [[ ${words[i]} != -?* && ${words[i - 1]} != ${2-} ||
${words[i]} == ${3-} ]]; then
((ret++))
elif [[ ${words[i]} == -- ]]; then
((ret += cword - i - 1))
break
fi
done
}
Expand Down
6 changes: 4 additions & 2 deletions bash_completion.d/000_bash_completion_compat.bash
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,8 @@ _fstypes()
# This function returns the first argument, excluding options
# @deprecated 2.12 Use `_comp_get_first_arg`. Note that the new function
# `_comp_get_first_arg` operates on `words` and `cword` instead of `COMP_WORDS`
# and `COMP_CWORD`.
# and `COMP_CWORD`. The new function considers a command-line argument after
# `--` as an argument.
_get_first_arg()
{
local i
Expand All @@ -423,7 +424,8 @@ _get_first_arg()
# @var[out] args Return the number of arguments
# @deprecated 2.12 Use `_comp_count_args`. Note that the new function
# `_comp_count_args` returns the result in variable `ret` instead of `args`.
# In the new function, `-` is also counted as an argument.
# In the new function, `-` is also counted as an argument. The new function
# counts all the arguments after `--`.
# shellcheck disable=SC2178 # assignments are not intended for global "args"
_count_args()
{
Expand Down
12 changes: 12 additions & 0 deletions test/t/unit/test_unit_count_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,15 @@ def test_10_single_hyphen_2(self, bash):
bash, "(a -b - c - e)", 5, "a -b - c - e", 11, arg='"" "-b"'
)
assert output == "3"

def test_11_double_hyphen_1(self, bash):
"""all the words after -- should be counted"""
output = self._test(
bash, "(a -b -- -c -d e)", 5, "a -b -- -c -d e", 14
)
assert output == "3"

def test_11_double_hyphen_2(self, bash):
"""all the words after -- should be counted"""
output = self._test(bash, "(a b -- -c -d e)", 5, "a b -- -c -d e", 13)
assert output == "4"
14 changes: 14 additions & 0 deletions test/t/unit/test_unit_get_first_arg.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,17 @@ def test_7_single_hyphen(self, bash, functions):
bash, '_comp__test_unit "(a -b - c -d e)" 5', want_output=None
).strip()
assert output == "-"

def test_8_double_hyphen_1(self, bash, functions):
"""any word after -- should be picked"""
output = assert_bash_exec(
bash, '_comp__test_unit "(a -b -- -c -d e)" 5', want_output=None
).strip()
assert output == "-c"

def test_8_double_hyphen_2(self, bash, functions):
"""any word after -- should be picked only without any preceding argument"""
output = assert_bash_exec(
bash, '_comp__test_unit "(a b -- -c -d e)" 5', want_output=None
).strip()
assert output == "b"

0 comments on commit 9bfd760

Please sign in to comment.