3
(^+                 @   s   d dl Z d dlZd dlZddlmZ ddlmZ ddlmZm	Z	m
Z
 ddlmZ yd dlmZ W n ek
r|   d dlZY nX dZd	Zd
ZejdZdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! ZdS )"    N   )echo)split_arg_string)MultiCommandOptionArgument)Choice)abc=a  
%(complete_func)s() {
    local IFS=$'
'
    COMPREPLY=( $( env COMP_WORDS="${COMP_WORDS[*]}" \
                   COMP_CWORD=$COMP_CWORD \
                   %(autocomplete_var)s=complete $1 ) )
    return 0
}

%(complete_func)setup() {
    local COMPLETION_OPTIONS=""
    local BASH_VERSION_ARR=(${BASH_VERSION//./ })
    # Only BASH version 4.4 and later have the nosort option.
    if [ ${BASH_VERSION_ARR[0]} -gt 4 ] || ([ ${BASH_VERSION_ARR[0]} -eq 4 ] && [ ${BASH_VERSION_ARR[1]} -ge 4 ]); then
        COMPLETION_OPTIONS="-o nosort"
    fi

    complete $COMPLETION_OPTIONS -F %(complete_func)s %(script_names)s
}

%(complete_func)setup
a9  
%(complete_func)s() {
    local -a completions
    local -a completions_with_descriptions
    local -a response
    response=("${(@f)$( env COMP_WORDS="${words[*]}" \
                        COMP_CWORD=$((CURRENT-1)) \
                        %(autocomplete_var)s="complete_zsh" \
                        %(script_names)s )}")

    for key descr in ${(kv)response}; do
      if [[ "$descr" == "_" ]]; then
          completions+=("$key")
      else
          completions_with_descriptions+=("$key":"$descr")
      fi
    done

    if [ -n "$completions_with_descriptions" ]; then
        _describe -V unsorted completions_with_descriptions -U -Q
    fi

    if [ -n "$completions" ]; then
        compadd -U -V unsorted -Q -a completions
    fi
    compstate[insert]="automenu"
}

compdef %(complete_func)s %(script_names)s
z[^a-zA-Z0-9_]c             C   s@   t jd| jdd}|dkr tnt}|d| | |d j d S )N -_zshz_%s_completion)Zcomplete_funcZscript_namesZautocomplete_var;)_invalid_ident_char_resubreplaceCOMPLETION_SCRIPT_ZSHCOMPLETION_SCRIPT_BASHstrip)	prog_namecomplete_varshellZcf_namescript r   4/tmp/pip-build-8app2_gc/Click/click/_bashcomplete.pyget_completion_scriptK   s    r   c             C   s   | j ||dd}|j|j }x|rt|jtr|jjsv|jj||\}}}|dkrV|S |j |||dd}|j|j }qxD|r|jj||\}}}|dkr|S |j |||dddd}|j}qxW |}|j|j }qP qW |S )a  
    Parse into a hierarchy of contexts. Contexts are connected through the parent variable.
    :param cli: command definition
    :param prog_name: the program that is running
    :param args: full list of args
    :return: the final context/command parsed
    T)resilient_parsingN)parentr   F)r   Zallow_extra_argsallow_interspersed_argsr   )Zmake_contextprotected_argsargs
isinstancecommandr   chainZresolve_command)clir   r!   ctxcmd_namecmdZsub_ctxr   r   r   resolve_ctxU   s0    


r)   c             C   s   | o| dd dkS )z
    :param param_str: param_str to check
    :return: whether or not this is the start of an option declaration (i.e. starts "-" or "--")
    Nr   r   r   )Z	param_strr   r   r   start_of_optionz   s    r*   c             C   sr   t |tsdS |jrdS d}x>ttdd | D D ]$\}}|d |jkrLP t|r4|}q4W |rn||jkrndS dS )aE  
    :param all_args: the full original list of args supplied
    :param cmd_param: the current command paramter
    :return: whether or not the last option declaration (i.e. starts "-" or "--") is incomplete and
    corresponds to this cmd_param. In other words whether this cmd_param option can still accept
    values
    FNc             S   s   g | ]}|t kr|qS r   )	WORDBREAK).0argr   r   r   
<listcomp>   s    z(is_incomplete_option.<locals>.<listcomp>r   T)r"   r   Zis_flag	enumeratereversednargsr*   opts)all_args	cmd_paramZlast_optionindexZarg_strr   r   r   is_incomplete_option   s    
 r6   c             C   s^   t |tsdS | |j }|dkr$dS |jdkr2dS t |tjrZ|jdkrZt||jk rZdS dS )aL  
    :param current_params: the current params and values for this argument as already entered
    :param cmd_param: the current command parameter
    :return: whether or not the last argument is incomplete and corresponds to this cmd_param. In
    other words whether or not the this cmd_param argument can still accept values
    FNTr   )r"   r   namer1   r	   Iterablelen)Zcurrent_paramsr4   Zcurrent_param_valuesr   r   r   is_incomplete_argument   s    


r;   c                sT   g }t |jtr( fdd|jjD }n(|jdk	rP|j| | d}dd |D }|S )a  
    :param ctx: context associated with the parsed command
    :param args: full list of args
    :param incomplete: the incomplete text to autocomplete
    :param cmd_param: command definition
    :return: all the possible user-specified completions for the param
    c                s"   g | ]}t |j r|d fqS )N)str
startswith)r,   c)
incompleter   r   r.      s   z,get_user_autocompletions.<locals>.<listcomp>N)r&   r!   r?   c             S   s"   g | ]}t |tr|n|d fqS )N)r"   tuple)r,   r>   r   r   r   r.      s   )r"   typer   choicesZautocompletion)r&   r!   r?   r4   resultsZdynamic_completionsr   )r?   r   get_user_autocompletions   s    

rD   c             c   s>   x8| j j| D ](}|j|r| j j| |}|js|V  qW dS )z
    :param ctx: context associated with the parsed command
    :starts_with: string that visible commands must start with.
    :return: all visible (not hidden) commands that start with starts_with.
    N)r#   Zlist_commandsr=   Zget_commandhidden)r&   Zstarts_withr>   r#   r   r   r   "get_visible_commands_starting_with   s
    
rF   c                s   t  jtr&|jdd t |D  xT jd k	rz j t  jtr( jjr( fddt |D }|jdd |D  q(W d S )Nc             S   s   g | ]}|j |j fqS r   )r8   get_short_help_str)r,   r>   r   r   r   r.      s    z.add_subcommand_completions.<locals>.<listcomp>c                s   g | ]}|j  jkr|qS r   )r8   r    )r,   r>   )r&   r   r   r.      s    c             S   s   g | ]}|j |j fqS r   )r8   rG   )r,   r>   r   r   r   r.      s    )r"   r#   r   extendrF   r   r$   )r&   r?   Zcompletions_outZremaining_commandsr   )r&   r   add_subcommand_completions   s    rI   c                s4  t j| t| ||}|dkr"g S trTtkrTjt} j|d  |d ntkr`dg }trxX|jjD ]Lt	t
rvj rv fddjj D }|jfdd|D  qvW |S x(|jjD ]t rt| S qW x*|jjD ]t|jrt| S qW t|| t|S )z
    :param cli: command definition
    :param prog_name: the program that is running
    :param args: full list of args
    :param incomplete: the incomplete text to autocomplete
    :return: all the possible completions for the incomplete
    Nr      r   c                s   g | ]}| ksj r|qS r   )multiple)r,   Z	param_opt)r3   paramr   r   r.      s    zget_choices.<locals>.<listcomp>c                s    g | ]}|j  r|jfqS r   )r=   help)r,   o)r?   rL   r   r   r.      s    )copydeepcopyr)   r*   r+   	partitionappendr#   paramsr"   r   rE   r2   Zsecondary_optsrH   r6   rD   r;   rI   sorted)r%   r   r!   r?   r&   Zpartition_incompleteZcompletionsZ
param_optsr   )r3   r?   rL   r   get_choices   s4    



rU   c             C   s   t tjd }ttjd }|d| }y|| }W n tk
rL   d}Y nX x>t| |||D ],}t|d  |r^t|d r|d nd q^W dS )N
COMP_WORDS
COMP_CWORDr   r   r   r   T)r   osenvironint
IndexErrorrU   r   )r%   r   Zinclude_descriptionscwordscwordr!   r?   itemr   r   r   do_complete  s    
r_   c             C   sR   |j dr.|dkrdnd}tt||| dS |dks>|dkrNt| ||dkS dS )	NsourceZ
source_zshr   bashTZcompleteZcomplete_zshF)r=   r   r   r_   )r%   r   r   Zcomplete_instrr   r   r   r   bashcomplete  s    
rb   ) rO   rX   reutilsr   parserr   corer   r   r   typesr   collectionsr	   ImportErrorr+   r   r   compiler   r   r)   r*   r6   r;   rD   rF   rI   rU   r_   rb   r   r   r   r   <module>   s2   

%.