B x `K=@sdZddlZddlmZddlmZddlmZddlmZddlmZd d Z d d Z d dZ ddZ ddZ GdddeZGdddeZGdddeZGdddeZdS)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)BadArgumentUsage)BadOptionUsage) NoSuchOption) UsageErrorcstt|}g}dfddx|r|}|dkrJ|q&|dkrfddt|D}dk rz||t|q&|dkr&dk rtdt||dq&Wdk 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`. Ncs6ydkr|S|SWntk r0dSXdS)N)popleftpop IndexError)c)spos0/tmp/pip-unpacked-wheel-za9e8r29/click/parser.py_fetch-s  z_unpack_args.._fetchrcsg|] }qSr r ).0_)rargsr r ;sz _unpack_args..rzCannot have two nargs < 0) rappendrangereversetuple TypeErrorlenreversedlist)rZ nargs_specrvnargsxr )rrr r _unpack_argss0    rcCs.|dkrt|d|t|d||dS)Nrz{} option requires an argumentz{} option requires {} arguments)rformat)roptr r r_error_opt_argsQsr"cCsT|dd}|rd|fS|dd|krD|dd|ddfS||ddfS)Nr)isalnum)r!firstr r r split_optWs  r'cCs0|dks|jdkr|St|\}}|||S)N)Ztoken_normalize_funcr')r!ctxprefixr r r normalize_opt`s r*c Csg}xtd|tjD]}|}|dd|ddkrh|dddkrh|ddddd}yt||}Wntk rYnX| |qW|S) zDGiven an argument string this attempts to split it into small parts.zB('([^'\\]*(?:\\.[^'\\]*)*)'|\"([^\"\\]*(?:\\.[^\"\\]*)*)\"|\S+)\s*Nrz"'asciibackslashreplacezunicode-escape) refinditerSgroupstripencodedecodetype UnicodeErrorr)stringrmatchargr r rsplit_arg_stringgs  (r:c@s*eZdZd ddZeddZddZdS) OptionNrc Csg|_g|_t|_xx|D]p}t|\}} |ssz#Argument.process..rzargument {} takes {} values) rsumrrr rCrHrQrrF)rGrIrRZholesr r rrSs   zArgument.process)rN)rTrUrVrJrSr r r rrXs rXc@seZdZddZdS) ParsingStatecCsi|_g|_||_g|_dS)N)rHlargsrargsrQ)rGr]r r rrJszParsingState.__init__N)rTrUrVrJr r r rr[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. NcCsJ||_d|_d|_|dk r*|j|_|j|_i|_i|_ddh|_g|_dS)NTF-z--)r(allow_interspersed_argsignore_unknown_options _short_opt _long_opt _opt_prefixes_args)rGr(r r rrJs zOptionParser.__init__rc sx|dkr |}fdd|D}t||||||d}j|jx|jD]}|j|<qHWx|jD]}|j|<qbWdS)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!)rGr rrsz+OptionParser.add_option..)rDrrErF)r;rdupdater@r=rbr>rc) rGrHrCrDrrErFoptionr!r )rGr 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. N)rCrrF)rerrX)rGrCrrFr r r add_argumentszOptionParser.add_argumentcCsZt|}y||||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)rGrrRr r r parse_argss zOptionParser.parse_argscCsZt|j|jdd|jD\}}x&t|jD]\}}||||q.W||_g|_dS)NcSsg|] }|jqSr )r)rrr r rrsz7OptionParser._process_args_for_args..)rr\r]re enumeraterS)rGrRZpargsridxr9r r rrks  z#OptionParser._process_args_for_argscCs~xx|jrx|jd}t|}|dkr(dS|dd|jkrP|dkrP|||q|jrd|j|q|jd|dSqWdS)Nrz--r) r]r rrd _process_optsr`r\rinsert)rGrRr9Zarglenr r rrjs 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 d nd}| ||dS)Ncsg|]}|r|qSr ) startswith)rword)r!r rrEsz0OptionParser._match_long_opt..) possibilitiesr(rrz{} option does not take a value)rcrr(rKr]rprrr"r rrr rS)rGr!explicit_valuerRrsrgrrIr )r!r_match_long_optCs$   zOptionParser._match_long_optc Cs6d}d}|d}g}x|ddD]}t|||j}|j|} |d7}| sn|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"Pq"W|jr2|r2|jd|d|dS)NFrr)r(Tz{}{}r#)r*r(rbrPrarrrKrr]rprr"r rrSr\r join) rGr9rRstopir)Zunknown_optionschr!rgrrIr r r_match_short_optbs<     zOptionParser._match_short_optcCsd}d|kr|dd\}}n|}t||j}y||||WnFtk r|dd|jkrn|||S|jsv|j |YnXdS)N=rr$) splitr*r(rurrdrzrar\r)rGr9rRrtZlong_optZ norm_long_optr r rros  zOptionParser._process_opts)N)NrNN)rN) rTrUrV__doc__rJrhrirlrkrjrurzror r r rr^s     $/r^)r}r. collectionsr exceptionsrrrrrr"r'r*r:objectr;rXr[r^r r r rs     3 .