U W<@sdZddlZddlmZddlmZmZmZmZddZ dd Z d d Z d d Z ddZ GdddeZGdddeZGdddeZGdddeZdS)aq 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 /parser.py_fetch's  z_unpack_args.._fetchrcsg|] }qSrr).0_)rargsrr 5sz _unpack_args..rzCannot have two nargs < 0) rappendrangereversetuple TypeErrorlenreversedlist)rZ nargs_specrvnargsxr)rrr r _unpack_argss0     r cCs(|dkrtd|td||fdS)Nrz%s option requires an argumentz%s option requires %d arguments)r)roptrrr_error_opt_argsKs r"cCsT|dd}|rd|fS|dd|krD|dd|ddfS||ddfS)Nr)isalnum)r!firstrrr split_optQs  r'cCs0|dks|jdkr|St|\}}|||Sr)Ztoken_normalize_funcr')r!ctxprefixrrr normalize_optZs 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_stringas  ( 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__us*  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< store_constrZ append_constcountrrzunknown action %r) rDrHrCrE setdefaultrgetrAorderrF)rGrIstaterrrprocesss     zOption.process)NrNN)__name__ __module__ __qualname__rJpropertyrLrSrrrrr;ss  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) rsumrrrCrHrQrrF)rGrIrRZholesrrrrSs    zArgument.process)rN)rTrUrVrJrSrrrrrXs rXc@seZdZddZdS) ParsingStatecCsi|_g|_||_g|_dSr)rHlargsrargsrQ)rGr`rrrrJszParsingState.__init__N)rTrUrVrJrrrrr^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;rhupdater@r=rfr>rg) 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. NrY)rirrXrZrrr 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_rQ)rGrrRrrr parse_argss zOptionParser.parse_argscCsVt|j|jdd|jD\}}t|jD]\}}||||q,||_g|_dS)NcSsg|] }|jqSr)rr[rrrrsz7OptionParser._process_args_for_args..)r r_r`ri enumeraterS)rGrRZpargsridxr9rrrros z#OptionParser._process_args_for_argscCsz|jrv|jd}t|}|dkr&dS|dd|jkrN|dkrN|||q|jrb|j|q|jd|dSqdS)Nrrcr) r`r rrh _process_optsrdr_rinsert)rGrRr9Zarglenrrrrns z&OptionParser._process_args_for_optionscs|jkr*fdd|jD}t|d|j}|jr|dk rP|jd||j}t|j|krpt|q|dkr|jd}qt |jd|}|jd|=n|dk rt dnd}| ||dS)Ncsg|]}|r|qSr) startswith)rZwordr!rrrBs z0OptionParser._match_long_opt..) possibilitiesrrz%s option does not take a value) rgrrLr`rtrrr"r rrrS)rGr!explicit_valuerRrwrkrrIrrvr_match_long_opt@s$    zOptionParser._match_long_optc Cs,d}d}|d}g}|ddD]}t|||j}|j|} |d7}| sf|jr^||q t|| jr|t|kr|j d||dd}| j } t|j | krt | |q| dkr|j d} qt|j d| } |j d| =nd} | | ||r qq |jr(|r(|j|d|dS)NFrrTr#)r*r(rfrPrerrrLrr`rtrr"r rrSr_join) rGr9rRstopir)Zunknown_optionsZchr!rkrrIrrr_match_short_opt`s<     zOptionParser._match_short_optcCsd}d|kr|dd\}}n|}t||j}z||||WnJtk r|dd|jkrr|||YS|jsz|j |YnXdS)N=rr$) splitr*r(ryrrhr}rer_r)rGr9rRrxZlong_optZ norm_long_optrrrrss zOptionParser._process_opts)N)NrNN)rN) rTrUrV__doc__rJrlrmrprornryr}rsrrrrras    $ /ra)rr. collectionsr exceptionsrrrrr r"r'r*r:objectr;rXr^rarrrrs 3 0