Skip to content

Commit

Permalink
fix(_comp_count_args): perform optarg check also on $3
Browse files Browse the repository at this point in the history
The check that unconditionally accepts a word by a pattern has been
added by commits 75ec298 and 3809d95.  However, even if the word
matches the specified pattern, if the word is an option argument of
the previous option, it should not be counted as an argument.  This
patch changes the behavior so that it does not treat the word matching
$3 as an argument when it is an option argument.  The current use case
of $3 is only by chmod, where $2 is unspecified, so the behavior is
unaffected by this change.
  • Loading branch information
akinomyoga committed Sep 1, 2023
1 parent 9bfd760 commit 21d3122
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
4 changes: 2 additions & 2 deletions bash_completion
Expand Up @@ -2190,8 +2190,8 @@ _comp_count_args()
ret=1
for ((i = 1; i < cword; i++)); do
# shellcheck disable=SC2053
if [[ ${words[i]} != -?* && ${words[i - 1]} != ${2-} ||
${words[i]} == ${3-} ]]; then
if [[ (${words[i]} != -?* || ${words[i]} == ${3-}) &&
${words[i - 1]} != ${2-} ]]; then
((ret++))
elif [[ ${words[i]} == -- ]]; then
((ret += cword - i - 1))
Expand Down
21 changes: 21 additions & 0 deletions test/t/unit/test_unit_count_args.py
Expand Up @@ -90,3 +90,24 @@ 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"

def test_12_exclude_optarg_1(self, bash):
"""an option argument should be skipped even if it matches the argument pattern"""
output = self._test(
bash, "(a -o -x b c)", 4, "a -o -x b c", 10, arg='"" "-o" "-x"'
)
assert output == "2"

def test_12_exclude_optarg_2(self, bash):
"""an option argument should be skipped even if it matches the argument pattern"""
output = self._test(
bash, "(a -o -x -x c)", 4, "a -o -x -x c", 11, arg='"" "-o" "-x"'
)
assert output == "2"

def test_12_exclude_optarg_3(self, bash):
"""an option argument should be skipped even if it matches the argument pattern"""
output = self._test(
bash, "(a -o -x -y c)", 4, "a -o -x -y c", 11, arg='"" "-o" "-x"'
)
assert output == "1"

0 comments on commit 21d3122

Please sign in to comment.