U acJ @s~dZddlZddlmZddlmZddlmZddlm Z ddlm Z dd lm Z dd lm Z ej rddlZdd lmZdd lmZdd lmZddlmZedZeZejeejeejejej eejej!edfej"efdddZ#eejeefdddZ$eej!dedddZ%eej"edddZ&GdddZGdd d ZGd!d"d"Z'Gd#d$d$Z(dS)%a; This module started out as largely a copy paste from the stdlib's optparse module with the features removed that we do not need from optparse because we implement them in Click on a higher level (for instance type handling, help formatting and a lot more). The plan is to remove more and more from here over time. The reason this is a different module and not optparse from the stdlib is that there are differences in 2.x and 3.x about the error messages generated and optparse in the stdlib uses gettext for no good reason and might cause us issues. Click uses parts of optparse written by Gregory P. Ward and maintained by the Python Software Foundation. This is limited to code in parser.py. Copyright 2001-2006 Gregory P. Ward. All rights reserved. Copyright 2002-2006 Python Software Foundation. All rights reserved. N)deque)gettext)ngettext)BadArgumentUsage)BadOptionUsage) NoSuchOption) UsageError)Argument)Context)Option) ParameterV)args nargs_specreturncstt|}g}ddtjtdfdd |r̈|}|dkrHq2|dkr`|q2|dkrfddt|D}dk r||t|q2|d kr2dk rtd t ||dq2dk rt|<gt |dd|dd<t|t fS) aGiven an iterable of arguments and an iterable of nargs specifications, it returns a tuple with all the unpacked arguments at the first index and all remaining arguments as the second. The nargs specification is the number of arguments that should be consumed or `-1` to indicate that this position should eat up all the remainders. Missing items are filled with `None`. Nz te.Deque[V])crcs<z dkr|WS|WSWntk r6YdSXdSN)popleftpop IndexError)r)sposMC:\Users\vtejo\AppData\Local\Temp\pip-unpacked-wheel-s9fh6ldx\click\parser.py_fetchBs  z_unpack_args.._fetchrcsg|] }qSrr).0_)rrrr Tsz _unpack_args..rzCannot have two nargs < 0) rtOptionalrappendrangereversetuple TypeErrorlenreversedlist)rrrvnargsxr)rrrr _unpack_args1s4      r+)optrcCsT|dd}|rd|fS|dd|krD|dd|ddfS||ddfS)Nr)isalnum)r,firstrrr split_optms  r1r )r,ctxrcCs4|dks|jdkr|St|\}}|||Sr)Ztoken_normalize_funcr1)r,r2prefixrrr normalize_optvs r4)stringrcCsdddl}|j|dd}d|_d|_g}z|D]}||q,Wn tk r^||jYnX|S)aSplit an argument string as with :func:`shlex.split`, but don't fail if the string is incomplete. Ignores a missing closing quote or incomplete escape sequence and uses the partial token as-is. .. code-block:: python split_arg_string("example 'my file") ["example", "my file"] split_arg_string("example my\") ["example", "my"] :param string: String to split. rNT)posixr-)shlexwhitespace_split commentersr ValueErrortoken)r5r7lexoutr;rrrsplit_arg_string}sr>c@sdeZdZddejeejeejeeejejdddZ e e ddd Z ed dd d d Z dS)r Nr CoreOption)objoptsdestactionr)constc Csg|_g|_t|_|D]r}t|\}} |ssz#Argument.process..rz'Argument {name!r} takes {nargs} values.namer)r) r)AssertionErrorsumr%rrformatrBr@envvarrArZr )rLrMrSZholesrrrr[s     zArgument.process)r) r\r]r^rrr`rarNUnionr_r[rrrrr s r c@s"eZdZejeddddZdS)rQN)rargsrcCsi|_g|_||_g|_dSr)rAlargsrqrZ)rLrqrrrrNszParsingState.__init__)r\r]r^rListr`rNrrrrrQsrQc @s(eZdZdZd$ejdddddZd%dejeejeejee ejej dd d d Z d&d ejee dd ddZ ej eejejeej fej eej dfdddZeddddZeddddZeejeeddddZeeddddZeeeej dd d!Zeeddd"d#ZdS)' OptionParsera+The option parser is an internal class that is ultimately used to parse options and arguments. It's modelled after optparse and brings a similar but vastly simplified API. It should generally not be used directly as the high level Click classes wrap it for you. It's not nearly as extensible as optparse or argparse as it does not implement features that are implemented on a higher level (such as types or defaults). :param ctx: optionally the :class:`~click.Context` where this parser should go with. Nr )r2rcCsJ||_d|_d|_|dk r*|j|_|j|_i|_i|_ddh|_g|_dS)NTF---)r2allow_interspersed_argsignore_unknown_options _short_opt _long_opt _opt_prefixes_args)rLr2rrrrN s zOptionParser.__init__rr?)r@rArBrCr)rDrc sdfdd|D}t||||||d}j|j|jD]}|j|<q:|jD]}|j|<qPdS)ayAdds a new option named `dest` to the parser. The destination is not inferred (unlike with optparse) and needs to be explicitly provided. Action can be any of ``store``, ``store_const``, ``append``, ``appnd_const`` or ``count``. The `obj` can be used to identify the option in the order list that is returned from the parser. csg|]}t|jqSr)r4r2)rr,rOrrr4sz+OptionParser.add_option..)rCr)rDN)r r{updaterJrGryrHrz) rLr@rArBrCr)rDoptionr,rrOr add_option#s   zOptionParser.add_optionre)r@rBr)rcCs|jt|||ddS)zAdds a positional argument named `dest` to the parser. The `obj` can be used to identify the option in the order list that is returned from the parser. )rBr)N)r|r r rfrrr add_argument<szOptionParser.add_argument CoreParameter)rrcCsZt|}z||||Wn(tk rH|jdksB|jjsDYnX|j|j|jfS)aZParses positional arguments and returns ``(values, args, order)`` for the parsed options and arguments as well as the leftover arguments if there are any. The order is a list of objects as they appear on the command line. If arguments appear multiple times they will be memorized multiple times as well. N) rQ_process_args_for_options_process_args_for_argsr r2Zresilient_parsingrArrrZ)rLrrSrrr parse_argsFs  zOptionParser.parse_args)rSrcCsVt|j|jdd|jD\}}t|jD]\}}||||q,||_g|_dS)NcSsg|] }|jqSr)r)rgrrrrZsz7OptionParser._process_args_for_args..)r+rrrqr| enumerater[)rLrSZpargsridxargrrrrXs z#OptionParser._process_args_for_argscCsz|jrv|jd}t|}|dkr&dS|dd|jkrN|dkrN|||q|jrb|j|q|jd|dSqdS)Nrrvr) rqrr%r{ _process_optsrwrrr insert)rLrSrZarglenrrrrcs z&OptionParser._process_args_for_options)r,explicit_valuerSrcCs||jkr2ddlm}|||j}t|||jd|j|}|jrh|dk rX|jd|||||}n$|dk rt |t dj |dnd}| ||dS)Nr)get_close_matches) possibilitiesr2z&Option {name!r} does not take a value.)rj) rzdifflibrrr2rPrqr_get_value_from_staterrrnr[)rLr,rrSrrr~rMrrr_match_long_opts     zOptionParser._match_long_opt)rrSrc Csd}d}|d}g}|ddD]}t|||j}|j|} |d7}| sp|jrb||q t||jd| jr|t|kr|j d||dd}| || |} nd} | | ||r qq |jr|r|j |d|dS)NFrr)r2Tr-)r4r2ryrYrxr rrPr%rqrrr[rrjoin) rLrrSstopir3Zunknown_optionschr,r~rMrrr_match_short_opts0     zOptionParser._match_short_opt) option_namer~rSrcCs|j}t|j|kr@|jjr"t}qt|tdd|j||dnt|dkr|jd}|jjrt|t r|dd|j krt|dkrt}q|j d}nt |jd|}|jd|=|S)Nz%Option {name!r} requires an argument.z+Option {name!r} requires {nargs} arguments.rirr) r)r%rqr@_flag_needs_valuerrrn isinstancer`r{rr#)rLrr~rSr)rMZ next_rargrrrrs<   z"OptionParser._get_value_from_statecCsd}d|kr|dd\}}n|}t||j}z||||WnLtk r|dd|jkrt|||YdS|js||j |YnXdS)N=rr.) splitr4r2rrr{rrxrrr )rLrrSrZlong_optZ norm_long_optrrrrs  zOptionParser._process_opts)N)NrN)r)r\r]r^__doc__rrrNr_r`rarbrrrsTupleDictrrQrrrrr rrrrrrrtsL    &  % ) &rt))rtypingr collectionsrrrr exceptionsrrrr TYPE_CHECKINGZtyping_extensionstecorer rer r r?r rTypeVarrobjectrr_r`rarrprrsr+r1r4r>rQrtrrrrs6            0 < "6