U
    6e                     @   s   d Z ddlZddlZddlZddlmZmZ dejeej	dddZ
eejdd	d
ZeedddZG dd deZeedf ed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dS )z
Utilities
~~~~~~~~~
    N)CallableTupleF)parsercreatereturnc                 C   s4   | j rdd | jD }|d S |r*|  S t dS )a  
    Returns the `argparse._SubParsersAction` instance for given
    :class:`argparse.ArgumentParser` instance as would have been returned by
    :meth:`argparse.ArgumentParser.add_subparsers`. The problem with the latter
    is that it only works once and raises an exception on the second attempt,
    and the public API seems to lack a method to get *existing* subparsers.

    :param create:
        If `True`, creates the subparser if it does not exist. Default if
        `False`.

    c                 S   s   g | ]}t |tjr|qS  )
isinstanceargparse_SubParsersAction).0ar   r   )lib/python3.8/site-packages/argh/utils.py
<listcomp>&   s     z"get_subparsers.<locals>.<listcomp>r   N)_subparsers_actionsadd_subparsersSubparsersNotDefinedError)r   r   actionsr   r   r   get_subparsers   s    r   )functionr   c                 C   s@   t | dr| j} q t| }t| r<|j|jdd d}|S )a  
    Returns argument specification for given function.

    Gets to the innermost function through decorators.

    Omits special arguments of instance methods (`self`) and class methods
    (usually `cls` or something like this).  Supports static methods.
    __wrapped__   N)args)hasattrr   inspectZgetfullargspecZismethod_replacer   )r   specr   r   r   get_arg_spec1   s    	


r   )textr   c                 C   s<   t d| }|s| S |d}t|}t d| dd| S )z
    Given a multi-line string, decreases indentation of all lines so that the
    first non-empty line has zero indentation and the remaining lines are
    adjusted accordingly.
    z	(^|
)( +)   z(^|\n) {}z\1)rematchgrouplensub)r   r"   Zfirst_line_indentationdepthr   r   r   unindentB   s    
r'   c                   @   s   e Zd ZdS )r   N__name__
__module____qualname__r   r   r   r   r   P   s   r   .)option_stringsr   c                 C   s   t t ddd}t| dkr(|| d S dd | D }t|rJt|sJtt|rVt| D ]"}|drZ||d	d    S qZtd
|  dd S )N)optr   c                 S   s   |  dddS )N-_)stripreplace)r-   r   r   r   _opt_to_func_arg_nameU   s    z8naive_guess_func_arg_name.<locals>._opt_to_func_arg_namer   r   c                 S   s   g | ]}| d  qS )r.   )
startswith)r   argr   r   r   r   \   s     z-naive_guess_func_arg_name.<locals>.<listcomp>z--r   zUnable to convert opt strings z to func arg name)strr$   anyall#MixedPositionalAndOptionalArgsErrorTooManyPositionalArgumentNamesr3   CliArgToFuncArgGuessingError)r,   r2   Zare_args_positionaloption_stringr   r   r   naive_guess_func_arg_nameT   s    

r<   c                   @   s   e Zd ZdS )	ArghErrorNr(   r   r   r   r   r=   n   s   r=   c                   @   s   e Zd ZdS )r:   Nr(   r   r   r   r   r:   r   s   r:   c                   @   s   e Zd ZdS )r9   Nr(   r   r   r   r   r9   v   s   r9   c                   @   s   e Zd ZdS )r8   Nr(   r   r   r   r   r8   z   s   r8   )F)__doc__r	   r   r!   typingr   r   ArgumentParserboolr
   r   ZFullArgSpecr   r5   r'   	Exceptionr   r<   r=   r:   r9   r8   r   r   r   r   <module>
   s$     