o
    )Ti<&                     @   s  d Z ddlZddlmZmZmZmZ ddlmZm	Z	m
Z
 ddlmZ ddlmZ ddlmZmZ dd	lmZ dd
lmZmZ g dZeeef Zeeeedf f Zeeeee ef  Zdd Zddeddddefdedeee  dedeeeef  de de de dee fddZ!defddZ"dd Z#d edede de de ddfd!d"Z$de fd#d$Z%d ede de de def
d%d&Z&d'd( Z'dS ))z+Simple creation of command line interfaces.    N)AnyCallableOptionalUnion   )ActionConfigFile_ActionPrintConfigremove_actions)ArgumentParser)%deprecation_warning_cli_return_parser)	Namespacedict_to_namespace)get_doc_short_description)capture_parserdefault_config_option_help)CLIauto_cliauto_parserDictComponentsTypec                  O   s   t | ddi|S )zAlias of :func:`auto_cli`._stacklevel   r   argskwargs r   E/home/ubuntu/.local/lib/python3.10/site-packages/jsonargparse/_cli.pyr      s   r   TF
componentsr   config_helpset_defaultsas_positionalreturn_instancefail_untypedparser_classc                    sz  | dd}	| dd}
| du r6t |
d  d }t|  fdd	t  D } t| dkr6td
t| t	rFt| dkrF| d } n| sLtdt| t	rYdd	 | D }nt| t
rlt| }dd	 | D }ndd	 | fD }|r}td| |di |}|jdt|d t| t	t
fst| ||||| |dur|| |	rt|
 |S ||}||}t| |S t| t	rdd | D } t| ||||| |dur|| |	rt|
 |S ||}||}t| }|d}t||tr1t|| dtr1|d || d }||v r|}nnt||tr1t|| dts	|| }t|||S )a\  Simple creation of command line interfaces.

    Previously called ``jsonargparse.CLI``, renamed to follow the standard of functions in lowercase.

    Creates an argument parser from one or more functions/classes, parses
    arguments and runs one of the functions or class methods depending on what
    was parsed. If the ``components`` parameter is not given, then the components
    will be all the locals in the context and defined in the same module as from
    where ``auto_cli`` is called.

    Args:
        components: One or more functions/classes to include in the command line interface.
        args: List of arguments to parse or ``None`` to use ``sys.argv``.
        config_help: Help string for config file option in help.
        set_defaults: Dictionary of values to override components defaults.
        as_positional: Whether to add required parameters as positional arguments.
        return_instance: Whether class components should be instantiated directly and returned,
            i.e. without exposing class methods as subcommands.
        fail_untyped: Whether to raise exception if a required parameter does not have a type.
        parser_class: The :class:`ArgumentParser` subclass to use.
        **kwargs: Used to instantiate :class:`.ArgumentParser`.

    Returns:
        The value returned by the executed function or class method.
    return_parserFr      Nr   r   c                    s0   g | ]}t |st|rt | u r|qS r   )inspectisclasscallable	getmodule).0vmoduler   r   
<listcomp>H   s    
zauto_cli.<locals>.<listcomp>zEither components parameter must be given or there must be at least one function or class among the locals in the context where CLI is called.z-components parameter expected to be non-emptyc                 S   "   g | ]}t |st|s|qS r   r&   r'   r(   r*   cr   r   r   r.   X      " c                 S   s0   g | ]\}}| d st|st|s|qS )z._help)endswithr&   r'   r(   )r*   kr2   r   r   r   r.   [   s   0 c                 S   r/   r   r0   r1   r   r   r   r.   ]   r3   z.Unexpected components, not class or function: --configactionhelpc                 S   s   i | ]}|j |qS r   )__name__r1   r   r   r   
<dictcomp>p   s    zauto_cli.<locals>.<dictcomp>
subcommand.r   )popr&   stackr)   varsvalueslen
ValueError
isinstancelistdictr   itemsadd_argumentr   _add_component_to_parserr   r   
parse_argsinstantiate_classes_run_component_add_subcommandsgetr   str)r   r   r   r   r    r!   r"   r#   r   r$   
stacklevelcaller
unexpectednsparsercfginitcomponents_nsr<   subsubcommand	componentr   r,   r   r      sp   $














(
(r   returnc                     s   t  fddS )zSame as :func:`.auto_cli`, but returns the parser, doesn't parse arguments or run.

    This is a shorthand for ``capture_parser(lambda: auto_cli(*args, **kwargs))``.
    c                      s   t  i S Nr   r   r   r   r   <lambda>   s    zauto_parser.<locals>.<lambda>)r   r   r   r   r   r      s   r   c                 C   s0   t | tr
| dS t| |d}|st| }|S )N_help)logger)rD   rF   rN   r   rO   )rY   r^   help_strr   r   r   get_help_str   s   

r`   rT   c                 C   s   |j dd}|  D ]G\}}|dkrq
t||j}	t||	d}
|
jdt|d |j||
|	d t|t	r?t
||
|||| q
t||
||||}|sQt|
ttf q
d S )NTrequiredr]   descriptionr6   r7   r9   )add_subcommandsrG   r`   r^   typerH   r   add_subcommandrD   rF   rM   rI   r	   r   )r   rT   r   r    r!   r"   subcommandsnamerY   rd   	subparser
added_argsr   r   r   rM      s"   
rM   c                 C   s   |t | jv S r[   )r&   	signature
parameters)rY   rj   r   r   r   has_parameter   s   ro   c                    sZ  ||dd}t | rdd t | D }|s|s0|j| fddi|}|js.t| |j|_|S |j| fi |}|jdd}	|D ]R t|  }
t|
|j}t	||d}t
|
tst|
d	sg|jd
t|d |j|  fddi|}| fdd|D 7 }|st|ttf |	j |t|
|jd qA|S |j| fddi|}|jst| |j|_|S )NT)r    r"   sub_configsc                 S   s2   g | ]\}}t |st|tr|d  dkr|qS )r   _)r(   rD   property)r*   r5   r+   r   r   r   r.      s    z,_add_component_to_parser.<locals>.<listcomp>as_groupFra   rc   configr6   r7   c                    s   g | ]	}  d | qS )r=   r   )r*   amethodr   r   r.      s    re   )r&   r'   
getmembersadd_class_argumentsrd   r`   r^   rf   getattrrg   rD   rr   ro   rH   r   add_method_argumentsr	   r   rh   add_function_arguments)rY   rT   r    r!   r"   r   r   class_methodsrl   ri   method_objectrd   rk   added_subargsr   rv   r   rI      s:   



rI   c                 C   s   | dd  | d}t| r9|r9| |i }| dd  | di |}tt| |tr2t||S t||} |}t| rJtd| di |S | di |S )Nrt   r<   asyncior   )	r>   r&   r'   rD   rz   rr   iscoroutinefunction
__import__run)rY   rU   r<   subcommand_cfgcomponent_objr   r   r   rL      s   



rL   )(__doc__r&   typingr   r   r   r   _actionsr   r   r	   _corer
   _deprecatedr   
_namespacer   r   
_optionalsr   _utilr   r   __all__rg   ComponentTyperF   rO   r   rE   ComponentsTyper   boolr   r   r`   rM   ro   rI   rL   r   r   r   r   <module>   s    

i	

'