o
    (j6h#J                     @  s"  d Z ddlmZ ddlm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 ddlmZ ejrWddlmZ ddlmZ ddlmZ ddlmZ edZe Zd.ddZd/ddZ d0d d!Z!G d"d# d#Z"G d$d% d%Z#G d&d' d'Z$G d(d) d)Z%d1d,d-Z&dS )2a;  
This module started out as largely a copy paste from the stdlib's
optparse module with the features removed that we do not need from
optparse because we implement them in Click on a higher level (for
instance type handling, help formatting and a lot more).

The plan is to remove more and more from here over time.

The reason this is a different module and not optparse from the stdlib
is that there are differences in 2.x and 3.x about the error messages
generated and optparse in the stdlib uses gettext for no good reason
and might cause us issues.

Click uses parts of optparse written by Gregory P. Ward and maintained
by the Python Software Foundation. This is limited to code in parser.py.

Copyright 2001-2006 Gregory P. Ward. All rights reserved.
Copyright 2002-2006 Python Software Foundation. All rights reserved.
    )annotationsN)deque)gettext)ngettext   )BadArgumentUsage)BadOptionUsage)NoSuchOption)
UsageError)Argument)Context)Option)	ParameterVargscabc.Sequence[str]
nargs_speccabc.Sequence[int]returnGtuple[cabc.Sequence[str | cabc.Sequence[str | None] | None], list[str]]c                   s
  t t |}g }ddfdd |ra |}|du rq|dkr*|  n5|dkrJ fd	d
t|D }durB|  |t| n|dk r_durVtdt||d |sdur}t|< g t|d d |d d< t|tfS )a  Given an iterable of arguments and an iterable of nargs specifications,
    it returns a tuple with all the unpacked arguments at the first index
    and all remaining arguments as the second.

    The nargs specification is the number of arguments that should be consumed
    or `-1` to indicate that this position should eat up all the remainders.

    Missing items are filled with `None`.
    Ncdeque[V]r   V | Nonec                   s2   z d u r
|   W S |  W S  ty   Y d S w N)popleftpop
IndexError)r   )spos [/var/www/html/chefvision.cloud.itp360.com/venv/lib/python3.10/site-packages/click/parser.py_fetchE   s   

z_unpack_args.<locals>._fetchr   c                   s   g | ]} qS r   r   ).0_)r    r   r   r   
<listcomp>W   s    z _unpack_args.<locals>.<listcomp>r   zCannot have two nargs < 0)r   r   r   r   )	r   appendrangereversetuple	TypeErrorlenreversedlist)r   r   rvnargsxr   )r    r   r   r   _unpack_args4   s6   	
 r/   optstrtuple[str, str]c                 C  sT   | d d }|  rd| fS | dd |kr"| d d | dd  fS || dd  fS )Nr       )isalnum)r0   firstr   r   r   
_split_optp   s   r7   ctxContext | Nonec                 C  s4   |d u s	|j d u r| S t| \}} | | |  S r   )token_normalize_funcr7   )r0   r8   prefixr   r   r   _normalize_opty   s   r<   c                   @  s6   e Zd Z			ddddZedddZdddZdS )_OptionNr   obj
CoreOptionoptsr   dest
str | Noneactionr-   intconstt.Any | Nonec           
      C  s   g | _ g | _t | _|D ]9}t|\}}	|std| d| j|d  t|dkr9t|	dkr9| j | q| j| | j| q|d u rLd}|| _	|| _
|| _|| _|| _d S )Nz$Invalid start character for option ()r   r   store)_short_opts
_long_optssetprefixesr7   
ValueErroraddr)   r$   rA   rC   r-   rE   r>   )
selfr>   r@   rA   rC   r-   rE   r0   r;   valuer   r   r   __init__   s&   	
z_Option.__init__r   boolc                 C  s
   | j dv S )N)rH   r$   )rC   rO   r   r   r   takes_value   s   
z_Option.takes_valuerP   t.Anystate_ParsingStateNonec                 C  s   | j dkr||j| j< nM| j dkr| j|j| j< n@| j dkr*|j| jg | n/| j dkr<|j| jg | j n| j dkrP|j| jdd |j| j< n	td| j  d	|j| j	 d S )
NrH   store_constr$   append_constcountr   r   zunknown action '')
rC   r@   rA   rE   
setdefaultr$   getrM   orderr>   )rO   rP   rV   r   r   r   process   s   




z_Option.processNr   N)r>   r?   r@   r   rA   rB   rC   rB   r-   rD   rE   rF   )r   rR   )rP   rU   rV   rW   r   rX   )__name__
__module____qualname__rQ   propertyrT   r`   r   r   r   r   r=      s    !r=   c                   @  s"   e Zd Zdddd	ZdddZdS )	_Argumentr   r>   CoreArgumentrA   rB   r-   rD   c                 C  s   || _ || _|| _d S r   )rA   r-   r>   rO   r>   rA   r-   r   r   r   rQ      s   
z_Argument.__init__rP   &str | cabc.Sequence[str | None] | NonerV   rW   r   rX   c                 C  s   | j dkr.|d usJ tdd |D }|t|krd }n|dkr.ttdj| j| j d| j dkr?| jjd ur?|dkr?d }||j	| j< |j
| j d S )	Nr   c                 s  s    | ]	}|d u rdV  qd S )Nr   r   r!   r.   r   r   r   	<genexpr>   s    z$_Argument.process.<locals>.<genexpr>r   z'Argument {name!r} takes {nargs} values.namer-   r   )r-   sumr)   r   r"   formatrA   r>   envvarr@   r_   r$   )rO   rP   rV   holesr   r   r   r`      s   
z_Argument.processNr   )r>   rg   rA   rB   r-   rD   )rP   ri   rV   rW   r   rX   )rb   rc   rd   rQ   r`   r   r   r   r   rf      s    rf   c                   @  s   e Zd ZdddZdS )	rW   rargs	list[str]r   rX   c                 C  s   i | _ g | _|| _g | _d S r   )r@   largsrt   r_   )rO   rt   r   r   r   rQ      s   
z_ParsingState.__init__N)rt   ru   r   rX   )rb   rc   rd   rQ   r   r   r   r   rW      s    rW   c                   @  s   e Zd ZdZd5d6ddZ				d7d8ddZd9d:ddZd;ddZd<d!d"Zd<d#d$Z	d=d(d)Z
d>d+d,Zd?d1d2Zd>d3d4ZdS )@_OptionParseraj  The option parser is an internal class that is ultimately used to
    parse options and arguments.  It's modelled after optparse and brings
    a similar but vastly simplified API.  It should generally not be used
    directly as the high level Click classes wrap it for you.

    It's not nearly as extensible as optparse or argparse as it does not
    implement features that are implemented on a higher level (such as
    types or defaults).

    :param ctx: optionally the :class:`~click.Context` where this parser
                should go with.

    .. deprecated:: 8.2
        Will be removed in Click 9.0.
    Nr8   r9   r   rX   c                 C  sJ   || _ d| _d| _|d ur|j| _|j| _i | _i | _ddh| _g | _d S )NTF---)r8   allow_interspersed_argsignore_unknown_options
_short_opt	_long_opt_opt_prefixes_args)rO   r8   r   r   r   rQ      s   

z_OptionParser.__init__r   r>   r?   r@   r   rA   rB   rC   r-   rD   rE   rF   c           	        sd    fdd|D }t ||||||d} j|j |jD ]}| j|< q|jD ]}| j|< q(dS )az  Adds a new option named `dest` to the parser.  The destination
        is not inferred (unlike with optparse) and needs to be explicitly
        provided.  Action can be any of ``store``, ``store_const``,
        ``append``, ``append_const`` or ``count``.

        The `obj` can be used to identify the option in the order list
        that is returned from the parser.
        c                   s   g | ]}t | jqS r   )r<   r8   )r!   r0   rS   r   r   r#     s    z,_OptionParser.add_option.<locals>.<listcomp>)rC   r-   rE   N)r=   r~   updaterL   rI   r|   rJ   r}   )	rO   r>   r@   rA   rC   r-   rE   optionr0   r   rS   r   
add_option  s   

z_OptionParser.add_optionrg   c                 C  s   | j t|||d dS )zAdds a positional argument named `dest` to the parser.

        The `obj` can be used to identify the option in the order list
        that is returned from the parser.
        )rA   r-   N)r   r$   rf   rh   r   r   r   add_argument   s   z_OptionParser.add_argumentr   ru   7tuple[dict[str, t.Any], list[str], list[CoreParameter]]c                 C  sX   t |}z| | | | W n ty#   | jdu s | jjs! Y nw |j|j|jfS )aZ  Parses positional arguments and returns ``(values, args, order)``
        for the parsed options and arguments as well as the leftover
        arguments if there are any.  The order is a list of objects as they
        appear on the command line.  If arguments appear multiple times they
        will be memorized multiple times as well.
        N)	rW   _process_args_for_options_process_args_for_argsr
   r8   resilient_parsingr@   rv   r_   )rO   r   rV   r   r   r   
parse_args(  s   	
z_OptionParser.parse_argsrV   rW   c                 C  sV   t |j|j dd | jD \}}t| jD ]\}}||| | q||_g |_d S )Nc                 S  s   g | ]}|j qS r   )r-   rj   r   r   r   r#   <  s    z8_OptionParser._process_args_for_args.<locals>.<listcomp>)r/   rv   rt   r   	enumerater`   )rO   rV   pargsr   idxargr   r   r   r   :  s   
z$_OptionParser._process_args_for_argsc                 C  s   |j r?|j d}t|}|dkrd S |d d | jv r'|dkr'| || n| jr1|j| n	|j d| d S |j sd S d S )Nr   ry   r   )	rt   r   r)   r~   _process_optsrz   rv   r$   insert)rO   rV   r   arglenr   r   r   r   E  s   z'_OptionParser._process_args_for_optionsr0   r1   explicit_valuec                 C  s   || j vrddlm} ||| j }t||| jd| j | }|jr4|d ur,|jd| | |||}n|d urCt	|t
dj|dd }||| d S )Nr   )get_close_matches)possibilitiesr8   z&Option {name!r} does not take a value.)rm   )r}   difflibr   r	   r8   rT   rt   r   _get_value_from_stater   r"   rp   r`   )rO   r0   r   rV   r   r   r   rP   r   r   r   _match_long_opti  s   

z_OptionParser._match_long_optr   c                 C  s   d}d}|d }g }|dd  D ]R}t | | | j}| j|}	|d7 }|	s8| jr1|| qt|| jd|	jrV|t|k rN|j	
d||d   d}| ||	|}
nd }
|	|
| |rb nq| jrw|ry|j| d|  d S d S d S )NFr   r   )r8   Tr3   )r<   r8   r|   r^   r{   r$   r	   rT   r)   rt   r   r   r`   rv   join)rO   r   rV   stopir;   unknown_optionschr0   r   rP   r   r   r   _match_short_opt  s4   

z_OptionParser._match_short_optoption_namer   r=   rU   c                 C  s   |j }t|j|k r |jjrt}|S t|tdd|j||d|dkrM|jd }|jjrEt|t	rE|d d | j
v rEt|dkrEt}|S |jd}|S t|jd | }|jd |= |S )Nz%Option {name!r} requires an argument.z+Option {name!r} requires {nargs} arguments.rl   r   r   )r-   r)   rt   r>   _flag_needs_valuer   r   rp   
isinstancer1   r~   r   r'   )rO   r   r   rV   r-   rP   	next_rargr   r   r   r     s<   
z#_OptionParser._get_value_from_statec                 C  s   d }d|v r| dd\}}n|}t|| j}z
| ||| W d S  tyG   |d d | jvr:| || Y d S | js> |j	| Y d S w )N=r   r4   )
splitr<   r8   r   r	   r~   r   r{   rv   r$   )rO   r   rV   r   long_optnorm_long_optr   r   r   r     s   z_OptionParser._process_optsr   )r8   r9   r   rX   ra   )r>   r?   r@   r   rA   rB   rC   rB   r-   rD   rE   rF   r   rX   rs   )r>   rg   rA   rB   r-   rD   r   rX   )r   ru   r   r   )rV   rW   r   rX   )r0   r1   r   rB   rV   rW   r   rX   )r   r1   rV   rW   r   rX   )r   r1   r   r=   rV   rW   r   rU   )rb   rc   rd   __doc__rQ   r   r   r   r   r   r   r   r   r   r   r   r   r   rw      s    



$

(&rw   rm   objectc                 C  sh   dd l }| dv r|jd|  dtdd t d|   S | dkr0d	d
lm} |jdtdd |S t| )Nr   >   r   r   	split_optOptionParserParsingStatenormalize_optz'parser.z\' is deprecated and will be removed in Click 9.0. The old parser is available in 'optparse'.r4   )
stacklevelr"   split_arg_stringr   )r   zpImporting 'parser.split_arg_string' is deprecated, it will only be available in 'shell_completion' in Click 9.0.)warningswarnDeprecationWarningglobalsshell_completionr   AttributeError)rm   r   r   r   r   r   __getattr__  s"   
r   )r   r   r   r   r   r   )r0   r1   r   r2   )r0   r1   r8   r9   r   r1   )rm   r1   r   r   )'r   
__future__r   collections.abcabccabctypingtcollectionsr   r   r"   r   
exceptionsr   r   r	   r
   TYPE_CHECKINGcorer   rg   r   r   r?   r   CoreParameterTypeVarr   r   r   r/   r7   r<   r=   rf   rW   rw   r   r   r   r   r   <module>   s8    


<
	6   