U W@sddlZddlZddlmZddlmZddlmZddlm Z m Z m Z ddl m Z mZmZmZddlmZmZmZmZmZdd lmZmZdd lmZmZdd lmZmZdd l m!Z!m"Z"dd l#m$Z$m%Z%m&Z&ddl'm(Z(m)Z)e*Z+dZ,dZ-d1ddZ.d2ddZ/ddZ0ddZ1ed3ddZ2ddZ3Gddde*Z4Gd d!d!e*Z5Gd"d#d#e5Z6Gd$d%d%e6Z7Gd&d'd'e7Z8Gd(d)d)e7Z9Gd*d+d+e*Z:Gd,d-d-e:Z;Gd.d/d/e:ZZ>m?Z?dS)4N)contextmanager)repeat)update_wrapper) convert_typeIntRangeBOOL)make_strmake_default_short_helpecho get_os_args)ClickException UsageError BadParameterAbortMissingParameter)promptconfirm) HelpFormatter join_options) OptionParser split_opt) push_context pop_context)PY2 isidentifier iteritems)_check_for_unicode_literals_verify_python3_envzCOMMAND [ARGS]...z*COMMAND1 [ARGS]... [COMMAND2 [ARGS]...]...cCsX|dkrd|dd}tj|}|s0dSddlm}|||||rTtddS)z1Internal handler for the bash completion support.Nz _%s_COMPLETE-_r) bashcomplete) replaceupperosenvironget _bashcompleter!sysexit)cmd prog_name complete_varZcomplete_instrr!r-/core.pyr's  r'Fc CsL|jrt|tsdS|rd}nd}td||j|||jj|jj|jfdS)NzcIt is not possible to add multi commands as children to another multi command that is in chain modezdFound a multi command as subcommand to a multi command that is in chain mode. This is not supporteda%s. Command "%s" is set to chain and "%s" was added as subcommand but it in itself is a multi command. ("%s" is a %s within a chained %s named "%s"). This restriction was supposed to be lifted in 6.0 but the fix was flawed. This will be fixed in Click 7.0)chain isinstance MultiCommand RuntimeErrorname __class____name__)Z base_commandcmd_namer*registerZhintr-r-r._check_multicommand(sr8cCstttt||SN)listzipriter)iterableZ batch_sizer-r-r.batch=sr>cCsVt|dd}t|dd}|dkrJddlm}|td|dd|||S||||S)N__code__ co_argcountr)warnz}Invoked legacy parameter callback "%s". The new signature for such callbacks starting with click 2.0 is (ctx, param, value).) stacklevel)getattrwarningsrBWarning)callbackctxparamvaluecodeargsrBr-r-r.invoke_param_callbackAs    rMc csz dVWn~tk rV}z.|jdkr,||_|dk rD|jdkrD||_W5d}~XYn4tk r}z|jdkrv||_W5d}~XYnXdS)zPContext manager that attaches extra information to exceptions that fly. N)rrHrIr)rHrIer-r-r.augment_usage_errorsPs   rOcsfdd}t||dS)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. cs8z|}Wntk r*td}YnX|j |fS)Ninf)index ValueErrorfloatis_eager)itemidxinvocation_orderr-r.sort_keyhs z,iter_params_for_processing..sort_keykey)sorted)rXZdeclaration_orderrYr-rWr.iter_params_for_processingcs r]c@seZdZdZd.ddZddZdd Zed/d d Ze d dZ ddZ ddZ ddZ e ddZddZddZddZddZdd Zd!d"Zd0d$d%Zd&d'Zd(d)Zd*d+Zd,d-ZdS)1ContextaCThe 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. :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. 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. NFcCs||_||_||_i|_g|_g|_|dkr:|dk r:|j}||_t|di|_|dkrt|dk rt|j dk rt|j |}||_ d|_ |dkr|dk r|j }||_ |dkr|dk r|j }||_ | dkr|j} | |_| dkr|j} | |_| dkr|j} | |_| dkr|dk r|j} ndg} | |_|dkr6|dk r6|j}||_| |_|dkr|dk r|jdk r|jdk rd|j|jf}n ||_||_|dkr|dk r|j}||_g|_d|_dS)Nmetaz--help%s_%sr)parentcommand info_nameparamsrLprotected_argsobjrD_meta default_mapr&invoked_subcommandterminal_widthmax_content_widthallow_extra_argsallow_interspersed_argsignore_unknown_optionshelp_option_namestoken_normalize_funcresilient_parsingauto_envvar_prefixr#color_close_callbacks_depth)selfrbrarcrfrrrhrjrkrqrlrmrnrorprsr-r-r.__init__st        zContext.__init__cCs|jd7_t||SNr)rurrvr-r-r. __enter__BszContext.__enter__cCs*|jd8_|jdkr |tdSNrr)rucloser)rvexc_type exc_valuetbr-r-r.__exit__Gs zContext.__exit__Tc csF|s|jd7_z| }|VW5QRXW5|s@|jd8_XdS)aIThis 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. rN)ru)rvcleanuprvr-r-r.scopeMsz Context.scopecCs|jS)aThis is a dictionary which is shared with all the contexts that are nested. It exists so that click utiltiies 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 = __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 )rgryr-r-r.r_ssz Context.metacCst|j|jdS)z4Creates the formatter for the help and usage output.)widthZ max_width)rrjrkryr-r-r.make_formatterszContext.make_formattercCs|j||S)atThis 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. )rtappend)rvfr-r-r. call_on_closes zContext.call_on_closecCs|jD] }|qg|_dS)zInvokes all close callbacks.N)rt)rvcbr-r-r.r|s z Context.closecCs6d}|jdk r|j}|jdk r.|jjd|}|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. N )rcra command_pathlstrip)rvrr-r-r.rs   zContext.command_pathcCs|}|jdk r|j}q|S)zFinds the outermost context.Nra)rvnoder-r-r. find_roots zContext.find_rootcCs*|}|dk r&t|j|r|jS|j}qdS)z)Finds the closest object of a given type.N)r0rfra)rv object_typerr-r-r. find_objects  zContext.find_objectcCs"||}|dkr||_}|S)zLike :meth:`find_object` but sets the innermost object to a new instance of `object_type` if it does not exist. N)rrf)rvrrr-r-r. ensure_objects  zContext.ensure_objectcCs,|jdk r(|j|}t|r$|}|SdS)z}Looks up the default for a parameter name. This by default looks into the :attr:`default_map` if available. N)rhr&callable)rvr3rr-r-r.lookup_defaults   zContext.lookup_defaultcCst||dS)zAborts the execution of the program with a specific error message. :param message: the error message to fail with. N)r)rvmessager-r-r.failsz Context.failcCs tdS)zAborts the script.N)rryr-r-r.abortsz Context.abortrcCst|dS)z-Exits the application with a given exit code.N)r(r))rvrKr-r-r.r)sz Context.exitcCs |j|S)zaHelper method to get formatted usage string for the current context and command. )rb get_usageryr-r-r.rszContext.get_usagecCs |j|S)z^Helper method to get formatted help page for the current context and command. )rbget_helpryr-r-r.rszContext.get_helpc Os|dd\}}|}t|trt|}|j}t||j|d}|dkrHtd|jD]$}|j|krN|jrN||||j<qN|dd}t |4|$|||W5QRW5QRSQRXW5QRXdS)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`. Nrcraz?The given command does not have a callback that can be invoked.) r0CommandrGr^r3 TypeErrorrd expose_value get_defaultrO)rLkwargsrvrGrHZ other_cmdrIr-r-r.invokes    zContext.invokecOsR|dd\}}t|ts"td|jD]}||kr(|j|||<q(|j|f|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. NrzCallback is not a command.)r0rrrdr)rLrrvr*rIr-r-r.forwards  zContext.forward)NNNNNNNFNNNNNN)T)r)r5 __module__ __qualname____doc__rwrzrrrpropertyr_rrr|rrrrrrrr)rrrrr-r-r-r.r^rsJN  %      'r^c@sbeZdZdZdZdZdZdddZddZd d Z dd d Z d dZ ddZ dddZ ddZdS) BaseCommandaThe 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. FTNcCs||_|dkri}||_dSr9)r3context_settings)rvr3rr-r-r.rwGszBaseCommand.__init__cCs tddS)NzBase commands cannot get usageNotImplementedErrorrvrHr-r-r.rRszBaseCommand.get_usagecCs tddS)NzBase commands cannot get helprrr-r-r.rUszBaseCommand.get_helpc Ksbt|jD]\}}||kr |||<q t|f||d|}|jdd|||W5QRX|S)aThis 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. rF)r)rrr^r parse_args)rvrcrLraextrar[rJrHr-r-r. make_contextXs zBaseCommand.make_contextcCs tddS)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.Nr)rvrHrLr-r-r.roszBaseCommand.parse_argscCs tddS)z{Given a context, this invokes the command. The default implementation is raising a not implemented error. z*Base commands are not invokable by defaultNrrr-r-r.rwszBaseCommand.invokec KsPts tnt|dkr"t}nt|}|dkrRttjt j rJt j dpLt }t |||zzJ|j ||f|0}||}|s|W5QRWWS|W5QRXWnfttfk rtt jdtYn>tk r}z|s|t |jW5d}~XYnXWn6tk rJ|s.tdt jdt dYnXdS)a1This 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 ``"__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)filezAborted!r)rrrr r:r r$pathbasenamer(argv__file__r'rrr)EOFErrorKeyboardInterruptr stderrrr ZshowZ exit_code) rvrLr+r,Zstandalone_moderrHrrNr-r-r.main}s>%    "zBaseCommand.maincOs |j||S)zAlias for :meth:`main`.)r)rvrLrr-r-r.__call__szBaseCommand.__call__)N)N)NNNT)r5rrrrlrmrnrwrrrrrrrr-r-r-r.r+s   Mrc@seZdZdZd#ddZddZd d Zd d Zd dZddZ ddZ ddZ ddZ ddZ ddZddZddZdd Zd!d"ZdS)$raCommands 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. :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. N [OPTIONS]Tc CsTt|||||_|pg|_||_||_||_|dkrD|rDt|}||_| |_ dSr9) rrwrGrdhelpepilogoptions_metavarr short_helpadd_help_option) rvr3rrGrdrrrrrr-r-r.rws  zCommand.__init__cCs"|}||||dS)N )r format_usagegetvaluerstriprvrH formatterr-r-r.rs zCommand.get_usagecCs&|j}||}|dk r"||g}|Sr9)rdget_help_option)rvrHrZ help_optionr-r-r. get_paramss   zCommand.get_paramscCs"||}||jd|dS)z)Writes the usage line into the formatter.rN)collect_usage_piecesZ write_usagerjoin)rvrHrpiecesr-r-r.rs zCommand.format_usagecCs,|jg}||D]}|||q|S)zhReturns all the pieces that go into the usage line and returns it as a list of strings. )rrextendget_usage_pieces)rvrHrrIr-r-r.r szCommand.collect_usage_piecescCs2t|j}|jD]}||j||jq|S)z&Returns the names for the help option.)setrorddifference_updateoptssecondary_opts)rvrHZ all_namesrIr-r-r.get_help_option_namess    zCommand.get_help_option_namescCs4||}|r|jsdSdd}t|ddd|ddS)zReturns the help option object.NcSs(|r$|js$t||jd|dS)Nrs)rqr rrsr))rHrIrJr-r-r. show_help!s z*Command.get_help_option..show_helpTFzShow this message and exit.)is_flagrTrrGr)rrOption)rvrHZ help_optionsrr-r-r.rs  zCommand.get_help_optioncCs8t|}|j|_|j|_||D]}|||q"|S)z6Creates the underlying option parser for this command.)rrmrnr add_to_parser)rvrHparserrIr-r-r. make_parser*s zCommand.make_parsercCs"|}||||dS)zFormats the help into a string and returns it. This creates a formatter and will call into the following formatting methods: r)r format_helprrrr-r-r.r3s zCommand.get_helpcCs4||||||||||||dS)zWrites the help into the formatter if it exists. This calls into the following methods: - :meth:`format_usage` - :meth:`format_help_text` - :meth:`format_options` - :meth:`format_epilog` N)rformat_help_textformat_options format_epilogrr-r-r.r;s   zCommand.format_helpc Cs2|jr.||||jW5QRXdS)z3Writes the help text to the formatter if it exists.N)rwrite_paragraph indentation write_textrr-r-r.rJs zCommand.format_help_textc CsXg}||D] }||}|dk r||q|rT|d||W5QRXdS)z8Writes all the options into the formatter if they exist.NZOptions)rget_help_recordrsectionwrite_dl)rvrHrrrIrr-r-r.rQs   zCommand.format_optionsc Cs2|jr.||||jW5QRXdS)z2Writes the epilog into the formatter if it exists.N)rrrrrr-r-r.r]s zCommand.format_epilogc Cs||}|j|d\}}}t|||D]}||||\}}q,|r|js|js|dt|dkrjdpldd t t |f||_ |S)N)rLz$Got unexpected extra argument%s (%s)rsrr) rrr]rhandle_parse_resultrlrqrlenrmapr rL)rvrHrLrrZ param_orderrIrJr-r-r.rds zCommand.parse_argscCs |jdk r|j|jf|jSdS)zeGiven a context, this invokes the attached callback (if it exists) in the right way. N)rGrrdrr-r-r.rts zCommand.invoke)NNNNNNrT)r5rrrrwrrrrrrrrrrrrrrr-r-r-r.rs.    rc@sleZdZdZdZdZdddZddZd d Zdd d Z d dZ ddZ ddZ ddZ ddZddZdS)r1aA 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 Kstj||f||dkr| }||_||_|dkr@|rjdksr_Sfdd}t|_}|S)Ncs|f||f||Sr9r-)Z_MultiCommand__valuerLrrZ old_callbackr-r.functions z@MultiCommand.resultcallback..decorator..function)rr)rrrr"rvrr. decorators z.MultiCommand.resultcallback..decoratorr-)rvr"rr-rr.resultcallbacks zMultiCommand.resultcallbackc Csjg}||D]2}|||}|dkr(q|jp0d}|||fq|rf|d||W5QRXdS)zeExtra format methods for multi methods that adds all the commands after the options. NrZCommands) list_commands get_commandrrrr)rvrHrZrowsZ subcommandr*rr-r-r.rs   zMultiCommand.format_commandscCst|s*|jr*|js*t||jd|t|||}|jrL||_ g|_ n"|rn|dd|dd|_ |_ |j S)Nrr) rrqr rrsr)rrr/rerL)rvrHrLrestr-r-r.rszMultiCommand.parse_argsc sfdd}jsbjrXjs,tS"t|gW5QRSQRXdjj}g_g_jsn|\}}}|_t|j ||d}|*||j |W5QRW5QRSQRXW5QRX|r dpd_tg}|rj|\}}}|j ||ddd}| ||jg}|_q"g}|D](}|| |j |W5QRXqr||W5QRSQRXdS) Ncs"jdk rjj|fj}|Sr9)rrrd)rJrHrvr-r._process_results   z,MultiCommand.invoke.._process_resultzMissing command.r*TF)rarlrm) rerr/rrrrLresolve_commandrirrbr) rvrHrrLr6r*Zsub_ctxZcontextsrr-rr.rsJ     8    zMultiCommand.invokecCst|d}|}|||}|dkrD|jdk rD||}|||}|dkrtt|drf|||j|d||||ddfS)NrzNo such command "%s".r)r rrprrrLr)rvrHrLr6Zoriginal_cmd_namer*r-r-r.rAs     zMultiCommand.resolve_commandcCs tdS)z{Given a context and a command name, this returns a :class:`Command` object if it exists or returns `None`. NrrvrHr6r-r-r.r[szMultiCommand.get_commandcCsgS)zTReturns a list of subcommand names in the order they should appear. r-rr-r-r.raszMultiCommand.list_commands)NFNNFN)F)r5rrrrlrmrwrrrrrrrrrr-r-r-r.r1|s$  %Dr1c@sDeZdZdZdddZdddZddZd d Zd d Zd dZ dS)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. NcKstj||f||pi|_dSr9)r1rwcommands)rvr3rrr-r-r.rwoszGroup.__init__cCs8|p|j}|dkrtdt|||dd||j|<dS)zRegisters another :class:`Command` with this group. If the name is not provided, the name of the command is used. NzCommand has no name.T)r7)r3rr8r)rvr*r3r-r-r. add_commandts  zGroup.add_commandcsfdd}|S)aA 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`. cst|}||Sr9)rbrrr*rLrrvr-r.rs z Group.command..decoratorr-rvrLrrr-rr.rb~sz Group.commandcsfdd}|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`. cst|}||Sr9)grouprrrr-r.rs zGroup.group..decoratorr-rr-rr.rsz Group.groupcCs |j|Sr9)rr&rr-r-r.rszGroup.get_commandcCs t|jSr9)r\rrr-r-r.rszGroup.list_commands)NN)N) r5rrrrwrrbrrrr-r-r-r.rhs    rc@s2eZdZdZd ddZddZddZd d ZdS) CommandCollectionaA 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. NcKstj||f||pg|_dSr9)r1rwsources)rvr3rrr-r-r.rwszCommandCollection.__init__cCs|j|dS)z1Adds a new multi command to the chain dispatcher.N)rr)rvZ multi_cmdr-r-r. add_sourceszCommandCollection.add_sourcecCs>|jD]2}|||}|dk r|jr0t||||SqdSr9)rrr/r8)rvrHr6sourcerr-r-r.rs    zCommandCollection.get_commandcCs*t}|jD]}|||q t|Sr9)rrupdaterr\)rvrHrr r-r-r.rs zCommandCollection.list_commands)NN)r5rrrrwrrrr-r-r-r.rs  rc @seZdZdZdZd$ddZedd Zd d Zd d Z ddZ ddZ ddZ ddZ ddZddZddZddZddZd d!Zd"d#ZdS)% 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. .. versionchanged:: 2.0 Changed signature for parameter callback to also be passed the parameter. In Click 2.0, the old callback format will still work, but it will raise a warning to give you change to migrate the code easier. :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. Before Click 2.0, the signature was ``(ctx, 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. Z parameterNFTc Cs||p d|\|_|_|_t|||_|dkrF|jjrB|jj}nd}||_||_ ||_ d|_ ||_ ||_ | |_||_| |_dS)Nr-rF) _parse_declsr3rrrtype is_compositeZarityrrGnargsmultiplerdefaultrTmetavarenvvar) rv param_declsr rrrGrrrrTrr-r-r.rws   zParameter.__init__cCs|jS)zReturns the human readable name of this parameter. This is the same as the name for options, but the metavar for arguments. )r3ryr-r-r.human_readable_nameszParameter.human_readable_namecCsF|jdk r|jS|j|}|dkr0|jj}|jdkrB|d7}|S)Nr...)rr Z get_metavarr3r#r)rvrr-r-r. make_metavar s    zParameter.make_metavarcCs&t|jr|}n|j}|||S)z;Given a context variable this calculates the default value.)rrtype_cast_valuerr-r-r.rs  zParameter.get_defaultcCsdSr9r-rvrrHr-r-r.rszParameter.add_to_parsercCs6||j}|dkr ||j}|dkr2||}|Sr9)r&r3rvalue_from_envvar)rvrHrrJr-r-r. consume_values    zParameter.consume_valuecs~jjrTjdkr tdjjrBtfdd|p:dDS|pLdSfdd|jdktjS)zGiven a value this runs it properly through the type system. This automatically handles things like `nargs` and `multiple` as well as composite types. rzAttempted to invoke composite type but nargs has been set to %s. This is not supported; nargs needs to be set to a fixed value > 1.c3s |]}|pdVqdS)r-N)r .0xrr-r. 3sz,Parameter.type_cast_value..r-cs2dkr|Stfdd|p*dDS)Nrc3s|]}|dVqdS)rNr-r)_convertlevelr-r.r9sz>Parameter.type_cast_value.._convert..r-)r tuple)rJr!r rHrv)r!r.r 6sz+Parameter.type_cast_value.._convert)r rrrrr"boolrvrHrJr-r#r.r's zParameter.type_cast_valuecCs|dk r|||SdS)zaGiven a value and context this runs the logic to convert the value as necessary. N)rr%r-r-r. process_value<szParameter.process_valuecCs,|dkr dS|jdks|jr(|dkr(dSdS)NTrr-F)rr)rvrJr-r-r.value_is_missingGs zParameter.value_is_missingcCs>|||}|dkr||}|jr:||r:t||d|S)N)rHrI)r&rrr'rr%r-r-r.full_process_valueNs    zParameter.full_process_valuecCsZ|jdkrdSt|jttfrH|jD] }tj|}|dk r$|Sq$ntj|jSdSr9)rr0r"r:r$r%r&)rvrHrrr-r-r.resolve_envvar_valueYs    zParameter.resolve_envvar_valuecCs,||}|dk r(|jdkr(|j|}|Srx)r)rr split_envvar_valuerr-r-r.rds  zParameter.value_from_envvarc Cst||d|||}z|||}Wn tk rJ|jsBd}YnX|jdk rzt|j|||}Wntk r|jsYnXW5QRX|jr||j|j <||fS)N)rI) rOrr( ExceptionrqrGrMrrdr3)rvrHrrLrJr-r-r.rjs,    zParameter.handle_parse_resultcCsdSr9r-rr-r-r.rszParameter.get_help_recordcCsgSr9r-rr-r-r.rszParameter.get_usage_pieces) NNFNNNNTFN)r5rrrparam_type_namerwrrrrrrrr&r'r(r)rrrrr-r-r-r.r s4)       r c @s^eZdZdZdZdddZdd Zd d Zd d ZddZ ddZ ddZ ddZ ddZ dS)ra+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. :param prompt: if set to `True` or a non empty string then the user will be prompted for input if not set. 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. ZoptionNFTc Ks| dttk}tj||fd| i| |dkrD|jdd}n|dkrRd}n|}||_||_||_ |dkr|dk r~d}n t |j }|r|rd|_ |dkr|j }||_ ||_|j rt|jt r| dkrt|_d|_nd|_| |_| r| dkrtdd|_|rd|_ ||_| |_| |_||_|jdkr:td |jrZ|j rZ|jsZtd |jsr|j rrtd |jr|j r|jdk rtd |jr|jrtd n|j rtddS)Nrr Tr rFr)minzOptions 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 rwr3r" capitalizerconfirmation_prompt hide_inputr$rrr flag_valuer0rr  is_bool_flagcountrrallow_from_autoenvr show_defaultrr)rvrr6rr0r1rr2rr4r5r rrZdefault_is_missingZ prompt_textr-r-r.rwsf     zOption.__init__c CsJg}g}d}g}|D]}t|r6|dk r0td|}q|dddkrJdpLd}||kr||d\} } | } | r|t| || | } | r|| q|t|||q|dkr|r|jddd|ddd d  }t|sd}|dkr(|s d||fStd |s@|s@td ||||fS) NzName defined twicer/;cSs t|dS)Nr)rrr-r-r. z%Option._parse_decls..rZrr z#Could not determine name for optionzhNo options defined but a name was passed (%s). Did you mean to declare an argument instead of an option?) rrsplitrrrrsortr"lower) rvdeclsrrrr3Zpossible_namesZdeclZ split_charfirstsecondr-r-r.r sF      zOption._parse_declscCs|j|j|d}|jrd}n|jr(d}nd}|jr|dd|jr|jr|j|j f|ddd||j|jf|dd d|q|j|j f|d|j d|n||d <|j|j f|dS) Ndestrrfrr4ZstorerZ_constT)actionconstFrE) r3rrr4rpopr3rZ add_optionrr2)rvrrHrrEr-r-r.rs>   zOption.add_to_parsercsgfdd}|jg}jr4||jjpt|\}}|rdgdd<js:js:|d7}|S)NTr)rrr4r)rrZ any_slashesZany_prefix_is_slashrvr-r. _write_opts:s   z+Option.get_help_record.._write_optsrz default: %sz, css|]}d|VqdS)z%sNr-)rdr-r-r.rJsz)Option.get_help_record..rz%s[%s]z z; z / ) rrrrrr6r0r:r"rr)rvrHrIrrrr-rHr.r7s$   zOption.get_help_recordcCsF|jr:|js:|jjD] }|j|jkr|jr|jSqdSt||Sr9) rr3rbrdr3rr2r r)rvrHrIr-r-r.rTs    zOption.get_defaultcs>}jrtj|Stj|jjfdddS)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. cs |Sr9)r&r9rr-r.r:qr;z)Option.prompt_for_value..)rr1r0Z value_proc)rr3rrr1r0)rvrHrr-rr.prompt_for_value`s   zOption.prompt_for_valuecCsLt||}|dk r|S|jrH|jdk rHd|j|jf}tj|SdS)Nr`) r r)r5rrr3r#r$r%r&)rvrHrrr-r-r.r)ss zOption.resolve_envvar_valuecCsf||}|dkrdS|jdkt|j}|dkrb|dk rb|j|}|jrb|jdkrbt||j}|Sr{)r)rr$rr r*r>)rvrHrZ value_depthr-r-r.r|s   zOption.value_from_envvarcCs0|dkr"|jdk r"|js"||St|||Sr9)rrqrKr r(r%r-r-r.r(s  zOption.full_process_value) NFFFFNNFFTNN)r5rrrr,rwr rrrrKr)rr(r-r-r-r.rs, F,   rc@sJeZdZdZdZdddZeddZdd Zd d Z d d Z ddZ dS)rzArguments 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. ZargumentNcKsd|dkr,|ddk rd}n|dddk}tj||fd|i||jdk r`|jdkr`tddS)NrFrrrrz>nargs=-1 in combination with a default value is not supported.)r&r rwrrr)rvrrrr-r-r.rwszArgument.__init__cCs|jdk r|jS|jSr9)rr3r#ryr-r-r.rs zArgument.human_readable_namecCs>|jdk r|jS|j}|js(d|}|jdkr:|d7}|S)Nz[%s]rr)rr3r#rr)rvvarr-r-r.rs   zArgument.make_metavarcCsv|s|sdggfStdt|dkrD|d}}|dd}n&t|dkrZ|\}}ntdt|||ggfS)Nz%Could not determine name for argumentrrrr rz@Arguments take exactly one or two parameter declarations, got %d)rrr"r?)rvr@rr3argr-r-r.r s     zArgument._parse_declscCs |gSr9)rrr-r-r.rszArgument.get_usage_piecescCs|j|j|j|ddS)NrC) add_argumentr3rrr-r-r.rs zArgument.add_to_parser)N) r5rrrr,rwrrrr rrr-r-r-r.rs   r)rbr)N)F)N)@r$r( contextlibr itertoolsr functoolsrtypesrrrZutilsr r r r exceptionsr rrrrZtermuirrZ formattingrrrrrglobalsrrZ_compatrrrZ _unicodefunrrobjectr.rrr'r8r>rMrOr]r^rrr1rrr rrZ decoratorsrbrr-r-r-r.sT     <%.m5K <