a DOgG@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@s(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],\}}|rZ|d||fq6|||<q6|rz|dt|d|d|fS)N%s=%rz**%s%s(%s), )type__name__ _get_argsappendrepr _get_kwargs isidentifierjoin)selfZ type_name arg_stringsZ star_argsargnamevaluer+-/opt/alt/python39/lib64/python3.9/argparse.py__repr__ts   z_AttributeHolder.__repr__cCst|jSN)list__dict__itemsr&r+r+r,r#sz_AttributeHolder._get_kwargscCsgSr.r+r2r+r+r,r sz_AttributeHolder._get_argsN)r __module__ __qualname____doc__r-r#r r+r+r+r,rksrcCs6|dur gSt|tur$|ddSddl}||SNr)rr/copy)r1r7r+r+r, _copy_itemss   r8c@seZdZdZd;ddZddZd d ZGd d d eZd dZ ddZ ddZ ddZ dz6HelpFormatter._Section.format_help..z%*s%s:  ) rYrXrV _join_partsr1rWrZrrE)r&r%Z item_helpZcurrent_indentrZr+r+r, format_helps    z"HelpFormatter._Section.format_help)N)rr3r4rRrcr+r+r+r,rHs rHcCs|jj||fdSr.)rJr1r!)r&r\r]r+r+r, _add_itemszHelpFormatter._add_itemcCs0||||j|}||jg||_dSr.)rVrHrJrdrc)r&rZZsectionr+r+r, start_sectionszHelpFormatter.start_sectioncCs|jj|_|dSr.)rJrYrWr2r+r+r, end_sections zHelpFormatter.end_sectioncCs$|tur |dur ||j|gdSr.)rrd _format_text)r&textr+r+r,add_textszHelpFormatter.add_textcCs&|tur"||||f}||j|dSr.)rrd _format_usage)r&usageactionsgroupsprefixr]r+r+r, add_usages zHelpFormatter.add_usagecCsr|jturn|j}||g}||D]}|||q$ttt|}||j}t|j ||_ | |j |gdSr.) helpr_format_action_invocation_iter_indented_subactionsr!rBmaplenrErGrd_format_action)r&actionZget_invocationZ invocations subactionZinvocation_lengthZ action_lengthr+r+r, add_arguments   zHelpFormatter.add_argumentcCs|D]}||qdSr.)rx)r&rlrvr+r+r, add_argumentsszHelpFormatter.add_argumentscCs.|j}|r*|jd|}|dd}|S)N ra)rIrcrOsubstrip)r&rpr+r+r,rcs  zHelpFormatter.format_helpcCsddd|DS)Nr`cSsg|]}|r|tur|qSr+)r)r[partr+r+r,r^"s z-HelpFormatter._join_parts..)r%)r&Z part_stringsr+r+r,rb!s zHelpFormatter._join_partscs|durtd}|dur,|t|jd}n|durL|sLdt|jd}n|durdt|jd}g}g}|D] }|jr||qr||qr|j} | |||} ddd|| fD}|j|jt |t |krd} | ||} | ||} t | | }t | | }dfdd }t |t |d krdt |t |d }|r|||g|||}| |||n |r||g|||}n|g}nZdt |}||}|||}t |d krg}| |||| ||||g|}d |}d ||fS)Nzusage: rPz%(prog)s cSsg|] }|r|qSr+r+)r[sr+r+r,r^Br_z/HelpFormatter._format_usage..z%\(.*?\)+(?=\s|$)|\[.*?\]+(?=\s|$)|\S+csg}g}|durt|d}n t|d}|D]Z}|dt|krn|rn||d|g}t|d}|||t|d7}q.|r||d||dur|dt|d|d<|S)NrTrr)rtr!r%)partsindentrnlineslineZline_lenr} text_widthr+r, get_linesVs"   z.HelpFormatter._format_usage..get_linesg?rTraz%s%s )N) _dictr?option_stringsr!_format_actions_usager%rDrErtrKfindallextend)r&rkrlrmrnrP optionals positionalsrvformatZ action_usageZ part_regexpZ opt_usageZ pos_usageZ opt_partsZ pos_partsrrrrr+rr,rj&sX             zHelpFormatter._format_usagec Cst}i}|D]*}|js(td|z||jd}WntyRYqYq0|t|j}||||jkr|jD]}||qz|js||vr||d7<nd||<||vr||d7<nd||<nF||vr||d7<nd||<||vr||d7<nd||<t|d |D]} d || <q*qg} t|D]"\} }|j t ur| d| | d kr| | n"| | d d krj| | d n|js||} ||| } ||vr| ddkr| d dkr| d d } | | nf|jd} |jdkr$|} n"||} ||| }d | |f} |js`||vr`d | } | | qHt|ddD]} || g| | | <qzddd| D}d}d}td|d|}td|d|}td||fd|}tdd|}|}|S)Nz empty group rz [[]z (()rT|%s %s[%s]T)reversercSsg|]}|dur|qSr.r+)r[itemr+r+r,r^r_z7HelpFormatter._format_actions_usage..z[\[(]z[\])]z(%s) z\1 (%s)z%s *%sr`z \(([^|]*)\))set_group_actions ValueErrorindexrtaddrequiredrange enumeraterprr!getpopr#_get_default_metavar_for_positional _format_argsnargs format_usage!_get_default_metavar_for_optionalsortedr%rKr{r|)r&rlrm group_actionsZinsertsgroupstartendrvirdefaultr} option_string args_stringrhopencloser+r+r,rs~                     z#HelpFormatter._format_actions_usagecCsFd|vr|t|jd}t|j|jd}d|j}||||dS)Nz%(prog)r~ rrz)rr?rBrDrE _fill_text)r&rhrrr+r+r,rgs  zHelpFormatter._format_textc CsNt|jd|j}t|j|d}||jd}||}|jsV|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| fqn| ds"| d||D]} | || q,||S) Nr9rr`z%*s%s z %*s%-*s rrTra)rArGrCrBrDrErqrprtr| _expand_help _split_linesr!endswithrrrurb) r&rvZ help_positionZ help_widthZ action_widthZ action_headertupZ indent_firstrZ help_textZ help_linesrrwr+r+r,rus:          zHelpFormatter._format_actioncCs|js&||}|||d\}|Sg}|jdkrB||jn4||}|||}|jD]}|d||fq^d|SdS)NrTrrr) rr_metavar_formatterrrrrr!r%)r&rvrmetavarrrrr+r+r,rq's     z'HelpFormatter._format_action_invocationcsP|jdur|jn.|jdur.z{%s},csttrSf|SdSr.) isinstancetuple)Z tuple_sizeresultr+r,rHs z0HelpFormatter._metavar_formatter..format)rchoicesr%)r&rvdefault_metavarZ choice_strsrr+rr,r?s   z HelpFormatter._metavar_formattercCs |||}|jdur$d|d}n|jtkr.zinvalid nargs valuer) rrrrrtr rrrr TypeErrorrr%)r&rvrZ get_metavarrrZformatsr+r+r,rOs0            zHelpFormatter._format_argscCstt||jd}t|D]}||tur||=qt|D] }t||dr:||j||<q:|ddurddd|dD}||d<| ||S)Nr~rrrcSsg|] }t|qSr+rr[cr+r+r,r^tr_z.HelpFormatter._expand_help..) rvarsr?r/rhasattrrrr%_get_help_string)r&rvparamsr)Z choices_strr+r+r,rks   zHelpFormatter._expand_helpccs>z |j}WntyYn0||EdH|dSr.)_get_subactionsAttributeErrorrVrW)r&rvZget_subactionsr+r+r,rrxs   z'HelpFormatter._iter_indented_subactionscCs&|jd|}ddl}|||S)Nrr)rNr{r|textwrapZwrap)r&rhrQrr+r+r,rszHelpFormatter._split_linescCs,|jd|}ddl}|j||||dS)Nrr)Zinitial_indentZsubsequent_indent)rNr{r|rZfill)r&rhrQrrr+r+r,rs zHelpFormatter._fill_textcCs|jSr.)rpr&rvr+r+r,rszHelpFormatter._get_help_stringcCs |jSr.)destupperrr+r+r,rsz/HelpFormatter._get_default_metavar_for_optionalcCs|jSr.)rrr+r+r,rsz1HelpFormatter._get_default_metavar_for_positional)r9r:N)N) rr3r4r5rRrVrWobjectrHrdrerfrirorxryrcrbrjrrgrurqrrrrrrrrrrr+r+r+r,rs>  `j0  rc@seZdZdZddZdS)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)Nr`c3s|]}|VqdSr.r+)r[rrr+r, r_z9RawDescriptionHelpFormatter._fill_text..T)keepends)r% splitlines)r&rhrQrr+rr,rsz&RawDescriptionHelpFormatter._fill_textN)rr3r4r5rr+r+r+r,rsrc@seZdZdZddZdS)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&rhrQr+r+r,rsz!RawTextHelpFormatter._split_linesN)rr3r4r5rr+r+r+r,r sr c@seZdZdZddZdS)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:|jtur:ttg}|js2|j|vr:|d7}|S)Nz %(default) (default: %(default)s))rprrrrrr)r&rvrpZdefaulting_nargsr+r+r,rs  z.ArgumentDefaultsHelpFormatter._get_help_stringN)rr3r4r5rr+r+r+r,rsrc@s 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. cCs|jjSr.rrrr+r+r,rsz:MetavarTypeHelpFormatter._get_default_metavar_for_optionalcCs|jjSr.rrr+r+r,rsz.r+r&namesr+r2r,r#Js zAction._get_kwargscCs |jdSr6rr2r+r+r,rYszAction.format_usagecCsttddS)Nz.__call__() not defined)NotImplementedErrorrr&parser namespacevaluesrr+r+r,__call__\szAction.__call__)NNNNNFNN)N)rr3r4r5rRr#rrr+r+r+r,r s5 r cs0eZdZd fdd Zd ddZddZZS) rNFc s~g} |D]2} | | | drd| dd} | | q|dur\|dur\|tur\|d7}tj| |d||||||d dS)N----no-r9rr) rrrrrrrrpr)r! startswithrsuperrR) r&rrrrrrrprZ_option_stringsr __class__r+r,rR`s&    zBooleanOptionalAction.__init__cCs$||jvr t||j|d dS)Nr)rsetattrrrrr+r+r,rs zBooleanOptionalAction.__call__cCs d|jS)Nz | )r%rr2r+r+r,rsz"BooleanOptionalAction.format_usage)NNNFNN)N)rr3r4rRrr __classcell__r+r+rr,r_s rcs(eZdZdfdd ZdddZZS) _StoreActionNFc sT|dkrtd|dur,|tkr,tdttt|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)rrrrrRrrr+r,rRs   z_StoreAction.__init__cCst||j|dSr.)rrrr+r+r,rsz_StoreAction.__call__)NNNNNFNN)Nrr3r4rRrrr+r+rr,rsrcs(eZdZdfdd ZdddZZS) _StoreConstActionNFc s"tt|j||d||||ddS)Nr)rrrrrrrp)rrrRr&rrrrrrprrr+r,rRs z_StoreConstAction.__init__cCst||j|jdSr.)rrrrr+r+r,rsz_StoreConstAction.__call__)NFNN)Nrr+r+rr,rs rcseZdZdfdd ZZS)_StoreTrueActionFNcs tt|j||d|||ddS)NTrrrrrrp)rrrRr&rrrrrprr+r,rRs z_StoreTrueAction.__init__)FFNrr3r4rRrr+r+rr,rsrcseZdZdfdd ZZS)_StoreFalseActionTFNcs tt|j||d|||ddS)NFr)rrrRrrr+r,rRs z_StoreFalseAction.__init__)TFNrr+r+rr,rsrcs(eZdZdfdd ZdddZZS) _AppendActionNFc sT|dkrtd|dur,|tkr,tdttt|j||||||||| | d dS)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)rrrrrRrrr+r,rRs   z_AppendAction.__init__cCs2t||jd}t|}||t||j|dSr.)rrr8r!rr&rrrrr1r+r+r,rs z_AppendAction.__call__)NNNNNFNN)Nrr+r+rr,rsrcs(eZdZdfdd ZdddZZS) _AppendConstActionNFc s$tt|j||d|||||ddS)Nr)rrrrrrrpr)rrrRrrr+r,rR s z_AppendConstAction.__init__cCs4t||jd}t|}||jt||j|dSr.)rrr8r!rrrr+r+r,rs z_AppendConstAction.__call__)NFNN)Nrr+r+rr,r s rcs(eZdZdfdd ZdddZZS) _CountActionNFcs tt|j||d|||ddS)Nr)rrrrrrp)rrrRrrr+r,rR's z_CountAction.__init__cCs0t||jd}|durd}t||j|ddSNrrT)rrr)r&rrrrcountr+r+r,r5sz_CountAction.__call__)NFN)Nrr+r+rr,r%s rcs.eZdZeedffdd ZdddZZS) _HelpActionNcstt|j|||d|ddSNr)rrrrrp)rr rR)r&rrrrprr+r,rR>s z_HelpAction.__init__cCs||dSr.) print_helpexitrr+r+r,rJsz_HelpAction.__call__)Nrr3r4rrRrrr+r+rr,r <s  r cs0eZdZdeedffdd ZdddZZS)_VersionActionNz&show program's version number and exitcs$tt|j|||d|d||_dSr )rrrRversion)r&rrrrrprr+r,rRQs z_VersionAction.__init__cCsD|j}|dur|j}|}||||tj|dSr.)r_get_formatterri_print_messagerc_sysstdoutr)r&rrrrrrXr+r+r,r_s z_VersionAction.__call__)Nrr+r+rr,rOs rcsPeZdZGdddeZedddffdd ZddZd d Zd d d Z Z S)_SubParsersActioncseZdZfddZZS)z&_SubParsersAction._ChoicesPseudoActioncs@|}}|r|dd|7}ttj|}|jg|||ddS)Nrr)rrrpr)r%rr_ChoicesPseudoActionrR)r&r)aliasesrprrZsuprr+r,rRms  z/_SubParsersAction._ChoicesPseudoAction.__init__rr+r+rr,rksrFNc s<||_||_i|_g|_tt|j||t|j|||ddS)N)rrrrrrpr) _prog_prefix _parser_class_name_parser_map_choices_actionsrrrRr)r&rrP parser_classrrrprrr+r,rRus  z_SubParsersAction.__init__cKs|ddur d|j|f|d<|dd}d|vrX|d}||||}|j||jfi|}||j|<|D]}||j|<qv|S)NrPrrr+rp)rrrrrr!rr)r&r)kwargsrrpZ choice_actionraliasr+r+r, add_parsers     z_SubParsersAction.add_parsercCs|jSr.)rr2r+r+r,rsz!_SubParsersAction._get_subactionsc Cs|d}|dd}|jtur,t||j|z|j|}Wn:tyt|d|jd}td|}t||Yn0||d\} }t |  D]\} } t|| | q|rt | t gt |t |dS)NrrTr) parser_namerz5unknown parser %(parser_name)r (choices: %(choices)s))rrrrKeyErrorr%rrparse_known_argsrr1 setdefault_UNRECOGNIZED_ARGS_ATTRrr) r&rrrrr!r'r]msgZ subnamespacekeyr*r+r+r,rs$      z_SubParsersAction.__call__)N) rr3r4r rrrRr rrrr+r+rr,risrc@seZdZdddZdS) _ExtendActionNcCs2t||jd}t|}||t||j|dSr.)rrr8rrrr+r+r,rs z_ExtendAction.__call__)N)rr3r4rr+r+r+r,r(sr(c@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,rRszFileType.__init__c s|dkrndjvr*djvr$tjjStjStfdddDrXdjvrRtjjStjStdj}t|zt|jj j j WSt y}z*||d}td }t ||WYd}~n d}~00dS) N-r)bc3s|]}|jvVqdSr.)r*rr2r+r,rr_z$FileType.__call__..Zwaxzargument "-" with mode %r)filenameerrorz$can't open '%(filename)s': %(error)s)r*rstdinbufferanyrrrrr+r,r-OSErrorr)r&stringr&er]rr+r2r,rs  zFileType.__call__cCsT|j|jf}d|jfd|jfg}ddd|Ddd|D}dt|j|fS)Nr0r1rcSsg|]}|dkrt|qS)r)r")r[r(r+r+r,r^r_z%FileType.__repr__..cSs$g|]\}}|durd||fqS)Nrr+)r[kwr(r+r+r,r^s r)r*r+r,r-r%rr)r&r]rZargs_strr+r+r,r-s   zFileType.__repr__)r)rNN)rr3r4r5rRrr-r+r+r+r,rs rc@s(eZdZdZddZddZddZdS) 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&rr)r+r+r,rRszNamespace.__init__cCst|tstSt|t|kSr.)rr NotImplementedr)r&otherr+r+r,__eq__s zNamespace.__eq__cCs ||jvSr.)r0)r&r'r+r+r, __contains__szNamespace.__contains__N)rr3r4r5rRr?r@r+r+r+r,r sr 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)NrvZstoreZ store_const store_trueZ store_falser!Z append_constr rprparsersrz^-\d+$|^-\d*\.\d+$)rrArR descriptionargument_default prefix_charsconflict_handler _registriesregisterrrrrrrrr rrr( _get_handler_actions_option_string_actions_action_groups_mutually_exclusive_groups _defaultsrKrL_negative_number_matcher_has_negative_number_optionals)r&rDrFrErGrr+r,rRs4 z_ActionsContainer.__init__cCs|j|i}|||<dSr.)rHr$)r& registry_namer*rregistryr+r+r,rISsz_ActionsContainer.registerNcCs|j|||Sr.)rHr)r&rRr*rr+r+r, _registry_getWsz_ActionsContainer._registry_getcKs2|j||jD]}|j|vr||j|_qdSr.)rOupdaterKrr)r&rrvr+r+r, set_defaults]s   z_ActionsContainer.set_defaultscCs8|jD]"}|j|kr|jdur|jSq|j|dSr.)rKrrrOr)r&rrvr+r+r, get_defaultfs  z_ActionsContainer.get_defaultcOsP|j}|r&t|dkrL|dd|vrL|r:d|vr:td|j|i|}n|j|i|}d|vr|d}||jvr|j||d<n|jdur|j|d<||}t|std|f|fi|}| d|j |j }t|std |f|t urtd |ft |d rFz| |dWntyDtd Yn0||S) z add_argument(dest, ..., name=value, ...) add_argument(option_string, option_string, ..., name=value, ...) rTrrz+dest supplied twice for positional argumentrNzunknown action "%s"r%r is not callablez<%r is a FileType class object, instance of it must be passedrz,length of metavar tuple does not match nargs)rFrtr_get_positional_kwargs_get_optional_kwargsrOrE_pop_action_classcallablerTrrrrrr _add_action)r&r]rcharsrZ action_classrv type_funcr+r+r,rxps:         z_ActionsContainer.add_argumentcOs&t|g|Ri|}|j||Sr.)_ArgumentGrouprMr!)r&r]rrr+r+r,add_argument_groups z$_ActionsContainer.add_argument_groupcKs t|fi|}|j||Sr.)_MutuallyExclusiveGrouprNr!)r&rrr+r+r,add_mutually_exclusive_groups z._ActionsContainer.add_mutually_exclusive_groupcCs`|||j|||_|jD]}||j|<q"|jD]"}|j|r8|js8|jdq8|S)NT) _check_conflictrKr! containerrrLrPmatchrQ)r&rvrr+r+r,r]s      z_ActionsContainer._add_actioncCs|j|dSr.)rKremoverr+r+r,_remove_actionsz _ActionsContainer._remove_actioncCsi}|jD].}|j|vr.td}t||j|||j<q i}|jD]D}|j|vrn|j|j|j|jd||j<|jD]}||j||<qtqD|jD]&}|j |j d}|jD] }|||<qq|j D]}| || |qdS)Nz.cannot merge actions - two groups are named %r)titlerDrG)r)rMrirrrarDrGrrNrcrrKrr])r&reZtitle_group_maprr&Z group_maprv mutex_groupr+r+r,_add_container_actionss0           z(_ActionsContainer._add_container_actionscKs^d|vrtd}t||dttfvr2d|d<|dtkrPd|vrPd|d<t||gdS)Nrz1'required' is an invalid argument for positionalsrTrrr)rrrrrr)r&rrr&r+r+r,rYsz(_ActionsContainer._get_positional_kwargsc Osg}g}|D]`}|d|jvr>||jd}td}t||||t|dkr |d|jvr ||q |dd}|dur|r|d}n|d}||j}|std}t|||dd}t|||d S) Nr)optionrFzNinvalid option string %(option)r: must start with a character %(prefix_chars)rrTrz%dest= is required for options like %rr2rrl) rFrrr!rtrlstripreplacer) r&r]rrZlong_option_stringsrr&rZdest_option_stringr+r+r,rZs.        z&_ActionsContainer._get_optional_kwargscCs|d|}|d||S)Nrv)rrT)r&rrrvr+r+r,r[s z#_ActionsContainer._pop_action_classcCsDd|j}z t||WSty>td}t||jYn0dS)Nz_handle_conflict_%sz%invalid conflict_resolution value: %r)rGrrrr)r&Zhandler_func_namer&r+r+r,rJ#s    z_ActionsContainer._get_handlercCsLg}|jD]&}||jvr |j|}|||fq |rH|}|||dSr.)rrLr!rJ)r&rvZconfl_optionalsrZconfl_optionalrGr+r+r,rd,s   z!_ActionsContainer._check_conflictcCs6tddt|}ddd|D}t|||dS)Nzconflicting option string: %szconflicting option strings: %srcSsg|] \}}|qSr+r+)r[rrvr+r+r,r^>sz<_ActionsContainer._handle_conflict_error..)rrtr%r)r&rvconflicting_actionsrZconflict_stringr+r+r,_handle_conflict_error:s z(_ActionsContainer._handle_conflict_errorcCs>|D]4\}}|j||j|d|js|j|qdSr.)rrgrLrrerh)r&rvrprr+r+r,_handle_conflict_resolveCs   z*_ActionsContainer._handle_conflict_resolve)N)N)rr3r4rRrIrTrVrWrxrarcr]rhrkrYrZr[rJrdrqrrrr+r+rr,rAs$ 5   3("   rAcs6eZdZdfdd ZfddZfddZZS) r`Nc s|j}|d|j|d|j|d|jtt|j}|fd|i|||_g|_|j |_ |j |_ |j |_ |j |_ |j |_ |j|_dS)NrGrFrErD)r$rGrFrErr`rRrirrHrKrLrOrQrN)r&rerirDrrUZ super_initrr+r,rRTs    z_ArgumentGroup.__init__cs tt||}|j||Sr.)rr`r]rr!rrr+r,r]js z_ArgumentGroup._add_actioncs tt|||j|dSr.)rr`rhrrgrrr+r,rhosz_ArgumentGroup._remove_action)NNrr3r4rRr]rhrr+r+rr,r`Rs r`cs.eZdZdfdd ZddZddZZS) rbFcs tt||||_||_dSr.)rrbrRr _container)r&rerrr+r,rRvsz _MutuallyExclusiveGroup.__init__cCs2|jrtd}t||j|}|j||S)Nz-mutually exclusive arguments must be optional)rrrrtr]rr!)r&rvr&r+r+r,r]{s   z#_MutuallyExclusiveGroup._add_actioncCs|j||j|dSr.)rtrhrrgrr+r+r,rhs z&_MutuallyExclusiveGroup._remove_action)Frsr+r+rr,rbtsrbc s,eZdZdZddddgedddddddf fdd Zdd Zd d Zd d ZddZ ddZ dAddZ dBddZ ddZ ddZddZddZddZd d!Zd"d#Zd$d%ZdCd&d'ZdDd(d)Zd*d+Zd,d-Zd.d/Zd0d1Zd2d3Zd4d5ZdEd6d7ZdFd8d9ZdGd:d;ZdHd=d>Z d?d@Z!Z"S)Ira)Object for parsing command line strings into Python objects. Keyword Arguments: - prog -- The name of the program (default: 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 Nr2r5Tc s&tt|j}|||| | d|dur6tjtjd}||_||_ ||_ ||_ ||_ | |_ | |_| |_|j}|td|_|td|_d|_dd}|dd|d|vrdn|d}|j r|j|d |d d d ttd d |D]:}||z |j}WntyYq0|j|qdS)N)rDrFrErGrzpositional argumentszoptional argumentscSs|Sr.r+)r:r+r+r,identitysz)ArgumentParser.__init__..identityrr2hr9rpzshow this help message and exit)rvrrp)rrrR_ospathbasenamerargvrPrkepilogformatter_classfromfile_prefix_charsadd_help allow_abbrev exit_on_errorrar _positionals _optionals _subparsersrIrxrrkrOrrU)r&rPrkrDr{parentsr|rFr}rErGr~rrZ superinitZ add_groupruZdefault_prefixrYdefaultsrr+r,rRsH   zArgumentParser.__init__csgd}fdd|DS)N)rPrkrDr|rGr~csg|]}|t|fqSr+rrr2r+r,r^r_z.ArgumentParser._get_kwargs..r+rr+r2r,r#szArgumentParser._get_kwargsc Ks|jdur|td|dt|d|vs8d|vrht|dd}t|dd}||||_n|j|_|ddur| }| }|j }| |j ||d||d<||d}|fd gi|}|j||S) Nz(cannot have multiple subparser argumentsrrirDZ subcommandsrPr`rCr)rr5rr$rrrarrr_get_positional_actionsrNrorkrcr|r[r]) r&rrirDrXrrmZ parsers_classrvr+r+r,add_subparserss$   zArgumentParser.add_subparserscCs$|jr|j|n |j||Sr.)rrr]rrr+r+r,r] s zArgumentParser._add_actioncCsdd|jDS)NcSsg|]}|jr|qSr+rr[rvr+r+r,r^sz8ArgumentParser._get_optional_actions..rKr2r+r+r,_get_optional_actionssz$ArgumentParser._get_optional_actionscCsdd|jDS)NcSsg|]}|js|qSr+rrr+r+r,r^sz:ArgumentParser._get_positional_actions..rr2r+r+r,rsz&ArgumentParser._get_positional_actionscCs4|||\}}|r0td}||d||SNzunrecognized arguments: %sr)r#rr5r%r&r]rrzr&r+r+r, parse_args s zArgumentParser.parse_argscCs|durtjdd}nt|}|dur.t}|jD]4}|jtur4t||js4|jtur4t ||j|jq4|j D] }t||spt |||j |qp|j rz| ||\}}Wqt ytd}|t|Yq0n| ||\}}t|tr|t|tt|t||fSrS)rrzr/r rKrrrrrrOr_parse_known_argsrexc_infor5rr%rrdelattr)r&r]rrvrerrr+r+r,r#'s0          zArgumentParser.parse_known_argscs jdur i jD]R}|j}t|jD]<\}}|g}||d||||ddq2qig}t} t| D]^\}} | dkr|d| D]} |dqq | } | durd} n | |<d} || qd |t t d fdd  fd d }  fd d }gd r`t }nd} |krt fddD} |kr| }| kr| qdn| vr҈ |}|| |  qd| }|dg} jD]|}|vr|jr(|t|nT|jdurt|jtrt|jr|jt|jurt|j ||jq|r tdd | jD]X}|jr|jD]}|vrqqdd|jD}td} |d |qfS)NrTrr2AOr`cs||||}||jurb||gD]*}|vr6td}t|}t|||q6|turx|||dS)Nznot allowed with argument %s)r _get_valuesrrrrrr)rvZargument_stringsrZargument_valuesZconflict_actionr&Z action_name)action_conflictsr seen_actionsseen_non_default_actionsr&r+r, take_action}s    z5ArgumentParser._parse_known_args..take_actioncs~|}|\}}}j}g}|dur:||dS|dur||d}j}|dkr|d|vr||g|f|d} | |d}|ddpd} j} || vr| |}| }ntd} t|| |nB|dkr|d} |g}||||fq\ntd} t|| |q|d}|d}|||}||} || }||||fq\q|D]\}}}|||q`| S)NrTrrzignored explicit argument %r)_match_argumentr!rFrLrr) start_index option_tuplervr explicit_argZmatch_argumentZ action_tuples arg_countr^charZnew_explicit_argZ optionals_mapr&stopr]rZselected_patterns)r'arg_strings_patternextrasoption_string_indicesr&rr+r,consume_optionalsL       z:ArgumentParser._parse_known_args..consume_optionalcsnj}|d}||}t|D]*\}}|||}||7}||q&t|ddd<|Sr.)_match_arguments_partialziprt)rZ match_partialZselected_patternZ arg_countsrvrr])r'rrr&rr+r,consume_positionalss   z=ArgumentParser._parse_known_args..consume_positionalsrrcsg|]}|kr|qSr+r+)r[r)rr+r,r^sz4ArgumentParser._parse_known_args..z(the following arguments are required: %srcSsg|]}|jturt|qSr+)rprrrr+r+r,r^;s z#one of the arguments %s is requiredr)N)r}_read_args_from_filesrNrrr$riterr!_parse_optionalr%rrrBrArKrrrrrrrrr _get_valuer5r)r&r'rrjrrZ mutex_actionZ conflictsZarg_string_pattern_partsZarg_strings_iter arg_stringrpatternrrZmax_option_string_indexZnext_option_string_indexZpositionals_end_indexZstringsZ stop_indexZrequired_actionsrvrrr&r+) rr'rrrrrrrr&rrr,rNs        J                    z ArgumentParser._parse_known_argsc Csg}|D]}|r|d|jvr*||qzxt|ddT}g}|D]}||D]}||q\qN||}||Wdn1s0YWqtyt d}| t |Yq0q|Sr ) r}r!rreadrconvert_arg_line_to_argsrrr9rrr5r)r&r'Znew_arg_stringsrZ args_filearg_liner(rr+r+r,rDs   ,  z$ArgumentParser._read_args_from_filescCs|gSr.r+)r&rr+r+r,r^sz'ArgumentParser.convert_arg_line_to_argscCsz||}t||}|durldtdttdttdi}||j}|durbtdd|j|j}t ||t | dS)Nzexpected one argumentzexpected at most one argumentzexpected at least one argumentzexpected %s argumentzexpected %s argumentsrT) _get_nargs_patternrKrfrrr rrrrrtr)r&rvr nargs_patternrfZ nargs_errorsr&r+r+r,ras"    zArgumentParser._match_argumentcsrg}tt|ddD]X}|d|}dfdd|D}t||}|dur|dd|Dqnq|S)Nrrr`csg|]}|qSr+)rrr2r+r,r^}sz;ArgumentParser._match_arguments_partial..cSsg|] }t|qSr+)rt)r[r:r+r+r,r^r_)rrtr%rKrfrrm)r&rlrrrZ actions_slicerrfr+r2r,rws  z'ArgumentParser._match_arguments_partialc Cs|sdS|d|jvrdS||jvr8|j|}||dfSt|dkrHdSd|vr~|dd\}}||jvr~|j|}|||fS||}t|dkrddd|D}||d}td}|||nt|dkr|\} | S|j |r|j sdSd |vrdSd|dfS) NrrT=rcSsg|]\}}}|qSr+r+)r[rvrrr+r+r,r^sz2ArgumentParser._parse_optional..)rmZmatchesz4ambiguous option: %(option)s could match %(matches)sr) rFrLrtsplit_get_option_tuplesr%rr5rPrfrQ) r&rrvrrZ option_tuplesZoptionsr]r&rr+r+r,rs>              zArgumentParser._parse_optionalc Cs0g}|j}|d|vr|d|vr|jr~d|vrB|dd\}}n|}d}|jD],}||rP|j|}|||f}||qPn|d|vr|d|vr|}d}|dd}|dd} |jD]T}||kr|j|}||| f}||q||r|j|}|||f}||qn|td||S)NrrTrr9zunexpected option string: %s)rFrrrLrr!r5r) r&rrr^Z option_prefixrrvrZshort_option_prefixZshort_explicit_argr+r+r,rs:             z!ArgumentParser._get_option_tuplescCs|j}|durd}nf|tkr"d}nX|tkr0d}nJ|tkr>d}n<|tkrLd}n.|tkrZd}n |tkrhd}ndd d |}|jr| d d }| d d }|S) Nz(-*A-*)z(-*A?-*)z (-*[A-]*)z (-*A[A-]*)z([-AO]*)z (-*A[-AO]*)z(-*-*)z(-*%s-*)z-*rr`r2) rrrr rrrr%rro)r&rvrrr+r+r,rs(  z!ArgumentParser._get_nargs_patterncCs4|||\}}|r0td}||d||Sr)parse_known_intermixed_argsrr5r%rr+r+r,parse_intermixed_args s z$ArgumentParser.parse_intermixed_argsc s|ddD}|r,td|djfdd|jDrHtdz|j}z|jdurp|dd|_D] }|j|_t|_|j|_ t|_qt| ||\}}D]J}t ||j rt ||j gkrddlm}|d |j |ft||j qWD]}|j|_|j |_qnD]}|j|_|j |_q0|}zt|D]}|j|_d |_q@|jD]} | j| _d | _q\| ||\}} W|D]}|j|_q|jD]} | j| _qn,|D]}|j|_q|jD]} | j| _q0W||_n||_0|| fS) NcSsg|]}|jttfvr|qSr+)rrrrr+r+r,r^4 sz>ArgumentParser.parse_known_intermixed_args..z3parse_intermixed_args: positional arg with nargs=%srcs&g|]}|jD]}|vr|jqqSr+)rr)r[rrvrr+r,r^: sz;parse_intermixed_args: positional in mutuallyExclusiveGroup)warnzDo not expect %s in %sF)rrrrNrkrZ save_nargsrrZ save_defaultr#rrrwarningsrrrrZ save_required) r&r]raZ save_usagervZremaining_argsrrrrr+rr,r& sn             z*ArgumentParser.parse_known_intermixed_argscsjttfvr0z|dWnty.Yn0|sxjtkrxjrLj}nj}t |t rt |} |n|sjt krjsjdurj}n|} |nt|dkrjdtfvr|\} |} |njtkr fdd|D}npjtkr>fdd|D} |dn>jtkrPt}n,fdd|D}|D]} |qh|S)NrrTcsg|]}|qSr+rr[vrvr&r+r,r^ r_z.ArgumentParser._get_values..csg|]}|qSr+rrrr+r,r^ r_rcsg|]}|qSr+rrrr+r,r^ r_)rrrrgrrrrrrrr _check_valuerrtr)r&rvr'r*rrr+rr,rs sD        zArgumentParser._get_valuesc Cs|d|j|j}t|s0td}t|||z ||}Wnty|t|jdt|j}tt d}t||YnJt t fyt|jdt|j}||d}td}t|||Yn0|S)NrrXrrT)rr*z!invalid %(type)s value: %(value)r) rTrr\rrrrr"rrrrr)r&rvrr_r&rr)r]r+r+r,r s    zArgumentParser._get_valuecCsF|jdurB||jvrB|dtt|jd}td}t|||dS)Nr)r*rz3invalid choice: %(value)r (choose from %(choices)s))rr%rsr"rr)r&rvr*r]r&r+r+r,r s zArgumentParser._check_valuecCs$|}||j|j|j|Sr.)rrorkrKrNrc)r&rXr+r+r,r s  zArgumentParser.format_usagecCst|}||j|j|j||j|jD]0}||j ||j| |j | q.||j |Sr.)rrorkrKrNrirDrMreriryrrfr{rc)r&rXZ action_groupr+r+r,rc s        zArgumentParser.format_helpcCs|j|jdS)Nr~)r|rPr2r+r+r,r szArgumentParser._get_formattercCs"|durtj}|||dSr.)rrrrr&filer+r+r, print_usage szArgumentParser.print_usagecCs"|durtj}|||dSr.)rrrrcrr+r+r,r  szArgumentParser.print_helpcCs |r|durtj}||dSr.)rstderrwrite)r&rrr+r+r,r szArgumentParser._print_messagercCs |r||tjt|dSr.)rrrr)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. )rPrr9z%(prog)s: error: %(message)s N)rrrrPrr)r&rr]r+r+r,r5 s  zArgumentParser.error)NN)NN)NN)NN)N)N)N)rN)#rr3r4r5rrRr#rr]rrrr#rrrrrrrrrrrrrrrcrrr rrr5rr+r+rr,rsXB  'w:-1  M8    r)2r5 __version____all__osrwrerKsysrrrrrrrr rrr%rrr8rrr rr r Exceptionrrr rrrrrrrrr rrr(rr rAr`rbrr+r+r+r,s`=   ^)#&] 78"