o ac^@sdZdZgdZddlZddlZddlZddl m Z m Z dZ dZ dZd Zd Zd Zd ZGd ddeZddZGdddeZGdddeZGdddeZGdddeZGdddeZddZGdddeZGdd d eZGd!d"d"eZGd#d$d$eZ Gd%d&d&eZ!Gd'd(d(eZ"Gd)d*d*e"Z#Gd+d,d,e"Z$Gd-d.d.eZ%Gd/d0d0eZ&Gd1d2d2eZ'Gd3d4d4eZ(Gd5d6d6eZ)Gd7d8d8eZ*Gd9d:d:e%Z+Gd;d<dd>eZ-Gd?d@d@eZ.GdAdBdBe.Z/GdCdDdDe/Z0GdEdFdFee.Z1dS)Ga Command-line parsing library This module is an optparse-inspired command-line parsing library that: - handles both optional and positional arguments - produces highly informative usage messages - supports parsers that dispatch to sub-parsers The following is a simple usage example that sums integers from the command-line and writes the result to a file:: parser = argparse.ArgumentParser( description='sum the integers at the command line') parser.add_argument( 'integers', metavar='int', nargs='+', type=int, help='an integer to be summed') parser.add_argument( '--log', default=sys.stdout, type=argparse.FileType('w'), help='the file where the sum should be written') args = parser.parse_args() args.log.write('%s' % sum(args.integers)) args.log.close() The module contains the following public classes: - ArgumentParser -- The main entry point for command-line parsing. As the example above shows, the add_argument() method is used to populate the parser with actions for optional and positional arguments. Then the parse_args() method is invoked to convert the args at the command-line into an object with attributes. - ArgumentError -- The exception raised by ArgumentParser objects when there are errors with the parser's actions. Errors raised while parsing the command-line are caught by ArgumentParser and emitted as command-line messages. - FileType -- A factory for defining types of files to be created. As the example above shows, instances of FileType are typically passed as the type= argument of add_argument() calls. - Action -- The base class for parser actions. Typically actions are selected by passing strings like 'store_true' or 'append_const' to the action= argument of add_argument(). However, for greater customization of ArgumentParser actions, subclasses of Action may be defined and passed as the action= argument. - HelpFormatter, RawDescriptionHelpFormatter, RawTextHelpFormatter, ArgumentDefaultsHelpFormatter -- Formatter classes which may be passed as the formatter_class= argument to the ArgumentParser constructor. HelpFormatter is the default, RawDescriptionHelpFormatter and RawTextHelpFormatter tell the parser not to change the formatting for help text, and ArgumentDefaultsHelpFormatter adds information about argument defaults to the help. All other classes in this module are considered implementation details. (Also note that HelpFormatter and RawDescriptionHelpFormatter are only considered public as object names -- the API of the formatter objects is still considered an implementation detail.) z1.1)ArgumentParser ArgumentErrorArgumentTypeErrorBooleanOptionalActionFileType HelpFormatterArgumentDefaultsHelpFormatterRawDescriptionHelpFormatterRawTextHelpFormatterMetavarTypeHelpFormatter NamespaceAction ONE_OR_MOREOPTIONALPARSER REMAINDERSUPPRESS ZERO_OR_MOREN)gettextngettextz ==SUPPRESS==?*+zA......Z_unrecognized_argsc@(eZdZdZddZddZddZdS) _AttributeHolderaAbstract base class that provides __repr__. The __repr__ method returns a string in the format:: ClassName(attr=name, attr=name, ...) The attributes are determined either by a class-level attribute, '_kwarg_names', or by inspecting the instance __dict__. cCst|j}g}i}|D] }|t|q |D]\}}|r-|d||fq|||<q|r=|dt|d|d|fS)N%s=%rz**%s%s(%s), )type__name__ _get_argsappendrepr _get_kwargs isidentifierjoin)selfZ type_name arg_stringsZ star_argsargnamevaluer,i/home/lily/lilypond-2.24.1/release/binaries/dependencies/install/Python-3.10.8/lib/python3.10/argparse.py__repr__ts   z_AttributeHolder.__repr__cCst|jSN)list__dict__itemsr'r,r,r-r$z_AttributeHolder._get_kwargscCsgSr/r,r3r,r,r-r!z_AttributeHolder._get_argsN)r __module__ __qualname____doc__r.r$r!r,r,r,r-rks  rcCs6|durgSt|tur|ddSddl}||SNr)rr0copy)r2r:r,r,r- _copy_itemss    r;c@seZdZdZ   d;ddZddZd d ZGd d d eZd dZ ddZ ddZ ddZ dz6HelpFormatter._Section.format_help..z%*s%s:  ) r\r[rY _join_partsr2rZr]rrH)r'r&Z item_helpZcurrent_indentr]r,r,r- format_helps    z"HelpFormatter._Section.format_helpr/)r r6r7rUrgr,r,r,r-rKs  rKcCs|jj||fdSr/)rMr2r")r'r`rar,r,r- _add_itemszHelpFormatter._add_itemcCs0||||j|}||jg||_dSr/)rYrKrMrhrg)r'r]Zsectionr,r,r- start_sections zHelpFormatter.start_sectioncCs|jj|_|dSr/)rMr\rZr3r,r,r- end_sections  zHelpFormatter.end_sectioncCs,|tur|dur||j|gdSdSdSr/)rrh _format_text)r'textr,r,r-add_textszHelpFormatter.add_textcCs*|tur||||f}||j|dSdSr/)rrh _format_usage)r'usageactionsgroupsprefixrar,r,r- add_usages zHelpFormatter.add_usagecCsv|jtur9|j}||g}||D] }|||qttt|}||j}t|j ||_ | |j |gdSdSr/) helpr_format_action_invocation_iter_indented_subactionsr"rEmaplenrHrJrh_format_action)r'actionZget_invocationZ invocations subactionZinvocation_lengthZ action_lengthr,r,r- add_arguments   zHelpFormatter.add_argumentcCs|D]}||qdSr/)r|)r'rprzr,r,r- add_argumentss zHelpFormatter.add_argumentscCs.|j}|r|jd|}|dd}|S)N re)rLrgrRsubstrip)r'rtr,r,r-rgs zHelpFormatter.format_helpcCsddd|DS)NrdcSsg|] }|r |tur|qSr,)r)r_partr,r,r-rb"sz-HelpFormatter._join_parts..)r&)r'Z part_stringsr,r,r-rf!s zHelpFormatter._join_partscs|durtd}|dur|t|jd}n|dur$|s$dt|jd}n|durdt|jd}g}g}|D]}|jrA||q6||q6|j} | |||} ddd|| fD}|j|jt |t |krd} | ||} | ||} t | | }t | | }dfdd }t |t |d krdt |t |d }|r||g|||}| |||n<|r||g|||}n0|g}n,dt |}||}|||}t |d krg}| |||| ||||g|}d |}d ||fS)Nzusage: rSz%(prog)s cSsg|]}|r|qSr,r,)r_sr,r,r-rbBz/HelpFormatter._format_usage..z%\(.*?\)+(?=\s|$)|\[.*?\]+(?=\s|$)|\S+csg}g}|durt|d}nt|d}|D]-}|dt|kr7|r7||d|g}t|d}|||t|d7}q|rQ||d||dura|dt|d|d<|S)NrWrr)rxr"r&)partsindentrrlineslineZline_lenr text_widthr,r- get_linesVs"   z.HelpFormatter._format_usage..get_linesg?rWrez%s%s r/) _dictrBoption_stringsr"_format_actions_usager&rGrHrxrNfindallextend)r'rorprqrrrS optionals positionalsrzformatZ action_usageZ part_regexpZ opt_usageZ pos_usageZ opt_partsZ pos_partsrrrrr,rr-rn&sX              zHelpFormatter._format_usagec Cst}i}|D]}|jstd|z ||jd}Wn ty&Yqw|t|j}||||jkr|jD]}||q:|jsh||vrR||d7<nd||<||vrc||d7<n'd||<n"||vru||d7<nd||<||vr||d7<nd||<t|d |D]} d || <qqg} t|D]\} }|j t ur| d| | d kr| | q| | d d kr| | d q|js||} ||| } ||vr| ddkr| d dkr| d d } | | q|jd} |jdkr|} n||} ||| }d | |f} |js#||vr#d | } | | qt|ddD] } || g| | | <q/ddd| D}d}d}td|d|}td|d|}td||fd|}tdd|}|}|S)Nz empty group rz [[]z (()rW|%s %s[%s]T)reversercSsg|]}|dur|qSr/r,)r_itemr,r,r-rbrcz7HelpFormatter._format_actions_usage..z[\[(]z[\])]z(%s) z\1 (%s)z%s *%srdz \(([^|]*)\))set_group_actions ValueErrorindexrxaddrequiredrange enumeratertrr"getpopr#_get_default_metavar_for_positional _format_argsnargs format_usage!_get_default_metavar_for_optionalsortedr&rNrr)r'rprq group_actionsZinsertsgroupstartendrzirdefaultr option_string args_stringrlopencloser,r,r-rs                   z#HelpFormatter._format_actions_usagecCsFd|vr |t|jd}t|j|jd}d|j}||||dS)Nz%(prog)r rr~)rrBrErGrH _fill_text)r'rlrrr,r,r-rks  zHelpFormatter._format_textc CsDt|jd|j}t|j|d}||jd}||}|js+|jd|f}d|}n t||kr?|jd||f}d|}d}n |jd|f}d|}|}|g}|jr|j r| |} | r| | |} | d|d| df| ddD] } | d|d| fqun | ds| d||D] } | || q||S) Nr<rrdz%*s%s z %*s%-*s rrWre)rDrJrFrErGrHrurtrxr _expand_help _split_linesr"endswithrvryrf) r'rzZ help_positionZ help_widthZ action_widthZ action_headertupZ indent_firstrZ help_textZ help_linesrr{r,r,r-rys<           zHelpFormatter._format_actioncCs|js||}|||d\}|Sg}|jdkr!||jn||}|||}|jD] }|d||fq/d|S)NrWrrr) rr_metavar_formatterrrrrr"r&)r'rzrmetavarrrrr,r,r-ru's      z'HelpFormatter._format_action_invocationcsP|jdur |jn|jdurdd|jD}dd|n|fdd}|S)NcSg|]}t|qSr,str)r_Zchoicer,r,r-rbCrz4HelpFormatter._metavar_formatter..z{%s},csttrSf|Sr/) isinstancetuple)Z tuple_sizeresultr,r-rHs  z0HelpFormatter._metavar_formatter..format)rchoicesr&)r'rzdefault_metavarZ choice_strsrr,rr-r?s   z HelpFormatter._metavar_formattercCs|||}|jdurd|d}|S|jtkr d|d}|S|jtkr;|d}t|dkr5d|}|Sd|}|S|jtkrHd|d}|S|jtkrQd}|S|jtkr^d |d}|S|jtkrgd }|Sz d d t |jD}Wn t yt d dwd |||j}|S)N%srWrr<z [%s [%s ...]]z[%s ...]z %s [%s ...]rz%s ...rdcSsg|]}dqS)rr,)r_rr,r,r-rbesz.HelpFormatter._format_args..zinvalid nargs valuer) rrrrrxr rrrr TypeErrorrr&)r'rzrZ get_metavarrrZformatsr,r,r-rOsB            zHelpFormatter._format_argscCstt||jd}t|D] }||tur||=q t|D]}t||dr-||j||<q|ddurEddd|dD}||d<| ||S)Nrr rrcSrr,rr_cr,r,r-rbtrz.HelpFormatter._expand_help..) rvarsrBr0rhasattrr rr&_get_help_string)r'rzparamsr*Z choices_strr,r,r-rks   zHelpFormatter._expand_helpccsBz|j}Wn tyYdSw||EdH|dSr/)_get_subactionsAttributeErrorrYrZ)r'rzZget_subactionsr,r,r-rvxs    z'HelpFormatter._iter_indented_subactionscCs&|jd|}ddl}|||S)Nrr)rQrrtextwrapZwrap)r'rlrTrr,r,r-rs zHelpFormatter._split_linescCs,|jd|}ddl}|j||||dS)Nrr)Zinitial_indentZsubsequent_indent)rQrrrZfill)r'rlrTrrr,r,r-rs zHelpFormatter._fill_textcC|jSr/)rtr'rzr,r,r-rzHelpFormatter._get_help_stringcCs |jSr/)destupperrr,r,r-r z/HelpFormatter._get_default_metavar_for_optionalcCrr/)rrr,r,r-rrz1HelpFormatter._get_default_metavar_for_positional)r<r=Nr/) r r6r7r8rUrYrZobjectrKrhrirjrmrsr|r}rgrfrnrrkryrurrrrvrrrrrr,r,r,r-rs@  `j0   rc@eZdZdZddZdS)rzHelp message formatter which retains any formatting in descriptions. Only the name of this class is considered a public API. All the methods provided by the class are considered an implementation detail. cs dfdd|jddDS)Nrdc3s|]}|VqdSr/r,)r_rrr,r- sz9RawDescriptionHelpFormatter._fill_text..T)keepends)r& splitlines)r'rlrTrr,rr-rs z&RawDescriptionHelpFormatter._fill_textN)r r6r7r8rr,r,r,r-r rc@r)r zHelp message formatter which retains formatting of all help text. Only the name of this class is considered a public API. All the methods provided by the class are considered an implementation detail. cCs|Sr/)r)r'rlrTr,r,r-rz!RawTextHelpFormatter._split_linesN)r r6r7r8rr,r,r,r-r rr c@r)rzHelp message formatter which adds default values to argument help. Only the name of this class is considered a public API. All the methods provided by the class are considered an implementation detail. cCs>|j}d|jvr|jturttg}|js|j|vr|d7}|S)Nz %(default) (default: %(default)s))rtrrrrrr)r'rzrtZdefaulting_nargsr,r,r-rs  z.ArgumentDefaultsHelpFormatter._get_help_stringN)r r6r7r8rr,r,r,r-rrrc@ eZdZdZddZddZdS)r a Help message formatter which uses the argument 'type' as the default metavar value (instead of the argument 'dest') Only the name of this class is considered a public API. All the methods provided by the class are considered an implementation detail. cC|jjSr/rr rr,r,r-rrz:MetavarTypeHelpFormatter._get_default_metavar_for_optionalcCrr/rrr,r,r-rrz.r,r'namesr,r3r-r$Js zAction._get_kwargscCs |jdSr9rr3r,r,r-rYrzAction.format_usagecCs ttd)Nz.__call__() not defined)NotImplementedErrorrr'parser namespacevaluesrr,r,r-__call__\ zAction.__call__NNNNNFNNr/)r r6r7r8rUr$rrr,r,r,r-r s5 r cs<eZdZ      d fdd Zd ddZddZZS) rNFc s~g} |D]} | | | drd| dd} | | q|dur.|dur.|tur.|d7}tj| |d||||||d dS)N----no-r<rr) rrrrrrrrtr)r" startswithrsuperrU) r'rrrrrrrtrZ_option_stringsr __class__r,r-rU`s(     zBooleanOptionalAction.__init__cCs(||jvrt||j|d dSdS)Nr)rsetattrrrrr,r,r-rs zBooleanOptionalAction.__call__cCs d|jS)Nz | )r&rr3r,r,r-rrz"BooleanOptionalAction.format_usage)NNNFNNr/)r r6r7rUrr __classcell__r,r,r r-r_s rc8eZdZ        dfdd ZdddZZS) _StoreActionNFc T|dkrtd|dur|tkrtdttt|j||||||||| | d dS)Nrznargs for store actions must be != 0; if you have nothing to store, actions such as store true or store const may be more appropriate nargs must be %r to supply constr)rrr rrUrr r,r-rU     z_StoreAction.__init__cCst||j|dSr/)r rrr,r,r-rz_StoreAction.__call__rr/r r6r7rUrr r,r,r r-rrc0eZdZ    dfdd ZdddZZS) _StoreConstActionNFc s"tt|j||d||||ddS)Nr)rrrrrrrt)r rrUr'rrrrrrtrr r,r-rUs  z_StoreConstAction.__init__cCst||j|jdSr/)r rrrr,r,r-rsz_StoreConstAction.__call__NFNNr/rr,r,r r-rsrcs$eZdZ   dfdd ZZS)_StoreTrueActionFNc tt|j||d|||ddS)NTrrrrrrt)r rrUr'rrrrrtr r,r-rU  z_StoreTrueAction.__init__)FFNr r6r7rUr r,r,r r-r rcs$eZdZ   dfdd ZZS)_StoreFalseActionTFNcr)NFr)r r!rUrr r,r-rUrz_StoreFalseAction.__init__)TFNrr,r,r r-r!r r!cr) _AppendActionNFc r)Nrznargs for append actions must be != 0; if arg strings are not supplying the value to append, the append const action may be more appropriaterr)rrr r"rUrr r,r-rUrz_AppendAction.__init__cC2t||jd}t|}||t||j|dSr/)rrr;r"r r'rrrrr2r,r,r-r z_AppendAction.__call__rr/rr,r,r r-r"rr"cr) _AppendConstActionNFc s$tt|j||d|||||ddS)Nr)rrrrrrrtr)r r&rUrr r,r-rU s  z_AppendConstAction.__init__cCs4t||jd}t|}||jt||j|dSr/)rrr;r"rr r$r,r,r-rs z_AppendConstAction.__call__rr/rr,r,r r-r& sr&cs.eZdZ   dfdd ZdddZZS) _CountActionNFcr)Nr)rrrrrrt)r r'rUrr r,r-rU'rz_CountAction.__init__cCs0t||jd}|dur d}t||j|ddSNrrW)rrr )r'rrrrcountr,r,r-r5sz_CountAction.__call__)NFNr/rr,r,r r-r'%s r'cs.eZdZeedffdd ZdddZZS) _HelpActionNcstt|j|||d|ddSNr)rrrrrt)r r*rU)r'rrrrtr r,r-rU>s  z_HelpAction.__init__cCs||dSr/) print_helpexitrr,r,r-rJs z_HelpAction.__call__r/r r6r7rrUrr r,r,r r-r*<s  r*cs0eZdZdeedffdd ZdddZZS)_VersionActionNz&show program's version number and exitcs$tt|j|||d|d||_dSr+)r r/rUversion)r'rr0rrrtr r,r-rUQs  z_VersionAction.__init__cCsD|j}|dur |j}|}||||tj|dSr/)r0_get_formatterrm_print_messagerg_sysstdoutr-)r'rrrrr0r[r,r,r-r_s  z_VersionAction.__call__r/r.r,r,r r-r/Osr/csPeZdZGdddeZedddffdd ZddZd d Zd d d Z Z S)_SubParsersActioncseZdZfddZZS)z&_SubParsersAction._ChoicesPseudoActioncs@|}}|r|dd|7}ttj|}|jg|||ddS)Nrr)rrrtr)r&r r5_ChoicesPseudoActionrU)r'r*aliasesrtrrZsupr r,r-rUms   z/_SubParsersAction._ChoicesPseudoAction.__init__rr,r,r r-r6ksr6FNc s<||_||_i|_g|_tt|j||t|j|||ddS)N)rrrrrrtr) _prog_prefix _parser_class_name_parser_map_choices_actionsr r5rUr)r'rrS parser_classrrrtrr r,r-rUus   z_SubParsersAction.__init__cKs|ddurd|j|f|d<|dd}d|vr,|d}||||}|j||jdi|}||j|<|D]}||j|<q;|S)NrSrr7r,rt)rr8rr6r;r"r9r:)r'r*kwargsr7rtZ choice_actionraliasr,r,r- add_parsers     z_SubParsersAction.add_parsercCs|jSr/)r;r3r,r,r-rrz!_SubParsersAction._get_subactionsc Cs|d}|dd}|jturt||j|z|j|}Wnty8|d|jd}td|}t||w||d\} }t |  D] \} } t|| | qG|rft | t gt |t |dSdS)NrrWr) parser_namerz5unknown parser %(parser_name)r (choices: %(choices)s))rrr r:KeyErrorr&rrparse_known_argsrr2 setdefault_UNRECOGNIZED_ARGS_ATTRrr) r'rrrrr@r(ramsgZ subnamespacekeyr+r,r,r-rs(       z_SubParsersAction.__call__r/) r r6r7r r6rrUr?rrr r,r,r r-r5isr5c@seZdZdddZdS) _ExtendActionNcCr#r/)rrr;rr r$r,r,r-rr%z_ExtendAction.__call__r/)r r6r7rr,r,r,r-rGsrGc@s*eZdZdZd ddZddZd d ZdS) raFactory for creating file object types Instances of FileType are typically passed as type= arguments to the ArgumentParser add_argument() method. Keyword Arguments: - mode -- A string indicating how the file is to be opened. Accepts the same values as the builtin open() function. - bufsize -- The file's desired buffer size. Accepts the same values as the builtin open() function. - encoding -- The file's encoding. Accepts the same values as the builtin open() function. - errors -- A string indicating how encoding and decoding errors are to be handled. Accepts the same value as the builtin open() function. rrNcCs||_||_||_||_dSr/)_mode_bufsize _encoding_errors)r'modebufsizeencodingerrorsr,r,r-rUr^zFileType.__init__c s|dkr7djvrdjvrtjjStjStfdddDr,djvr)tjjStjStdj}t|z t|jj j j WSt y_}z||d}td }t ||d}~ww) N-rHbc3s|]}|jvVqdSr/)rIrr3r,r-rsz$FileType.__call__..Zwaxzargument "-" with mode %r)filenameerrorz$can't open '%(filename)s': %(error)s)rIr3stdinbufferanyr4rrrrJrKrLOSErrorr)r'stringrEerarr,r3r-rs"   zFileType.__call__cCsT|j|jf}d|jfd|jfg}ddd|Ddd|D}dt|j|fS)NrOrPrcSsg|] }|dkrt|qS)r)r#)r_r)r,r,r-rbsz%FileType.__repr__..cSs$g|]\}}|durd||fqS)Nrr,)r_kwr)r,r,r-rbs  r)rIrJrKrLr&rr )r'rar=Zargs_strr,r,r-r.s  zFileType.__repr__)rHrNN)r r6r7r8rUrr.r,r,r,r-rs   rc@r) r zSimple object for storing attributes. Implements equality by attribute names and values, and provides a simple string representation. cKs|D] }t||||qdSr/)r )r'r=r*r,r,r-rUszNamespace.__init__cCst|tstSt|t|kSr/)rr NotImplementedr)r'otherr,r,r-__eq__s zNamespace.__eq__cCs ||jvSr/)r1)r'rFr,r,r- __contains__rzNamespace.__contains__N)r r6r7r8rUr^r_r,r,r,r-r s  r cseZdZfddZddZd&ddZdd Zd d Zd d ZddZ ddZ ddZ ddZ ddZ ddZddZd&ddZddZd d!Zd"d#Zd$d%ZZS)'_ActionsContainercstt|||_||_||_||_i|_|ddt |ddt |ddt |ddt |ddt |ddt |ddt|ddt|dd t|dd t|dd t|dd t|g|_i|_g|_g|_i|_td |_g|_dS)NrzZstore store_const store_trueZ store_falser"Z append_constr)rtr0parsersrz^-\d+$|^-\d*\.\d+$)r r`rU descriptionargument_default prefix_charsconflict_handler _registriesregisterrrrr!r"r&r'r*r/r5rG _get_handler_actions_option_string_actions_action_groups_mutually_exclusive_groups _defaultsrNrO_negative_number_matcher_has_negative_number_optionals)r'rdrfrergr r,r-rUs4  z_ActionsContainer.__init__cCs|j|i}|||<dSr/)rhrC)r' registry_namer+rregistryr,r,r-riSs z_ActionsContainer.registerNcCs|j|||Sr/)rhr)r'rrr+rr,r,r- _registry_getWrz_ActionsContainer._registry_getcKs2|j||jD] }|j|vr||j|_q dSr/)roupdaterkrr)r'r=rzr,r,r- set_defaults]s    z_ActionsContainer.set_defaultscCs8|jD]}|j|kr|jdur|jSq|j|dSr/)rkrrror)r'rrzr,r,r- get_defaultfs  z_ActionsContainer.get_defaultcOsF|j}|rt|dkr&|dd|vr&|rd|vrtd|j|i|}n|j|i|}d|vrM|d}||jvrC|j||d<n |jdurM|j|d<||}t|s]td|f|d i|}| d|j |j }t|sxtd |f|t urtd |ft |d rz | |dWn tytd w||S)z add_argument(dest, ..., name=value, ...) add_argument(option_string, option_string, ..., name=value, ...) rWrrz+dest supplied twice for positional argumentrNzunknown action "%s"r%r is not callablez<%r is a FileType class object, instance of it must be passedr1z,length of metavar tuple does not match nargsr,)rfrxr_get_positional_kwargs_get_optional_kwargsrore_pop_action_classcallablertrrrr1rr _add_action)r'rar=charsrZ action_classrz type_funcr,r,r-r|ps<         z_ActionsContainer.add_argumentcOs&t|g|Ri|}|j||Sr/)_ArgumentGrouprmr")r'rar=rr,r,r-add_argument_groups z$_ActionsContainer.add_argument_groupcKs t|fi|}|j||Sr/)_MutuallyExclusiveGrouprnr")r'r=rr,r,r-add_mutually_exclusive_group z._ActionsContainer.add_mutually_exclusive_groupcCs`|||j|||_|jD]}||j|<q|jD]}|j|r-|js-|jdq|S)NT) _check_conflictrkr" containerrrlrpmatchrq)r'rzrr,r,r-r}s       z_ActionsContainer._add_actioncCs|j|dSr/)rkremoverr,r,r-_remove_actionsz _ActionsContainer._remove_actioncCsi}|jD]}|j|vrtd}t||j|||j<qi}|jD]"}|j|vr7|j|j|j|jd||j<|jD] }||j||<q:q"|jD]}|j |j d}|jD]}|||<qTqH|j D] }| || |q_dS)Nz.cannot merge actions - two groups are named %r)titlerdrg)r)rmrrrrrdrgrrnrrrkrr})r'rZtitle_group_maprrEZ group_maprz mutex_groupr,r,r-_add_container_actionss6          z(_ActionsContainer._add_container_actionscKs^d|vr td}t||dttfvrd|d<|dtkr(d|vr(d|d<t||gdS)Nrz1'required' is an invalid argument for positionalsrTrrr)rrrrrr)r'rr=rEr,r,r-rysz(_ActionsContainer._get_positional_kwargsc Osg}g}|D]0}|d|jvr||jd}td}t||||t|dkr6|d|jvr6||q|dd}|durd|rH|d}n|d}||j}|s^td}t|||dd}t|||d S) Nr)optionrfzNinvalid option string %(option)r: must start with a character %(prefix_chars)rrWrz%dest= is required for options like %rrQrr) rfrrr"rxrlstripreplacer) r'rar=rZlong_option_stringsrrErZdest_option_stringr,r,r-rzs0        z&_ActionsContainer._get_optional_kwargscCs|d|}|d||S)Nrz)rrt)r'r=rrzr,r,r-r{s z#_ActionsContainer._pop_action_classcCs<d|j}zt||WStytd}t||jw)Nz_handle_conflict_%sz%invalid conflict_resolution value: %r)rgrrrr)r'Zhandler_func_namerEr,r,r-rj#s   z_ActionsContainer._get_handlercCsPg}|jD]}||jvr|j|}|||fq|r&|}|||dSdSr/)rrlr"rj)r'rzZconfl_optionalsrZconfl_optionalrgr,r,r-r,s   z!_ActionsContainer._check_conflictcCs2tddt|}ddd|D}t|||)Nzconflicting option string: %szconflicting option strings: %srcSsg|]\}}|qSr,r,)r_rrzr,r,r-rb>sz<_ActionsContainer._handle_conflict_error..)rrxr&r)r'rzconflicting_actionsrZconflict_stringr,r,r-_handle_conflict_error:s z(_ActionsContainer._handle_conflict_errorcCs>|D]\}}|j||j|d|js|j|qdSr/)rrrlrrr)r'rzrrr,r,r-_handle_conflict_resolveCs   z*_ActionsContainer._handle_conflict_resolver/)r r6r7rUrirtrvrwr|rrr}rrryrzr{rjrrrr r,r,r r-r`s& 5   3( "  r`cs6eZdZdfdd ZfddZfddZZS) rNc s|j}|d|j|d|j|d|jtt|j}|dd|i|||_g|_|j |_ |j |_ |j |_ |j |_ |j |_ |j|_dS)Nrgrfrerdr,)rCrgrfrer rrUrrrhrkrlrorqrn)r'rrrdr=ruZ super_initr r,r-rUTs     z_ArgumentGroup.__init__cs tt||}|j||Sr/)r rr}rr"rr r,r-r}jrz_ArgumentGroup._add_actioncs tt|||j|dSr/)r rrrrrr r,r-rosz_ArgumentGroup._remove_actionNNr r6r7rUr}rr r,r,r r-rRs rcs.eZdZdfdd ZddZddZZS) rFcs tt||||_||_dSr/)r rrUr _container)r'rrr r,r-rUvs z _MutuallyExclusiveGroup.__init__cCs2|jr td}t||j|}|j||S)Nz-mutually exclusive arguments must be optional)rrrrr}rr")r'rzrEr,r,r-r}{s   z#_MutuallyExclusiveGroup._add_actioncCs|j||j|dSr/)rrrrrr,r,r-rs z&_MutuallyExclusiveGroup._remove_action)Frr,r,r r-rtsrc s,eZdZdZddddgedddddddf fdd Zdd Zd d Zd d ZddZ ddZ dAddZ dAddZ ddZ ddZddZddZddZd d!Zd"d#Zd$d%ZdAd&d'ZdAd(d)Zd*d+Zd,d-Zd.d/Zd0d1Zd2d3Zd4d5ZdBd6d7ZdBd8d9ZdBd:d;ZdCd=d>Z d?d@Z!Z"S)DraKObject for parsing command line strings into Python objects. Keyword Arguments: - prog -- The name of the program (default: ``os.path.basename(sys.argv[0])``) - usage -- A usage message (default: auto-generated from arguments) - description -- A description of what the program does - epilog -- Text following the argument descriptions - parents -- Parsers whose arguments should be copied into this one - formatter_class -- HelpFormatter class for printing help messages - prefix_chars -- Characters that prefix optional arguments - fromfile_prefix_chars -- Characters that prefix files containing additional arguments - argument_default -- The default value for all arguments - conflict_handler -- String indicating how to handle conflicts - add_help -- Add a -h/-help option - allow_abbrev -- Allow long options to be abbreviated unambiguously - exit_on_error -- Determines whether or not ArgumentParser exits with error info when an error occurs NrQrTTc s$tt|j}|||| | d|durtjtjd}||_||_ ||_ ||_ ||_ | |_ | |_| |_|j}|td|_|td|_d|_dd}|dd|d|vrXdn|d}|j rq|j|d |d d d ttd d |D]}||z|j}Wn tyYqsw|j|qsdS)N)rdrfrergrzpositional argumentsoptionscSs|Sr/r,)rYr,r,r-identityr5z)ArgumentParser.__init__..identityrrQhr<rtzshow this help message and exit)rzrrt)r rrU_ospathbasenamer3argvrSroepilogformatter_classfromfile_prefix_charsadd_help allow_abbrev exit_on_errorrr _positionals _optionals _subparsersrir|rrrorru)r'rSrordrparentsrrfrrergrrrZ superinitZ add_grouprZdefault_prefixr\defaultsr r,r-rUsL    zArgumentParser.__init__cr)N)rSrordrrgrcrr,rrr3r,r-rbrz.ArgumentParser._get_kwargs..r,rr,r3r-r$szArgumentParser._get_kwargsc Ks|jdur |td|dt|d|vsd|vr4t|dd}t|dd}||||_n|j|_|ddur[| }| }|j }| |j ||d||d<||d}|d d gi|}|j||S) Nz(cannot have multiple subparser argumentsr<rrdZ subcommandsrSrdrcrr,)rrTrrCrrrrrr1_get_positional_actionsrnrsrorgrr{r}) r'r=rrdr[rrqZ parsers_classrzr,r,r-add_subparserss$   zArgumentParser.add_subparserscCs&|jr |j||S|j||Sr/)rrr}rrr,r,r-r} s   zArgumentParser._add_actioncCdd|jDS)NcSsg|]}|jr|qSr,rr_rzr,r,r-rb z8ArgumentParser._get_optional_actions..rkr3r,r,r-_get_optional_actionsz$ArgumentParser._get_optional_actionscCr)NcSsg|]}|js|qSr,rrr,r,r-rbrz:ArgumentParser._get_positional_actions..rr3r,r,r-rrz&ArgumentParser._get_positional_actionscC4|||\}}|rtd}||d||SNzunrecognized arguments: %sr)rBrrTr&r'rarrrEr,r,r- parse_args! zArgumentParser.parse_argscCs|dur tjdd}nt|}|durt}|jD]}|jtur4t||js4|jtur4t ||j|jq|j D]}t||sHt |||j |q8|j rmz | ||\}}Wnt yltd}|t|Yn w| ||\}}t|tr|t|tt|t||fSrV)r3rr0r rkrrrrr ror_parse_known_argsrexc_inforTrrDrrdelattr)r'rarrzrerrr,r,r-rB(s6          zArgumentParser.parse_known_argscs jdur i jD])}|j}t|jD]\}}|g}||d||||ddqqig}t} t| D]/\}} | dkr]|d| D]} |dqTqE | } | durid} n| |<d} || qEd |t t d fdd  fd d }  fd d }gd rt }nd} |krt fddD} |kr| }| kr| q| vr |}|| |  |ks| }|dg} jD]=}|vr9|jr|t|q|jdur9t|jtr9t|jr9|jt|jur9t|j ||jq|rI tdd | jD]+}|jrv|jD] }|vr^nqUdd|jD}td} |d |qLfS)NrWrrQAOrdcs|||}||jur1||gD]}|vr0td}t|}t|||q|tur>|||dSdS)Nznot allowed with argument %s)r _get_valuesrrrrrr)rzZargument_stringsrZargument_valuesZconflict_actionrEZ action_name)action_conflictsr seen_actionsseen_non_default_actionsr'r,r- take_action~s    z5ArgumentParser._parse_known_args..take_actioncsr|}|\}}}j}g} |dur||dS|dur||d}j}|dkrg|d|vrg||g|f|d} | |d}|ddpMd} j} || vr\| |}| }nLtd} t|| ||dkr{|d} |g}||||fn.td} t|| ||d}|d}|||}||} || }||||fnq|D] \}}}|||q| S)NTrWrrzignored explicit argument %r)_match_argumentr"rfrlrr) start_index option_tuplerzr explicit_argZmatch_argumentZ action_tuples arg_countr~charZnew_explicit_argZ optionals_maprEstoprarZselected_patterns)r(arg_strings_patternextrasoption_string_indicesr'rr,r-consume_optionalsP      :z:ArgumentParser._parse_known_args..consume_optionalcsnj}|d}||}t|D]\}}|||}||7}||qt|ddd<|Sr/)_match_arguments_partialziprx)rZ match_partialZselected_patternZ arg_countsrzrra)r(rrr'rr,r-consume_positionalss   z=ArgumentParser._parse_known_args..consume_positionalsrrcsg|]}|kr|qSr,r,)r_r)rr,r-rbs z4ArgumentParser._parse_known_args..z(the following arguments are required: %srcSsg|] }|jturt|qSr,)rtrrrr,r,r-rb<s  z#one of the arguments %s is requiredrr/)r_read_args_from_filesrnrrrCriterr"_parse_optionalr&rrrErDrkrrrrrrrrr  _get_valuerTr)r'r(rrrrZ mutex_actionZ conflictsZarg_string_pattern_partsZarg_strings_iter arg_stringrpatternrrZmax_option_string_indexZnext_option_string_indexZpositionals_end_indexZstringsZ stop_indexZrequired_actionsrzrrrEr,) rr(rrrrrrrr'rrr-rOs         J              z ArgumentParser._parse_known_argsc Csg}|D]c}|r|d|jvr||qz.cSrr,)rx)r_rYr,r,r-rbr)rrxr&rNrrrq)r'rprrrZ actions_slicerrr,r3r-rxs  z'ArgumentParser._match_arguments_partialc Cs |sdS|d|jvr dS||jvr|j|}||dfSt|dkr$dSd|vr?|dd\}}||jvr?|j|}|||fS||}t|dkreddd|D}||d}td}|||n t|dkrp|\} | S|j |r{|j s{dSd |vrdSd|dfS) NrrW=rcSsg|]\}}}|qSr,r,)r_rzrrr,r,r-rbsz2ArgumentParser._parse_optional..)rZmatchesz4ambiguous option: %(option)s could match %(matches)sr) rfrlrxsplit_get_option_tuplesr&rrTrprrq) r'rrzrrZ option_tuplesrrarErr,r,r-rs>              zArgumentParser._parse_optionalc Cs0g}|j}|d|vrA|d|vrA|jr?d|vr!|dd\}}n|}d}|jD]}||r>|j|}|||f}||q(|S|d|vr|d|vr|}d}|dd}|dd} |jD]*}||krv|j|}||| f}||q`||r|j|}|||f}||q`|S|td||S)NrrWrr<zunexpected option string: %s)rfrrrlrr"rTr) r'rrr~Z option_prefixrrzrZshort_option_prefixZshort_explicit_argr,r,r-rsB               z!ArgumentParser._get_option_tuplescCs|j}|dur d}n3|tkrd}n,|tkrd}n%|tkrd}n|tkr&d}n|tkr-d}n|tkr4d}n dd d |}|jrL| d d }| d d }|S) Nz(-*A-*)z(-*A?-*)z (-*[A-]*)z (-*A[A-]*)z([-AO]*)z (-*A[-AO]*)z(-*-*)z(-*%s-*)z-*rrdrQ) rrrr rrrr&rr)r'rzrrr,r,r-rs(  z!ArgumentParser._get_nargs_patterncCrr)parse_known_intermixed_argsrrTr&rr,r,r-parse_intermixed_args rz$ArgumentParser.parse_intermixed_argsc s|ddD}|rtd|djfdd|jDr$tdz|j}z`|jdur7|dd|_D]}|j|_t|_|j|_ t|_q9| ||\}}D]%}t ||j ryt ||j gkryddlm}|d |j |ft||j qTWD] }|j|_|j |_q}nD] }|j|_|j |_qw|}z6|D] }|j|_d |_q|jD] } | j| _d | _q| ||\}} W|D]}|j|_q|jD]} | j| _qn|D]}|j|_q|jD]} | j| _qwW||_|| fS||_w) NcSsg|] }|jttfvr|qSr,)rrrrr,r,r-rb5 sz>ArgumentParser.parse_known_intermixed_args..z3parse_intermixed_args: positional arg with nargs=%srcs&g|]}|jD] }|vr|jqqSr,)rr)r_rrzrr,r-rb; s  z;parse_intermixed_args: positional in mutuallyExclusiveGroup)warnzDo not expect %s in %sF)rrrrnrorZ save_nargsrrZ save_defaultrBrrrwarningsrrrrZ save_required) r'raraZ save_usagerzZremaining_argsrrrrr,rr-r' sx                 z*ArgumentParser.parse_known_intermixed_argscsjttfvrz|dWn tyYnw|s<jtkr<jr&j}nj}t |t r: |} ||S|sYjt krYjsYjdurOj}n|} ||St|dkrwjdtfvrw|\} |} ||Sjtkrfdd|D}|Sjtkrfdd|D} |d|Sjtkrt}|Sfdd|D}|D]} |q|S)NrrWcg|]}|qSr,rr_vrzr'r,r-rb rcz.ArgumentParser._get_values..crr,rrrr,r-rb rcrcrr,rrrr,r-rb rc)rrrrrrrrrrrr _check_valuerrxr)r'rzr(r+rrr,rr-rt sR    &        zArgumentParser._get_valuesc Cs|d|j|j}t|std}t|||z||}W|Sty=t|jdt|j}tt d}t||t t fy_t|jdt|j}||d}td}t|||w)Nrrxr rW)rr+z!invalid %(type)s value: %(value)r) rtrr|rrrrr#rr3rrr)r'rzrrrErr*rar,r,r-r s"    zArgumentParser._get_valuecCsJ|jdur!||jvr#|dtt|jd}td}t|||dSdS)Nr)r+rz3invalid choice: %(value)r (choose from %(choices)s))rr&rwr#rr)r'rzr+rarEr,r,r-r szArgumentParser._check_valuecCs$|}||j|j|j|Sr/)r1rsrorkrnrg)r'r[r,r,r-r s  zArgumentParser.format_usagecCst|}||j|j|j||j|jD]}||j ||j| |j | q||j |Sr/)r1rsrorkrnrmrdrmrirr}rrjrrg)r'r[Z action_groupr,r,r-rg s        zArgumentParser.format_helpcCs|j|jdS)Nr)rrSr3r,r,r-r1 r4zArgumentParser._get_formattercC"|durtj}|||dSr/)r3r4r2rr'filer,r,r- print_usage zArgumentParser.print_usagecCrr/)r3r4r2rgrr,r,r-r, rzArgumentParser.print_helpcCs$|r|dur tj}||dSdSr/)r3stderrwrite)r'rrr,r,r-r2 s zArgumentParser._print_messagercCs |r ||tjt|dSr/)r2r3rr-)r'Zstatusrr,r,r-r- szArgumentParser.exitcCs0|tj|j|d}|dtd|dS)zerror(message: string) Prints a usage message incorporating the message to stderr and exits. If you override this in a subclass, it should not return -- it should either exit or raise an exception. )rSrr<z%(prog)s: error: %(message)s N)rr3rrSr-r)r'rrar,r,r-rT s zArgumentParser.errorrr/)rN)#r r6r7r8rrUr$rr}rrrrBrrrrrrrrrrrrrrrgr1rr,r2r-rTr r,r,r r-rsZB  'w:- 1 M8     r)2r8 __version____all__osrrerNsysr3rrrrrrr rrrDrrr;rrr rr r Exceptionrrr rrrrr!r"r&r'r*r/r5rGrr r`rrrr,r,r,r-sb=   ^)#&] 78"