_G='COMP_CWORD' _F='COMP_WORDS' _E='zsh' _D='bash' _C=False _B=None _A=True import copy,os,re from collections import abc from.core import Argument from.core import MultiCommand from.core import Option from.parser import split_arg_string from.types import Choice from.utils import echo WORDBREAK='=' COMPLETION_SCRIPT_BASH='\n%(complete_func)s() {\n local IFS=$\'\n\'\n COMPREPLY=( $( env COMP_WORDS="${COMP_WORDS[*]}" \\\n COMP_CWORD=$COMP_CWORD \\\n %(autocomplete_var)s=complete $1 ) )\n return 0\n}\n\n%(complete_func)setup() {\n local COMPLETION_OPTIONS=""\n local BASH_VERSION_ARR=(${BASH_VERSION//./ })\n # Only BASH version 4.4 and later have the nosort option.\n if [ ${BASH_VERSION_ARR[0]} -gt 4 ] || ([ ${BASH_VERSION_ARR[0]} -eq 4 ] && [ ${BASH_VERSION_ARR[1]} -ge 4 ]); then\n COMPLETION_OPTIONS="-o nosort"\n fi\n\n complete $COMPLETION_OPTIONS -F %(complete_func)s %(script_names)s\n}\n\n%(complete_func)setup\n' COMPLETION_SCRIPT_ZSH='\n#compdef %(script_names)s\n\n%(complete_func)s() {\n local -a completions\n local -a completions_with_descriptions\n local -a response\n (( ! $+commands[%(script_names)s] )) && return 1\n\n response=("${(@f)$( env COMP_WORDS="${words[*]}" \\\n COMP_CWORD=$((CURRENT-1)) \\\n %(autocomplete_var)s="complete_zsh" \\\n %(script_names)s )}")\n\n for key descr in ${(kv)response}; do\n if [[ "$descr" == "_" ]]; then\n completions+=("$key")\n else\n completions_with_descriptions+=("$key":"$descr")\n fi\n done\n\n if [ -n "$completions_with_descriptions" ]; then\n _describe -V unsorted completions_with_descriptions -U\n fi\n\n if [ -n "$completions" ]; then\n compadd -U -V unsorted -a completions\n fi\n compstate[insert]="automenu"\n}\n\ncompdef %(complete_func)s %(script_names)s\n' COMPLETION_SCRIPT_FISH='complete --no-files --command %(script_names)s --arguments "(env %(autocomplete_var)s=complete_fish COMP_WORDS=(commandline -cp) COMP_CWORD=(commandline -t) %(script_names)s)"' _completion_scripts={_D:COMPLETION_SCRIPT_BASH,_E:COMPLETION_SCRIPT_ZSH,'fish':COMPLETION_SCRIPT_FISH} _invalid_ident_char_re=re.compile('[^a-zA-Z0-9_]') def get_completion_script(prog_name,complete_var,shell):A=prog_name;B=_invalid_ident_char_re.sub('',A.replace('-','_'));C=_completion_scripts.get(shell,COMPLETION_SCRIPT_BASH);return(C%{'complete_func':f"_{B}_completion",'script_names':A,'autocomplete_var':complete_var}).strip()+';' def resolve_ctx(cli,prog_name,args): B=args;A=cli.make_context(prog_name,B,resilient_parsing=_A);B=A.protected_args+A.args while B: if isinstance(A.command,MultiCommand): if not A.command.chain: E,C,B=A.command.resolve_command(A,B) if C is _B:return A A=C.make_context(E,B,parent=A,resilient_parsing=_A);B=A.protected_args+A.args else: while B: E,C,B=A.command.resolve_command(A,B) if C is _B:return A D=C.make_context(E,B,parent=A,allow_extra_args=_A,allow_interspersed_args=_C,resilient_parsing=_A);B=D.args A=D;B=D.protected_args+D.args else:break return A def start_of_option(param_str):A=param_str;return A and A[:1]=='-' def is_incomplete_option(all_args,cmd_param): A=cmd_param if not isinstance(A,Option):return _C if A.is_flag:return _C B=_B for(D,C)in enumerate(reversed([A for A in all_args if A!=WORDBREAK])): if D+1>A.nargs:break if start_of_option(C):B=C return _A if B and B in A.opts else _C def is_incomplete_argument(current_params,cmd_param): A=cmd_param if not isinstance(A,Argument):return _C B=current_params[A.name] if B is _B:return _A if A.nargs==-1:return _A if isinstance(B,abc.Iterable)and A.nargs>1 and len(B)