Skip to content

Commit

Permalink
bash: support -- delimeter
Browse files Browse the repository at this point in the history
- fixes #163
  • Loading branch information
casperdcl committed Feb 22, 2024
1 parent 2be59e4 commit 3d583e0
Showing 1 changed file with 26 additions and 21 deletions.
47 changes: 26 additions & 21 deletions shtab/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,40 +397,45 @@ def complete_bash(parser, root_prefix=None, preamble="", choice_functions=None):
local word_index=0
_set_parser_defaults
word_index=1
local pos_only=0 # "--" delimeter not encountered yet
# determine what arguments are appropriate for the current state
# of the arg parser
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 >= \\
$current_action_nargs + $pos_only )); 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

0 comments on commit 3d583e0

Please sign in to comment.