U x6[<@sdZddlZddlmZddlmZmZmZmZddZ dd Z d d Z d d Z ddZ GdddeZGdddeZGdddeZGdddeZdS)aE click.parser ~~~~~~~~~~~~ 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. N)deque) UsageError NoSuchOptionBadOptionUsageBadArgumentUsagecstt|}g}dfdd|r|}|dkrH|q$|dkrfddt|D}dk rx||t|q$|dkr$dk rtdt||dq$dk rt|<gt|dd|dd<t|tfS) 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`. Ncs<z dkr|WS|WSWntk r6YdSXdSN)popleftpop IndexError)c)spos./usr/lib/python3/dist-packages/click/parser.py_fetch(s  z_unpack_args.._fetchrcsg|] }qSrr).0_)rargsrr 6sz _unpack_args..rzCannot have two nargs < 0) rappendrangereversetuple TypeErrorlenreversedlist)rZ nargs_specrvnargsxr)rrr r _unpack_argss0     r cCs,|dkrt|d|t|d||fdS)Nrz%s option requires an argumentz%s option requires %d arguments)r)roptrrr_error_opt_argsLsr"cCsT|dd}|rd|fS|dd|krD|dd|ddfS||ddfS)Nr)isalnum)r!firstrrr split_optRs  r'cCs0|dks|jdkr|St|\}}|||Sr)Ztoken_normalize_funcr')r!ctxprefixrrr normalize_opt[s r*c Csg}td|tjD]}|}|dd|ddkrf|dddkrf|ddddd}zt||}Wntk rYnX| |q|S) zDGiven an argument string this attempts to split it into small parts.z>('([^'\\]*(?:\\.[^'\\]*)*)'|"([^"\\]*(?:\\.[^"\\]*)*)"|\S+)\s*Nrz"'asciibackslashreplacezunicode-escape) refinditerSgroupstripencodedecodetype UnicodeErrorr)stringrmatchargrrrsplit_arg_stringbs  ( r:c@s*eZdZd ddZeddZddZdS) OptionNrc Csg|_g|_t|_|D]n}t|\}} |s8td||j|dt|dkrnt| dkrn|j|q|j||j|q|dkrd}||_ ||_ ||_ ||_ ||_ dS)Nz'Invalid start character for option (%s)rrstore) _short_opts _long_optssetprefixesr' ValueErroraddrrdestactionrconstobj) selfoptsrCrDrrErFr!r)valuerrr__init__vs*  zOption.__init__cCs |jdkS)N)r<r)rDrGrrr takes_valueszOption.takes_valuecCs|jdkr||j|j<n|jdkr2|j|j|j<n||jdkrT|j|jg|nZ|jdkrx|j|jg|jn6|jdkr|j|jdd|j|j<ntd|j|j|j dS) Nr<Z store_constrZ append_constcountrrzunknown action %r) rDrHrCrE setdefaultrgetrAorderrF)rGrIstaterrrprocesss     zOption.process)NrNN)__name__ __module__ __qualname__rJpropertyrLrRrrrrr;ts  r;c@seZdZdddZddZdS)ArgumentrNcCs||_||_||_dSrrCrrFrGrCrrFrrrrJszArgument.__init__cCsh|jdkrJtdd|D}|t|kr.d}n|dkrJtd|j|jf||j|j<|j|jdS)Nrcss|]}|dkrdVqdS)Nrrrrrrr sz#Argument.process..rzargument %s takes %d values) rsumrrrCrHrPrrF)rGrIrQZholesrrrrRs    zArgument.process)rN)rSrTrUrJrRrrrrrWs rWc@seZdZddZdS) ParsingStatecCsi|_g|_||_g|_dSr)rHlargsrargsrP)rGr_rrrrJszParsingState.__init__N)rSrTrUrJrrrrr]sr]c@s^eZdZdZdddZdddZddd Zd d Zd d ZddZ ddZ ddZ ddZ dS) 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. NcCsN||_d|_d|_|dk r*|j|_|j|_i|_i|_tddg|_g|_dS)NTF---)r(allow_interspersed_argsignore_unknown_options _short_opt _long_optr? _opt_prefixes_args)rGr(rrrrJszOptionParser.__init__rc sp|dkr |}fdd|D}t||||||d}j|j|jD]}|j|<qF|jD]}|j|<q\dS)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. Ncsg|]}t|jqSr)r*r()rr!rKrrrsz+OptionParser.add_option..)rDrrErF)r;rgupdater@r=rer>rf) rGrHrCrDrrErFoptionr!rrKr add_options     zOptionParser.add_optioncCs&|dkr |}|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. NrX)rhrrWrYrrr add_argumentszOptionParser.add_argumentcCsZt|}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) r]_process_args_for_options_process_args_for_argsrr(Zresilient_parsingrHr^rP)rGrrQrrr parse_argss zOptionParser.parse_argscCsVt|j|jdd|jD\}}t|jD]\}}||||q,||_g|_dS)NcSsg|] }|jqSr)rrZrrrrsz7OptionParser._process_args_for_args..)r r^r_rh enumeraterR)rGrQZpargsridxr9rrrrns z#OptionParser._process_args_for_argscCsz|jrv|jd}t|}|dkr&dS|dd|jkrN|dkrN|||q|jrb|j|q|jd|dSqdS)Nrrbr) r_r rrg _process_optsrcr^rinsert)rGrQr9Zarglenrrrrms z&OptionParser._process_args_for_optionscs|jkr.fdd|jD}t||jd|j}|jr|dk rT|jd||j}t|j|krtt|q|dkr|j d}qt |jd|}|jd|=n|dk rt dnd}| ||dS)Ncsg|]}|r|qSr) startswith)rZwordr!rrrCs z0OptionParser._match_long_opt..) possibilitiesr(rrz%s option does not take a value) rfrr(rLr_rsrrr"r rrrR)rGr!explicit_valuerQrvrjrrIrrur_match_long_optAs$   zOptionParser._match_long_optc Cs2d}d}|d}g}|ddD]}t|||j}|j|} |d7}| sl|jr^||q t||jd| jr|t|kr|j d||dd}| j } t|j | krt | |q| dkr|j d} qt|j d| } |j d| =nd} | | ||r q q |jr.|r.|j|d|dS)NFrr)r(Tr#)r*r(rerOrdrrrLrr_rsrr"r rrRr^join) rGr9rQstopir)Zunknown_optionsZchr!rjrrIrrr_match_short_optas<     zOptionParser._match_short_optcCsd}d|kr|dd\}}n|}t||j}z||||WnJtk r|dd|jkrr|||YS|jsz|j |YnXdS)N=rr$) splitr*r(rxrrgr|rdr^r)rGr9rQrwZlong_optZ norm_long_optrrrrrs zOptionParser._process_opts)N)NrNN)rN) rSrTrU__doc__rJrkrlrornrmrxr|rrrrrrr`s    $ /r`)rr. collectionsr exceptionsrrrrr r"r'r*r:objectr;rWr]r`rrrrs 3 0