
_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
zcomplete --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