a
    /§»aaP  ã                   @   sV  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	 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Zd dlZd dlmZ d dlmZ erìd dlmZ d dlmZ dZeG dd„ dƒƒZG dd„ deƒZG dd„ dƒZG dd„ dƒZG dd„ de j ƒZ!G dd„ de j"ƒZ#dS )é    N)Úgettext)ÚAny)ÚCallable)Úcast)ÚDict)ÚList)ÚMapping)ÚOptional)ÚSequence)ÚTuple)ÚTYPE_CHECKING)ÚUnion)Úfinal)Ú
UsageError)ÚNoReturn)ÚLiteralZfile_or_dirc                   @   sn  e Zd ZU dZdZee ed< d%ee eedgdf  ddœdd„Z	dddœd	d
„Z
d&eeee ddœdd„Zeeddœdd„Zd'eeeejjf  eej ejdœdd„Zddœdd„Zd(eeeejjf  ejeej ee dœdd„Zd)eeeejjf  eej ejdœdd„Zd*eeeejjf  eej eejee f dœdd „Zd+eeed! dd"œd#d$„ZdS ),ÚParserzÄParser for command line arguments and ini-file values.

    :ivar extra_info: Dict of generic param -> value to display in case
        there's an error processing the command line arguments.
    NÚprogÚArgument)ÚusageÚ
processoptÚreturnc                 C   s6   t d| d�| _g | _|| _|| _i | _g | _i | _d S )Nzcustom options©Úparser)ÚOptionGroupÚ
_anonymousÚ_groupsÚ_processoptÚ_usageÚ_inidictÚ	_ininamesÚ
extra_info)Úselfr   r   © r#   úS/home/yuhw/wc/pochoir/venv/lib/python3.9/site-packages/_pytest/config/argparsing.pyÚ__init__(   s    zParser.__init__)Úoptionr   c                 C   s   | j r|jr|   |¡ d S ©N)r   Údest)r"   r&   r#   r#   r$   Úprocessoption5   s    zParser.processoptionÚ r   )ÚnameÚdescriptionÚafterr   c                 C   sh   | j D ]}|j|kr|  S qt||| d�}d}t| j ƒD ]\}}|j|kr: qRq:| j  |d |¡ |S )aä  Get (or create) a named option Group.

        :name: Name of the option group.
        :description: Long description for --help output.
        :after: Name of another group, used for ordering --help output.

        The returned group object has an ``addoption`` method with the same
        signature as :py:func:`parser.addoption
        <_pytest.config.argparsing.Parser.addoption>` but will be shown in the
        respective group in the output of ``pytest. --help``.
        r   r   é   )r   r+   r   Ú	enumerateÚinsert)r"   r+   r,   r-   ÚgroupÚiÚgrpr#   r#   r$   Úgetgroup:   s    



zParser.getgroup)ÚoptsÚattrsr   c                 O   s   | j j|i |¤Ž dS )a  Register a command line option.

        :opts: Option names, can be short or long options.
        :attrs: Same attributes which the ``add_argument()`` function of the
           `argparse library <https://docs.python.org/library/argparse.html>`_
           accepts.

        After command line parsing, options are available on the pytest config
        object via ``config.option.NAME`` where ``NAME`` is usually set
        by passing a ``dest`` attribute, for example
        ``addoption("--long", dest="NAME", ...)``.
        N)r   Ú	addoption)r"   r5   r6   r#   r#   r$   r7   S   s    zParser.addoption©ÚargsÚ	namespacer   c                 C   s>   ddl m} |  ¡ | _|| jƒ dd„ |D ƒ}| jj||d�S )Nr   )Útry_argcompletec                 S   s&   g | ]}t |tjjƒrt|ƒn|‘qS r#   ©Ú
isinstanceÚpyÚpathÚlocalÚstr©Ú.0Úxr#   r#   r$   Ú
<listcomp>k   ó    z Parser.parse.<locals>.<listcomp>©r:   )Ú_pytest._argcompleter;   Ú
_getparserÚ	optparserÚ
parse_args)r"   r9   r:   r;   Ústrargsr#   r#   r$   Úparseb   s
    

zParser.parseÚMyOptionParser©r   c                 C   s–   ddl m} t| | j| jd�}| j| jg }|D ]L}|jr0|jpD|j	}| 
|¡}|jD ]$}| ¡ }| ¡ }	|j|i |	¤Ž qVq0|jtdd�}
||
_|S )Nr   )Úfilescompleter)r   Ú*)Únargs)rH   rP   rN   r!   r   r   r   Úoptionsr,   r+   Úadd_argument_groupÚnamesr6   Úadd_argumentÚFILE_OR_DIRZ	completer)r"   rP   rJ   Úgroupsr1   ÚdescZarggroupr&   ÚnÚaZfile_or_dir_argr#   r#   r$   rI   n   s    

zParser._getparser)r9   r&   r:   r   c                 C   sB   | j ||d�}|j ¡ D ]\}}t|||ƒ qttt t|tƒƒS )NrG   )	rM   Ú__dict__ÚitemsÚsetattrr   r   rA   ÚgetattrrW   )r"   r9   r&   r:   Zparsedoptionr+   Úvaluer#   r#   r$   Úparse_setoption�   s    zParser.parse_setoptionc                 C   s   | j ||d�d S )zGParse and return a namespace object with known arguments at this point.rG   r   )Úparse_known_and_unknown_args)r"   r9   r:   r#   r#   r$   Úparse_known_argsŒ   s    zParser.parse_known_argsc                 C   s$   |   ¡ }dd„ |D ƒ}|j||d�S )ztParse and return a namespace object with known arguments, and
        the remaining arguments unknown at this point.c                 S   s&   g | ]}t |tjjƒrt|ƒn|‘qS r#   r<   rB   r#   r#   r$   rE   œ   rF   z7Parser.parse_known_and_unknown_args.<locals>.<listcomp>rG   )rI   rc   )r"   r9   r:   rJ   rL   r#   r#   r$   rb   ”   s    z#Parser.parse_known_and_unknown_argsz9Literal['string', 'pathlist', 'args', 'linelist', 'bool'])r+   ÚhelpÚtyper   c                 C   s,   |dv sJ ‚|||f| j |< | j |¡ dS )aá  Register an ini-file option.

        :name: Name of the ini-variable.
        :type: Type of the variable, can be ``string``, ``pathlist``, ``args``,
               ``linelist`` or ``bool``.  Defaults to ``string`` if ``None`` or
               not passed.
        :default: Default value if no ini-file option exists but is queried.

        The value of ini-variables can be retrieved via a call to
        :py:func:`config.getini(name) <_pytest.config.Config.getini>`.
        )NÚstringÚpathlistr9   ZlinelistÚboolN)r   r    Úappend)r"   r+   rd   re   Údefaultr#   r#   r$   ÚaddiniŸ   s    zParser.addini)NN)r*   N)N)N)N)N)NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r	   rA   Ú__annotations__r   r%   r)   r4   r   r7   r
   r   r>   r?   r@   ÚargparseÚ	NamespacerM   rI   r   ra   rc   r   rb   rk   r#   r#   r#   r$   r      sh   
  ýü ÿ
þ ýü üû ýü ýü  ùÿør   c                   @   s8   e Zd ZdZeedef ddœdd„Zedœdd	„ZdS )
ÚArgumentErrorzURaised if an Argument instance is created with invalid or
    inconsistent arguments.r   N)Úmsgr&   r   c                 C   s   || _ t|ƒ| _d S r'   )rt   rA   Ú	option_id)r"   rt   r&   r#   r#   r$   r%   ¼   s    zArgumentError.__init__rO   c                 C   s$   | j rd| j › d| j› �S | jS d S )Nzoption ú: )ru   rt   ©r"   r#   r#   r$   Ú__str__À   s    zArgumentError.__str__)rl   rm   rn   ro   rA   r   r%   rx   r#   r#   r#   r$   rs   ¸   s   rs   c                   @   sz   e Zd ZdZeeeedœZee	ddœdd„Z
ee dœdd	„Zeee	f dœd
d„Zee ddœdd„Zedœdd„ZdS )r   z÷Class that mimics the necessary behaviour of optparse.Option.

    It's currently a least effort implementation and ignoring choices
    and integer prefixes.

    https://docs.python.org/3/library/optparse.html#optparse-standard-option-types
    )Úintrf   ÚfloatÚcomplexN)rU   r6   r   c              
   O   sŒ  || _ g | _g | _d| d¡p dv r4tjdtdd� z|d }W n tyR   Y nz0 t|t	ƒrÆ|dkr”tjd	||f td
d� t
|d d ƒ|d< n&tjd||f td
d� tj| |d< |d | _
n|| _
z|d | _W n tyì   Y n0 |  |¡ | d¡}|�r|| _nx| j�r6| jd dd…  dd¡| _nRz| jd dd… | _W n8 t�y† } zd| _td| ƒ|‚W Y d}~n
d}~0 0 dS )z4Store parms in private vars for use in add_argument.ú%defaultrd   r*   zHpytest now uses argparse. "%default" should be changed to "%(default)s" é   )Ú
stacklevelre   Úchoicez±`type` argument to addoption() is the string %r. For choices this is optional and can be omitted,  but when supplied should be a type (for example `str` or `int`). (options: %s)é   Úchoicesr   z€`type` argument to addoption() is the string %r,  but when supplied should be a type (for example `str` or `int`). (options: %s)rj   r(   é   Nú-Ú_r.   z???zneed a long or short option)Ú_attrsÚ_short_optsÚ
_long_optsÚgetÚwarningsÚwarnÚDeprecationWarningÚKeyErrorr=   rA   re   r   Ú_typ_maprj   Ú_set_opt_stringsr(   ÚreplaceÚ
IndexErrorrs   )r"   rU   r6   Útypr(   Úer#   r#   r$   r%   Ò   s`    ü
ýú
þû

zArgument.__init__rO   c                 C   s   | j | j S r'   )r†   r‡   rw   r#   r#   r$   rU     s    zArgument.namesc              	   C   sx   d  ¡ }| | j¡ |D ],}zt| |ƒ| j|< W q tyB   Y q0 q| j d¡rr| jd }| dd¡}|| jd< | jS )Nzdefault dest helprd   r|   z%(default)s)Úsplitri   r(   r_   r…   ÚAttributeErrorrˆ   r�   )r"   r6   Úattrr[   r#   r#   r$   r6     s    

zArgument.attrs)r5   r   c                 C   s    |D ]–}t |ƒdk r$td| | ƒ‚qt |ƒdkrd|d dkrH|d dksVtd| | ƒ‚| j |¡ q|dd… dkr€|d dksŽtd| | ƒ‚| j |¡ qd	S )
zhDirectly from optparse.

        Might not be necessary as this is passed to argparse later on.
        r‚   z>invalid option string %r: must be at least two characters longr   rƒ   r.   zMinvalid short option string %r: must be of the form -x, (x any non-dash char)ú--zGinvalid long option string %r: must start with --, followed by non-dashN)Úlenrs   r†   ri   r‡   )r"   r5   Úoptr#   r#   r$   rŽ   #  s2    ÿýÿýÿýzArgument._set_opt_stringsc                 C   s˜   g }| j r|dt| j ƒ g7 }| jr8|dt| jƒ g7 }|dt| jƒ g7 }t| dƒrj|dt| jƒ g7 }t| dƒrˆ|dt| jƒ g7 }d d	 |¡¡S )
Nz_short_opts: z_long_opts: zdest: re   ztype: rj   z	default: zArgument({})ú, )	r†   Úreprr‡   r(   Úhasattrre   rj   ÚformatÚjoin)r"   r9   r#   r#   r$   Ú__repr__@  s    

zArgument.__repr__)rl   rm   rn   ro   ry   rA   rz   r{   r�   r   r%   r   rU   r   r6   r
   rŽ   rž   r#   r#   r#   r$   r   Ç   s   >r   c                   @   s^   e Zd Zdeeee ddœdd„Zeeddœdd„Zeeddœd	d
„Z	dde
ddœdd„ZdS )r   r*   N)r+   r,   r   r   c                 C   s   || _ || _g | _|| _d S r'   )r+   r,   rS   r   )r"   r+   r,   r   r#   r#   r$   r%   O  s    zOptionGroup.__init__)Úoptnamesr6   r   c                 O   sJ   t |ƒ dd„ | jD ƒ¡}|r*td| ƒ‚t|i |¤Ž}| j|dd� dS )aF  Add an option to this group.

        If a shortened version of a long option is specified, it will
        be suppressed in the help. addoption('--twowords', '--two-words')
        results in help showing '--two-words' only, but --twowords gets
        accepted **and** the automatic destination is in args.twowords.
        c                 s   s    | ]}|  ¡ D ]
}|V  qqd S r'   )rU   )rC   r˜   r+   r#   r#   r$   Ú	<genexpr>_  s   z(OptionGroup.addoption.<locals>.<genexpr>zoption names %s already addedF©Ú
shortupperN)ÚsetÚintersectionrS   Ú
ValueErrorr   Ú_addoption_instance)r"   rŸ   r6   Úconflictr&   r#   r#   r$   r7   W  s    ÿzOptionGroup.addoptionc                 O   s    t |i |¤Ž}| j|dd� d S )NTr¡   )r   r¦   )r"   rŸ   r6   r&   r#   r#   r$   Ú
_addoptiong  s    zOptionGroup._addoptionFr   )r&   r¢   r   c                 C   sR   |s0|j D ]$}|d dkr
|d  ¡ r
tdƒ‚q
| jrB| j |¡ | j |¡ d S )Nr   rƒ   r.   zlowercase shortoptions reserved)r†   Úislowerr¥   r   r)   rS   ri   )r"   r&   r¢   r˜   r#   r#   r$   r¦   k  s    

zOptionGroup._addoption_instance)r*   N)F)rl   rm   rn   rA   r	   r   r%   r   r7   r¨   rh   r¦   r#   r#   r#   r$   r   N  s    ÿ
þr   c                   @   s    e Zd Zdeeeeef  ee ddœdd„Zeddœdd„Z	dee
e  eej ejd	œd
d„Zejdd… dk rœeeeeej eee f  dœdd„ZdS )rN   N)r   r!   r   r   c                 C   s2   || _ tjj| ||jdtdd� |r(|ni | _d S )NF)r   r   Úadd_helpÚformatter_classÚallow_abbrev)Ú_parserrq   ÚArgumentParserr%   r   ÚDropShorterLongHelpFormatterr!   )r"   r   r!   r   r#   r#   r$   r%   v  s    ú
zMyOptionParser.__init__r   )Úmessager   c                 C   sD   | j › d|› �}t| jdƒr0|› d| jj› d�}t|  ¡ | ƒ‚dS )z1Transform argparse error message into UsageError.z	: error: Ú_config_source_hintz (ú)N)r   r›   r­   r±   r   Úformat_usage)r"   r°   rt   r#   r#   r$   Úerror‰  s    zMyOptionParser.errorr8   c           	      C   s�   |   ||¡\}}|rŒ|D ]b}|r|d dkrdd |¡ g}t| j ¡ ƒD ]\}}| d|› d|› �¡ qJ|  d |¡¡ qt|tƒ 	|¡ |S )z(Allow splitting of positional arguments.r   rƒ   zunrecognized arguments: %sú z  rv   Ú
)
rc   r�   Úsortedr!   r]   ri   r´   r_   rW   Úextend)	r"   r9   r:   ÚparsedZunrecognizedÚargÚlinesÚkÚvr#   r#   r$   rK   ”  s    zMyOptionParser.parse_argsr‚   )r}   é	   )Ú
arg_stringr   c           	      C   s  |sd S |d | j vrd S || jv r8| j| }||d fS t|ƒdkrHd S d|v r~| dd¡\}}|| jv r~| j| }|||fS | jsŽ| d¡sì|  |¡}t|ƒdkrÖtdƒ}d dd„ |D ƒ¡}|  	|||d	œ ¡ nt|ƒdkrì|\}|S | j
 |¡�r| j�sd S d
|v �rd S d |d fS )Nr   r.   ú=r–   z4ambiguous option: %(option)s could match %(matches)sr™   c                 s   s   | ]\}}}|V  qd S r'   r#   )rC   r„   r&   r#   r#   r$   r    ¿  rF   z1MyOptionParser._parse_optional.<locals>.<genexpr>)r&   Úmatchesrµ   )Úprefix_charsÚ_option_string_actionsr—   r“   r¬   Ú
startswithÚ_get_option_tuplesr   r�   r´   Ú_negative_number_matcherÚmatchÚ_has_negative_number_optionals)	r"   r¿   ÚactionÚoption_stringÚexplicit_argÚoption_tuplesrt   rS   Úoption_tupler#   r#   r$   Ú_parse_optional¨  s>    






ÿ
zMyOptionParser._parse_optional)NN)NN)rl   rm   rn   r   r	   r   rA   r   r%   r´   r
   rq   rr   rK   ÚsysÚversion_infor   ÚActionrÎ   r#   r#   r#   r$   rN   u  s(     üû  ý
üþrN   c                       sD   e Zd ZdZeeddœ‡ fdd„Zejedœdd„Z	d	d
„ Z
‡  ZS )r¯   a+  Shorten help for long options that differ only in extra hyphens.

    - Collapse **long** options that are the same except for extra hyphens.
    - Shortcut if there are only two options and one of them is a short one.
    - Cache result on the action object as this is called at least 2 times.
    N)r9   Úkwargsr   c                    s,   d|vrt j ¡ |d< tƒ j|i |¤Ž d S )NÚwidth)Ú_pytestÚ_ioZget_terminal_widthÚsuperr%   )r"   r9   rÒ   ©Ú	__class__r#   r$   r%   Ô  s    z%DropShorterLongHelpFormatter.__init__)rÉ   r   c                 C   sl  t j | |¡}|r"|d dkr"|S t|dd ƒ}|r6|S | d¡}t|ƒdkrvt|d ƒdkslt|d ƒdkrv||_|S g }i }|D ]r}t|ƒdks‚|d dkr q‚| d¡s¸td	| |ƒ‚|dd … }| 	dd
¡}	|	|vsìt||	 ƒt|ƒk r‚|||	< q‚|D ]\}t|ƒdk�s|d dk�r$| 
|¡ |dd … | | 	dd
¡¡krú| 
| 	ddd¡¡ qúd |¡}
|
|_|
S )Nr   rƒ   Ú_formatted_action_invocationr™   r‚   r.   rµ   r–   z)long optional argument without "--": [%s]r*   rÀ   )rq   ÚHelpFormatterÚ_format_action_invocationr_   r“   r—   rÙ   rÄ   rs   r�   ri   rˆ   r�   )r"   rÉ   ZorgstrÚresrS   Zreturn_listZ
short_longr&   ZxxoptionZ	shortenedZformatted_action_invocationr#   r#   r$   rÛ   Ú  sB    
,
ÿÿ


z6DropShorterLongHelpFormatter._format_action_invocationc                 C   s4   ddl }g }| ¡ D ]}| | | ¡ |¡¡ q|S )z}Wrap lines after splitting on original newlines.

        This allows to have explicit line breaks in the help text.
        r   N)ÚtextwrapÚ
splitlinesr¸   ÚwrapÚstrip)r"   ÚtextrÓ   rÝ   r»   Úliner#   r#   r$   Ú_split_lines   s
    z)DropShorterLongHelpFormatter._split_lines)rl   rm   rn   ro   r   r%   rq   rÑ   rA   rÛ   rã   Ú__classcell__r#   r#   r×   r$   r¯   Ì  s   &r¯   )$rq   rÏ   r‰   r   Útypingr   r   r   r   r   r   r	   r
   r   r   r   r>   Z_pytest._iorÔ   Z_pytest.compatr   Z_pytest.config.exceptionsr   r   Ztyping_extensionsr   rW   r   Ú	Exceptionrs   r   r   r®   rN   rÚ   r¯   r#   r#   r#   r$   Ú<module>   s>     'W