
¿«‘_0  ã               @   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 d d l m Z d d l	 m
 Z
 d d l m Z y d  d	 l m Z Wn e k
 r¸ d  d l Z Yn Xd
 Z d Z d Z d Z d e d e d e i 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( d) „  Z" d S)*é    Né   )ÚArgument)ÚMultiCommand)ÚOption)Úsplit_arg_string)ÚChoice)Úecho)Ú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
a„  
#compdef %(script_names)s

%(complete_func)s() {
    local -a completions
    local -a completions_with_descriptions
    local -a response
    (( ! $+commands[%(script_names)s] )) && return 1

    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
    fi

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

compdef %(complete_func)s %(script_names)s
z¯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)"ÚbashÚzshÚfishz[^a-zA-Z0-9_]c             C   s]   t  j d |  j d d ƒ ƒ } t j | t ƒ } | d d j | ƒ d |  d | i j ƒ  d S)	NÚ ú-Ú_Zcomplete_funcz_{}_completionZscript_namesZautocomplete_varú;)Ú_invalid_ident_char_reÚsubÚreplaceÚ_completion_scriptsÚgetÚCOMPLETION_SCRIPT_BASHÚformatÚstrip)Ú	prog_nameÚcomplete_varÚshellZcf_nameÚscript© r   ú4/tmp/pip-build-5gj8f0j9/click/click/_bashcomplete.pyÚget_completion_script_   s    r    c             C   s>  |  j  | | d d ƒ} | j | j } x| r9t | j t ƒ r5| j j s® | j j | | ƒ \ } } } | d k r} | S| j  | | d | d d ƒ} | j | j } q6xk | r| j j | | ƒ \ } } } | d k rå | S| j  | | d | d d d d d d ƒ} | j } q± W| } | j | j } q+ Pq+ 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
    Zresilient_parsingTNÚparentZallow_extra_argsÚallow_interspersed_argsF)Zmake_contextÚprotected_argsÚargsÚ
isinstanceÚcommandr   ÚchainZresolve_command)Úclir   r$   ÚctxÚcmd_nameÚcmdZsub_ctxr   r   r   Úresolve_ctxl   s6    				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_option—   s    r-   c             C   s™   t  | t ƒ s d S| j r  d Sd } xS t t d d „  |  Dƒ ƒ ƒ D]2 \ } } | d | j k rf Pt | ƒ rF | } qF W| r• | | j k r• d Sd S)aU  
    :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 Sr   )Ú	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    		&
r9   c             C   s�   t  | t ƒ s d S|  | j } | d k r0 d S| j d k rC d St  | t j ƒ r} | j d k r} t | ƒ | j k  r} d Sd S)a`  
    :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   Únamer4   r	   ÚIterableÚlen)Zcurrent_paramsr7   Zcurrent_param_valuesr   r   r   Úis_incomplete_argument¹   s    	r>   c                s~   g  } t  | j t ƒ r: ‡  f d d †  | j j Dƒ } n@ | j d k	 rz | j d |  d | 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                s1   g  |  ]' } t  | ƒ j ˆ  ƒ r | d  f ‘ q S)N)ÚstrÚ
startswith)r/   Úc)Ú
incompleter   r   r1   Þ   s   	 z,get_user_autocompletions.<locals>.<listcomp>Nr)   r$   rB   c             S   s1   g  |  ]' } t  | t ƒ r! | n	 | d  f ‘ q S)N)r%   Útuple)r/   rA   r   r   r   r1   å   s   	 )r%   Útyper   ÚchoicesZautocompletion)r)   r$   rB   r7   ÚresultsZdynamic_completionsr   )rB   r   Úget_user_autocompletionsÒ   s    "	rG   c             c   sS   xL |  j  j |  ƒ D]8 } | j | ƒ r |  j  j |  | ƒ } | j s | Vq Wd 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_withrA   r&   r   r   r   Ú"get_visible_commands_starting_withê   s
    	rI   c                s²   t  ˆ  j t ƒ r5 | j d d „  t ˆ  | ƒ Dƒ ƒ xv ˆ  j d  k	 r­ ˆ  j ‰  t  ˆ  j t ƒ r8 ˆ  j j r8 ‡  f d d †  t ˆ  | ƒ Dƒ } | j d d „  | Dƒ ƒ q8 Wd  S)Nc             S   s%   g  |  ] } | j  | j ƒ  f ‘ q Sr   )r;   Úget_short_help_str)r/   rA   r   r   r   r1   ü   s   	z.add_subcommand_completions.<locals>.<listcomp>c                s(   g  |  ] } | j  ˆ  j k r | ‘ q Sr   )r;   r#   )r/   rA   )r)   r   r   r1     s   	c             S   s%   g  |  ] } | j  | j ƒ  f ‘ q Sr   )r;   rJ   )r/   rA   r   r   r   r1     s   	 )r%   r&   r   ÚextendrI   r!   r'   )r)   rB   Zcompletions_outZremaining_commandsr   )r)   r   Úadd_subcommand_completions÷   s    		rL   c       	         s¹  t  j | ƒ ‰  t |  | | ƒ } | d k r1 g  Sd ˆ  k } t ˆ ƒ r‚ t ˆ k r‚ ˆ j t ƒ } ˆ  j | d ƒ | d ‰ n ˆ t k r” d ‰ g  } | r*t ˆ ƒ r*xv | j j D]h ‰ t	 ˆ t
 ƒ rº ˆ j rº ‡  ‡ f d d †  ˆ j ˆ j Dƒ } | j ‡ ‡ f d d †  | Dƒ ƒ qº W| Sx6 | j j D]( ‰ t ˆ  ˆ ƒ r7t | ˆ  ˆ ˆ ƒ Sq7Wx9 | j j D]+ ‰ t | j ˆ ƒ rpt | ˆ  ˆ ˆ ƒ SqpWt | ˆ | ƒ 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
    Nz--r   é   r   c                s+   g  |  ]! } | ˆ  k s! ˆ j  r | ‘ q Sr   )Úmultiple)r/   Z	param_opt)r6   Úparamr   r   r1   /  s   	zget_choices.<locals>.<listcomp>c                s.   g  |  ]$ } | j  ˆ  ƒ r | ˆ j f ‘ q Sr   )r@   Úhelp)r/   Úo)rB   rO   r   r   r1   4  s   	 )ÚcopyÚdeepcopyr,   r-   r.   Ú	partitionÚappendr&   Úparamsr%   r   rH   r5   Zsecondary_optsrK   r9   rG   r>   rL   Úsorted)	r(   r   r$   rB   r)   Zhas_double_dashZpartition_incompleteZcompletionsZ
param_optsr   )r6   rB   rO   r   Úget_choices  s8    !rX   c             C   sµ   t  t j d ƒ } t t j d ƒ } | d | … } y | | } Wn t k
 r^ d } Yn XxO t |  | | | ƒ D]8 } t | d ƒ | ru t | d r¦ | d n d ƒ qu Wd S)NÚ
COMP_WORDSÚ
COMP_CWORDr   r   r   r   T)r   ÚosÚenvironÚintÚ
IndexErrorrX   r   )r(   r   Zinclude_descriptionsÚcwordsÚcwordr$   rB   Úitemr   r   r   Údo_completeE  s    "rb   c             C   s“   t  t j d ƒ } t j d } | d d  … } x\ t |  | | | ƒ D]E } | d r} t d j d | d d | d ƒ ƒ qF t | d ƒ qF Wd S)	NrY   rZ   r   z{arg}	{desc}r0   r   ÚdescT)r   r[   r\   rX   r   r   )r(   r   r_   rB   r$   ra   r   r   r   Údo_complete_fishY  s    
'rd   c             C   s¤   d | k r' | j  d d ƒ \ } } n | } d } | d k rY t t | | | ƒ ƒ d S| d k r  | d k r~ t |  | ƒ S| d
 k r  t |  | | d k ƒ Sd	 S)Nr   r   r   ÚsourceTZcompleter   r   F>   r   r   )Úsplitr   r    rd   rb   )r(   r   r   Zcomplete_instrr&   r   r   r   r   Úbashcompleteg  s    rg   )#rR   r[   ÚreÚcorer   r   r   Úparserr   Útypesr   Úutilsr   Úcollectionsr	   ÚImportErrorr.   r   ZCOMPLETION_SCRIPT_ZSHZCOMPLETION_SCRIPT_FISHr   Úcompiler   r    r,   r-   r9   r>   rG   rI   rL   rX   rb   rd   rg   r   r   r   r   Ú<module>   s@   #+	5