Skip to content

Commit

Permalink
fix(_comp_{first_arg,count_args}): count - as argument
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed Sep 1, 2023
1 parent 9fee0e8 commit e23a79e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
4 changes: 2 additions & 2 deletions bash_completion
Original file line number Diff line number Diff line change
Expand Up @@ -2165,7 +2165,7 @@ _comp_get_first_arg()

arg=
for ((i = 1; i < cword; i++)); do
if [[ ${words[i]} != -* ]]; then
if [[ ${words[i]} != -?* ]]; then
arg=${words[i]}
break
fi
Expand All @@ -2187,7 +2187,7 @@ _comp_count_args()
ret=1
for ((i = 1; i < cword; i++)); do
# shellcheck disable=SC2053
if [[ ${words[i]} != -* && ${words[i - 1]} != ${2-} ||
if [[ ${words[i]} != -?* && ${words[i - 1]} != ${2-} ||
${words[i]} == ${3-} ]]; then
((ret++))
fi
Expand Down
17 changes: 13 additions & 4 deletions bash_completion.d/000_bash_completion_compat.bash
Original file line number Diff line number Diff line change
Expand Up @@ -423,12 +423,21 @@ _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.
# shellcheck disable=SC2178 # assignments are not intended for global "args"
_count_args()
{
local ret
_comp_count_args "$@"
# shellcheck disable=SC2178
args=$ret
local i cword words
_comp__reassemble_words "${1-}" words cword

args=1
for ((i = 1; i < cword; i++)); do
# shellcheck disable=SC2053
if [[ ${words[i]} != -* && ${words[i - 1]} != ${2-} ||
${words[i]} == ${3-} ]]; then
((args++))
fi
done
}

# ex: filetype=sh
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 @@ -66,3 +66,15 @@ def test_9(self, bash):
bash, "(a -b -c d e)", 4, "a -b -c d e", 11, arg='"" "" "-b"'
)
assert output == "3"

def test_10_single_hyphen_1(self, bash):
"""- should be counted as an argument representing stdout/stdin"""
output = self._test(bash, "(a -b - c -d e)", 5, "a -b - c -d e", 12)
assert output == "3"

def test_10_single_hyphen_2(self, bash):
"""- in an option argument should be skipped"""
output = self._test(
bash, "(a -b - c - e)", 5, "a -b - c - e", 11, arg='"" "-b"'
)
assert output == "3"
7 changes: 7 additions & 0 deletions test/t/unit/test_unit_get_first_arg.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,10 @@ def test_6(self, bash, functions):
bash, '_comp__test_unit "(a -b -c d e)" 4', want_output=None
).strip()
assert output == "d"

def test_7_single_hyphen(self, bash, functions):
"""- should be counted as an argument representing stdout/stdin"""
output = assert_bash_exec(
bash, '_comp__test_unit "(a -b - c -d e)" 5', want_output=None
).strip()
assert output == "-"

0 comments on commit e23a79e

Please sign in to comment.