def get_subparser_by_name(name: str, parser_actions: List[argparse.Action]) -> Optional[argparse.ArgumentParser]:
+ if name in ["-s", "--socket", "-c", "--config"]:
+ return None
+
for action in parser_actions:
if isinstance(action, argparse._SubParsersAction): # pylint: disable=protected-access
- if action.choices and name in action.choices:
+ if action.choices and name in action.choices.keys():
return action.choices[name]
return None
words = cmd.completion(subparser, subparser_args)
else:
words = cmd.completion(subparser)
- break
- elif uarg in ["-s", "--socket", "-c", "--config"]:
- # if arg is socket config, skip next arg
- next(uargs)
- continue
- elif uarg in words:
- # uarg is valid (complete) arg, continue
- continue
+ # break
+ # elif uarg in ["-s", "--socket", "-c", "--config"]:
+ # # if arg is socket config, skip next arg
+ # next(uargs)
+ # continue
+ # elif uarg in words:
+ # # uarg is valid (complete) arg, continue
+ # continue
# print completion words
# based on required bash/fish shell format
#/usr/bin/env bash
+_kresctl_skip_next=0
+
+_kresctl_filter_switches()
+{
+ # skip kresctl, it is not a valid argument
+ local words=("${COMP_WORDS[@]:1}")
+ local new_words=()
+ local count=0
+
+ for WORD in "${words[@]}"
+ do
+ if [[ $_kresctl_skip_next -eq 0 ]]
+ then
+ if [[ ! $WORD =~ ^-{1,2} ]]
+ then
+ new_words[count]="$WORD"
+ ((count++))
+ else
+ _kresctl_skip_next=1
+ fi
+ else
+ _kresctl_skip_next=0
+ fi
+
+ done
+
+ printf "%s\n" "${new_words[@]}"
+ return $count
+}
+
+
_kresctl_completion()
{
COMPREPLY=()
- local cur prev opts
+ local cur opts cmp_words
cur="${COMP_WORDS[COMP_CWORD]}"
- prev="${COMP_WORDS[COMP_CWORD-1]}"
- # skip kresctl, it is not a valid argument
- local cmd_words=("${COMP_WORDS[@]:1}")
+ cmp_words=($(_kresctl_filter_switches))
# check if there is a word is empty
# that means there is a space after last non-empty word
if [[ -z "$cur" ]]
then
# no word to complete, return all posible options
- opts=$(kresctl completion --bash --space "${cmd_words[@]}")
+ opts=$(kresctl completion --bash --space "${cmp_words[@]}")
else
- opts=$(kresctl completion --bash "${cmd_words[@]}")
+ opts=$(kresctl completion --bash "${cmp_words[@]}")
fi
# if there is no completion from kresctl
# auto-complete just directories and files
if [[ -z "$opts" ]]
then
- COMPREPLY=($(compgen -d -f "${cur}"))
+ COMPREPLY=($(compgen -d -f -- "${cur}"))
else
- COMPREPLY=( $(compgen -W "${opts}" ${cur}) )
+ COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
fi
return 0
}
-complete -o filenames -o dirnames -F _kresctl_completion kresctl
+complete -o filenames -o dirnames -o nosort -F _kresctl_completion kresctl