diff --git a/lib/bash/cli/lib_cli.sh b/lib/bash/cli/lib_cli.sh index 2a4ca66..d55a6e1 100644 --- a/lib/bash/cli/lib_cli.sh +++ b/lib/bash/cli/lib_cli.sh @@ -1622,10 +1622,9 @@ base_cli_completion_script() { printf '%s\n' ' if [[ "$cursor" =~ ^[0-9]+$ ]]; then cursor=$((10#$cursor)); else cursor=0; fi' printf '%s\n' ' word_count="${#completion_words[@]}"' printf '%s\n' ' if ((cursor > 0)); then' - printf '%s\n' ' if ((cursor < word_count)); then' - printf '%s\n' ' cli_words=( "${completion_words[@]:1:cursor}" )' - printf '%s\n' ' else' - printf '%s\n' ' cli_words=( "${completion_words[@]:1}" )' + printf '%s\n' ' cli_words=( "${completion_words[@]:1:cursor}" )' + printf '%s\n' ' if ((cursor >= word_count)); then' + printf '%s\n' ' # COMP_CWORD at or beyond the array end means the current word is empty.' printf '%s\n' ' cli_words+=("")' printf '%s\n' ' fi' printf '%s\n' ' fi' diff --git a/lib/bash/cli/tests/lib_cli.bats b/lib/bash/cli/tests/lib_cli.bats index 28669e4..d2557ed 100644 --- a/lib/bash/cli/tests/lib_cli.bats +++ b/lib/bash/cli/tests/lib_cli.bats @@ -760,10 +760,22 @@ EOF _cursor_complete [ "${COMPREPLY[*]}" = user ] + COMP_CWORD=99 + _cursor_complete + [ "${COMPREPLY[*]}" = user ] + COMP_WORDS=(cursor) COMP_CWORD=1 _cursor_complete [ "${COMPREPLY[*]}" = admin ] + + unset COMP_WORDS COMP_CWORD + set -u + _cursor_complete + status=$? + set +u + [ "$status" -eq 0 ] + [ "${COMPREPLY[*]}" = admin ] } @test "completion consumes option values and honors the double-dash boundary" {