
_R/                @   s  d  d l  Z  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 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& 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/ e0   Z1 d# Z2 d$ Z3 d% Z4 d& Z5 d' d(   Z6 d) d*   Z7 d d+ d,  Z8 d- d. d/  Z9 d0 d1   Z: d2 d3   Z; e d d4 d5   Z< d6 d7   Z= Gd8 d9   d9 e0  Z> Gd: d;   d; e0  Z? Gd< d=   d= e?  Z@ Gd> d?   d? e@  ZA Gd@ dA   dA eA  ZB GdB dC   dC eA  ZC GdD dE   dE e0  ZD GdF dG   dG eD  ZE GdH dI   dI eD  ZF d S)J    N)contextmanager)update_wrapper)repeat   )isidentifier)	iteritems)PY2)string_types)_check_for_unicode_literals)_verify_python3_env)Abort)BadParameter)ClickException)Exit)MissingParameter)
UsageError)HelpFormatter)join_options)pop_context)push_context)OptionParser)	split_opt)confirm)prompt)style)BOOL)convert_type)IntRange)echo)get_os_args)make_default_short_help)make_str)PacifyFlushWrapperzCOMMAND [ARGS]...z*COMMAND1 [ARGS]... [COMMAND2 [ARGS]...]...z (DEPRECATED)z7DeprecationWarning: The command %(name)s is deprecated.c             C   s6   |  j  r2 t t t d |  j i d d d d d  S)NnameZfgZrederrT)
deprecatedr   r   DEPRECATED_INVOKE_NOTICEr#   )cmd r(   +/tmp/pip-build-5gj8f0j9/click/click/core.py_maybe_show_deprecated_notice0   s    	r*   c             C   s+   t  j j   t  j j   t j |   d S)zlExit without garbage collection, this speeds up exit by about 10ms for
    things like bash completion.
    N)sysstdoutflushstderros_exit)coder(   r(   r)   	fast_exit5   s    r2   c             C   s|   | d k r- d j  | j d d  j    } t j j |  } | sI d Sd d l m } | |  | | |  rx t d  d S)z1Internal handler for the bash completion support.Nz_{}_COMPLETE-_r   )bashcomplete)	formatreplaceupperr/   environget_bashcompleter5   r2   )r'   	prog_namecomplete_varZcomplete_instrr5   r(   r(   r)   r;   >   s    !r;   Fc          	   C   sp   |  j  s t | t  r d  S| r- d } n d } t d j | |  j | | | j j |  j j |  j    d  S)NzdIt is not possible to add multi commands as children to another multi command that is in chain mode.zdFound a multi command as subcommand to a multi command that is in chain mode. This is not supported.z{}. Command '{}' is set to chain and '{}' was added as subcommand but it in itself is a multi command. ('{}' is a {} within a chained {} named '{}').)chain
isinstanceMultiCommandRuntimeErrorr6   r#   	__class____name__)Zbase_commandcmd_namer'   registerZhintr(   r(   r)   _check_multicommandL   s    			rF   c             C   s   t  t t t |   |     S)N)listzipr   iter)iterableZ
batch_sizer(   r(   r)   batchh   s    rK   c             C   sy   t  |  d d   } t  | d d  } | d k  ri d d l m } | d j |   t d d |  | |  S|  | | |  S)N__code__co_argcount   r   )warnzoParameter callbacks take 3 args, (ctx, param, value). The 2-arg style is deprecated and will be removed in 8.0.
stacklevel)getattrwarningsrO   r6   DeprecationWarning)callbackctxparamvaluer1   argsrO   r(   r(   r)   invoke_param_callbackl   s    rY   c             c   s   y	 d VWn t  k
 ro } zD | j d k r6 |  | _ | d k	 rZ | j d k rZ | | _   WYd d } ~ Xn@ t k
 r } z  | j d k r |  | _   WYd d } ~ Xn Xd S)z>Context manager that attaches extra information to exceptions.N)r   rU   rV   r   )rU   rV   er(   r(   r)   augment_usage_errors~   s    				r[   c                s"     f d d   } t  | d | S)zGiven a sequence of parameters in the order as should be considered
    for processing and an iterable of parameters that exist, this returns
    a list in the correct order as they should be processed.
    c                sB   y   j  |   } Wn t k
 r3 t d  } Yn X|  j | f S)Ninf)index
ValueErrorfloatis_eager)itemidx)invocation_orderr(   r)   sort_key   s
    z,iter_params_for_processing.<locals>.sort_keykey)sorted)rc   Zdeclaration_orderrd   r(   )rc   r)   iter_params_for_processing   s    rg   c               @   sK  e  Z d  Z d Z d d d d d d d d d d d d d d d d d  Z d d   Z d d	   Z e d
 d d   Z e	 d d    Z
 d d   Z d d   Z d d   Z e	 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 d S).Contexta  The context is a special internal object that holds state relevant
    for the script execution at every single level.  It's normally invisible
    to commands unless they opt-in to getting access to it.

    The context is useful as it can pass internal objects around and can
    control special execution features such as reading data from
    environment variables.

    A context can be used as context manager in which case it will call
    :meth:`close` on teardown.

    .. versionadded:: 2.0
       Added the `resilient_parsing`, `help_option_names`,
       `token_normalize_func` parameters.

    .. versionadded:: 3.0
       Added the `allow_extra_args` and `allow_interspersed_args`
       parameters.

    .. versionadded:: 4.0
       Added the `color`, `ignore_unknown_options`, and
       `max_content_width` parameters.

    .. versionadded:: 7.1
       Added the `show_default` parameter.

    :param command: the command class for this context.
    :param parent: the parent context.
    :param info_name: the info name for this invocation.  Generally this
                      is the most descriptive name for the script or
                      command.  For the toplevel script it is usually
                      the name of the script, for commands below it it's
                      the name of the script.
    :param obj: an arbitrary object of user data.
    :param auto_envvar_prefix: the prefix to use for automatic environment
                               variables.  If this is `None` then reading
                               from environment variables is disabled.  This
                               does not affect manually set environment
                               variables which are always read.
    :param default_map: a dictionary (like object) with default values
                        for parameters.
    :param terminal_width: the width of the terminal.  The default is
                           inherit from parent context.  If no context
                           defines the terminal width then auto
                           detection will be applied.
    :param max_content_width: the maximum width for content rendered by
                              Click (this currently only affects help
                              pages).  This defaults to 80 characters if
                              not overridden.  In other words: even if the
                              terminal is larger than that, Click will not
                              format things wider than 80 characters by
                              default.  In addition to that, formatters might
                              add some safety mapping on the right.
    :param resilient_parsing: if this flag is enabled then Click will
                              parse without any interactivity or callback
                              invocation.  Default values will also be
                              ignored.  This is useful for implementing
                              things such as completion support.
    :param allow_extra_args: if this is set to `True` then extra arguments
                             at the end will not raise an error and will be
                             kept on the context.  The default is to inherit
                             from the command.
    :param allow_interspersed_args: if this is set to `False` then options
                                    and arguments cannot be mixed.  The
                                    default is to inherit from the command.
    :param ignore_unknown_options: instructs click to ignore options it does
                                   not know and keeps them for later
                                   processing.
    :param help_option_names: optionally a list of strings that define how
                              the default help parameter is named.  The
                              default is ``['--help']``.
    :param token_normalize_func: an optional function that is used to
                                 normalize tokens (options, choices,
                                 etc.).  This for instance can be used to
                                 implement case insensitive behavior.
    :param color: controls if the terminal supports ANSI colors or not.  The
                  default is autodetection.  This is only needed if ANSI
                  codes are used in texts that Click prints which is by
                  default not the case.  This for instance would affect
                  help output.
    :param show_default: if True, shows defaults for all options.
                    Even if an option is later created with show_default=False,
                    this command-level setting overrides it.
    NFc             C   s  | |  _  | |  _ | |  _ i  |  _ g  |  _ g  |  _ | d  k rW | d  k	 rW | j } | |  _ t | d i   |  _ | d  k r | d  k	 r | j	 d  k	 r | j	 j
 |  } | |  _	 d  |  _ | d  k r | d  k	 r | j } | |  _ | d  k r| d  k	 r| j } | |  _ |
 d  k r)| j }
 |
 |  _ | d  k rG| j } | |  _ | d  k re| j } | |  _ | d  k r| d  k	 r| j } n	 d g } | |  _ | d  k r| d  k	 r| j } | |  _ |	 |  _ | d  k r.| d  k	 r:| j d  k	 r:|  j d  k	 r:d j | j |  j j    } n | j   } | d  k	 rX| j d d  } | |  _ | d  k r| d  k	 r| j } | |  _ | |  _ g  |  _ d |  _ d  S)Nmetaz--helpz{}_{}r3   r4   r   )parentcommand	info_nameparamsrX   protected_argsobjrQ   _metadefault_mapr:   invoked_subcommandterminal_widthmax_content_widthallow_extra_argsallow_interspersed_argsignore_unknown_optionshelp_option_namestoken_normalize_funcresilient_parsingauto_envvar_prefixr6   r8   r7   colorshow_default_close_callbacks_depth)selfrk   rj   rl   ro   r{   rq   rs   rt   rz   ru   rv   rw   rx   ry   r|   r}   r(   r(   r)   __init__   sp    																															zContext.__init__c             C   s   |  j  d 7_  t |   |  S)Nr   )r   r   )r   r(   r(   r)   	__enter__  s    
zContext.__enter__c             C   s3   |  j  d 8_  |  j  d k r( |  j   t   d  S)Nr   r   )r   closer   )r   exc_type	exc_valuetbr(   r(   r)   __exit__  s    
zContext.__exit__Tc             c   sK   | s |  j  d 7_  z |   } | VWd QRXWd | sF |  j  d 8_  Xd S)aI  This helper method can be used with the context object to promote
        it to the current thread local (see :func:`get_current_context`).
        The default behavior of this is to invoke the cleanup functions which
        can be disabled by setting `cleanup` to `False`.  The cleanup
        functions are typically used for things such as closing file handles.

        If the cleanup is intended the context object can also be directly
        used as a context manager.

        Example usage::

            with ctx.scope():
                assert get_current_context() is ctx

        This is equivalent::

            with ctx:
                assert get_current_context() is ctx

        .. versionadded:: 5.0

        :param cleanup: controls if the cleanup functions should be run or
                        not.  The default is to run these functions.  In
                        some situations the context only wants to be
                        temporarily pushed in which case this can be disabled.
                        Nested pushes automatically defer the cleanup.
        r   N)r   )r   cleanuprvr(   r(   r)   scope  s    	zContext.scopec             C   s   |  j  S)a  This is a dictionary which is shared with all the contexts
        that are nested.  It exists so that click utilities can store some
        state here if they need to.  It is however the responsibility of
        that code to manage this dictionary well.

        The keys are supposed to be unique dotted strings.  For instance
        module paths are a good choice for it.  What is stored in there is
        irrelevant for the operation of click.  However what is important is
        that code that places data here adheres to the general semantics of
        the system.

        Example usage::

            LANG_KEY = f'{__name__}.lang'

            def set_language(value):
                ctx = get_current_context()
                ctx.meta[LANG_KEY] = value

            def get_language():
                return get_current_context().meta.get(LANG_KEY, 'en_US')

        .. versionadded:: 5.0
        )rp   )r   r(   r(   r)   ri     s    zContext.metac             C   s   t  d |  j d |  j  S)z4Creates the formatter for the help and usage output.width	max_width)r   rs   rt   )r   r(   r(   r)   make_formatter  s    zContext.make_formatterc             C   s   |  j  j |  | S)at  This decorator remembers a function as callback that should be
        executed when the context tears down.  This is most useful to bind
        resource handling to the script execution.  For instance, file objects
        opened by the :class:`File` type will register their close callbacks
        here.

        :param f: the function to execute on teardown.
        )r~   append)r   fr(   r(   r)   call_on_close  s    	zContext.call_on_closec             C   s(   x |  j  D] } |   q
 Wg  |  _  d S)zInvokes all close callbacks.N)r~   )r   cbr(   r(   r)   r     s    zContext.closec             C   sO   d } |  j  d k	 r |  j  } |  j d k	 rE d j |  j j |  } | j   S)zThe computed command path.  This is used for the ``usage``
        information on the help page.  It's automatically created by
        combining the info names of the chain of contexts to the root.
         Nz{} {})rl   rj   r6   command_pathlstrip)r   r   r(   r(   r)   r     s    	zContext.command_pathc             C   s)   |  } x | j  d k	 r$ | j  } q	 W| S)zFinds the outermost context.N)rj   )r   noder(   r(   r)   	find_root  s    zContext.find_rootc             C   s?   |  } x2 | d k	 r: t  | j |  r. | j S| j } q	 Wd S)z)Finds the closest object of a given type.N)r?   ro   rj   )r   object_typer   r(   r(   r)   find_object  s
    zContext.find_objectc             C   s/   |  j  |  } | d k r+ |   |  _ } | S)zLike :meth:`find_object` but sets the innermost object to a
        new instance of `object_type` if it does not exist.
        N)r   ro   )r   r   r   r(   r(   r)   ensure_object  s    zContext.ensure_objectc             C   s>   |  j  d k	 r: |  j  j |  } t |  r6 |   } | Sd S)z}Looks up the default for a parameter name.  This by default
        looks into the :attr:`default_map` if available.
        N)rq   r:   callable)r   r#   r   r(   r(   r)   lookup_default  s
    	zContext.lookup_defaultc             C   s   t  | |    d S)zAborts the execution of the program with a specific error
        message.

        :param message: the error message to fail with.
        N)r   )r   messager(   r(   r)   fail   s    zContext.failc             C   s   t     d S)zAborts the script.N)r   )r   r(   r(   r)   abort(  s    zContext.abortr   c             C   s   t  |   d S)z-Exits the application with a given exit code.N)r   )r   r1   r(   r(   r)   exit,  s    zContext.exitc             C   s   |  j  j |   S)zaHelper method to get formatted usage string for the current
        context and command.
        )rk   	get_usage)r   r(   r(   r)   r   0  s    zContext.get_usagec             C   s   |  j  j |   S)z^Helper method to get formatted help page for the current
        context and command.
        )rk   get_help)r   r(   r(   r)   r   6  s    zContext.get_helpc              O   s   |  d d  \ } } | } t  | t  r | } | j } t | d | j d | } | d k rm t d   x? | j D]4 } | j | k rw | j rw | j |  | | j <qw W|  d d  }  t	 |    |  | |  |   SWd QRXWd QRXd S)a"  Invokes a command callback in exactly the way it expects.  There
        are two ways to invoke this method:

        1.  the first argument can be a callback and all other arguments and
            keyword arguments are forwarded directly to the function.
        2.  the first argument is a click command object.  In that case all
            arguments are forwarded as well but proper click parameters
            (options and click arguments) must be keyword arguments and Click
            will fill in defaults.

        Note that before Click 3.2 keyword arguments were not properly filled
        in against the intention of this code and no context was created.  For
        more information about this change and why it was done in a bugfix
        release see :ref:`upgrade-to-3.2`.
        N   rl   rj   z?The given command does not have a callback that can be invoked.)
r?   CommandrT   rh   r#   	TypeErrorrm   expose_valueget_defaultr[   )rX   kwargsr   rT   rU   Z	other_cmdrV   r(   r(   r)   invoke<  s     		zContext.invokec              O   sr   |  d d  \ } } t  | t  s1 t d   x. | j D]# } | | k r; | j | | | <q; W| j | |  S)zSimilar to :meth:`invoke` but fills in default keyword
        arguments from the current context if the other command expects
        it.  This cannot invoke callbacks directly, only other commands.
        Nr   zCallback is not a command.)r?   r   r   rm   r   )rX   r   r   r'   rV   r(   r(   r)   forwardd  s    zContext.forward)rC   
__module____qualname____doc__r   r   r   r   r   propertyri   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r(   r(   r(   r)   rh      sH   T%	
(rh   c               @   s   e  Z d  Z d Z d Z d Z d Z d d d  Z d d   Z d	 d
   Z	 d d   Z
 d d d  Z d d   Z d d   Z d d d d d d  Z d d   Z d S)BaseCommanda  The base command implements the minimal API contract of commands.
    Most code will never use this as it does not implement a lot of useful
    functionality but it can act as the direct subclass of alternative
    parsing methods that do not depend on the Click parser.

    For instance, this can be used to bridge Click and other systems like
    argparse or docopt.

    Because base commands do not implement a lot of the API that other
    parts of Click take for granted, they are not supported for all
    operations.  For instance, they cannot be used with the decorators
    usually and they have no built-in callback system.

    .. versionchanged:: 2.0
       Added the `context_settings` parameter.

    :param name: the name of the command to use unless a group overrides it.
    :param context_settings: an optional dictionary with defaults that are
                             passed to the context object.
    FTNc             C   s(   | |  _  | d  k r i  } | |  _ d  S)N)r#   context_settings)r   r#   r   r(   r(   r)   r     s    	zBaseCommand.__init__c             C   s   d j  |  j j |  j  S)Nz<{} {}>)r6   rB   rC   r#   )r   r(   r(   r)   __repr__  s    zBaseCommand.__repr__c             C   s   t  d   d  S)NzBase commands cannot get usage)NotImplementedError)r   rU   r(   r(   r)   r     s    zBaseCommand.get_usagec             C   s   t  d   d  S)NzBase commands cannot get help)r   )r   rU   r(   r(   r)   r     s    zBaseCommand.get_helpc          
   K   s   x3 t  |  j  D]" \ } } | | k r | | | <q Wt |  d | d | | } | j d d   |  j | |  Wd QRX| S)a  This function when given an info name and arguments will kick
        off the parsing and create a new :class:`Context`.  It does not
        invoke the actual command callback though.

        :param info_name: the info name for this invokation.  Generally this
                          is the most descriptive name for the script or
                          command.  For the toplevel script it's usually
                          the name of the script, for commands below it it's
                          the name of the script.
        :param args: the arguments to parse as list of strings.
        :param parent: the parent context if available.
        :param extra: extra keyword arguments forwarded to the context
                      constructor.
        rl   rj   r   FN)r   r   rh   r   
parse_args)r   rl   rX   rj   extrare   rW   rU   r(   r(   r)   make_context  s    zBaseCommand.make_contextc             C   s   t  d   d S)zGiven a context and a list of arguments this creates the parser
        and parses the arguments, then modifies the context as necessary.
        This is automatically invoked by :meth:`make_context`.
        z1Base commands do not know how to parse arguments.N)r   )r   rU   rX   r(   r(   r)   r     s    zBaseCommand.parse_argsc             C   s   t  d   d S)z{Given a context, this invokes the command.  The default
        implementation is raising a not implemented error.
        z*Base commands are not invokable by defaultN)r   )r   rU   r(   r(   r)   r     s    zBaseCommand.invokec       	      K   s=  t  s t   n t   | d k r/ t   } n t |  } | d k ru t t j j t	 j
 ri t	 j
 d n t   } t |  | |  y2yF |  j | | |  * } |  j |  } | s | S| j   Wd QRXWn t t f k
 rt d t	 j  t    Yn t k
 rH} z( | s  | j   t	 j | j  WYd d } ~ Xnn t k
 r} zN | j t j k rt t	 j  t	 _ t t	 j  t	 _ t	 j d  n   WYd d } ~ Xn XWn t k
 r} z% | rt	 j | j  n | j SWYd d } ~ Xn; t k
 r8| s  t d d t	 j t	 j d  Yn Xd S)a1  This is the way to invoke a script with all the bells and
        whistles as a command line application.  This will always terminate
        the application after a call.  If this is not wanted, ``SystemExit``
        needs to be caught.

        This method is also available by directly calling the instance of
        a :class:`Command`.

        .. versionadded:: 3.0
           Added the `standalone_mode` flag to control the standalone mode.

        :param args: the arguments that should be used for parsing.  If not
                     provided, ``sys.argv[1:]`` is used.
        :param prog_name: the program name that should be used.  By default
                          the program name is constructed by taking the file
                          name from ``sys.argv[0]``.
        :param complete_var: the environment variable that controls the
                             bash completion support.  The default is
                             ``"_<prog_name>_COMPLETE"`` with prog_name in
                             uppercase.
        :param standalone_mode: the default behavior is to invoke the script
                                in standalone mode.  Click will then
                                handle exceptions and convert them into
                                error messages and the function will never
                                return but shut down the interpreter.  If
                                this is set to `False` they will be
                                propagated to the caller and the return
                                value of this function is the return value
                                of :meth:`invoke`.
        :param extra: extra keyword arguments are forwarded to the context
                      constructor.  See :class:`Context` for more information.
        Nr   filer   zAborted!)r   r   r
   r   rG   r!   r/   pathbasenamer+   argv__file__r;   r   r   r   EOFErrorKeyboardInterruptr   r.   r   r   showZ	exit_codeIOErrorerrnoEPIPEr"   r,   r   )	r   rX   r<   r=   Zstandalone_moder   rU   r   rZ   r(   r(   r)   main  sP    +
+
"
zBaseCommand.mainc             O   s   |  j  | |   S)zAlias for :meth:`main`.)r   )r   rX   r   r(   r(   r)   __call__;  s    zBaseCommand.__call__)rC   r   r   r   ru   rv   rw   r   r   r   r   r   r   r   r   r   r(   r(   r(   r)   r   w  s    jr   c               @   s   e  Z d  Z d Z d d d d d d d d d d 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 d  Z d d   Z d d   Z d d    Z d! d"   Z d# d$   Z d% d&   Z d S)'r   a  Commands are the basic building block of command line interfaces in
    Click.  A basic command handles command line parsing and might dispatch
    more parsing to commands nested below it.

    .. versionchanged:: 2.0
       Added the `context_settings` parameter.
    .. versionchanged:: 7.1
       Added the `no_args_is_help` parameter.

    :param name: the name of the command to use unless a group overrides it.
    :param context_settings: an optional dictionary with defaults that are
                             passed to the context object.
    :param callback: the callback to invoke.  This is optional.
    :param params: the parameters to register with this command.  This can
                   be either :class:`Option` or :class:`Argument` objects.
    :param help: the help string to use for this command.
    :param epilog: like the help string but it's printed at the end of the
                   help page after everything else.
    :param short_help: the short help to use for this command.  This is
                       shown on the command listing of the parent command.
    :param add_help_option: by default each command registers a ``--help``
                            option.  This can be disabled by this parameter.
    :param no_args_is_help: this controls what happens if no arguments are
                            provided.  This option is disabled by default.
                            If enabled this will add ``--help`` as argument
                            if no arguments are passed
    :param hidden: hide this command from help outputs.

    :param deprecated: issues a message indicating that
                             the command is deprecated.
    Nz	[OPTIONS]TFc             C   s   t  j |  | |  | |  _ | p% g  |  _ | rS d | k rS | j d d  d } | |  _ | |  _ | |  _ | |  _ |	 |  _	 |
 |  _
 | |  _ | |  _ d  S)Nr   r   )r   r   rT   rm   splithelpepilogoptions_metavar
short_helpadd_help_optionno_args_is_helphiddenr%   )r   r#   r   rT   rm   r   r   r   r   r   r   r   r%   r(   r(   r)   r   a  s    								zCommand.__init__c             C   s/   | j    } |  j | |  | j   j d  S)zmFormats the usage line into a string and returns it.

        Calls :meth:`format_usage` internally.
        
)r   format_usagegetvaluerstrip)r   rU   	formatterr(   r(   r)   r     s    zCommand.get_usagec             C   s5   |  j  } |  j |  } | d  k	 r1 | | g } | S)N)rm   get_help_option)r   rU   r   help_optionr(   r(   r)   
get_params  s
    	zCommand.get_paramsc             C   s/   |  j  |  } | j | j d j |   d S)zsWrites the usage line into the formatter.

        This is a low-level method called by :meth:`get_usage`.
         N)collect_usage_piecesZwrite_usager   join)r   rU   r   piecesr(   r(   r)   r     s    zCommand.format_usagec             C   s@   |  j  g } x- |  j |  D] } | j | j |   q W| S)zhReturns all the pieces that go into the usage line and returns
        it as a list of strings.
        )r   r   extendget_usage_pieces)r   rU   r   rV   r(   r(   r)   r     s    zCommand.collect_usage_piecesc             C   sG   t  | j  } x1 |  j D]& } | j | j  | j | j  q W| S)z&Returns the names for the help option.)setrx   rm   difference_updateoptssecondary_opts)r   rU   Z	all_namesrV   r(   r(   r)   get_help_option_names  s
    zCommand.get_help_option_namesc             C   sX   |  j  |  } | s  |  j r$ d Sd d   } t | d d d d d d d	 | d
 d S)zReturns the help option object.Nc             S   s7   | r3 |  j  r3 t |  j   d |  j |  j   d  S)Nr|   )rz   r   r   r|   r   )rU   rV   rW   r(   r(   r)   	show_help  s    z*Command.get_help_option.<locals>.show_helpis_flagTr`   r   FrT   r   zShow this message and exit.)r   r   Option)r   rU   help_optionsr   r(   r(   r)   r     s    zCommand.get_help_optionc             C   s:   t  |  } x' |  j |  D] } | j | |  q W| S)z6Creates the underlying option parser for this command.)r   r   add_to_parser)r   rU   parserrV   r(   r(   r)   make_parser  s    zCommand.make_parserc             C   s/   | j    } |  j | |  | j   j d  S)zfFormats the help into a string and returns it.

        Calls :meth:`format_help` internally.
        r   )r   format_helpr   r   )r   rU   r   r(   r(   r)   r     s    zCommand.get_help-   c             C   s(   |  j  p' |  j r$ t |  j |  p' d S)z`Gets short help for the command or makes it by shortening the
        long help string.
        r   )r   r   r    )r   limitr(   r(   r)   get_short_help_str  s    		zCommand.get_short_help_strc             C   sD   |  j  | |  |  j | |  |  j | |  |  j | |  d S)a0  Writes the help into the formatter if it exists.

        This is a low-level method called by :meth:`get_help`.

        This calls the following methods:

        -   :meth:`format_usage`
        -   :meth:`format_help_text`
        -   :meth:`format_options`
        -   :meth:`format_epilog`
        N)r   format_help_textformat_optionsformat_epilog)r   rU   r   r(   r(   r)   r     s    zCommand.format_helpc          	   C   s   |  j  rS | j   | j   . |  j  } |  j r< | t 7} | j |  Wd QRXn4 |  j r | j   | j    | j t  Wd QRXd S)z3Writes the help text to the formatter if it exists.N)r   write_paragraphindentationr%   DEPRECATED_HELP_NOTICE
write_text)r   rU   r   	help_textr(   r(   r)   r     s    	
		
	
zCommand.format_help_textc          	   C   sv   g  } x? |  j  |  D]. } | j |  } | d k	 r | j |  q W| rr | j d   | j |  Wd QRXd S)z8Writes all the options into the formatter if they exist.NOptions)r   get_help_recordr   sectionwrite_dl)r   rU   r   r   rV   r   r(   r(   r)   r     s    zCommand.format_optionsc          	   C   s;   |  j  r7 | j   | j    | j |  j   Wd QRXd S)z2Writes the epilog into the formatter if it exists.N)r   r   r   r   )r   rU   r   r(   r(   r)   r     s    	
zCommand.format_epilogc             C   s  | r= |  j  r= | j r= t | j   d | j | j   |  j |  } | j d |  \ } } } x; t | |  j	 |   D]! } | j
 | | |  \ } } q W| r| j r| j r| j d j t |  d k r d n d d j t t |     | | _ | S)Nr|   rX   z$Got unexpected extra argument{} ({})r   sr   r   )r   rz   r   r   r|   r   r   r   rg   r   handle_parse_resultru   r   r6   lenr   mapr!   rX   )r   rU   rX   r   r   Zparam_orderrV   rW   r(   r(   r)   r     s    
7	zCommand.parse_argsc             C   s3   t  |   |  j d k	 r/ | j |  j | j  Sd S)zeGiven a context, this invokes the attached callback (if it exists)
        in the right way.
        N)r*   rT   r   rm   )r   rU   r(   r(   r)   r   $  s    
zCommand.invoke)rC   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r(   r(   r(   r)   r   @  s6   			r   c               @   s   e  Z d  Z d Z d Z d Z d d d d d d 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 S)r@   a  A multi command is the basic implementation of a command that
    dispatches to subcommands.  The most common version is the
    :class:`Group`.

    :param invoke_without_command: this controls how the multi command itself
                                   is invoked.  By default it's only invoked
                                   if a subcommand is provided.
    :param no_args_is_help: this controls what happens if no arguments are
                            provided.  This option is enabled by default if
                            `invoke_without_command` is disabled or disabled
                            if it's enabled.  If enabled this will add
                            ``--help`` as argument if no arguments are
                            passed.
    :param subcommand_metavar: the string that is used in the documentation
                               to indicate the subcommand place.
    :param chain: if this is set to `True` chaining of multiple subcommands
                  is enabled.  This restricts the form of commands in that
                  they cannot have optional arguments but it allows
                  multiple commands to be chained together.
    :param result_callback: the result callback to attach to this multi
                            command.
    TFNc       	      K   s   t  j |  | |  | d  k r& | } | |  _ | |  _ | d  k rY | rS t } n t } | |  _ | |  _ | |  _ |  j r x6 |  j	 D]+ } t
 | t  r | j r t d   q Wd  S)Nz<Multi commands in chain mode cannot have optional arguments.)r   r   r   invoke_without_commandSUBCOMMANDS_METAVARSUBCOMMAND_METAVARsubcommand_metavarr>   result_callbackrm   r?   ArgumentrequiredrA   )	r   r#   r   r   r   r>   r   attrsrV   r(   r(   r)   r   H  s"    
							zMultiCommand.__init__c             C   s&   t  j |  |  } | j |  j  | S)N)r   r   r   r   )r   rU   r   r(   r(   r)   r   j  s    z!MultiCommand.collect_usage_piecesc             C   s'   t  j |  | |  |  j | |  d  S)N)r   r   format_commands)r   rU   r   r(   r(   r)   r   o  s    zMultiCommand.format_optionsc                s      f d d   } | S)am  Adds a result callback to the chain command.  By default if a
        result callback is already registered this will chain them but
        this can be disabled with the `replace` parameter.  The result
        callback is invoked with the return value of the subcommand
        (or the list of return values from all subcommands if chaining
        is enabled) as well as the parameters as they would be passed
        to the main callback.

        Example::

            @click.group()
            @click.option('-i', '--input', default=23)
            def cli(input):
                return 42

            @cli.resultcallback()
            def process_result(result, input):
                return result + input

        .. versionadded:: 3.0

        :param replace: if set to `True` an already existing result
                        callback will be removed.
        c                sW    j    d  k s  r(    _    S   f d d   } t |     _  } | S)Nc                s      |  | |  | |  S)Nr(   )Z_MultiCommand__valuerX   r   )r   old_callbackr(   r)   function  s    z@MultiCommand.resultcallback.<locals>.decorator.<locals>.function)r   r   )r   r  r   )r7   r   )r   r  r)   	decorator  s    		z.MultiCommand.resultcallback.<locals>.decoratorr(   )r   r7   r  r(   )r7   r   r)   resultcallbacks  s    zMultiCommand.resultcallbackc       	   	   C   s   g  } xW |  j  |  D]F } |  j | |  } | d k r= q | j rI q | j | | f  q Wt |  r | j d t d d   | D  } g  } x6 | D]. \ } } | j |  } | j | | f  q W| r | j d   | j	 |  Wd QRXd S)zeExtra format methods for multi methods that adds all the commands
        after the options.
        N   c             s   s   |  ] } t  | d   Vq d S)r   N)r   ).0r'   r(   r(   r)   	<genexpr>  s    z/MultiCommand.format_commands.<locals>.<genexpr>Commands)
list_commandsget_commandr   r   r   r   maxr   r   r   )	r   rU   r   commands
subcommandr'   r   Zrowsr   r(   r(   r)   r    s"    	$zMultiCommand.format_commandsc             C   s   | r= |  j  r= | j r= t | j   d | j | j   t j |  | |  } |  j rp | | _	 g  | _
 n- | r | d  d  | d d   | _	 | _
 | j
 S)Nr|   r   )r   rz   r   r   r|   r   r   r   r>   rn   rX   )r   rU   rX   restr(   r(   r)   r     s    
		'zMultiCommand.parse_argsc       	   '      s     f d d   }   j  su  j rh  j s@ t j     S   t j     | g   SWd  QRX  j d    j    j } g    _ g    _   j s  u  j   |  \ } } } |   _ t j     | j	 | | d   } |  | | j
 j |   SWd  QRXWd  QRX   | r1d n d    _ t j     g  } xi | r j   |  \ } } } | j	 | | d   d d d d	 } | j |  | j g  } | _ qSWg  } x5 | D]- } |  | j | j
 j |   Wd  QRXqW| |  SWd  QRXd  S)
Nc                s.    j  d  k	 r*   j  j  |    j  }  |  S)N)r   r   rm   )rW   )rU   r   r(   r)   _process_result  s    z,MultiCommand.invoke.<locals>._process_resultzMissing command.rj   *ru   Trv   F)rn   r   r>   r   r   r   rX   resolve_commandrr   r   rk   r   )	r   rU   r  rX   rD   r'   Zsub_ctxZcontextsr   r(   )rU   r   r)   r     sN    							$		$zMultiCommand.invokec             C   s   t  | d  } | } |  j | |  } | d  k rd | j d  k	 rd | j |  } |  j | |  } | d  k r | j r t |  d r |  j | | j  | j d j |   | | | d d   f S)Nr   zNo such command '{}'.r   )	r!   r  ry   rz   r   r   rX   r   r6   )r   rU   rX   rD   Zoriginal_cmd_namer'   r(   r(   r)   r    s    zMultiCommand.resolve_commandc             C   s   t     d S)z{Given a context and a command name, this returns a
        :class:`Command` object if it exists or returns `None`.
        N)r   )r   rU   rD   r(   r(   r)   r  &  s    zMultiCommand.get_commandc             C   s   g  S)zTReturns a list of subcommand names in the order they should
        appear.
        r(   )r   rU   r(   r(   r)   r
  ,  s    zMultiCommand.list_commands)rC   r   r   r   ru   rv   r   r   r   r  r  r   r   r  r  r
  r(   r(   r(   r)   r@   -  s$   (Gr@   c               @   sg   e  Z d  Z d Z d d d d  Z d d d  Z d d   Z d	 d
   Z d d   Z d d   Z	 d S)GroupzA group allows a command to have subcommands attached.  This is the
    most common way to implement nesting in Click.

    :param commands: a dictionary of commands.
    Nc             K   s&   t  j |  | |  | p i  |  _ d  S)N)r@   r   r  )r   r#   r  r   r(   r(   r)   r   :  s    zGroup.__init__c             C   sN   | p | j  } | d k r' t d   t |  | | d d | |  j | <d S)zRegisters another :class:`Command` with this group.  If the name
        is not provided, the name of the command is used.
        NzCommand has no name.rE   T)r#   r   rF   r  )r   r'   r#   r(   r(   r)   add_command?  s
    zGroup.add_commandc                s/   d d l  m       f d d   } | S)a  A shortcut decorator for declaring and attaching a command to
        the group.  This takes the same arguments as :func:`command` but
        immediately registers the created command with this instance by
        calling into :meth:`add_command`.
        r   )rk   c                s&         |   }  j  |  | S)N)r  )r   r'   )rX   rk   r   r   r(   r)   r  Q  s    z Group.command.<locals>.decorator)
decoratorsrk   )r   rX   r   r  r(   )rX   rk   r   r   r)   rk   I  s    zGroup.commandc                s/   d d l  m       f d d   } | S)zA shortcut decorator for declaring and attaching a group to
        the group.  This takes the same arguments as :func:`group` but
        immediately registers the created command with this instance by
        calling into :meth:`add_command`.
        r   )groupc                s&         |   }  j  |  | S)N)r  )r   r'   )rX   r  r   r   r(   r)   r  `  s    zGroup.group.<locals>.decorator)r  r  )r   rX   r   r  r(   )rX   r  r   r   r)   r  X  s    zGroup.groupc             C   s   |  j  j |  S)N)r  r:   )r   rU   rD   r(   r(   r)   r  g  s    zGroup.get_commandc             C   s   t  |  j  S)N)rf   r  )r   rU   r(   r(   r)   r
  j  s    zGroup.list_commands)
rC   r   r   r   r   r  rk   r  r  r
  r(   r(   r(   r)   r  3  s   
r  c               @   sL   e  Z d  Z d Z d d d d  Z d d   Z d d   Z d	 d
   Z d S)CommandCollectiona  A command collection is a multi command that merges multiple multi
    commands together into one.  This is a straightforward implementation
    that accepts a list of different multi commands as sources and
    provides all the commands for each of them.
    Nc             K   s&   t  j |  | |  | p g  |  _ d  S)N)r@   r   sources)r   r#   r  r   r(   r(   r)   r   u  s    zCommandCollection.__init__c             C   s   |  j  j |  d S)z1Adds a new multi command to the chain dispatcher.N)r  r   )r   Z	multi_cmdr(   r(   r)   
add_sourcez  s    zCommandCollection.add_sourcec             C   sS   xL |  j  D]A } | j | |  } | d  k	 r
 |  j rG t |  | |  | Sq
 Wd  S)N)r  r  r>   rF   )r   rU   rD   sourcer   r(   r(   r)   r  ~  s    	zCommandCollection.get_commandc             C   s=   t    } x' |  j D] } | j | j |   q Wt |  S)N)r   r  updater
  rf   )r   rU   r   r  r(   r(   r)   r
    s    	zCommandCollection.list_commands)rC   r   r   r   r   r  r  r
  r(   r(   r(   r)   r  n  s
   r  c               @   s  e  Z d  Z d Z d Z d d d d d d d d d d d d d  Z d d	   Z e 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$ d%   Z d& d'   Z d S)(	Parametera	  A parameter to a command comes in two versions: they are either
    :class:`Option`\s or :class:`Argument`\s.  Other subclasses are currently
    not supported by design as some of the internals for parsing are
    intentionally not finalized.

    Some settings are supported by both options and arguments.

    :param param_decls: the parameter declarations for this option or
                        argument.  This is a list of flags or argument
                        names.
    :param type: the type that should be used.  Either a :class:`ParamType`
                 or a Python type.  The later is converted into the former
                 automatically if supported.
    :param required: controls if this is optional or not.
    :param default: the default value if omitted.  This can also be a callable,
                    in which case it's invoked when the default is needed
                    without any arguments.
    :param callback: a callback that should be executed after the parameter
                     was matched.  This is called as ``fn(ctx, param,
                     value)`` and needs to return the value.
    :param nargs: the number of arguments to match.  If not ``1`` the return
                  value is a tuple instead of single value.  The default for
                  nargs is ``1`` (except if the type is a tuple, then it's
                  the arity of the tuple).
    :param metavar: how the value is represented in the help page.
    :param expose_value: if this is `True` then the value is passed onwards
                         to the command callback and stored on the context,
                         otherwise it's skipped.
    :param is_eager: eager values are processed before non eager ones.  This
                     should not be set for arguments or it will inverse the
                     order of processing.
    :param envvar: a string or list of strings that are environment variables
                   that should be checked.

    .. versionchanged:: 7.1
        Empty environment variables are ignored rather than taking the
        empty string value. This makes it possible for scripts to clear
        variables if they can't unset them.

    .. versionchanged:: 2.0
        Changed signature for parameter callback to also be passed the
        parameter. The old callback format will still work, but it will
        raise a warning to give you a chance to migrate the code easier.
    Z	parameterNFTc             C   s   |  j  | p f  |  \ |  _ |  _ |  _ t | |  |  _ | d  k ri |  j j rc |  j j } n d } | |  _ | |  _	 | |  _
 d |  _ | |  _ | |  _ |	 |  _ | |  _ |
 |  _ | |  _ d  S)Nr   F)_parse_declsr#   r   r   r   typeis_compositearityr   rT   nargsmultipler   defaultr`   metavarenvvarautocompletion)r   param_declsr  r   r#  rT   r!  r$  r   r`   r%  r&  r(   r(   r)   r     s"    $									zParameter.__init__c             C   s   d j  |  j j |  j  S)Nz<{} {}>)r6   rB   rC   r#   )r   r(   r(   r)   r     s    zParameter.__repr__c             C   s   |  j  S)zReturns the human readable name of this parameter.  This is the
        same as the name for options, but the metavar for arguments.
        )r#   )r   r(   r(   r)   human_readable_name  s    zParameter.human_readable_namec             C   sc   |  j  d  k	 r |  j  S|  j j |   } | d  k rF |  j j j   } |  j d k r_ | d 7} | S)Nr   z...)r$  r  get_metavarr#   r8   r!  )r   r$  r(   r(   r)   make_metavar  s    
zParameter.make_metavarc             C   s7   t  |  j  r |  j   } n	 |  j } |  j | |  S)z;Given a context variable this calculates the default value.)r   r#  type_cast_value)r   rU   r   r(   r(   r)   r     s    	zParameter.get_defaultc             C   s   d  S)Nr(   )r   r   rU   r(   r(   r)   r      s    zParameter.add_to_parserc             C   sO   | j  |  j  } | d  k r- |  j |  } | d  k rK | j |  j  } | S)N)r:   r#   value_from_envvarr   )r   rU   r   rW   r(   r(   r)   consume_value  s    zParameter.consume_valuec                s    j  j r{  j d k r3 t d j  j     j rb t   f d d   | pZ f  D  S j  | pq f     S    f d d       |  j d k t  j   S)zGiven a value this runs it properly through the type system.
        This automatically handles things like `nargs` and `multiple` as
        well as composite types.
        r   zAttempted to invoke composite type but nargs has been set to {}. This is not supported; nargs needs to be set to a fixed value > 1.c             3   s*   |  ]  }  j  | p f      Vq d  S)N)r  )r  x)rU   r   r(   r)   r    s    z,Parameter.type_cast_value.<locals>.<genexpr>c                sE     d k r  j  |     St    f d d   |  p= f  D  S)Nr   c             3   s"   |  ] }   |  d   Vq d S)r   Nr(   )r  r.  )_convertlevelr(   r)   r    s    z>Parameter.type_cast_value.<locals>._convert.<locals>.<genexpr>)r  tuple)rW   r0  )r/  rU   r   )r0  r)   r/    s    z+Parameter.type_cast_value.<locals>._convert)r  r  r!  r   r6   r"  r1  bool)r   rU   rW   r(   )r/  rU   r   r)   r+    s    	&zParameter.type_cast_valuec             C   s    | d k	 r |  j  | |  Sd S)zaGiven a value and context this runs the logic to convert the
        value as necessary.
        N)r+  )r   rU   rW   r(   r(   r)   process_value"  s    zParameter.process_valuec             C   s<   | d  k r d S|  j  d k s( |  j r8 | f  k r8 d Sd S)NTr   F)r!  r"  )r   rW   r(   r(   r)   value_is_missing-  s
    $zParameter.value_is_missingc             C   sh   |  j  | |  } | d  k r7 | j r7 |  j |  } |  j rd |  j |  rd t d | d |    | S)NrU   rV   )r3  rz   r   r   r4  r   )r   rU   rW   r(   r(   r)   full_process_value4  s    zParameter.full_process_valuec             C   s   |  j  d  k r d  St |  j  t t f  rd x[ |  j  D]( } t j j |  } | d  k	 r5 | Sq5 Wn% t j j |  j   } | d k r | Sd  S)Nr   )r%  r?   r1  rG   r/   r9   r:   )r   rU   r%  r   r(   r(   r)   resolve_envvar_value?  s    zParameter.resolve_envvar_valuec             C   s@   |  j  |  } | d  k	 r< |  j d k r< |  j j |  } | S)Nr   )r6  r!  r  split_envvar_value)r   rU   r   r(   r(   r)   r,  M  s    zParameter.value_from_envvarc             C   s   t  | d |   |  j | |  } y |  j | |  } Wn$ t k
 ra | j sW   d  } Yn X|  j d  k	 r y t |  j | |  |  } Wn t k
 r | j s   Yn XWd  QRX|  j r | | j |  j	 <| | f S)NrV   )
r[   r-  r5  	Exceptionrz   rT   rY   r   rm   r#   )r   rU   r   rX   rW   r(   r(   r)   r   S  s"    			zParameter.handle_parse_resultc             C   s   d  S)Nr(   )r   rU   r(   r(   r)   r   g  s    zParameter.get_help_recordc             C   s   g  S)Nr(   )r   rU   r(   r(   r)   r   j  s    zParameter.get_usage_piecesc             C   s/   |  j  p |  j g } d j d d   | D  S)z{Get a stringified version of the param for use in error messages to
        indicate which param caused the error.
        z / c             s   s   |  ] } t  |  Vq d  S)N)repr)r  r.  r(   r(   r)   r  r  s    z+Parameter.get_error_hint.<locals>.<genexpr>)r   r(  r   )r   rU   Z	hint_listr(   r(   r)   get_error_hintm  s    zParameter.get_error_hint)rC   r   r   r   param_type_namer   r   r   r(  r*  r   r   r-  r+  r3  r4  r5  r6  r,  r   r   r   r:  r(   r(   r(   r)   r    s:   ,
	r  c               @   s   e  Z d  Z d Z d Z d d d d d d d d d d d d d d 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 S)r   a  Options are usually optional values on the command line and
    have some extra features that arguments don't have.

    All other parameters are passed onwards to the parameter constructor.

    :param show_default: controls if the default value should be shown on the
                         help page. Normally, defaults are not shown. If this
                         value is a string, it shows the string instead of the
                         value. This is particularly useful for dynamic options.
    :param show_envvar: controls if an environment variable should be shown on
                        the help page.  Normally, environment variables
                        are not shown.
    :param prompt: if set to `True` or a non empty string then the user will be
                   prompted for input.  If set to `True` the prompt will be the
                   option name capitalized.
    :param confirmation_prompt: if set then the value will need to be confirmed
                                if it was prompted for.
    :param hide_input: if this is `True` then the input on the prompt will be
                       hidden from the user.  This is useful for password
                       input.
    :param is_flag: forces this option to act as a flag.  The default is
                    auto detection.
    :param flag_value: which value should be used for this flag if it's
                       enabled.  This is set to a boolean automatically if
                       the option string contains a slash to mark two options.
    :param multiple: if this is set to `True` then the argument is accepted
                     multiple times and recorded.  This is similar to ``nargs``
                     in how it works but supports arbitrary number of
                     arguments.
    :param count: this flag makes an option increment an integer.
    :param allow_from_autoenv: if this is enabled then the value of this
                               parameter will be pulled from an environment
                               variable in case a prefix is defined on the
                               context.
    :param help: the help string.
    :param hidden: hide this option from help outputs.
    optionNFTc             K   s  | j  d t  t k } t j |  | d | | | d k r[ |  j j d d  j   } n | d k rp d  } n | } | |  _ | |  _ | |  _	 | |  _
 | d  k r | d  k	 r d } n t |  j  } | r | r d |  _ | d  k r |  j } | |  _ | |  _ |  j rIt |  j t  rI| d  t g k rIt |  _ d |  _ n	 d |  _ |	 |  _ |	 r| d  k rt d d  |  _ | rd |  _ | |  _ |
 |  _ | |  _ | |  _ | |  _ | |  _ |  j d k  rt d	   |  j r|  j r|  j rt d
   |  j r&|  j r&t d   |  j rS|  j	 rS|  j d  k	 rSt d   |  j r|  j rtt d   n |  j rt d   d  S)Nr#  r  Tr4   r   Fminr   zOptions cannot have nargs < 0z+Cannot prompt for flags that are not bools.z*Got secondary option for non boolean flag.z5Hidden input does not work with boolean flag prompts.z6Options cannot be multiple and count at the same time.z3Options cannot be count and flags at the same time.)r:   _missingr  r   r#   r7   
capitalizer   confirmation_prompt
hide_inputr   r2  r   r#  r   
flag_valuer?   r   r  is_bool_flagcountr   r"  allow_from_autoenvr   r}   show_choicesshow_envvarr!  r   )r   r'  r}   r   r@  rA  r   rB  r"  rD  rE  r  r   r   rF  rG  r   Zdefault_is_missingZprompt_textr(   r(   r)   r     sh    							
		-										!			zOption.__init__c             C   s  g  } g  } d  } g  } x | D] } t  |  rR | d  k	 rI t d   | } q | d  d  d k rn d n d } | | k r | j | d  \ }	 }
 |	 j   }	 |	 r | j t |	   | j |	  |
 j   }
 |
 r| j |
 j    q | j t |   | j |  q W| d  k rp| rp| j d d d    | d d j d	 d
  j	   } t  |  spd  } | d  k r| sd  | | f St d   | r| rt d j
 |    | | | f S)NzName defined twicer   /;re   c             S   s   t  |  d  S)Nr   )r   )r.  r(   r(   r)   <lambda>  s    z%Option._parse_decls.<locals>.<lambda>r   r3   r4   z#Could not determine name for optionzhNo options defined but a name was passed ({}). Did you mean to declare an argument instead of an option?)r   r   r   r   r   r   r   sortr7   lowerr6   )r   declsr   r   r   r#   Zpossible_namesdeclZ
split_charfirstsecondr(   r(   r)   r    sF    	" zOption._parse_declsc             C   s	  d |  j  d |  j d |  i } |  j r0 d } n |  j rB d } n d } |  j r | j d d   d j |  } |  j r |  j r | j	 |  j
 d | d	 d
 | | j	 |  j d | d	 d | q| j	 |  j
 d | d	 |  j | n | | d <| j	 |  j
 |  d  S)Ndestr!  ro   r   rD  storez{}_constactionconstTF)r#   r!  r"  rD  r   popr6   rC  r   
add_optionr   rB  )r   r   rU   r   rS  Zaction_constr(   r(   r)   r   !  s&    							
zOption.add_to_parserc                s<   j  r d  Sg       f d d   } |  j  g }  j rY | j |  j    j pe d } g  }  j r j } | d  k r  j r | j d  k	 r d j	 | j  j
 j    } | d  k	 r| j d j	 t | t t f  rd j d d   | D  n |    j d  k	 r j s6| j rt  j t  r]d	 j	  j  } n^ t  j t t f  rd j d
 d    j D  } n$ t j  j  rd } n	  j } | j d j	 |    j r| j d  | rd j	 | rd j	 |  n d d j |   }   r)d n d j |  | f S)Nc                s\   t  |   \ } } | r+ d g   d  d   < j rX  j rX | d j  j    7} | S)NTz {})r   r   rD  r6   r*  )r   r   Zany_slashes)any_prefix_is_slashr   r(   r)   _write_optsD  s    z+Option.get_help_record.<locals>._write_optsr   z{}_{}zenv var: {}z, c             s   s   |  ] } t  |  Vq d  S)N)str)r  dr(   r(   r)   r  Z  s    z)Option.get_help_record.<locals>.<genexpr>z({})c             s   s   |  ] } t  |  Vq d  S)N)rY  )r  rZ  r(   r(   r)   r  c  s    z	(dynamic)zdefault: {}r   z{}[{}]z{}  z; z / )r   r   r   r   r   rG  r%  rE  r{   r6   r#   r8   r?   rG   r1  r   r#  r}   r	   inspectZ
isfunctionr   )r   rU   rX  r   r   r   r%  Zdefault_stringr(   )rW  r   r)   r   ?  sD    				1
!"			*zOption.get_help_recordc             C   s`   |  j  rP |  j rP x6 | j j D]( } | j |  j k r  | j r  | j Sq  Wd  St j |  |  S)N)	r   rC  rk   rm   r#   r#  rB  r  r   )r   rU   rV   r(   r(   r)   r   s  s    zOption.get_defaultc                st    j     }  j r( t  j |  St  j d | d  j d  j d  j d  j d    f d d   S)	zThis is an alternative flow that can be activated in the full
        value processing if a value does not exist.  It will prompt the
        user until a valid value exists and then returns the processed
        value as result.
        r#  r  rA  rF  r@  Z
value_procc                s    j    |   S)N)r3  )r.  )rU   r   r(   r)   rJ    s    z)Option.prompt_for_value.<locals>.<lambda>)r   rC  r   r   r  rA  rF  r@  )r   rU   r#  r(   )rU   r   r)   prompt_for_value  s    						zOption.prompt_for_valuec             C   sl   t  j |  |  } | d  k	 r" | S|  j rh | j d  k	 rh d j | j |  j j    } t j j	 |  Sd  S)Nz{}_{})
r  r6  rE  r{   r6   r#   r8   r/   r9   r:   )r   rU   r   r%  r(   r(   r)   r6    s    zOption.resolve_envvar_valuec             C   s   |  j  |  } | d  k r d  S|  j d k t |  j  } | d k r | d  k	 r |  j j |  } |  j r |  j d k r t | |  j  } | S)Nr   r   )r6  r!  r2  r"  r  r7  rK   )r   rU   r   Zvalue_depthr(   r(   r)   r,    s    zOption.value_from_envvarc             C   sE   | d  k r2 |  j  d  k	 r2 | j r2 |  j |  St j |  | |  S)N)r   rz   r\  r  r5  )r   rU   rW   r(   r(   r)   r5    s    %zOption.full_process_value)rC   r   r   r   r;  r   r  r   r   r   r\  r6  r,  r5  r(   r(   r(   r)   r   u  s2   %F-4r   c               @   sy   e  Z d  Z d Z d Z d d d  Z e d d    Z d d	   Z d
 d   Z	 d d   Z
 d d   Z d d   Z d S)r   zArguments are positional parameters to a command.  They generally
    provide fewer features than options but can have infinite ``nargs``
    and are required by default.

    All parameters are passed onwards to the parameter constructor.
    argumentNc             K   s   | d  k rB | j  d  d  k	 r* d } n | j  d d  d k } t j |  | d | | |  j d  k	 r |  j d k  r t d   d  S)Nr#  Fr!  r   r   r   z>nargs=-1 in combination with a default value is not supported.)r:   r  r   r#  r!  r   )r   r'  r   r   r(   r(   r)   r     s    	zArgument.__init__c             C   s#   |  j  d  k	 r |  j  S|  j j   S)N)r$  r#   r8   )r   r(   r(   r)   r(    s    zArgument.human_readable_namec             C   sr   |  j  d  k	 r |  j  S|  j j |   } | s= |  j j   } |  j sU d j |  } |  j d k rn | d 7} | S)Nz[{}]r   z...)r$  r  r)  r#   r8   r   r6   r!  )r   varr(   r(   r)   r*    s    	
zArgument.make_metavarc             C   s   | s% | s d  g  g  f St  d   t |  d k r` | d } } | j d d  j   } n t  d j t |     | | g g  f S)Nz%Could not determine name for argumentr   r   r3   r4   z8Arguments take exactly one parameter declaration, got {})r   r   r7   rL  r6   )r   rM  r   r#   argr(   r(   r)   r    s    zArgument._parse_declsc             C   s   |  j    g S)N)r*  )r   rU   r(   r(   r)   r     s    zArgument.get_usage_piecesc             C   s   t  |  j    S)N)r9  r*  )r   rU   r(   r(   r)   r:    s    zArgument.get_error_hintc             C   s&   | j  d |  j d |  j d |   d  S)NrQ  r!  ro   )add_argumentr#   r!  )r   r   rU   r(   r(   r)   r     s    zArgument.add_to_parser)rC   r   r   r   r;  r   r   r(  r*  r  r   r:  r   r(   r(   r(   r)   r     s   r   )Gr   r[  r/   r+   
contextlibr   	functoolsr   	itertoolsr   _compatr   r   r   r	   Z_unicodefunr
   r   
exceptionsr   r   r   r   r   r   Z
formattingr   r   globalsr   r   r   r   r   Ztermuir   r   r   typesr   r   r   utilsr   r   r    r!   r"   objectr>  r   r   r   r&   r*   r2   r;   rF   rK   rY   r[   rg   rh   r   r   r@   r  r  r  r   r   r(   r(   r(   r)   <module>   sz   		  ; <