o Ƌ|c^@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)self type_name arg_stringsZ star_argsargnamevaluer-%/opt/conda/lib/python3.10/argparse.py__repr__ts   z_AttributeHolder.__repr__cCst|jSN)list__dict__itemsr'r-r-r.r$z_AttributeHolder._get_kwargscCsgSr0r-r4r-r-r.r!z_AttributeHolder._get_argsN)r __module__ __qualname____doc__r/r$r!r-r-r-r.rks  rcCs6|durgSt|tur|ddSddl}||SNr)rr1copy)r3r;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\rZ _join_partsr3r[r^rrI)r'r&Z item_helpZcurrent_indentr^r-r-r. format_helps    z"HelpFormatter._Section.format_helpr0)r r7r8rVrhr-r-r-r.rLs  rLcCs|jj||fdSr0)rNr3r")r'rarbr-r-r. _add_itemszHelpFormatter._add_itemcCs0||||j|}||jg||_dSr0)rZrLrNrirh)r'r^sectionr-r-r. start_sections zHelpFormatter.start_sectioncCs|jj|_|dSr0)rNr]r[r4r-r-r. end_sections  zHelpFormatter.end_sectioncCs,|tur|dur||j|gdSdSdSr0)rri _format_text)r'textr-r-r.add_textszHelpFormatter.add_textcCs*|tur||||f}||j|dSdSr0)rri _format_usage)r'usageactionsgroupsprefixrbr-r-r. add_usages zHelpFormatter.add_usagecCsv|jtur9|j}||g}||D] }|||qttt|}||j}t|j ||_ | |j |gdSdSr0) helpr_format_action_invocation_iter_indented_subactionsr"rFmaplenrIrKri_format_action)r'actionZget_invocationZ invocations subactionZinvocation_lengthZ action_lengthr-r-r. add_arguments   zHelpFormatter.add_argumentcCs|D]}||qdSr0)r~)r'rrr|r-r-r. add_argumentss zHelpFormatter.add_argumentscCs.|j}|r|jd|}|dd}|S)N rf)rMrhrSsubstrip)r'rvr-r-r.rhs zHelpFormatter.format_helpcCsddd|DS)NrecSsg|] }|r |tur|qSr-)r)r`partr-r-r.rc"sz-HelpFormatter._join_parts..)r&)r'Z part_stringsr-r-r.rg!s zHelpFormatter._join_partscs(|durtd}|dur|t|jd}n|dur$|s$dt|jd}n|durdt|jd}g}g}|D]}|jrB||q7||q7|j} | |||} ddd|| fD}|j|jt |t |krd} | ||} | ||} t | | }t | | }d|| ksJd|| ksJdfdd }t |t |d krdt |t |d }|r||g|||}| |||n=|r||g|||}n1|g}n-dt |}||}|||}t |d krg}| |||| ||||g|}d |}d ||fS)Nzusage: rTz%(prog)s cSsg|]}|r|qSr-r-)r`sr-r-r.rcBz/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)NrXrr)rzr"r&)partsindentrtlineslineZline_lenr text_widthr-r. get_linesVs"   z.HelpFormatter._format_usage..get_linesg?rXrfz%s%s r0) _dictrCoption_stringsr"_format_actions_usager&rHrIrzrOfindallextend)r'rqrrrsrtrT optionals positionalsr|formatZ action_usageZ part_regexpZ opt_usageZ pos_usageZ opt_partsZ pos_partsrrrrr-rr.rp&s\              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 (()rX|%s %s[%s]T)reversercSsg|]}|dur|qSr0r-)r`itemr-r-r.rcrdz7HelpFormatter._format_actions_usage..z[\[(]z[\])]z(%s) z\1 (%s)z%s *%srez \(([^|]*)\))set_group_actions ValueErrorindexrzaddrequiredrange enumeratervrr"getpopr#_get_default_metavar_for_positional _format_argsnargs format_usage!_get_default_metavar_for_optionalsortedr&rOrr)r'rrrs group_actionsZinsertsgroupstartendr|irdefaultr option_string args_stringrnopencloser-r-r.rs                   z#HelpFormatter._format_actions_usagecCsFd|vr |t|jd}t|j|jd}d|j}||||dS)Nz%(prog)r rr)rrCrFrHrI _fill_text)r'rnrrr-r-r.rms  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=rrez%*s%s z %*s%-*s rrXrf)rErKrGrFrHrIrwrvrzr _expand_help _split_linesr"endswithrxr{rg) r'r|Z help_positionZ help_widthZ action_widthZ action_headertupZ indent_firstrZ help_textZ help_linesrr}r-r-r.r{s<           zHelpFormatter._format_actioncCs|js||}|||d\}|Sg}|jdkr!||jn||}|||}|jD] }|d||fq/d|S)NrXrrr) rr_metavar_formatterrrrrr"r&)r'r|rmetavarrrrr-r-r.rw's      z'HelpFormatter._format_action_invocationcsP|jdur |jn|jdurdd|jD}dd|n|fdd}|S)NcSg|]}t|qSr-str)r`choicer-r-r.rcCrz4HelpFormatter._metavar_formatter..z{%s},csttrSf|Sr0) isinstancetuple)Z tuple_sizeresultr-r.rHs  z0HelpFormatter._metavar_formatter..format)rchoicesr&)r'r|default_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%srXrr=z [%s [%s ...]]z[%s ...]z %s [%s ...]rz%s ...recSsg|]}dqS)rr-)r`rr-r-r.rcesz.HelpFormatter._format_args..zinvalid nargs valuer) rrrrrzr rrrr TypeErrorrr&)r'r|rZ get_metavarrrformatsr-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.rctrz.HelpFormatter._expand_help..) rvarsrCr1rhasattrr rr&_get_help_string)r'r|paramsr+Z choices_strr-r-r.rks   zHelpFormatter._expand_helpccsBz|j}Wn tyYdSw||EdH|dSr0)_get_subactionsAttributeErrorrZr[)r'r|Zget_subactionsr-r-r.rxxs    z'HelpFormatter._iter_indented_subactionscCs&|jd|}ddl}|||S)Nrr)rRrrtextwrapwrap)r'rnrUrr-r-r.rs zHelpFormatter._split_linescCs,|jd|}ddl}|j||||dS)Nrr)initial_indentsubsequent_indent)rRrrrfill)r'rnrUrrr-r-r.rs zHelpFormatter._fill_textcC|jSr0)rvr'r|r-r-r.rzHelpFormatter._get_help_stringcCs |jSr0)destupperrr-r-r.r z/HelpFormatter._get_default_metavar_for_optionalcCrr0)rrr-r-r.rrz1HelpFormatter._get_default_metavar_for_positional)r=r>Nr0) r r7r8r9rVrZr[objectrLrirkrlrorur~rrhrgrprrmr{rwrrrrxrrrrrr-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)Nrec3s|]}|VqdSr0r-)r`rrr-r. sz9RawDescriptionHelpFormatter._fill_text..T)keepends)r& splitlines)r'rnrUrr-rr.rs z&RawDescriptionHelpFormatter._fill_textN)r r7r8r9rr-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|Sr0)r)r'rnrUr-r-r.rz!RawTextHelpFormatter._split_linesN)r r7r8r9rr-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))rvrrrrrr)r'r|rvZdefaulting_nargsr-r-r.rs  z.ArgumentDefaultsHelpFormatter._get_help_stringN)r r7r8r9rr-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|jjSr0rr rr-r-r.rrz:MetavarTypeHelpFormatter._get_default_metavar_for_optionalcCrr0rrr-r-r.rrz.r-r'namesr-r4r.r$Js zAction._get_kwargscCs |jdSr:rr4r-r-r.rYrzAction.format_usagecCs ttd)Nz.__call__() not defined)NotImplementedErrorrr'parser namespacevaluesrr-r-r.__call__\ zAction.__call__NNNNNFNNr0)r r7r8r9rVr$rr r-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) rrrrrrrrvr)r" startswithrsuperrV) r'rrrrrrrvrZ_option_stringsr __class__r-r.rV`s(     zBooleanOptionalAction.__init__cCs(||jvrt||j|d dSdS)Nr)rsetattrrrrr-r-r.r s zBooleanOptionalAction.__call__cCs d|jS)Nz | )r&rr4r-r-r.rr z"BooleanOptionalAction.format_usage)NNNFNNr0)r r7r8rVr r __classcell__r-r-rr.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)rrrrrVrrr-r.rV     z_StoreAction.__init__cCst||j|dSr0)rrrr-r-r.r z_StoreAction.__call__r r0r r7r8rVr rr-r-rr.rrc0eZdZ    dfdd ZdddZZS) _StoreConstActionNFc s"tt|j||d||||ddS)Nr)rrrrrrrv)rrrVr'rrrrrrvrrr-r.rVs  z_StoreConstAction.__init__cCst||j|jdSr0)rrrrr-r-r.r sz_StoreConstAction.__call__NFNNr0rr-r-rr.rsrcs$eZdZ   dfdd ZZS)_StoreTrueActionFNc tt|j||d|||ddS)NTrrrrrrv)rr"rVr'rrrrrvrr-r.rV  z_StoreTrueAction.__init__)FFNr r7r8rVrr-r-rr.r" r"cs$eZdZ   dfdd ZZS)_StoreFalseActionTFNcr#)NFr$)rr)rVr%rr-r.rVr&z_StoreFalseAction.__init__)TFNr'r-r-rr.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)rrrr*rVrrr-r.rVrz_AppendAction.__init__cC2t||jd}t|}||t||j|dSr0)rrr<r"rr'rr r rr3r-r-r.r  z_AppendAction.__call__r r0rr-r-rr.r*rr*cr) _AppendConstActionNFc s$tt|j||d|||||ddS)Nr)rrrrrrrvr)rr.rVr rr-r.rV s  z_AppendConstAction.__init__cCs4t||jd}t|}||jt||j|dSr0)rrr<r"rrr,r-r-r.r s z_AppendConstAction.__call__r!r0rr-r-rr.r. sr.cs.eZdZ   dfdd ZdddZZS) _CountActionNFcr#)Nr)rrrrrrv)rr/rVr%rr-r.rV'r&z_CountAction.__init__cCs0t||jd}|dur d}t||j|ddSNrrX)rrr)r'rr r rcountr-r-r.r 5sz_CountAction.__call__)NFNr0rr-r-rr.r/%s r/cs.eZdZeedffdd ZdddZZS) _HelpActionNcstt|j|||d|ddSNr)rrrrrv)rr2rV)r'rrrrvrr-r.rV>s  z_HelpAction.__init__cCs||dSr0) print_helpexitrr-r-r.r Js z_HelpAction.__call__r0r r7r8rrVr rr-r-rr.r2<s  r2cs0eZdZdeedffdd ZdddZZS)_VersionActionNz&show program's version number and exitcs$tt|j|||d|d||_dSr3)rr7rVversion)r'rr8rrrvrr-r.rVQs  z_VersionAction.__init__cCsD|j}|dur |j}|}||||tj|dSr0)r8_get_formatterro_print_messagerh_sysstdoutr5)r'rr r rr8r\r-r-r.r _s  z_VersionAction.__call__r0r6r-r-rr.r7Osr7csPeZdZGdddeZedddffdd ZddZd d Zd d d Z Z S)_SubParsersActioncseZdZfddZZS)z&_SubParsersAction._ChoicesPseudoActioncs@|}}|r|dd|7}ttj|}|jg|||ddS)Nrr)rrrvr)r&rr=_ChoicesPseudoActionrV)r'r+aliasesrvrrsuprr-r.rVms   z/_SubParsersAction._ChoicesPseudoAction.__init__r'r-r-rr.r>ksr>FNc s<||_||_i|_g|_tt|j||t|j|||ddS)N)rrrrrrvr) _prog_prefix _parser_class_name_parser_map_choices_actionsrr=rVr)r'rrT parser_classrrrvrrr-r.rVus   z_SubParsersAction.__init__cKs|ddurd|j|f|d<|dd}d|vr,|d}||||}|j||jdi|}||j|<|D]}||j|<q;|S)NrTrr?r-rv)rrArr>rDr"rBrC)r'r+kwargsr?rvZ choice_actionraliasr-r-r. add_parsers     z_SubParsersAction.add_parsercCs|jSr0)rDr4r-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)NrrXr) parser_namerz5unknown parser %(parser_name)r (choices: %(choices)s))rrrrCKeyErrorr&rrparse_known_argsrr3 setdefault_UNRECOGNIZED_ARGS_ATTRrr) r'rr r rrIr)rbmsgZ subnamespacekeyr,r-r-r.r s(       z_SubParsersAction.__call__r0) r r7r8r r>rrVrHrr rr-r-rr.r=isr=c@seZdZdddZdS) _ExtendActionNcCr+r0)rrr<rrr,r-r-r.r r-z_ExtendAction.__call__r0)r r7r8r r-r-r-r.rPsrPc@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||_||_||_||_dSr0)_mode_bufsize _encoding_errors)r'modebufsizeencodingerrorsr-r-r.rVr_zFileType.__init__c s|dkr7djvrdjvrtjjStjStfdddDr,djvr)tjjStjStdj}t|z t|jj j j WSt y_}z||d}td }t ||d}~ww) N-rQbc3s|]}|jvVqdSr0)rRrr4r-r.rsz$FileType.__call__..Zwaxzargument "-" with mode %r)filenameerrorz$can't open '%(filename)s': %(error)s)rRr;stdinbufferanyr<rrrrSrTrUOSErrorr)r'stringrNerbrr-r4r.r s"   zFileType.__call__cCsT|j|jf}d|jfd|jfg}ddd|Ddd|D}dt|j|fS)NrXrYrcSsg|] }|dkrt|qS)r)r#)r`r*r-r-r.rcsz%FileType.__repr__..cSs$g|]\}}|durd||fqS)Nrr-)r`kwr*r-r-r.rcs  r)rRrSrTrUr&rr )r'rbrFZargs_strr-r-r.r/s  zFileType.__repr__)rQrNN)r r7r8r9rVr r/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||||qdSr0)r)r'rFr+r-r-r.rVszNamespace.__init__cCst|tstSt|t|kSr0)rr NotImplementedr)r'otherr-r-r.__eq__s zNamespace.__eq__cCs ||jvSr0)r2)r'rOr-r-r. __contains__rzNamespace.__contains__N)r r7r8r9rVrgrhr-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)Nr|store store_const store_true store_falser"Z append_constr1rvr8parsersrz^-\d+$|^-\d*\.\d+$)rrirV descriptionargument_default prefix_charsconflict_handler _registriesregisterrrr"r)r*r.r/r2r7r=rP _get_handler_actions_option_string_actions_action_groups_mutually_exclusive_groups _defaultsrOrP_negative_number_matcher_has_negative_number_optionals)r'rorqrprrrr-r.rVs4  z_ActionsContainer.__init__cCs|j|i}|||<dSr0)rsrL)r' registry_namer,rregistryr-r-r.rtSs z_ActionsContainer.registerNcCs|j|||Sr0)rsr)r'r}r,rr-r-r. _registry_getWrz_ActionsContainer._registry_getcKs2|j||jD] }|j|vr||j|_q dSr0)rzupdatervrr)r'rFr|r-r-r. set_defaults]s    z_ActionsContainer.set_defaultscCs8|jD]}|j|kr|jdur|jSq|j|dSr0)rvrrrzr)r'rr|r-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, ...) rXrrz+dest supplied twice for positional argumentrNzunknown action "%s"r%r is not callablez<%r is a FileType class object, instance of it must be passedr9z,length of metavar tuple does not match nargsr-)rqrzr_get_positional_kwargs_get_optional_kwargsrzrp_pop_action_classcallablerrrrr9rr _add_action)r'rbrFcharsrZ action_classr| type_funcr-r-r.r~ps<         z_ActionsContainer.add_argumentcOs&t|g|Ri|}|j||Sr0)_ArgumentGrouprxr")r'rbrFrr-r-r.add_argument_groups z$_ActionsContainer.add_argument_groupcKs t|fi|}|j||Sr0)_MutuallyExclusiveGroupryr")r'rFrr-r-r.add_mutually_exclusive_group z._ActionsContainer.add_mutually_exclusive_groupcCs`|||j|||_|jD]}||j|<q|jD]}|j|r-|js-|jdq|S)NT) _check_conflictrvr" containerrrwr{matchr|)r'r|rr-r-r.rs       z_ActionsContainer._add_actioncCs|j|dSr0)rvremoverr-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)titlerorr)r)rxrrrrrorrrryrrrvrr)r'rZtitle_group_maprrNZ group_mapr| 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'rrFrNr-r-r.rsz(_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)optionrqzNinvalid option string %(option)r: must start with a character %(prefix_chars)rrXrz%dest= is required for options like %rrZrr) rqrrr"rzrlstripreplacer) r'rbrFrZlong_option_stringsrrNrZdest_option_stringr-r-r.rs0        z&_ActionsContainer._get_optional_kwargscCs|d|}|d||S)Nr|)rr)r'rFrr|r-r-r.rs z#_ActionsContainer._pop_action_classcCs<d|j}zt||WStytd}t||jw)Nz_handle_conflict_%sz%invalid conflict_resolution value: %r)rrrrrr)r'Zhandler_func_namerNr-r-r.ru#s   z_ActionsContainer._get_handlercCsPg}|jD]}||jvr|j|}|||fq|r&|}|||dSdSr0)rrwr"ru)r'r|Zconfl_optionalsrZconfl_optionalrrr-r-r.r,s   z!_ActionsContainer._check_conflictcCs2tddt|}ddd|D}t|||)Nzconflicting option string: %szconflicting option strings: %srcSsg|]\}}|qSr-r-)r`rr|r-r-r.rc>sz<_ActionsContainer._handle_conflict_error..)rrzr&r)r'r|conflicting_actionsrZconflict_stringr-r-r._handle_conflict_error:s z(_ActionsContainer._handle_conflict_errorcCs>|D]\}}|j||j|d|js|j|qdSr0)rrrwrrr)r'r|rrr-r-r._handle_conflict_resolveCs   z*_ActionsContainer._handle_conflict_resolver0)r r7r8rVrtrrrr~rrrrrrrrrurrrrr-r-rr.ris& 5   3( "  rics6eZdZdfdd ZfddZfddZZS) rNc s|j}|d|j|d|j|d|jtt|j}|dd|i|||_g|_|j |_ |j |_ |j |_ |j |_ |j |_ |j|_dS)Nrrrqrpror-)rLrrrqrprrrVrrrsrvrwrzr|ry)r'rrrorFrZ super_initrr-r.rVTs     z_ArgumentGroup.__init__cs tt||}|j||Sr0)rrrrr"rrr-r.rjrz_ArgumentGroup._add_actioncs tt|||j|dSr0)rrrrrrrr-r.rosz_ArgumentGroup._remove_actionNNr r7r8rVrrrr-r-rr.rRs rcs.eZdZdfdd ZddZddZZS) rFcs tt||||_||_dSr0)rrrVr _container)r'rrrr-r.rVvs z _MutuallyExclusiveGroup.__init__cCs2|jr td}t||j|}|j||S)Nz-mutually exclusive arguments must be optional)rrrrrrr")r'r|rNr-r-r.r{s   z#_MutuallyExclusiveGroup._add_actioncCs|j||j|dSr0)rrrrrr-r-r.rs z&_MutuallyExclusiveGroup._remove_action)Frr-r-rr.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 NrZr]Tc 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)rorqrprrrzpositional argumentsoptionscSs|Sr0r-)rbr-r-r.identityr6z)ArgumentParser.__init__..identityrrZhr=rvzshow this help message and exit)r|rrv)rrrV_ospathbasenamer;argvrTrqepilogformatter_classfromfile_prefix_charsadd_help allow_abbrev exit_on_errorrr _positionals _optionals _subparsersrtr~rrrzrr)r'rTrqrorparentsrrqrrprrrrrZ superinitZ add_grouprdefault_prefixr]defaultsrr-r.rVsL    zArgumentParser.__init__cr)N)rTrqrorrrrcrr-rrr4r-r.rcrz.ArgumentParser._get_kwargs..r-rr-r4r.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 argumentsrErroZ subcommandsrTrernrr-)rr]rrLrrrrrr9_get_positional_actionsryrurqrhrrr) r'rFrror\rrsZ parsers_classr|r-r-r.add_subparserss$   zArgumentParser.add_subparserscCs&|jr |j||S|j||Sr0)rrrrrr-r-r.r s   zArgumentParser._add_actioncCdd|jDS)NcSsg|]}|jr|qSr-rr`r|r-r-r.rc z8ArgumentParser._get_optional_actions..rvr4r-r-r._get_optional_actionsz$ArgumentParser._get_optional_actionscCr)NcSsg|]}|js|qSr-rrr-r-r.rcrz:ArgumentParser._get_positional_actions..rr4r-r-r.rrz&ArgumentParser._get_positional_actionscC4|||\}}|rtd}||d||SNzunrecognized arguments: %sr)rKrr]r&r'rbr rrNr-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||fSrW)r;rr1r rvrrrrrrzr_parse_known_argsrexc_infor]rrMrrdelattr)r'rbr r|rerrr-r-r.rK(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)NrXrrZAOrecs|||}||jur1||gD]}|vr0td}t|}t|||q|tur>|||dSdS)Nznot allowed with argument %s)r _get_valuesrrrrrr)r|Zargument_stringsrZargument_valuesZconflict_actionrNZ action_name)action_conflictsr  seen_actionsseen_non_default_actionsr'r-r. take_action~s    z5ArgumentParser._parse_known_args..take_actioncsz|}|\}}}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|sJ|D] \}}}|||q| S)NTrXrrzignored explicit argument %r)_match_argumentr"rqrwrr) start_index option_tupler|r explicit_argZmatch_argumentZ action_tuples arg_countrcharZnew_explicit_argZ optionals_maprNstoprbrZselected_patterns)r)arg_strings_patternextrasoption_string_indicesr'rr-r.consume_optionalsR      9z:ArgumentParser._parse_known_args..consume_optionalcsnj}|d}||}t|D]\}}|||}||7}||qt|ddd<|Sr0)_match_arguments_partialziprz)rZ match_partialZselected_patternZ arg_countsr|rrb)r)rrr'rr-r.consume_positionalss   z=ArgumentParser._parse_known_args..consume_positionalsrrcsg|]}|kr|qSr-r-)r`r)rr-r.rcs z4ArgumentParser._parse_known_args..z(the following arguments are required: %srcSsg|] }|jturt|qSr-)rvrrrr-r-r.rc<s  z#one of the arguments %s is requiredrr0)r_read_args_from_filesryrrrLriterr"_parse_optionalr&rrrFrErvrrrrrrrrr _get_valuer]r)r'r)r rrrZ mutex_actionZ conflictsZarg_string_pattern_partsZarg_strings_iter arg_stringrpatternrrZmax_option_string_indexZnext_option_string_indexZpositionals_end_indexstringsZ stop_indexZrequired_actionsr|rrrNr-) rr)rrr rrrrr'rrr.rOs         J              z ArgumentParser._parse_known_argsc Csg}|D]c}|r|d|jvr||qz.cSrr-)rz)r`rbr-r-r.rcr)rrzr&rOrrrs)r'rrrrrZ actions_slicerrr-r4r.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) NrrX=rcSsg|]\}}}|qSr-r-)r`r|rrr-r-r.rcsz2ArgumentParser._parse_optional..)rmatchesz4ambiguous option: %(option)s could match %(matches)sr) rqrwrzsplit_get_option_tuplesr&rr]r{rr|) r'rr|rrZ option_tuplesrrbrNrr-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)NrrXrr=zunexpected option string: %s)rqrrrwrr"r]r) r'rrrZ option_prefixrr|rZshort_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-*rrerZ) rrrr rrrr&rr)r'r|rrr-r-r.rs(  z!ArgumentParser._get_nargs_patterncCrr)parse_known_intermixed_argsrr]r&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.rc5 sz>ArgumentParser.parse_known_intermixed_args..z3parse_intermixed_args: positional arg with nargs=%srcs&g|]}|jD] }|vr|jqqSr-)rr)r`rr|rr-r.rc; s  z;parse_intermixed_args: positional in mutuallyExclusiveGroup)warnzDo not expect %s in %sF)rrrryrqrZ save_nargsrrZ save_defaultrKrrrwarningsrrrrZ save_required) r'rbr aZ save_usager|Zremaining_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)NrrXcg|]}|qSr-rr`vr|r'r-r.rc rdz.ArgumentParser._get_values..crr-rrrr-r.rc rdrcrr-rrrr-r.rc rd)rrrrrrrrrrrr _check_valuerrzr)r'r|r)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)Nrrr rX)rr,z!invalid %(type)s value: %(value)r) rrrrrrrr#rr;rrr)r'r|rrrNrr+rbr-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&ryr#rr)r'r|r,rbrNr-r-r.r szArgumentParser._check_valuecCs$|}||j|j|j|Sr0)r9rurqrvryrh)r'r\r-r-r.r s  zArgumentParser.format_usagecCst|}||j|j|j||j|jD]}||j ||j| |j | q||j |Sr0)r9rurqrvryrororxrkrrrrlrrh)r'r\Z action_groupr-r-r.rh s        zArgumentParser.format_helpcCs|j|jdS)Nr)rrTr4r-r-r.r9 r5zArgumentParser._get_formattercC"|durtj}|||dSr0)r;r<r:rr'filer-r-r. print_usage zArgumentParser.print_usagecCrr0)r;r<r:rhrr-r-r.r4 rzArgumentParser.print_helpcCs$|r|dur tj}||dSdSr0)r;stderrwrite)r'rrr-r-r.r: s zArgumentParser._print_messagercCs |r ||tjt|dSr0)r:r;rr5)r'statusrr-r-r.r5 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. )rTrr=z%(prog)s: error: %(message)s N)rr;rrTr5r)r'rrbr-r-r.r] s zArgumentParser.errorrr0)rN)#r r7r8r9rrVr$rrrrrrKrrrrrrrrrrrrrrrhr9rr4r:r5r]rr-r-rr.rsZB  'w:- 1 M8     r)2r9 __version____all__osrrerOsysr;rrrrrrr rrrMrrr<rrr rr r Exceptionrrr rrrr"r)r*r.r/r2r7r=rPrr rirrrr-r-r-r.sb=   ^)#&] 78"