Skip to content

Commit 21d3122

Browse files
committed
fix(_comp_count_args): perform optarg check also on $3
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.
1 parent 9bfd760 commit 21d3122

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

bash_completion

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2190,8 +2190,8 @@ _comp_count_args()
21902190
ret=1
21912191
for ((i = 1; i < cword; i++)); do
21922192
# shellcheck disable=SC2053
2193-
if [[ ${words[i]} != -?* && ${words[i - 1]} != ${2-} ||
2194-
${words[i]} == ${3-} ]]; then
2193+
if [[ (${words[i]} != -?* || ${words[i]} == ${3-}) &&
2194+
${words[i - 1]} != ${2-} ]]; then
21952195
((ret++))
21962196
elif [[ ${words[i]} == -- ]]; then
21972197
((ret += cword - i - 1))

test/t/unit/test_unit_count_args.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,24 @@ def test_11_double_hyphen_2(self, bash):
9090
"""all the words after -- should be counted"""
9191
output = self._test(bash, "(a b -- -c -d e)", 5, "a b -- -c -d e", 13)
9292
assert output == "4"
93+
94+
def test_12_exclude_optarg_1(self, bash):
95+
"""an option argument should be skipped even if it matches the argument pattern"""
96+
output = self._test(
97+
bash, "(a -o -x b c)", 4, "a -o -x b c", 10, arg='"" "-o" "-x"'
98+
)
99+
assert output == "2"
100+
101+
def test_12_exclude_optarg_2(self, bash):
102+
"""an option argument should be skipped even if it matches the argument pattern"""
103+
output = self._test(
104+
bash, "(a -o -x -x c)", 4, "a -o -x -x c", 11, arg='"" "-o" "-x"'
105+
)
106+
assert output == "2"
107+
108+
def test_12_exclude_optarg_3(self, bash):
109+
"""an option argument should be skipped even if it matches the argument pattern"""
110+
output = self._test(
111+
bash, "(a -o -x -y c)", 4, "a -o -x -y c", 11, arg='"" "-o" "-x"'
112+
)
113+
assert output == "1"

0 commit comments

Comments
 (0)