Skip to content

Commit

Permalink
Merge pull request #164 from iterative/double-dash
Browse files Browse the repository at this point in the history
bash: support -- delimeter
  • Loading branch information
casperdcl authored Mar 7, 2024
2 parents c65c362 + 3b40ff3 commit 61ec99d
Showing 1 changed file with 27 additions and 22 deletions.
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

0 comments on commit 61ec99d

Please sign in to comment.