o +cadJ @sdZddlZddlmZddlmZddlmZddlm Z ddlm Z dd lm Z dd lm Z ej rOddlZdd lmZdd lmZdd lmZddlmZedZeZdejedejedejejej eejej!edfej"effddZ#dedejeeffddZ$dedej!ddefddZ%dedej"efddZ&Gdd d ZGd!d"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) ParameterVargs nargs_specreturncstt|}g}ddddtjtffdd |rh|}|dur%q|dkr1|n5|dkrQfdd t|D}durI||t|n|d krfdur]td t ||d|sdurt|<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`. Ncz te.Deque[V]rcs2zdur |WS|WStyYdSwN)popleftpop IndexError)r)spos./usr/lib/python3/dist-packages/click/parser.py_fetchBs   z_unpack_args.._fetchrcsg|]}qSrr).0_)rrrr Tsz _unpack_args..rzCannot have two nargs < 0) rtOptionalrappendrangereversetuple TypeErrorlenreversedlist)rrrvnargsxr)rrrr _unpack_args1s6     r+optcCsT|dd}|rd|fS|dd|kr"|dd|ddfS||ddfS)Nr)isalnum)r,firstrrr split_optms r1ctxr cCs4|dus |jdur |St|\}}|||Sr)token_normalize_funcr1)r,r2prefixrrr normalize_optvs r5stringcCsbddl}|j|dd}d|_d|_g}z |D]}||qW|Sty0||jY|Sw)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)r6r8lexoutr<rrrsplit_arg_string}s  r?c@sxeZdZ   ddddejedejedejeded ejejf d d Z e d e fd dZ deddd dfddZ dS)r Nrobj CoreOptionoptsdestactionr)constc Csg|_g|_t|_|D]9}t|\}} |std|d|j|dt|dkr9t| dkr9|j|q |j||j|q |durLd}||_ ||_ ||_ ||_ ||_ dS)Nz$Invalid start character for option ()rrstore) _short_opts _long_optssetprefixesr1r;addr%r rCrDr)rEr@) selfr@rBrCrDr)rEr,r4valuerrr__init__s&    zOption.__init__rcCs |jdvS)N)rGr )rDrMrrr takes_values zOption.takes_valuerNstate ParsingStatecCs|jdkr ||j|j<nM|jdkr|j|j|j<n@|jdkr*|j|jg|n/|jdkr<|j|jg|jn|jdkrP|j|jdd|j|j<n td|jd |j|j dS) NrG store_constr append_constcountrrzunknown action '') rDrBrCrE setdefaultr getr;orderr@)rMrNrRrrrprocesss     zOption.processNrN)__name__ __module__ __qualname__rSequencestrrintAnyrOpropertyboolrQr[rrrrr s(  !r c@s\eZdZddddejedefddZdejejeej ejefd d d d fd dZ d S)r rr@ CoreArgumentrCr)cCs||_||_||_dSr)rCr)r@rMr@rCr)rrrrOs zArgument.__init__rNrRrSrNcCs|jdkr.|dus Jtdd|D}|t|krd}n|dkr.ttdj|j|jd|jdkr?|jjdur?|dkr?d}||j |j<|j |jdS) Nrcss|] }|durdVqdS)Nrrrr*rrr sz#Argument.process..rz'Argument {name!r} takes {nargs} values.namer)r) r)sumr%rrformatrCr@envvarrBrZr )rMrNrRholesrrrr[s    zArgument.processr) r]r^r_rrrarbrOUnionr`r[rrrrr sr c@s$eZdZdejeddfddZdS)rSrargsrNcCsi|_g|_||_g|_dSr)rBlargsrsrZ)rMrsrrrrOs zParsingState.__init__)r]r^r_rListrarOrrrrrSsrSc@s^eZdZdZd+dejdddfddZ   d,d d d ejed ejed ejede dejej ddfddZ d-d dd ejede ddfddZ dej edejejeej fej eej dffddZdeddfddZdeddfddZdedejededdfd d!Zd"ededdfd#d$Zd%ed&ededej fd'd(Zd"ededdfd)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. Nr2r rcCsJ||_d|_d|_|dur|j|_|j|_i|_i|_ddh|_g|_dS)NTF---)r2allow_interspersed_argsignore_unknown_options _short_opt _long_opt _opt_prefixes_args)rMr2rrrrO s  zOptionParser.__init__rr@rArBrCrDr)rEc sdfdd|D}t||||||d}j|j|jD]}|j|<q|jD]}|j|<q(dS)azAdds 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``, ``append_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)r5r2)rr,rPrrr4sz+OptionParser.add_option..)rDr)rEN)r r}updaterKrHr{rIr|) rMr@rBrCrDr)rEoptionr,rrPr add_option#s    zOptionParser.add_optionrfcCs|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. )rCr)N)r~r r rgrrr add_argument<szOptionParser.add_argumentr CoreParametercCsXt|}z ||||Wnty#|jdus |jjs!Ynw|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) rS_process_args_for_options_process_args_for_argsr r2resilient_parsingrBrtrZ)rMrrRrrr parse_argsFs   zOptionParser.parse_argsrRcCsVt|j|jdd|jD\}}t|jD] \}}||||q||_g|_dS)NcSsg|]}|jqSr)r)rhrrrrZsz7OptionParser._process_args_for_args..)r+rtrsr~ enumerater[)rMrRpargsridxargrrrrXs z#OptionParser._process_args_for_argscCs|jr?|jd}t|}|dkrdS|dd|jvr'|dkr'|||n|jr1|j|n |jd|dS|jsdSdS)Nrrxr) rsrr%r} _process_optsryrtr insert)rMrRrarglenrrrrcs z&OptionParser._process_args_for_optionsr,explicit_valuecCs||jvrddlm}|||j}t|||jd|j|}|jr4|dur,|jd|||||}n|durCt |t dj |dd}| ||dS)Nr)get_close_matches) possibilitiesr2z&Option {name!r} does not take a value.)rk) r|difflibrrr2rQrsr_get_value_from_staterrrnr[)rMr,rrRrrrrNrrr_match_long_opts    zOptionParser._match_long_optrc Csd}d}|d}g}|ddD]R}t|||j}|j|} |d7}| s8|jr1||qt||jd| jrV|t|krN|j d||dd}| || |} nd} | | ||rbnq|jrw|ry|j |d|dSdSdS)NFrr)r2Tr-)r5r2r{rYrzr rrQr%rsrrr[rtjoin) rMrrRstopir4unknown_optionschr,rrNrrr_match_short_opts4     zOptionParser._match_short_opt option_namercCs|j}t|j|kr |jjrt}|St|tdd|j||d|dkrM|jd}|jjrEt|t rE|dd|j vrEt|dkrEt}|S|j d}|St |jd|}|jd|=|S)Nz%Option {name!r} requires an argument.z+Option {name!r} requires {nargs} arguments.rjrr) r)r%rsr@_flag_needs_valuerrrn isinstancerar}rr#)rMrrrRr)rN next_rargrrrrs<    z"OptionParser._get_value_from_statecCsd}d|vr|dd\}}n|}t||j}z ||||WdStyG|dd|jvr:|||YdS|js>|j |YdSw)N=rr.) splitr5r2rrr}rrzrtr )rMrrRrlong_opt norm_long_optrrrrs   zOptionParser._process_optsrr\rq)r]r^r___doc__rrrOr`rarbrcrrruTupleDictrrSrrrrr rrrrrrrvst     &  $ ( &rv))rtypingr collectionsrrrr exceptionsrrrr TYPE_CHECKINGtyping_extensionstecorer rfr r rAr rTypeVarrobjectrr`rarbrrrrrur+r1r5r?rSrvrrrrs>            0 < "6