Skip to content

Commit caf7bf2

Browse files
committed
fix(_comp_count_args): check optarg correctly
When the current implementation checks an option argument, it tests whether the previous word matches $2 (i.e., a pattern of options taking an option argument). This implementation has multiple issues: * When the options taking an option argument does not start with `-`, the option is counted as an independent argument. For example, `ssh` completion passes `@(-c|[-+]o)` as $2, but `+o` is counted as an argument with the current implementation. * An option argument that looks like an option taking an option argument can prevent the next word counted as an argument. For example, when `cmd -o -o arg` is processed with $2 being `-o`, the second `-o` should be treated as an option argument and `arg` should be counted as an argument. However, with the current implementation, `arg` is not counted because the previous word `-o` matches the pattern. * When `cmd -o -- -x` is processed with $2 being `-o`, `--` should be treated as an option argument so should lose its special meaning, and `-x` is still treated as an option. However, with the current implementation, `--` does not lose its special meaning, so `-x` is unexpectedly treated as an argument. * Also, with the current implementation, when $2 is not specified, an argument after an empty is not counted as an argument because $2 matches an empty word, (though Readline usually do not store an empty word in COMP_WORDS). This patch fixes those issues by changing how options taking an option argument are processed.
1 parent de888ac commit caf7bf2

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

bash_completion

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2171,8 +2171,9 @@ _comp_count_args()
21712171
ret=1
21722172
for ((i = 1; i < cword; i++)); do
21732173
# shellcheck disable=SC2053
2174-
if [[ (${words[i]} != -?* || ${3-} && ${words[i]} == ${3-}) &&
2175-
${words[i - 1]} != ${2-} ]]; then
2174+
if [[ ${2-} && ${words[i]} == ${2-} ]]; then
2175+
((i++))
2176+
elif [[ ${words[i]} != -?* || ${3-} && ${words[i]} == ${3-} ]]; then
21762177
((ret++))
21772178
elif [[ ${words[i]} == -- ]]; then
21782179
((ret += cword - i - 1))

0 commit comments

Comments
 (0)