Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bash: support -- delimeter #164

Merged
merged 1 commit into from
Mar 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 27 additions & 22 deletions shtab/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ def complete_bash(parser, root_prefix=None, preamble="", choice_functions=None):
current_action_nargs=1
fi

current_action_args_start_index=$(( $word_index + 1 ))
current_action_args_start_index=$(( $word_index + 1 - $pos_only ))

current_action_is_positional=$2
}
Expand All @@ -395,6 +395,7 @@ def complete_bash(parser, root_prefix=None, preamble="", choice_functions=None):

local prefix=${root_prefix}
local word_index=0
local pos_only=0 # "--" delimeter not encountered yet
_set_parser_defaults
word_index=1

Expand All @@ -403,34 +404,38 @@ def complete_bash(parser, root_prefix=None, preamble="", choice_functions=None):
while [ $word_index -ne $COMP_CWORD ]; do
local this_word="${COMP_WORDS[$word_index]}"

if [[ -n $sub_parsers && " ${sub_parsers[@]} " == *" ${this_word} "* ]]; then
# valid subcommand: add it to the prefix & reset the current action
prefix="${prefix}_$(_shtab_replace_nonword $this_word)"
_set_parser_defaults
fi

if [[ " ${current_option_strings[@]} " == *" ${this_word} "* ]]; then
# a new action should be acquired (due to recognised option string or
# no more input expected from current action);
# the next positional action can fill in here
_set_new_action $this_word false
fi

if [[ "$current_action_nargs" != "*" ]] && \\
[[ "$current_action_nargs" != "+" ]] && \\
[[ "$current_action_nargs" != *"..." ]] && \\
(( $word_index + 1 - $current_action_args_start_index >= \\
$current_action_nargs )); then
$current_action_is_positional && let "completed_positional_actions += 1"
_set_new_action "pos_${completed_positional_actions}" true
if [[ $pos_only = 1 || " $this_word " != " -- " ]]; then
if [[ -n $sub_parsers && " ${sub_parsers[@]} " == *" ${this_word} "* ]]; then
# valid subcommand: add it to the prefix & reset the current action
prefix="${prefix}_$(_shtab_replace_nonword $this_word)"
_set_parser_defaults
fi

if [[ " ${current_option_strings[@]} " == *" ${this_word} "* ]]; then
# a new action should be acquired (due to recognised option string or
# no more input expected from current action);
# the next positional action can fill in here
_set_new_action $this_word false
fi

if [[ "$current_action_nargs" != "*" ]] && \\
[[ "$current_action_nargs" != "+" ]] && \\
[[ "$current_action_nargs" != *"..." ]] && \\
(( $word_index + 1 - $current_action_args_start_index - $pos_only >= \\
$current_action_nargs )); then
$current_action_is_positional && let "completed_positional_actions += 1"
_set_new_action "pos_${completed_positional_actions}" true
fi
else
pos_only=1 # "--" delimeter encountered
fi

let "word_index+=1"
done

# Generate the completions

if [[ "${completing_word}" == -* ]]; then
if [[ $pos_only = 0 && "${completing_word}" == -* ]]; then
# optional argument started: use option strings
COMPREPLY=( $(compgen -W "${current_option_strings[*]}" -- "${completing_word}") )
else
Expand Down