ction_is_positional local current_action_nargs local current_option_strings local sub_parsers COMPREPLY=() local prefix=${root_prefix} local word_index=0 local pos_only=0 # "--" delimeter not encountered yet _set_parser_defaults word_index=1 # 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 [[ $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" != "?" ]] && \ [[ "$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 [[ $pos_only = 0 && "${completing_word}" == -* ]]; then # optional argument started: use option strings COMPREPLY=( $(compgen -W "${current_option_strings[*]}" -- "${completing_word}") ) elif [[ "${previous_word}" == ">" || "${previous_word}" == ">>" || "${previous_word}" =~ ^[12]">" || "${previous_word}" =~ ^[12]">>" ]]; then # handle redirection operators COMPREPLY=( $(compgen -f -- "${completing_word}") ) else # use choices & compgen local IFS=$'\n' # items may contain spaces, so delimit using newline COMPREPLY=( $([ -n "${current_action_compgen}" ] \ && "${current_action_compgen}" "${completing_word}") ) unset IFS COMPREPLY+=( $(compgen -W "${current_action_choices[*]}" -- "${completing_word}") ) fi return 0 } complete -o filenames -F ${root_prefix} ${prog}Ú