U
    pd7x                    @   sR  d Z dZdddddddd	d
ddddddddgZddlZddlZddlZzddl	m	Z
mZ W n$ ek
r~   dd Z
dd ZY nX dZdZdZdZdZdZd ZG d!d" d"eZd#d$ ZG d%d deZG d&d deZG d'd	 d	eZG d(d deZG d)d
 d
eZd*d+ ZG d,d deZG d-d deZG d.d deZ G d/d0 d0e Z!G d1d2 d2e Z"G d3d4 d4e"Z#G d5d6 d6e"Z$G d7d8 d8e Z%G d9d: d:e Z&G d;d< d<e Z'G d=d> d>e Z(G d?d@ d@e Z)G dAdB dBe Z*G dCdD dDe%Z+G dEd deZ,G dFd deZ-G dGdH dHeZ.G dIdJ dJe.Z/G dKdL dLe/Z0G dMd dee.Z1dS )Na
  Command-line parsing library

This module is an optparse-inspired command-line parsing library that:

    - handles both optional and positional arguments
    - produces highly informative usage messages
    - supports parsers that dispatch to sub-parsers

The following is a simple usage example that sums integers from the
command-line and writes the result to a file::

    parser = argparse.ArgumentParser(
        description='sum the integers at the command line')
    parser.add_argument(
        'integers', metavar='int', nargs='+', type=int,
        help='an integer to be summed')
    parser.add_argument(
        '--log', default=sys.stdout, type=argparse.FileType('w'),
        help='the file where the sum should be written')
    args = parser.parse_args()
    args.log.write('%s' % sum(args.integers))
    args.log.close()

The module contains the following public classes:

    - ArgumentParser -- The main entry point for command-line parsing. As the
        example above shows, the add_argument() method is used to populate
        the parser with actions for optional and positional arguments. Then
        the parse_args() method is invoked to convert the args at the
        command-line into an object with attributes.

    - ArgumentError -- The exception raised by ArgumentParser objects when
        there are errors with the parser's actions. Errors raised while
        parsing the command-line are caught by ArgumentParser and emitted
        as command-line messages.

    - FileType -- A factory for defining types of files to be created. As the
        example above shows, instances of FileType are typically passed as
        the type= argument of add_argument() calls.

    - Action -- The base class for parser actions. Typically actions are
        selected by passing strings like 'store_true' or 'append_const' to
        the action= argument of add_argument(). However, for greater
        customization of ArgumentParser actions, subclasses of Action may
        be defined and passed as the action= argument.

    - HelpFormatter, RawDescriptionHelpFormatter, RawTextHelpFormatter,
        ArgumentDefaultsHelpFormatter -- Formatter classes which
        may be passed as the formatter_class= argument to the
        ArgumentParser constructor. HelpFormatter is the default,
        RawDescriptionHelpFormatter and RawTextHelpFormatter tell the parser
        not to change the formatting for help text, and
        ArgumentDefaultsHelpFormatter adds information about argument defaults
        to the help.

All other classes in this module are considered implementation details.
(Also note that HelpFormatter and RawDescriptionHelpFormatter are only
considered public as object names -- the API of the formatter objects is
still considered an implementation detail.)
z1.1ArgumentParserArgumentErrorArgumentTypeErrorFileTypeHelpFormatterArgumentDefaultsHelpFormatterRawDescriptionHelpFormatterRawTextHelpFormatterMetavarTypeHelpFormatter	NamespaceActionONE_OR_MOREOPTIONALPARSER	REMAINDERSUPPRESSZERO_OR_MORE    N)gettextngettextc                 C   s   | S N )messager   r   /usr/lib/python3.8/argparse.py_^   s    r   c                 C   s   |dkr| S |S d S N   r   )ZsingularZpluralnr   r   r   r   `   s    r   z==SUPPRESS==?*+zA......Z_unrecognized_argsc                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	_AttributeHoldera  Abstract base class that provides __repr__.

    The __repr__ method returns a string in the format::
        ClassName(attr=name, attr=name, ...)
    The attributes are determined either by a class-level attribute,
    '_kwarg_names', or by inspecting the instance __dict__.
    c                 C   s   t | j}g }i }|  D ]}|t| q|  D ],\}}| rZ|d||f  q6|||< q6|rz|dt|  d|d|f S )N%s=%rz**%s%s(%s), )type__name__	_get_argsappendrepr_get_kwargsisidentifierjoin)selfZ	type_namearg_stringsZ	star_argsargnamevaluer   r   r   __repr__|   s    

z_AttributeHolder.__repr__c                 C   s   t | j S r   )sorted__dict__itemsr-   r   r   r   r*      s    z_AttributeHolder._get_kwargsc                 C   s   g S r   r   r6   r   r   r   r'      s    z_AttributeHolder._get_argsN)r&   
__module____qualname____doc__r2   r*   r'   r   r   r   r   r!   s   s   r!   c                 C   s6   | d krg S t | tkr$| d d  S dd l}|| S )Nr   )r%   listcopy)r5   r;   r   r   r   _copy_items   s    r<   c                   @   s   e Zd ZdZd;ddZdd Zd	d
 ZG dd deZdd Z	dd Z
dd Zdd Zd<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-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: ZdS )=r   zFormatter for generating usage messages and argument help strings.

    Only the name of this class is considered a public API. All the methods
    provided by the class are considered an implementation detail.
          Nc                 C   s   |d kr@zdd l }| j}|d8 }W n tk
r>   d}Y nX || _|| _t|t|d |d | _|| _	d| _
d| _d| _| | d | _| j| _tdtj| _td| _d S )Nr   r=   F      z\s+z\n\n\n+)Zshutilget_terminal_sizecolumnsImportError_prog_indent_incrementminmax_max_help_position_width_current_indent_level_action_max_length_Section_root_section_current_section_recompileASCII_whitespace_matcher_long_break_matcher)r-   progZindent_incrementZmax_help_positionwidthZ_shutilr   r   r   __init__   s(    

zHelpFormatter.__init__c                 C   s"   |  j | j7  _ |  jd7  _d S r   )rJ   rE   rK   r6   r   r   r   _indent   s    zHelpFormatter._indentc                 C   s4   |  j | j8  _ | j dks"td|  jd8  _d S )Nr   zIndent decreased below 0.r   )rJ   rE   AssertionErrorrK   r6   r   r   r   _dedent   s    zHelpFormatter._dedentc                   @   s   e Zd ZdddZdd ZdS )zHelpFormatter._SectionNc                 C   s   || _ || _|| _g | _d S r   )	formatterparentheadingr5   )r-   r[   r\   r]   r   r   r   rW      s    zHelpFormatter._Section.__init__c                 C   s   | j d k	r| j  | jj}|dd | jD }| j d k	rD| j  |sLdS | jtk	rz| jd k	rz| jj}d|d| jf }nd}|d||dgS )Nc                 S   s   g | ]\}}|| qS r   r   ).0funcargsr   r   r   
<listcomp>   s     z6HelpFormatter._Section.format_help.<locals>.<listcomp> z%*s%s:

)	r\   r[   rX   _join_partsr5   rZ   r]   r   rJ   )r-   r,   Z	item_helpZcurrent_indentr]   r   r   r   format_help   s    



z"HelpFormatter._Section.format_help)N)r&   r7   r8   rW   re   r   r   r   r   rM      s   
rM   c                 C   s   | j j||f d S r   )rO   r5   r(   )r-   r_   r`   r   r   r   	_add_item   s    zHelpFormatter._add_itemc                 C   s0   |    | | | j|}| |jg  || _d S r   )rX   rM   rO   rf   re   )r-   r]   Zsectionr   r   r   start_section   s    zHelpFormatter.start_sectionc                 C   s   | j j| _ |   d S r   )rO   r\   rZ   r6   r   r   r   end_section   s    
zHelpFormatter.end_sectionc                 C   s$   |t k	r |d k	r | | j|g d S r   )r   rf   _format_text)r-   textr   r   r   add_text  s    zHelpFormatter.add_textc                 C   s&   |t k	r"||||f}| | j| d S r   )r   rf   _format_usage)r-   usageactionsgroupsprefixr`   r   r   r   	add_usage  s    zHelpFormatter.add_usagec                 C   sv   |j tk	rr| j}||g}| |D ]}||| q$tdd |D }|| j }t| j|| _| | j	|g d S )Nc                 S   s   g | ]}t |qS r   lenr^   sr   r   r   ra     s     z.HelpFormatter.add_argument.<locals>.<listcomp>)
helpr   _format_action_invocation_iter_indented_subactionsr(   rG   rJ   rL   rf   _format_action)r-   actionZget_invocationZinvocations	subactionZinvocation_lengthZaction_lengthr   r   r   add_argument  s    


zHelpFormatter.add_argumentc                 C   s   |D ]}|  | qd S r   )r|   )r-   rn   rz   r   r   r   add_arguments  s    zHelpFormatter.add_argumentsc                 C   s.   | j  }|r*| jd|}|dd }|S )N

rc   )rN   re   rT   substrip)r-   rv   r   r   r   re   %  s
    
zHelpFormatter.format_helpc                 C   s   d dd |D S )Nrb   c                 S   s   g | ]}|r|t k	r|qS r   )r   )r^   partr   r   r   ra   -  s    z-HelpFormatter._join_parts.<locals>.<listcomp>)r,   )r-   Zpart_stringsr   r   r   rd   ,  s    
zHelpFormatter._join_partsc                    s6  |d krt d}|d k	r,|t| jd }n|d krL|sLdt| jd }n|d kr*dt| jd }g }g }|D ] }|jr|| qr|| qr| j}	|	|| |}
ddd ||
fD }| j| j  t	|t	|  kr*d}|	||}|	||}t
||}t
||}d||ks&td||ks:td fdd		}t	|t	| d
  krdt	|t	| d  }|r||g| ||}|||| n |r||g| ||}n|g}nZdt	| }|| }|||}t	|dkrg }|||| |||| |g| }d|}d||f S )Nzusage: rU   z%(prog)s c                 S   s   g | ]}|r|qS r   r   rt   r   r   r   ra   M  s      z/HelpFormatter._format_usage.<locals>.<listcomp>z%\(.*?\)+(?=\s|$)|\[.*?\]+(?=\s|$)|\S+c                    s   g }g }|d k	rt |d }nt |d }| D ]Z}|d t |  krn|rn||d|  g }t |d }|| |t |d 7 }q.|r||d|  |d k	r|d t |d  |d< |S )Nr   r   r   )rs   r(   r,   )partsindentrp   lineslineZline_lenr   
text_widthr   r   	get_linesa  s"    
z.HelpFormatter._format_usage.<locals>.get_linesg      ?r   rc   z%s%s

)N)r   dictrD   option_stringsr(   _format_actions_usager,   rI   rJ   rs   rP   findallrY   extend)r-   rm   rn   ro   rp   rU   	optionalspositionalsrz   formatZaction_usageZpart_regexpZ	opt_usageZ	pos_usageZ	opt_partsZ	pos_partsr   r   r   r   r   r   r   rl   1  s\    





zHelpFormatter._format_usagec              	   C   s  t  }i }|D ]}z||jd }W n tk
r@   Y qY qX |t|j }||| |jkr|jD ]}|| qh|js||kr||  d7  < nd||< ||kr||  d7  < nd||< nF||kr||  d7  < nd||< ||kr||  d7  < nd||< t|d |D ]}	d	||	< qqg }
t|D ]"\}	}|j	t
kr|
d  ||	d	krr||	 n"||	d d	krX||	d  n|js| |}| ||}||kr|d dkr|d
 dkr|dd
 }|
| nf|jd }|jdkrd| }n"| |}| ||}d||f }|jsN||krNd| }|
| q6t|ddD ]}	||	 g|
|	|	< qhddd |
D }d}d}td| d|}td| d|}td||f d|}tdd|}| }|S )Nr   z [[]z (()r   |%s%s %s[%s]T)reverser   c                 S   s   g | ]}|d k	r|qS r   r   )r^   itemr   r   r   ra     s      z7HelpFormatter._format_actions_usage.<locals>.<listcomp>z[\[(]z[\])]z(%s) z\1 (%s)z%s *%srb   z\(([^|]*)\))setindex_group_actions
ValueErrorrs   addrequiredrange	enumeraterv   r   r(   getpopr   #_get_default_metavar_for_positional_format_argsnargs!_get_default_metavar_for_optionalr3   r,   rP   r   r   )r-   rn   ro   group_actionsZinsertsgroupstartendrz   ir   defaultr   option_stringargs_stringrj   opencloser   r   r   r     sz    










z#HelpFormatter._format_actions_usagec                 C   sF   d|kr|t | jd }t| j| j d}d| j }| |||d S )Nz%(prog)r      r   r~   )r   rD   rG   rI   rJ   
_fill_text)r-   rj   r   r   r   r   r   ri     s
    
zHelpFormatter._format_textc                 C   s:  t | jd | j}t| j| d}|| j d }| |}|jsV| jd|f}d| }n@t||kr~| jd||f}d| }d}n| jd|f}d| }|}|g}|jr| 	|}	| 
|	|}
|d|d|
d f  |
dd  D ]}|d|d|f  qn|ds|d | |D ]}|| | q| |S )	Nr=   r   rb   z%*s%s
z	%*s%-*s  r   r   rc   )rF   rL   rH   rG   rI   rJ   rw   rv   rs   _expand_help_split_linesr(   endswithrx   ry   rd   )r-   rz   Zhelp_positionZ
help_widthZaction_widthZaction_headertupZindent_firstr   Z	help_textZ
help_linesr   r{   r   r   r   ry     s8    




zHelpFormatter._format_actionc                 C   s   |j s&| |}| ||d\}|S g }|jdkrB||j  n4| |}| ||}|j D ]}|d||f  q^d|S d S )Nr   r   r   r$   )	r   r   _metavar_formatterr   r   r   r   r(   r,   )r-   rz   r   metavarr   r   r   r   r   r   rw   .  s    



z'HelpFormatter._format_action_invocationc                    sP   |j d k	r|j  n.|jd k	r<dd |jD }dd|  n|  fdd}|S )Nc                 S   s   g | ]}t |qS r   str)r^   Zchoicer   r   r   ra   J  s     z4HelpFormatter._metavar_formatter.<locals>.<listcomp>z{%s},c                    s   t  tr S  f|  S d S r   )
isinstancetuple)Z
tuple_sizeresultr   r   r   O  s    
z0HelpFormatter._metavar_formatter.<locals>.format)r   choicesr,   )r-   rz   default_metavarZchoice_strsr   r   r   r   r   F  s    

z HelpFormatter._metavar_formatterc                 C   s   |  ||}|jd kr$d|d }n|jtkr<d|d }n|jtkrTd|d }n|jtkrld|d }n|jtkr|d}nt|jtkrd|d }n\|jtkrd	}nLzd
d t|jD }W n t	k
r   t
dd Y nX d|||j }|S )Nr   r   r   z[%s [%s ...]]r=   z%s [%s ...]r    z%s ...rb   c                 S   s   g | ]}d qS )r   r   )r^   r   r   r   r   ra   h  s     z.HelpFormatter._format_args.<locals>.<listcomp>zinvalid nargs valuer   )r   r   r   r   r   r   r   r   r   	TypeErrorr   r,   )r-   rz   r   Zget_metavarr   Zformatsr   r   r   r   V  s*    






zHelpFormatter._format_argsc                 C   s   t t|| jd}t|D ]}|| tkr||= qt|D ] }t|| dr:|| j||< q:|dd k	rddd |d D }||d< | 	|| S )Nr   r&   r   r$   c                 S   s   g | ]}t |qS r   r   )r^   cr   r   r   ra   w  s     z.HelpFormatter._expand_help.<locals>.<listcomp>)
r   varsrD   r:   r   hasattrr&   r   r,   _get_help_string)r-   rz   Zparamsr0   Zchoices_strr   r   r   r   n  s    zHelpFormatter._expand_helpc                 c   s@   z
|j }W n tk
r   Y nX |   | E d H  |   d S r   )_get_subactionsAttributeErrorrX   rZ   )r-   rz   Zget_subactionsr   r   r   rx   {  s    
z'HelpFormatter._iter_indented_subactionsc                 C   s&   | j d| }dd l}|||S )Nr   r   )rS   r   r   textwrapZwrap)r-   rj   rV   r   r   r   r   r     s    zHelpFormatter._split_linesc                 C   s,   | j d| }dd l}|j||||dS )Nr   r   )Zinitial_indentZsubsequent_indent)rS   r   r   r   Zfill)r-   rj   rV   r   r   r   r   r   r     s    zHelpFormatter._fill_textc                 C   s   |j S r   )rv   r-   rz   r   r   r   r     s    zHelpFormatter._get_help_stringc                 C   s
   |j  S r   )destupperr   r   r   r   r     s    z/HelpFormatter._get_default_metavar_for_optionalc                 C   s   |j S r   )r   r   r   r   r   r     s    z1HelpFormatter._get_default_metavar_for_positional)r=   r>   N)N) r&   r7   r8   r9   rW   rX   rZ   objectrM   rf   rg   rh   rk   rq   r|   r}   re   rd   rl   r   ri   ry   rw   r   r   r   rx   r   r   r   r   r   r   r   r   r   r      s>      
"
`g/
c                   @   s   e Zd ZdZdd ZdS )r   zHelp message formatter which retains any formatting in descriptions.

    Only the name of this class is considered a public API. All the methods
    provided by the class are considered an implementation detail.
    c                    s    d  fdd|jddD S )Nrb   c                 3   s   | ]} | V  qd S r   r   )r^   r   r   r   r   	<genexpr>  s     z9RawDescriptionHelpFormatter._fill_text.<locals>.<genexpr>T)keepends)r,   
splitlines)r-   rj   rV   r   r   r   r   r     s    z&RawDescriptionHelpFormatter._fill_textN)r&   r7   r8   r9   r   r   r   r   r   r     s   c                   @   s   e Zd ZdZdd ZdS )r   zHelp message formatter which retains formatting of all help text.

    Only the name of this class is considered a public API. All the methods
    provided by the class are considered an implementation detail.
    c                 C   s   |  S r   )r   )r-   rj   rV   r   r   r   r     s    z!RawTextHelpFormatter._split_linesN)r&   r7   r8   r9   r   r   r   r   r   r     s   c                   @   s   e Zd ZdZdd ZdS )r   zHelp message formatter which adds default values to argument help.

    Only the name of this class is considered a public API. All the methods
    provided by the class are considered an implementation detail.
    c                 C   s>   |j }d|j kr:|jtk	r:ttg}|js2|j|kr:|d7 }|S )Nz
%(default)z (default: %(default)s))rv   r   r   r   r   r   r   )r-   rz   rv   Zdefaulting_nargsr   r   r   r     s    

z.ArgumentDefaultsHelpFormatter._get_help_stringN)r&   r7   r8   r9   r   r   r   r   r   r     s   c                   @   s    e Zd ZdZdd Zdd ZdS )r	   a  Help message formatter which uses the argument 'type' as the default
    metavar value (instead of the argument 'dest')

    Only the name of this class is considered a public API. All the methods
    provided by the class are considered an implementation detail.
    c                 C   s   |j jS r   r%   r&   r   r   r   r   r     s    z:MetavarTypeHelpFormatter._get_default_metavar_for_optionalc                 C   s   |j jS r   r   r   r   r   r   r     s    z<MetavarTypeHelpFormatter._get_default_metavar_for_positionalN)r&   r7   r8   r9   r   r   r   r   r   r   r	     s   c                 C   sN   | d krd S | j rd| j S | jd tfkr2| jS | jd tfkrF| jS d S d S )N/)r   r,   r   r   r   )argumentr   r   r   _get_action_name  s    r   c                   @   s    e Zd ZdZdd Zdd ZdS )r   zAn error from creating or using an argument (optional or positional).

    The string value of this exception is the message, augmented with
    information about the argument that caused it.
    c                 C   s   t || _|| _d S r   )r   argument_namer   )r-   r   r   r   r   r   rW     s    
zArgumentError.__init__c                 C   s(   | j d krd}nd}|t| j| j d S )Nz%(message)sz'argument %(argument_name)s: %(message)s)r   r   )r   r   r   )r-   r   r   r   r   __str__  s    
zArgumentError.__str__N)r&   r7   r8   r9   rW   r   r   r   r   r   r     s   c                   @   s   e Zd ZdZdS )r   z@An error from trying to convert a command line string to a type.N)r&   r7   r8   r9   r   r   r   r   r     s   c                   @   s,   e Zd ZdZd
ddZdd Zddd	ZdS )r   a\	  Information about how to convert command line strings to Python objects.

    Action objects are used by an ArgumentParser to represent the information
    needed to parse a single argument from one or more strings from the
    command line. The keyword arguments to the Action constructor are also
    all attributes of Action instances.

    Keyword Arguments:

        - option_strings -- A list of command-line option strings which
            should be associated with this action.

        - dest -- The name of the attribute to hold the created object(s)

        - nargs -- The number of command-line arguments that should be
            consumed. By default, one argument will be consumed and a single
            value will be produced.  Other values include:
                - N (an integer) consumes N arguments (and produces a list)
                - '?' consumes zero or one arguments
                - '*' consumes zero or more arguments (and produces a list)
                - '+' consumes one or more arguments (and produces a list)
            Note that the difference between the default and nargs=1 is that
            with the default, a single value will be produced, while with
            nargs=1, a list containing a single value will be produced.

        - const -- The value to be produced if the option is specified and the
            option uses an action that takes no values.

        - default -- The value to be produced if the option is not specified.

        - type -- A callable that accepts a single string argument, and
            returns the converted value.  The standard Python types str, int,
            float, and complex are useful examples of such callables.  If None,
            str is used.

        - choices -- A container of values that should be allowed. If not None,
            after a command-line argument has been converted to the appropriate
            type, an exception will be raised if it is not a member of this
            collection.

        - required -- True if the action must always be specified at the
            command line. This is only meaningful for optional command-line
            arguments.

        - help -- The help string describing the argument.

        - metavar -- The name to be used for the option's argument with the
            help string. If None, the 'dest' value will be used as the name.
    NFc                 C   s@   || _ || _|| _|| _|| _|| _|| _|| _|	| _|
| _	d S r   
r   r   r   constr   r%   r   r   rv   r   r-   r   r   r   r   r   r%   r   r   rv   r   r   r   r   rW   5  s    zAction.__init__c              	      s(   ddddddddd	g	} fd
d|D S )Nr   r   r   r   r   r%   r   rv   r   c                    s   g | ]}|t  |fqS r   getattrr^   r0   r6   r   r   ra   W  s     z&Action._get_kwargs.<locals>.<listcomp>r   r-   namesr   r6   r   r*   K  s    zAction._get_kwargsc                 C   s   t tdd S )Nz.__call__() not defined)NotImplementedErrorr   r-   parser	namespacevaluesr   r   r   r   __call__Y  s    zAction.__call__)NNNNNFNN)N)r&   r7   r8   r9   rW   r*   r   r   r   r   r   r     s   5        
c                       s(   e Zd Zd fdd	ZdddZ  ZS )	_StoreActionNFc                    sT   |dkrt d|d k	r,|tkr,t dt tt| j|||||||||	|
d
 d S )Nr   znargs for store actions must be != 0; if you have nothing to store, actions such as store true or store const may be more appropriate nargs must be %r to supply constr   )r   r   superr   rW   r   	__class__r   r   rW   _  s     
z_StoreAction.__init__c                 C   s   t || j| d S r   )setattrr   r   r   r   r   r   |  s    z_StoreAction.__call__)NNNNNFNN)Nr&   r7   r8   rW   r   __classcell__r   r   r   r   r   ]  s           r   c                       s(   e Zd Zd fdd	ZdddZ  ZS )	_StoreConstActionNFc              	      s"   t t| j||d||||d d S )Nr   )r   r   r   r   r   r   rv   )r   r   rW   r-   r   r   r   r   r   rv   r   r   r   r   rW     s    
z_StoreConstAction.__init__c                 C   s   t || j| j d S r   )r   r   r   r   r   r   r   r     s    z_StoreConstAction.__call__)NFNN)Nr   r   r   r   r   r     s       r   c                       s   e Zd Zd fdd	Z  ZS )_StoreTrueActionFNc                    s    t t| j||d|||d d S )NTr   r   r   r   r   rv   )r   r   rW   r-   r   r   r   r   rv   r   r   r   rW     s    
z_StoreTrueAction.__init__)FFNr&   r7   r8   rW   r   r   r   r   r   r     s      r   c                       s   e Zd Zd fdd	Z  ZS )_StoreFalseActionTFNc                    s    t t| j||d|||d d S )NFr   )r   r   rW   r   r   r   r   rW     s    
z_StoreFalseAction.__init__)TFNr   r   r   r   r   r     s      r   c                       s(   e Zd Zd fdd	ZdddZ  ZS )	_AppendActionNFc                    sT   |dkrt d|d k	r,|tkr,t dt tt| j|||||||||	|
d
 d S )Nr   znargs for append actions must be != 0; if arg strings are not supplying the value to append, the append const action may be more appropriater   r   )r   r   r   r   rW   r   r   r   r   rW     s     
z_AppendAction.__init__c                 C   s2   t || jd }t|}|| t|| j| d S r   )r   r   r<   r(   r   r-   r   r   r   r   r5   r   r   r   r     s    
z_AppendAction.__call__)NNNNNFNN)Nr   r   r   r   r   r     s           r   c                       s(   e Zd Zd fdd	ZdddZ  ZS )	_AppendConstActionNFc              
      s$   t t| j||d|||||d d S )Nr   )r   r   r   r   r   r   rv   r   )r   r   rW   r   r   r   r   rW     s    
z_AppendConstAction.__init__c                 C   s4   t || jd }t|}|| j t|| j| d S r   )r   r   r<   r(   r   r   r   r   r   r   r     s    z_AppendConstAction.__call__)NFNN)Nr   r   r   r   r   r     s       r   c                       s(   e Zd Zd fdd	ZdddZ  ZS )	_CountActionNFc                    s    t t| j||d|||d d S )Nr   )r   r   r   r   r   rv   )r   r   rW   r   r   r   r   rW     s    
z_CountAction.__init__c                 C   s0   t || jd }|d krd}t|| j|d  d S Nr   r   )r   r   r   )r-   r   r   r   r   countr   r   r   r   
  s    z_CountAction.__call__)NFN)Nr   r   r   r   r   r     s
      r   c                       s.   e Zd Zeedf fdd	ZdddZ  ZS )_HelpActionNc                    s   t t| j|||d|d d S Nr   )r   r   r   r   rv   )r   r  rW   )r-   r   r   r   rv   r   r   r   rW     s    
z_HelpAction.__init__c                 C   s   |   |  d S r   )
print_helpexitr   r   r   r   r     s    z_HelpAction.__call__)Nr&   r7   r8   r   rW   r   r   r   r   r   r   r    s
   r  c                       s0   e Zd Zdeedf fdd	ZdddZ  ZS )_VersionActionNz&show program's version number and exitc                    s$   t t| j|||d|d || _d S r  )r   r  rW   version)r-   r   r  r   r   rv   r   r   r   rW   &  s    
z_VersionAction.__init__c                 C   sD   | j }|d kr|j }| }|| || tj |  d S r   )r  _get_formatterrk   _print_messagere   _sysstdoutr  )r-   r   r   r   r   r  r[   r   r   r   r   4  s    
z_VersionAction.__call__)Nr  r   r   r   r   r  $  s   r  c                       sP   e Zd ZG dd deZedddf fdd	Zdd Zd	d
 ZdddZ	  Z
S )_SubParsersActionc                       s   e Zd Z fddZ  ZS )z&_SubParsersAction._ChoicesPseudoActionc                    s@   | }}|r|dd | 7 }ttj| }|jg |||d d S )Nr   r$   )r   r   rv   r   )r,   r   r  _ChoicesPseudoActionrW   )r-   r0   aliasesrv   r   r   Zsupr   r   r   rW   B  s    
z/_SubParsersAction._ChoicesPseudoAction.__init__r   r   r   r   r   r  @  s   r  FNc              	      s<   || _ || _i | _g | _tt| j||t| j|||d d S )N)r   r   r   r   r   rv   r   )_prog_prefix_parser_class_name_parser_map_choices_actionsr   r  rW   r   )r-   r   rU   parser_classr   r   rv   r   r   r   r   rW   J  s    	
z_SubParsersAction.__init__c                 K   s   | dd kr d| j|f |d< |dd}d|krX|d}| |||}| j| | jf |}|| j|< |D ]}|| j|< qr|S )NrU   r   r  r   rv   )r   r  r   r  r  r(   r  r  )r-   r0   kwargsr  rv   Zchoice_actionr   aliasr   r   r   
add_parsera  s    

z_SubParsersAction.add_parserc                 C   s   | j S r   )r  r6   r   r   r   r   x  s    z!_SubParsersAction._get_subactionsc                 C   s   |d }|dd  }| j tk	r,t|| j | z| j| }W n< tk
rv   |d| jd}td| }t| |Y nX ||d \}	}t	|	
 D ]\}
}t||
| q|rt	|tg  t|t| d S )Nr   r   r$   )parser_namer   z5unknown parser %(parser_name)r (choices: %(choices)s))r   r   r   r  KeyErrorr,   r   r   parse_known_argsr   r5   
setdefault_UNRECOGNIZED_ARGS_ATTRr   r   )r-   r   r   r   r   r  r.   r`   msgZsubnamespacekeyr1   r   r   r   r   {  s$    

	z_SubParsersAction.__call__)N)r&   r7   r8   r   r  r   rW   r  r   r   r   r   r   r   r   r  >  s   r  c                   @   s   e Zd ZdddZdS )_ExtendActionNc                 C   s2   t || jd }t|}|| t|| j| d S r   )r   r   r<   r   r   r   r   r   r   r     s    
z_ExtendAction.__call__)N)r&   r7   r8   r   r   r   r   r   r    s   r  c                   @   s*   e Zd ZdZdddZdd Zd	d
 ZdS )r   a  Factory for creating file object types

    Instances of FileType are typically passed as type= arguments to the
    ArgumentParser add_argument() method.

    Keyword Arguments:
        - mode -- A string indicating how the file is to be opened. Accepts the
            same values as the builtin open() function.
        - bufsize -- The file's desired buffer size. Accepts the same values as
            the builtin open() function.
        - encoding -- The file's encoding. Accepts the same values as the
            builtin open() function.
        - errors -- A string indicating how encoding and decoding errors are to
            be handled. Accepts the same value as the builtin open() function.
    rr   Nc                 C   s   || _ || _|| _|| _d S r   )_mode_bufsize	_encoding_errors)r-   modebufsizeencodingerrorsr   r   r   rW     s    zFileType.__init__c              
   C   s   |dkr>d| j krtjS d| j kr(tjS td| j  }t|zt|| j | j| j| j	W S  t
k
r } z"||d}td}t|| W 5 d }~X Y nX d S )N-r  wzargument "-" with mode %r)filenameerrorz$can't open '%(filename)s': %(error)s)r   r
  stdinr  r   r   r   r!  r"  r#  OSErrorr   )r-   stringr  er`   r   r   r   r   r     s    


zFileType.__call__c                 C   sT   | j | jf}d| jfd| jfg}ddd |D dd |D  }dt| j|f S )Nr&  r'  r$   c                 S   s   g | ]}|d krt |qS )r   )r)   )r^   r/   r   r   r   ra     s      z%FileType.__repr__.<locals>.<listcomp>c                 S   s$   g | ]\}}|d k	rd||f qS )Nr"   r   )r^   kwr/   r   r   r   ra     s    r#   )r   r!  r"  r#  r,   r%   r&   )r-   r`   r  Zargs_strr   r   r   r2     s    zFileType.__repr__)r  r   NN)r&   r7   r8   r9   rW   r   r2   r   r   r   r   r     s   
c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	r
   zSimple object for storing attributes.

    Implements equality by attribute names and values, and provides a simple
    string representation.
    c                 K   s   |D ]}t | |||  qd S r   )r   )r-   r  r0   r   r   r   rW     s    zNamespace.__init__c                 C   s   t |tstS t| t|kS r   )r   r
   NotImplementedr   )r-   otherr   r   r   __eq__  s    
zNamespace.__eq__c                 C   s
   || j kS r   )r4   )r-   r  r   r   r   __contains__  s    zNamespace.__contains__N)r&   r7   r8   r9   rW   r3  r4  r   r   r   r   r
     s   c                       s   e Zd Z fddZdd Zd&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dZdd Zd d! Zd"d# Zd$d% Z  ZS )(_ActionsContainerc                    s  t t|   || _|| _|| _|| _i | _| dd t	 | ddt	 | ddt
 | ddt | ddt | ddt | ddt | ddt | dd	t | dd
t | ddt | ddt |   g | _i | _g | _g | _i | _td| _g | _d S )Nrz   ZstoreZstore_const
store_trueZstore_falser(   Zappend_constr   rv   r  parsersr   z^-\d+$|^-\d*\.\d+$)r   r5  rW   descriptionargument_defaultprefix_charsconflict_handler_registriesregisterr   r   r   r   r   r   r   r  r  r  r  _get_handler_actions_option_string_actions_action_groups_mutually_exclusive_groups	_defaultsrP   rQ   _negative_number_matcher_has_negative_number_optionals)r-   r8  r:  r9  r;  r   r   r   rW     s4    z_ActionsContainer.__init__c                 C   s   | j |i }|||< d S r   )r<  r  )r-   registry_namer1   r   registryr   r   r   r=  (  s    z_ActionsContainer.registerNc                 C   s   | j | ||S r   )r<  r   )r-   rF  r1   r   r   r   r   _registry_get,  s    z_ActionsContainer._registry_getc                 K   s2   | j | | jD ]}|j|kr||j |_qd S r   )rC  updater?  r   r   )r-   r  rz   r   r   r   set_defaults2  s    

z_ActionsContainer.set_defaultsc                 C   s8   | j D ]"}|j|kr|jd k	r|j  S q| j|d S r   )r?  r   r   rC  r   )r-   r   rz   r   r   r   get_default;  s    
z_ActionsContainer.get_defaultc                 O   sD  | j }|r&t|dkrH|d d |krH|r:d|kr:td| j||}n| j||}d|kr|d }|| jkr~| j| |d< n| jdk	r| j|d< | |}t|std|f |f |}| 	d|j
|j
}t|std	|f |tkrtd
|f t| dr:z|  |d W n tk
r8   tdY nX | |S )z
        add_argument(dest, ..., name=value, ...)
        add_argument(option_string, option_string, ..., name=value, ...)
        r   r   r   z+dest supplied twice for positional argumentr   Nzunknown action "%s"r%   %r is not callablez<%r is a FileType class object, instance of it must be passedr  z,length of metavar tuple does not match nargs)r:  rs   r   _get_positional_kwargs_get_optional_kwargsrC  r9  _pop_action_classcallablerH  r%   r   r   r  r   r   _add_action)r-   r`   r  charsr   Zaction_classrz   	type_funcr   r   r   r|   E  s:    	 




z_ActionsContainer.add_argumentc                 O   s    t | f||}| j| |S r   )_ArgumentGrouprA  r(   )r-   r`   r  r   r   r   r   add_argument_groupx  s    z$_ActionsContainer.add_argument_groupc                 K   s   t | f|}| j| |S r   )_MutuallyExclusiveGrouprB  r(   )r-   r  r   r   r   r   add_mutually_exclusive_group}  s    z._ActionsContainer.add_mutually_exclusive_groupc                 C   s`   |  | | j| | |_|jD ]}|| j|< q"|jD ]"}| j|r8| js8| jd q8|S )NT)	_check_conflictr?  r(   	containerr   r@  rD  matchrE  )r-   rz   r   r   r   r   rQ    s    


z_ActionsContainer._add_actionc                 C   s   | j | d S r   )r?  remover   r   r   r   _remove_action  s    z _ActionsContainer._remove_actionc                 C   s   i }| j D ].}|j|kr.td}t||j |||j< q
i }|j D ]D}|j|krn| j|j|j|jd||j< |jD ]}||j ||< qtqD|jD ]&}| j	|j
d}|jD ]}|||< qq|jD ]}||| | qd S )Nz.cannot merge actions - two groups are named %r)titler8  r;  )r   )rA  r]  r   r   rU  r8  r;  r   rB  rW  r   r?  r   rQ  )r-   rY  Ztitle_group_mapr   r  Z	group_maprz   mutex_groupr   r   r   _add_container_actions  s0    







z(_ActionsContainer._add_container_actionsc                 K   s^   d|krt d}t||dttfkr2d|d< |dtkrPd|krPd|d< t||g dS )Nr   z1'required' is an invalid argument for positionalsr   Tr   r   r   )r   r   r   r   r   r   )r-   r   r  r  r   r   r   rM    s    z(_ActionsContainer._get_positional_kwargsc           	      O   s   g }g }|D ]n}|d | j kr>|| j d}td}t|| || |d | j krt|dkr|d | j kr|| q|dd }|d kr|r|d }n|d }|| j }|std}t|| |dd}t|||d	S )
Nr   )optionr:  zNinvalid option string %(option)r: must start with a character %(prefix_chars)rr   r   z%dest= is required for options like %rr(  r   r`  )	r:  r   r   r(   rs   r   lstripreplacer   )	r-   r`   r  r   Zlong_option_stringsr   r  r   Zdest_option_stringr   r   r   rN    s2    

z&_ActionsContainer._get_optional_kwargsc                 C   s   | d|}| d||S )Nrz   )r   rH  )r-   r  r   rz   r   r   r   rO    s    z#_ActionsContainer._pop_action_classc                 C   sF   d| j  }zt| |W S  tk
r@   td}t|| j  Y nX d S )Nz_handle_conflict_%sz%invalid conflict_resolution value: %r)r;  r   r   r   r   )r-   Zhandler_func_namer  r   r   r   r>    s    
z_ActionsContainer._get_handlerc                 C   sL   g }|j D ]&}|| jkr
| j| }|||f q
|rH|  }||| d S r   )r   r@  r(   r>  )r-   rz   Zconfl_optionalsr   Zconfl_optionalr;  r   r   r   rX    s    


z!_ActionsContainer._check_conflictc                 C   s6   t ddt|}ddd |D }t||| d S )Nzconflicting option string: %szconflicting option strings: %sr$   c                 S   s   g | ]\}}|qS r   r   )r^   r   rz   r   r   r   ra     s   z<_ActionsContainer._handle_conflict_error.<locals>.<listcomp>)r   rs   r,   r   )r-   rz   conflicting_actionsr   Zconflict_stringr   r   r   _handle_conflict_error  s    
z(_ActionsContainer._handle_conflict_errorc                 C   s>   |D ]4\}}|j | | j|d  |j s|j| qd S r   )r   r[  r@  r   rY  r\  )r-   rz   rd  r   r   r   r   _handle_conflict_resolve  s
    z*_ActionsContainer._handle_conflict_resolve)N)N)r&   r7   r8   rW   r=  rH  rJ  rK  r|   rU  rW  rQ  r\  r_  rM  rN  rO  r>  rX  re  rf  r   r   r   r   r   r5    s$   5
	
3($
		r5  c                       s6   e Zd Zd fdd	Z fddZ fddZ  ZS )	rT  Nc                    s   |j }|d|j |d|j |d|j tt| j}|f d|i| || _g | _|j	| _	|j
| _
|j| _|j| _|j| _|j| _d S )Nr;  r:  r9  r8  )r  r;  r:  r9  r   rT  rW   r]  r   r<  r?  r@  rC  rE  rB  )r-   rY  r]  r8  r  rI  Z
super_initr   r   r   rW   +  s    z_ArgumentGroup.__init__c                    s    t t| |}| j| |S r   )r   rT  rQ  r   r(   r   r   r   r   rQ  A  s    z_ArgumentGroup._add_actionc                    s    t t| | | j| d S r   )r   rT  r\  r   r[  r   r   r   r   r\  F  s    z_ArgumentGroup._remove_action)NNr&   r7   r8   rW   rQ  r\  r   r   r   r   r   rT  )  s   rT  c                       s.   e Zd Zd fdd	Zdd Zdd Z  ZS )	rV  Fc                    s    t t| | || _|| _d S r   )r   rV  rW   r   
_container)r-   rY  r   r   r   r   rW   M  s    z _MutuallyExclusiveGroup.__init__c                 C   s2   |j rtd}t|| j|}| j| |S )Nz-mutually exclusive arguments must be optional)r   r   r   rh  rQ  r   r(   )r-   rz   r  r   r   r   rQ  R  s    z#_MutuallyExclusiveGroup._add_actionc                 C   s   | j | | j| d S r   )rh  r\  r   r[  r   r   r   r   r\  Z  s    z&_MutuallyExclusiveGroup._remove_action)Frg  r   r   r   r   rV  K  s   rV  c                       s*  e Zd ZdZddddg eddddddf fdd	Zdd	 Zd
d Zdd Zdd Z	dd Z
dAddZdBd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dCd&d'ZdDd(d)Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 ZdEd6d7ZdFd8d9ZdGd:d;ZdHd=d>Z d?d@ Z!  Z"S )Ir   a  Object for parsing command line strings into Python objects.

    Keyword Arguments:
        - prog -- The name of the program (default: sys.argv[0])
        - usage -- A usage message (default: auto-generated from arguments)
        - description -- A description of what the program does
        - epilog -- Text following the argument descriptions
        - parents -- Parsers whose arguments should be copied into this one
        - formatter_class -- HelpFormatter class for printing help messages
        - prefix_chars -- Characters that prefix optional arguments
        - fromfile_prefix_chars -- Characters that prefix files containing
            additional arguments
        - argument_default -- The default value for all arguments
        - conflict_handler -- String indicating how to handle conflicts
        - add_help -- Add a -h/-help option
        - allow_abbrev -- Allow long options to be abbreviated unambiguously
    Nr(  r+  Tc              	      s"  t t| j}||||	|
d |d kr6tjtjd }|| _|| _	|| _
|| _|| _|| _|| _| j}|td| _|td| _d | _dd }| dd | d|krdn|d }| jr| j|d	 |d
 d dttdd |D ]<}| | z
|j}W n tk
r   Y qX | j| qd S )N)r8  r:  r9  r;  r   zpositional argumentszoptional argumentsc                 S   s   | S r   r   )r.  r   r   r   identity  s    z)ArgumentParser.__init__.<locals>.identityr%   r(  hr=   rv   zshow this help message and exit)rz   r   rv   )r   r   rW   _ospathbasenamer
  argvrU   rm   epilogformatter_classfromfile_prefix_charsadd_helpallow_abbrevrU  r   _positionals
_optionals_subparsersr=  r|   r   r_  rC  r   rI  )r-   rU   rm   r8  ro  parentsrp  r:  rq  r9  r;  rr  rs  Z	superinitZ	add_groupri  Zdefault_prefixr\   defaultsr   r   r   rW   r  sJ     
 

zArgumentParser.__init__c                    s"   ddddddg} fdd|D S )	NrU   rm   r8  rp  r;  rr  c                    s   g | ]}|t  |fqS r   r   r   r6   r   r   ra     s     z.ArgumentParser._get_kwargs.<locals>.<listcomp>r   r   r   r6   r   r*     s    zArgumentParser._get_kwargsc           	      K   s   | j d k	r| td |dt|  d|ks8d|krht|dd}t|dd }| ||| _ n| j| _ |dd kr| 	 }| 
 }| j}|| j||d |  |d< | |d}|f d	g i|}| j | |S )
Nz(cannot have multiple subparser argumentsr  r]  r8  ZsubcommandsrU   rb   r7  r   )rv  r+  r   r  r%   r   rU  rt  r   r  _get_positional_actionsrB  rq   rm   re   r   rO  rQ  )	r-   r  r]  r8  r[   r   ro   Zparsers_classrz   r   r   r   add_subparsers  s$    
zArgumentParser.add_subparsersc                 C   s$   |j r| j| n| j| |S r   )r   ru  rQ  rt  r   r   r   r   rQ    s    zArgumentParser._add_actionc                 C   s   dd | j D S )Nc                 S   s   g | ]}|j r|qS r   r   r^   rz   r   r   r   ra     s   z8ArgumentParser._get_optional_actions.<locals>.<listcomp>r?  r6   r   r   r   _get_optional_actions  s    z$ArgumentParser._get_optional_actionsc                 C   s   dd | j D S )Nc                 S   s   g | ]}|j s|qS r   r{  r|  r   r   r   ra     s   z:ArgumentParser._get_positional_actions.<locals>.<listcomp>r}  r6   r   r   r   ry    s    z&ArgumentParser._get_positional_actionsc                 C   s4   |  ||\}}|r0td}| |d|  |S Nzunrecognized arguments: %sr   )r  r   r+  r,   r-   r`   r   rn  r  r   r   r   
parse_args  s
    zArgumentParser.parse_argsc                 C   s  |d krt jdd  }nt|}|d kr.t }| jD ]4}|jtk	r4t||js4|jtk	r4t	||j|j q4| j
D ] }t||spt	||| j
|  qpz>| ||\}}t|tr|t|t t|t ||fW S  tk
r    t  d }| t| Y nX d S r   )r
  rn  r:   r
   r?  r   r   r   r   r   rC  _parse_known_argsr  r   r   delattrr   exc_infor+  r   )r-   r`   r   rz   r   errr   r   r   r    s,    







zArgumentParser.parse_known_argsc                    s  	j d k	r	i  	jD ]R}|j}t|jD ]<\}} |g }||d |  |||d d   q2qi g }t}	t|	D ]^\}}
|
dkr|d |	D ]}
|d qq		|
}|d krd}n||< d}|| qd
|t t d 	fdd		fd	d
}	 	fdd}g d
r`t}nd}
|krt
fddD }
|kr|
}|
kr|
qdn|

kr҈
| }| |
|

qd|
}|d   g }	jD ]|}|kr|jr(|t| nT|jd k	rt|jtrt|jr|jt|jkrt|j	||j q|r	tdd
|  	jD ]X}|jr|jD ]}|kr qqdd |jD }td}	|d
|  qfS )Nr   --r(  AOrb   c                    s|    |  | |}|| jk	rb |   | g D ]*}|kr6td}t|}t| || q6|tk	rx| || d S )Nznot allowed with argument %s)r   _get_valuesr   r   r   r   r   r   )rz   Zargument_stringsr   Zargument_valuesZconflict_actionr  Zaction_name)action_conflictsr   seen_actionsseen_non_default_actionsr-   r   r   take_actionL  s    


z5ArgumentParser._parse_known_args.<locals>.take_actionc                    s  |  }|\}}}j }g }|d kr: |   | d S |d k	r||d}j}|dkr|d |kr||g |f |d }	|	|d  }|dd  pd }
j}||kr|| }|
}ntd}t||| nB|dkr| d }|g}||||f q\ntd}t||| q| d }|d  }|||}|| } || }||||f q\q|sft|D ]\}}}||| qj|S )Nr   r  r   zignored explicit argument %r)_match_argumentr(   r:  r@  r   r   rY   )start_indexoption_tuplerz   r   explicit_argZmatch_argumentZaction_tuples	arg_countrR  charZnew_explicit_argZoptionals_mapr  stopr`   r   Zselected_patterns)r.   arg_strings_patternextrasoption_string_indicesr-   r  r   r   consume_optionala  sN    




z:ArgumentParser._parse_known_args.<locals>.consume_optionalc                    sn   j }| d  }||}t|D ]*\}} | | |  }| |7 } || q&t|d  d d < | S r   )_match_arguments_partialziprs   )r  Zmatch_partialZselected_patternZ
arg_countsrz   r  r`   )r.   r  r   r-   r  r   r   consume_positionals  s    
z=ArgumentParser._parse_known_args.<locals>.consume_positionalsr   r   c                    s   g | ]}| kr|qS r   r   )r^   r   )r  r   r   ra     s   z4ArgumentParser._parse_known_args.<locals>.<listcomp>z(the following arguments are required: %sr$   c                 S   s   g | ]}|j tk	rt|qS r   )rv   r   r   r|  r   r   r   ra   
  s   
z#one of the arguments %s is requiredr   )N)rq  _read_args_from_filesrB  r   r   r  r   iterr(   _parse_optionalr,   r   ry  rG   rF   r?  r   r   r   r   r   r   r   r   r   
_get_valuer+  r   )r-   r.   r   r^  r   r   Zmutex_actionZ	conflictsZarg_string_pattern_partsZarg_strings_iter
arg_stringr  patternr  r  Zmax_option_string_indexZnext_option_string_indexZpositionals_end_indexZstringsZ
stop_indexZrequired_actionsrz   r   r   r  r   )r  r.   r  r  r   r  r   r  r  r-   r  r  r   r    s    





J














z ArgumentParser._parse_known_argsc              
   C   s   g }|D ]}|r|d | j kr*|| qzdt|dd  J}g }|  D ]}| |D ]}|| q\qN| |}|| W 5 Q R X W q tk
r   t	
 d }| t| Y qX q|S r   )rq  r(   r   readr   convert_arg_line_to_argsr  r   r-  r
  r  r+  r   )r-   r.   Znew_arg_stringsr  Z	args_filearg_liner/   r  r   r   r   r    s     
z$ArgumentParser._read_args_from_filesc                 C   s   |gS r   r   )r-   r  r   r   r   r  -  s    z'ArgumentParser.convert_arg_line_to_argsc                 C   sz   |  |}t||}|d krld tdttdttdi}||j}|d krbtdd|j|j }t	||t
|dS )Nzexpected one argumentzexpected at most one argumentzexpected at least one argumentzexpected %s argumentzexpected %s argumentsr   )_get_nargs_patternrP   rZ  r   r   r   r   r   r   r   rs   r   )r-   rz   r  nargs_patternrZ  Znargs_errorsr  r   r   r   r  0  s(    
   
zArgumentParser._match_argumentc                    sr   g }t t|ddD ]X}|d | }d fdd|D }t||}|d k	r|dd | D   qnq|S )Nr   r   rb   c                    s   g | ]}  |qS r   )r  r|  r6   r   r   ra   L  s   z;ArgumentParser._match_arguments_partial.<locals>.<listcomp>c                 S   s   g | ]}t |qS r   rr   )r^   r.  r   r   r   ra   P  s     )r   rs   r,   rP   rZ  r   ro   )r-   rn   r  r   r   Zactions_slicer  rZ  r   r6   r   r  F  s    z'ArgumentParser._match_arguments_partialc           
      C   s  |sd S |d | j krd S || jkr8| j| }||d fS t|dkrHd S d|kr~|dd\}}|| jkr~| j| }|||fS | |}t|dkrddd |D }||d}td}| ||  nt|dkr|\}	|	S | j	|r| j
sd S d	|krd S d |d fS )
Nr   r   =r$   c                 S   s   g | ]\}}}|qS r   r   )r^   rz   r   r  r   r   r   ra   u  s   z2ArgumentParser._parse_optional.<locals>.<listcomp>)ra  Zmatchesz4ambiguous option: %(option)s could match %(matches)sr   )r:  r@  rs   split_get_option_tuplesr,   r   r+  rD  rZ  rE  )
r-   r  rz   r   r  Zoption_tuplesZoptionsr`   r  r  r   r   r   r  V  s>    









zArgumentParser._parse_optionalc           
      C   s0  g }| j }|d |kr|d |kr| jr~d|krB|dd\}}n|}d }| jD ],}||rP| j| }|||f}|| qPn|d |kr|d |kr|}d }|d d }|dd  }	| jD ]T}||kr| j| }|||	f}|| q||r| j| }|||f}|| qn| td|  |S )Nr   r   r  r=   zunexpected option string: %s)r:  rs  r  r@  
startswithr(   r+  r   )
r-   r   r   rR  Zoption_prefixr  rz   r   Zshort_option_prefixZshort_explicit_argr   r   r   r    s:    









z!ArgumentParser._get_option_tuplesc                 C   s   |j }|d krd}nf|tkr"d}nX|tkr0d}nJ|tkr>d}n<|tkrLd}n.|tkrZd}n |tkrhd}ndd	d
|  }|jr|	d	d}|	dd}|S )Nz(-*A-*)z(-*A?-*)z	(-*[A-]*)z
(-*A[A-]*)z([-AO]*)z(-*A[-AO]*)z(-*-*)z(-*%s-*)z-*r  rb   r(  )
r   r   r   r   r   r   r   r,   r   rc  )r-   rz   r   r  r   r   r   r    s(    z!ArgumentParser._get_nargs_patternc                 C   s4   |  ||\}}|r0td}| |d|  |S r  )parse_known_intermixed_argsr   r+  r,   r  r   r   r   parse_intermixed_args  s
    z$ArgumentParser.parse_intermixed_argsc              	      s  |    dd  D }|r,td|d j  fdd| jD rHtdzN| j}z| jd krp|  dd  | _ D ] }|j|_t	|_|j|_t	|_qt| 
||\}} D ]J}t||jrt||jg krddlm} |d	|j|f  t||j qW 5  D ]}|j|_|j|_qX |  }zJ|D ]}|j|_d
|_q$| jD ]}	|	j|	_d
|	_q@| 
||\}}
W 5 |D ]}|j|_qn| jD ]}	|	j|	_qX W 5 || _X ||
fS )Nc                 S   s   g | ]}|j ttfkr|qS r   )r   r   r   r|  r   r   r   ra   	  s    z>ArgumentParser.parse_known_intermixed_args.<locals>.<listcomp>z3parse_intermixed_args: positional arg with nargs=%sr   c                    s&   g | ]}|j D ]}| kr|jqqS r   )r   r   )r^   r   rz   r   r   r   ra   		  s
      z;parse_intermixed_args: positional in mutuallyExclusiveGroup   )warnzDo not expect %s in %sF)ry  r   r   rB  rm   Z
save_nargsZsave_defaultr   format_usager   r  r   r   r   warningsr  r  r~  Zsave_requiredr   )r-   r`   r   aZ
save_usagerz   Zremaining_argsr  r   r   r  r   r  r   r    s`    




z*ArgumentParser.parse_known_intermixed_argsc                    s   j ttfkr2z|d W n tk
r0   Y nX |sz j tkrz jrN j}n j}t	|t
rv |} | n|s j tkr js jd k	r j}n|} | nt|dkr j d tfkr|\} |} | n j tkr fdd|D }np j tkr@ fdd|D } |d  n> j tkrRt}n, fdd|D }|D ]} | qj|S )Nr  r   c                    s   g | ]}  |qS r   r  r^   vrz   r-   r   r   ra   f	  s     z.ArgumentParser._get_values.<locals>.<listcomp>c                    s   g | ]}  |qS r   r  r  r  r   r   ra   j	  s     r   c                    s   g | ]}  |qS r   r  r  r  r   r   ra   s	  s     )r   r   r   r[  r   r   r   r   r   r   r   r  _check_valuer   rs   r   )r-   rz   r.   r1   r  r  r   r  r   r  B	  sD    

zArgumentParser._get_valuesc              	   C   s   |  d|j|j}t|s0td}t||| z||}W n tk
r~   t|jdt|j}tt	
 d }t||Y nL ttfk
r   t|jdt|j}||d}td}t||| Y nX |S )Nr%   rL  r&   r   )r%   r1   z!invalid %(type)s value: %(value)r)rH  r%   rP  r   r   r   r   r)   r   r
  r  r   r   )r-   rz   r  rS  r  r   r0   r`   r   r   r   r  z	  s     
zArgumentParser._get_valuec                 C   sF   |j d k	rB||j krB|dtt|j d}td}t||| d S )Nr$   )r1   r   z3invalid choice: %(value)r (choose from %(choices)s))r   r,   mapr)   r   r   )r-   rz   r1   r`   r  r   r   r   r  	  s    zArgumentParser._check_valuec                 C   s$   |   }|| j| j| j | S r   )r  rq   rm   r?  rB  re   )r-   r[   r   r   r   r  	  s
    zArgumentParser.format_usagec                 C   st   |   }|| j| j| j || j | jD ]0}||j	 ||j |
|j |  q.|| j | S r   )r  rq   rm   r?  rB  rk   r8  rA  rg   r]  r}   r   rh   ro  re   )r-   r[   Zaction_groupr   r   r   re   	  s    

zArgumentParser.format_helpc                 C   s   | j | jdS )Nr   )rp  rU   r6   r   r   r   r  	  s    zArgumentParser._get_formatterc                 C   s"   |d krt j}| |  | d S r   )r
  r  r	  r  r-   filer   r   r   print_usage	  s    zArgumentParser.print_usagec                 C   s"   |d krt j}| |  | d S r   )r
  r  r	  re   r  r   r   r   r  	  s    zArgumentParser.print_helpc                 C   s    |r|d krt j}|| d S r   )r
  stderrwrite)r-   r   r  r   r   r   r	  	  s    zArgumentParser._print_messager   c                 C   s    |r|  |tj t| d S r   )r	  r
  r  r  )r-   Zstatusr   r   r   r   r  	  s    zArgumentParser.exitc                 C   s0   |  tj | j|d}| dtd|  dS )zerror(message: string)

        Prints a usage message incorporating the message to stderr and
        exits.

        If you override this in a subclass, it should not return -- it
        should either exit or raise an exception.
        )rU   r   r=   z%(prog)s: error: %(message)s
N)r  r
  r  rU   r  r   )r-   r   r`   r   r   r   r+  	  s    	zArgumentParser.error)NN)NN)NN)NN)N)N)N)r   N)#r&   r7   r8   r9   r   rW   r*   rz  rQ  r~  ry  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  re   r  r  r  r	  r  r+  r   r   r   r   r   r   _  sV   @

# w:-1

M8


	
)2r9   __version____all__osrk  rerP   sysr
  r   r   r   rC   r   r   r   r   r   r   r  r   r!   r<   r   r   r   r   r	   r   	Exceptionr   r   r   r   r   r   r   r   r   r   r  r  r  r  r   r
   r5  rT  rV  r   r   r   r   r   <module>   s   =   ~	[#&]7  :"