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......_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)self type_name arg_strings star_argsargnamevaluer. /usr/lib64/python3.9/argparse.py__repr__ts   z_AttributeHolder.__repr__cCst|jSN)list__dict__itemsr'r.r.r/r$sz_AttributeHolder._get_kwargscCsgSr1r.r5r.r.r/r!sz_AttributeHolder._get_argsN)r __module__ __qualname____doc__r0r$r!r.r.r.r/rksrcCs6|dur gSt|tur$|ddSddl}||SNr)rr2copy)r4r:r.r.r/ _copy_itemss   r;c@seZdZdZd;ddZddZd d ZGd d d eZd dZ ddZ ddZ ddZ dz6HelpFormatter._Section.format_help..z%*s%s:  ) r^r]r[ _join_partsr4r\r_rrH)r'r& item_helpcurrent_indentr_r.r.r/ format_helps    z"HelpFormatter._Section.format_help)N)r r6r7rWrjr.r.r.r/rKs rKcCs|jj||fdSr1)rMr4r")r'rarbr.r.r/ _add_itemszHelpFormatter._add_itemcCs0||||j|}||jg||_dSr1)r[rKrMrkrj)r'r_sectionr.r.r/ start_sectionszHelpFormatter.start_sectioncCs|jj|_|dSr1)rMr^r\r5r.r.r/ end_sections zHelpFormatter.end_sectioncCs$|tur |dur ||j|gdSr1)rrk _format_text)r'textr.r.r/add_textszHelpFormatter.add_textcCs&|tur"||||f}||j|dSr1)rrk _format_usage)r'usageactionsgroupsprefixrbr.r.r/ add_usages zHelpFormatter.add_usagecCsr|jturn|j}||g}||D]}|||q$ttt|}||j}t|j ||_ | |j |gdSr1) helpr_format_action_invocation_iter_indented_subactionsr"rEmaplenrHrJrk_format_action)r'actionget_invocation invocations subactioninvocation_length action_lengthr.r.r/ add_arguments   zHelpFormatter.add_argumentcCs|D]}||qdSr1)r)r'rtr~r.r.r/ add_argumentsszHelpFormatter.add_argumentscCs.|j}|r*|jd|}|dd}|S)N rf)rLrjrRsubstrip)r'rxr.r.r/rjs  zHelpFormatter.format_helpcCsddd|DS)NrecSsg|]}|r|tur|qSr.)r)r`partr.r.r/rc"s z-HelpFormatter._join_parts..)r&)r' part_stringsr.r.r/rg!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: rSz%(prog)s cSsg|] }|r|qSr.r.)r`sr.r.r/rcBrdz/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)NrYrr)r|r"r&)partsindentrvlineslineline_lenr text_widthr.r/ get_linesVs"   z.HelpFormatter._format_usage..get_linesg?rYrfz%s%s )N) _dictrBoption_stringsr"_format_actions_usager&rGrHr|rNfindallextend)r'rsrtrurvrS optionals positionalsr~format action_usage part_regexp opt_usage pos_usage opt_parts pos_partsrrrrr.rr/rr&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 (()rY|%s %s[%s]T)reversercSsg|]}|dur|qSr1r.)r`itemr.r.r/rcrdz7HelpFormatter._format_actions_usage..z[\[(]z[\])]z(%s) z\1 (%s)z%s *%srez \(([^|]*)\))set_group_actions ValueErrorindexr|addrequiredrange enumeraterxrr"getpopr#_get_default_metavar_for_positional _format_argsnargs format_usage!_get_default_metavar_for_optionalsortedr&rNrr)r'rtru group_actionsinsertsgroupstartendr~irdefaultr option_string args_stringrpopencloser.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'rprrr.r.r/ros  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) Nr<rrez%*s%s z %*s%-*s rrYrf)rDrJrFrErGrHryrxr|r _expand_help _split_linesr"endswithrzr}rg) r'r~ help_position help_width action_width action_headertup indent_firstr help_text help_linesrrr.r.r/r}s:          zHelpFormatter._format_actioncCs|js&||}|||d\}|Sg}|jdkrB||jn4||}|||}|jD]}|d||fq^d|SdS)NrYrrr) rr_metavar_formatterrrrrr"r&)r'r~rmetavarrrrr.r.r/ry's     z'HelpFormatter._format_action_invocationcsP|jdur|jn.|jdur.z{%s},csttrSf|SdSr1) isinstancetuple) tuple_sizeresultr.r/rHs z0HelpFormatter._metavar_formatter..format)rchoicesr&)r'r~default_metavar choice_strsrr.rr/r?s   z HelpFormatter._metavar_formattercCs |||}|jdur$d|d}n|jtkr.zinvalid nargs valuer) rrrrr|r rrrr TypeErrorrr&)r'r~r get_metavarrrformatsr.r.r/rOs0            zHelpFormatter._format_argscCstt||jd}t|D]}||tur||=qt|D] }t||dr:||j||<q:|ddurddd|dD}||d<| ||S)Nrr rrcSsg|] }t|qSr.rr`cr.r.r/rctrdz.HelpFormatter._expand_help..) rvarsrBr2rhasattrr rr&_get_help_string)r'r~paramsr, choices_strr.r.r/rks   zHelpFormatter._expand_helpccs>z |j}WntyYn0||EdH|dSr1)_get_subactionsAttributeErrorr[r\)r'r~get_subactionsr.r.r/rzxs   z'HelpFormatter._iter_indented_subactionscCs&|jd|}ddl}|||S)Nrr)rQrrtextwrapwrap)r'rprVrr.r.r/rszHelpFormatter._split_linescCs,|jd|}ddl}|j||||dS)Nrr)initial_indentsubsequent_indent)rQrrrfill)r'rprVrrr.r.r/rs zHelpFormatter._fill_textcCs|jSr1)rxr'r~r.r.r/rszHelpFormatter._get_help_stringcCs |jSr1)destupperrr.r.r/rsz/HelpFormatter._get_default_metavar_for_optionalcCs|jSr1)rrr.r.r/rsz1HelpFormatter._get_default_metavar_for_positional)r<r=N)N) r r6r7r8rWr[r\objectrKrkrmrnrqrwrrrjrgrrrror}ryrrrrzrrrrrr.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)Nrec3s|]}|VqdSr1r.)r`rrr.r/ rdz9RawDescriptionHelpFormatter._fill_text..T)keepends)r& splitlines)r'rprVrr.rr/rsz&RawDescriptionHelpFormatter._fill_textN)r r6r7r8rr.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|Sr1)r)r'rprVr.r.r/rsz!RawTextHelpFormatter._split_linesN)r r6r7r8rr.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))rxrrrrrr)r'r~rxdefaulting_nargsr.r.r/rs  z.ArgumentDefaultsHelpFormatter._get_help_stringN)r r6r7r8rr.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|jjSr1rr rr.r.r/rsz:MetavarTypeHelpFormatter._get_default_metavar_for_optionalcCs|jjSr1rrr.r.r/rsz.r.r'namesr.r5r/r$Js zAction._get_kwargscCs |jdSr9rr5r.r.r/rYszAction.format_usagecCsttddS)Nz.__call__() not defined)NotImplementedErrorrr'parser namespacevaluesrr.r.r/__call__\szAction.__call__)NNNNNFNN)N)r r6r7r8rWr$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-r<rr) rrrrrrrrxr)r" startswithrsuperrW) r'rrrrrrrxr_option_stringsr __class__r.r/rW`s&    zBooleanOptionalAction.__init__cCs$||jvr t||j|d dS)Nr)rsetattrrrrr.r.r/rs zBooleanOptionalAction.__call__cCs d|jS)Nz | )r&rr5r.r.r/rsz"BooleanOptionalAction.format_usage)NNNFNN)N)r r6r7rWrr __classcell__r.r.r r/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 )rrrr$rWrr r.r/rWs   z_StoreAction.__init__cCst||j|dSr1)r"rrr.r.r/rsz_StoreAction.__call__)NNNNNFNN)Nr r6r7rWrr#r.r.r r/r$sr$cs(eZdZdfdd ZdddZZS) _StoreConstActionNFc s"tt|j||d||||ddS)Nr)rrrr rrrx)rr'rWr'rrr rrrxrr r.r/rWs z_StoreConstAction.__init__cCst||j|jdSr1)r"rr rr.r.r/rsz_StoreConstAction.__call__)NFNN)Nr&r.r.r r/r's r'cseZdZdfdd ZZS)_StoreTrueActionFNcs tt|j||d|||ddS)NTrrr rrrx)rr)rWr'rrrrrxr r.r/rWs z_StoreTrueAction.__init__)FFNr r6r7rWr#r.r.r r/r)sr)cseZdZdfdd ZZS)_StoreFalseActionTFNcs tt|j||d|||ddS)NFr*)rr-rWr+r r.r/rWs z_StoreFalseAction.__init__)TFNr,r.r.r r/r-sr-cs(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 appropriater%r )rrrr.rWrr r.r/rWs   z_AppendAction.__init__cCs2t||jd}t|}||t||j|dSr1)rrr;r"r"r'rrrrr4r.r.r/rs z_AppendAction.__call__)NNNNNFNN)Nr&r.r.r r/r.sr.cs(eZdZdfdd ZdddZZS) _AppendConstActionNFc s$tt|j||d|||||ddS)Nr)rrrr rrrxr)rr0rWr(r r.r/rW s z_AppendConstAction.__init__cCs4t||jd}t|}||jt||j|dSr1)rrr;r"r r"r/r.r.r/rs z_AppendConstAction.__call__)NFNN)Nr&r.r.r r/r0 s r0cs(eZdZdfdd ZdddZZS) _CountActionNFcs tt|j||d|||ddS)Nr)rrrrrrx)rr1rWr+r r.r/rW's z_CountAction.__init__cCs0t||jd}|durd}t||j|ddSNrrY)rrr")r'rrrrcountr.r.r/r5sz_CountAction.__call__)NFN)Nr&r.r.r r/r1%s r1cs.eZdZeedffdd ZdddZZS) _HelpActionNcstt|j|||d|ddSNr)rrrrrx)rr4rW)r'rrrrxr r.r/rW>s z_HelpAction.__init__cCs||dSr1) print_helpexitrr.r.r/rJsz_HelpAction.__call__)Nr r6r7rrWrr#r.r.r r/r4<s  r4cs0eZdZdeedffdd ZdddZZS)_VersionActionNz&show program's version number and exitcs$tt|j|||d|d||_dSr5)rr9rWversion)r'rr:rrrxr r.r/rWQs z_VersionAction.__init__cCsD|j}|dur|j}|}||||tj|dSr1)r:_get_formatterrq_print_messagerj_sysstdoutr7)r'rrrrr:r]r.r.r/r_s z_VersionAction.__call__)Nr8r.r.r r/r9Os r9csPeZdZGdddeZedddffdd ZddZd d Zd d d Z Z S)_SubParsersActioncseZdZfddZZS)z&_SubParsersAction._ChoicesPseudoActioncs@|}}|r|dd|7}ttj|}|jg|||ddS)Nrr)rrrxr)r&rr?_ChoicesPseudoActionrW)r'r,aliasesrxrrsupr r.r/rWms  z/_SubParsersAction._ChoicesPseudoAction.__init__r,r.r.r r/r@ksr@FNc s<||_||_i|_g|_tt|j||t|j|||ddS)N)rrrrrrxr) _prog_prefix _parser_class_name_parser_map_choices_actionsrr?rWr)r'rrS parser_classrrrxrr r.r/rWus  z_SubParsersAction.__init__cKs|ddur d|j|f|d<|dd}d|vrX|d}||||}|j||jfi|}||j|<|D]}||j|<qv|S)NrSrrAr.rx)rrCrr@rFr"rDrE)r'r,kwargsrArx choice_actionraliasr.r.r/ add_parsers     z_SubParsersAction.add_parsercCs|jSr1)rFr5r.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)NrrYr) parser_namerz5unknown parser %(parser_name)r (choices: %(choices)s))rrr"rEKeyErrorr&rrparse_known_argsrr4 setdefault_UNRECOGNIZED_ARGS_ATTRrr) r'rrrrrLr)rbmsg subnamespacekeyr-r.r.r/rs$      z_SubParsersAction.__call__)N) r r6r7r r@rrWrKrrr#r.r.r r/r?isr?c@seZdZdddZdS) _ExtendActionNcCs2t||jd}t|}||t||j|dSr1)rrr;rr"r/r.r.r/rs z_ExtendAction.__call__)N)r r6r7rr.r.r.r/rTsrTc@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||_||_||_||_dSr1)_mode_bufsize _encoding_errors)r'modebufsizeencodingerrorsr.r.r/rWszFileType.__init__c s|dkrndjvr*djvr$tjjStjStfdddDrXdjvrRtjjStjStdj}t|zt|jj j j WSt y}z*||d}td }t ||WYd}~n d}~00dS) N-rUbc3s|]}|jvVqdSr1)rVrr5r.r/rrdz$FileType.__call__..waxzargument "-" with mode %r)filenameerrorz$can't open '%(filename)s': %(error)s)rVr=stdinbufferanyr>rrrrWrXrYOSErrorr)r'stringrQerbr r.r5r/rs  zFileType.__call__cCsT|j|jf}d|jfd|jfg}ddd|Ddd|D}dt|j|fS)Nr\r]rcSsg|]}|dkrt|qS)r)r#)r`r+r.r.r/rcrdz%FileType.__repr__..cSs$g|]\}}|durd||fqS)Nrr.)r`kwr+r.r.r/rcs r)rVrWrXrYr&rr )r'rbrHargs_strr.r.r/r0s   zFileType.__repr__)rUrNN)r r6r7r8rWrr0r.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||||qdSr1)r")r'rHr,r.r.r/rWszNamespace.__init__cCst|tstSt|t|kSr1)rr NotImplementedr)r'otherr.r.r/__eq__s zNamespace.__eq__cCs ||jvSr1)r3)r'rSr.r.r/ __contains__szNamespace.__contains__N)r r6r7r8rWrmrnr.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)Nr~store store_const store_true store_falser" append_constr3rxr:parsersrz^-\d+$|^-\d*\.\d+$)rrorW descriptionargument_default prefix_charsconflict_handler _registriesregisterr$r'r)r-r.r0r1r4r9r?rT _get_handler_actions_option_string_actions_action_groups_mutually_exclusive_groups _defaultsrNrO_negative_number_matcher_has_negative_number_optionals)r'rvrxrwryr r.r/rWs4 z_ActionsContainer.__init__cCs|j|i}|||<dSr1)rzrO)r' registry_namer-rregistryr.r.r/r{Ssz_ActionsContainer.registerNcCs|j|||Sr1)rzr)r'rr-rr.r.r/ _registry_getWsz_ActionsContainer._registry_getcKs2|j||jD]}|j|vr||j|_qdSr1)rupdater}rr)r'rHr~r.r.r/ set_defaults]s   z_ActionsContainer.set_defaultscCs8|jD]"}|j|kr|jdur|jSq|j|dSr1)r}rrrr)r'rr~r.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, ...) rYrrz+dest supplied twice for positional argumentrNzunknown action "%s"r%r is not callablez<%r is a FileType class object, instance of it must be passedr;z,length of metavar tuple does not match nargs)rxr|r_get_positional_kwargs_get_optional_kwargsrrw_pop_action_classcallablerrrrr;rr _add_action)r'rbrHcharsr action_classr~ type_funcr.r.r/rps:         z_ActionsContainer.add_argumentcOs&t|g|Ri|}|j||Sr1)_ArgumentGrouprr")r'rbrHrr.r.r/add_argument_groups z$_ActionsContainer.add_argument_groupcKs t|fi|}|j||Sr1)_MutuallyExclusiveGrouprr")r'rHrr.r.r/add_mutually_exclusive_groups z._ActionsContainer.add_mutually_exclusive_groupcCs`|||j|||_|jD]}||j|<q"|jD]"}|j|r8|js8|jdq8|S)NT) _check_conflictr}r" containerrr~rmatchr)r'r~rr.r.r/rs      z_ActionsContainer._add_actioncCs|j|dSr1)r}removerr.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)titlervry)r)rrrrrrvryrrrrr}rr)r'rtitle_group_maprrQ group_mapr~ 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'rrHrQr.r.r/rsz(_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)optionrxzNinvalid option string %(option)r: must start with a character %(prefix_chars)rrYrz%dest= is required for options like %rr^rr) rxrrr"r|rlstripreplacer) r'rbrHrlong_option_stringsrrQrdest_option_stringr.r.r/rs.        z&_ActionsContainer._get_optional_kwargscCs|d|}|d||S)Nr~)rr)r'rHrr~r.r.r/rs z#_ActionsContainer._pop_action_classcCsDd|j}z t||WSty>td}t||jYn0dS)Nz_handle_conflict_%sz%invalid conflict_resolution value: %r)ryrrrr)r'handler_func_namerQr.r.r/r|#s    z_ActionsContainer._get_handlercCsLg}|jD]&}||jvr |j|}|||fq |rH|}|||dSr1)rr~r"r|)r'r~confl_optionalsrconfl_optionalryr.r.r/r,s   z!_ActionsContainer._check_conflictcCs6tddt|}ddd|D}t|||dS)Nzconflicting option string: %szconflicting option strings: %srcSsg|] \}}|qSr.r.)r`rr~r.r.r/rc>sz<_ActionsContainer._handle_conflict_error..)rr|r&r)r'r~conflicting_actionsr conflict_stringr.r.r/_handle_conflict_error:s z(_ActionsContainer._handle_conflict_errorcCs>|D]4\}}|j||j|d|js|j|qdSr1)rrr~rrr)r'r~rrr.r.r/_handle_conflict_resolveCs   z*_ActionsContainer._handle_conflict_resolve)N)N)r r6r7rWr{rrrrrrrrrrrrr|rrrr#r.r.r r/ros$ 5   3("   rocs6eZdZdfdd ZfddZfddZZS) rNc s|j}|d|j|d|j|d|jtt|j}|fd|i|||_g|_|j |_ |j |_ |j |_ |j |_ |j |_ |j|_dS)Nryrxrwrv)rOryrxrwrrrWrrrzr}r~rrr)r'rrrvrHr super_initr r.r/rWTs    z_ArgumentGroup.__init__cs tt||}|j||Sr1)rrrrr"rr r.r/rjs z_ArgumentGroup._add_actioncs tt|||j|dSr1)rrrrrrr r.r/rosz_ArgumentGroup._remove_action)NNr r6r7rWrrr#r.r.r r/rRs rcs.eZdZdfdd ZddZddZZS) rFcs tt||||_||_dSr1)rrrWr _container)r'rrr r.r/rWvsz _MutuallyExclusiveGroup.__init__cCs2|jrtd}t||j|}|j||S)Nz-mutually exclusive arguments must be optional)rrrrrrr")r'r~rQr.r.r/r{s   z#_MutuallyExclusiveGroup._add_actioncCs|j||j|dSr1)rrrrrr.r.r/rs z&_MutuallyExclusiveGroup._remove_action)Frr.r.r r/rtsrc 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 Nr^rbTc 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)rvrxrwryrzpositional argumentszoptional argumentscSs|Sr1r.)rgr.r.r/identitysz)ArgumentParser.__init__..identityrr^hr<rxzshow this help message and exit)r~rrx)rrrW_ospathbasenamer=argvrSrsepilogformatter_classfromfile_prefix_charsadd_help allow_abbrev exit_on_errorrr _positionals _optionals _subparsersr{rrrrrr)r'rSrsrvrparentsrrxrrwryrrr superinit add_grouprdefault_prefixr^defaultsr r.r/rWsH   zArgumentParser.__init__csgd}fdd|DS)N)rSrsrvrryrcsg|]}|t|fqSr.rrr5r.r/rcrdz.ArgumentParser._get_kwargs..r.rr.r5r/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 argumentsrGrrv subcommandsrSrerur)rrbrrOrrrrrr;_get_positional_actionsrrwrsrjrrr) r'rHrrvr]rru parsers_classr~r.r.r/add_subparserss$   zArgumentParser.add_subparserscCs$|jr|j|n |j||Sr1)rrrrrr.r.r/r s zArgumentParser._add_actioncCsdd|jDS)NcSsg|]}|jr|qSr.rr`r~r.r.r/rcsz8ArgumentParser._get_optional_actions..r}r5r.r.r/_get_optional_actionssz$ArgumentParser._get_optional_actionscCsdd|jDS)NcSsg|]}|js|qSr.rrr.r.r/rcsz:ArgumentParser._get_positional_actions..rr5r.r.r/rsz&ArgumentParser._get_positional_actionscCs4|||\}}|r0td}||d||SNzunrecognized arguments: %sr)rNrrbr&r'rbrrrQr.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||fSrX)r=rr2r r}rrrrr"rr_parse_known_argsrexc_inforbrrPrrdelattr)r'rbrr~rerrr.r.r/rN'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)NrYrr^AOrecs||||}||jurb||gD]*}|vr6td}t|}t|||q6|turx|||dS)Nznot allowed with argument %s)r _get_valuesrrrrrr)r~argument_stringsrargument_valuesconflict_actionrQ 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)NrYrrzignored explicit argument %r)_match_argumentr"rxr~rr) start_index option_tupler~r explicit_argmatch_argument action_tuples arg_countrcharnew_explicit_arg optionals_maprQstoprbrselected_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<|Sr1)_match_arguments_partialzipr|)r match_partialselected_pattern arg_countsr~rrb)r)rrr'rr.r/consume_positionalss   z=ArgumentParser._parse_known_args..consume_positionalsrrcsg|]}|kr|qSr.r.)r`r)rr.r/rcsz4ArgumentParser._parse_known_args..z(the following arguments are required: %srcSsg|]}|jturt|qSr.)rxrrrr.r.r/rc;s z#one of the arguments %s is requiredr)N)r_read_args_from_filesrrrrOriterr"_parse_optionalr&rrrErDr}rrrrrrrrr" _get_valuerbr)r'r)rrrr mutex_action conflictsarg_string_pattern_partsarg_strings_iter arg_stringrpatternrrmax_option_string_indexnext_option_string_indexpositionals_end_indexstrings stop_indexrequired_actionsr~rrrQr.) 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|Sr2) rr"rreadrconvert_arg_line_to_argsrrrfr=rrbr)r'r)new_arg_stringsr args_filearg_liner+rr.r.r/rDs   ,  z$ArgumentParser._read_args_from_filescCs|gSr1r.)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 argumentsrY) _get_nargs_patternrNrrrr rrrrr|r)r'r~r nargs_patternr nargs_errorsrQr.r.r/ras"    zArgumentParser._match_argumentcsrg}tt|ddD]X}|d|}dfdd|D}t||}|dur|dd|Dqnq|S)Nrrrecsg|]}|qSr.)r rr5r.r/rc}sz;ArgumentParser._match_arguments_partial..cSsg|] }t|qSr.)r|)r`rgr.r.r/rcrd)rr|r&rNrrru)r'rtrrr actions_slicerrr.r5r/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) NrrY=rcSsg|]\}}}|qSr.r.)r`r~rrr.r.r/rcsz2ArgumentParser._parse_optional..)rmatchesz4ambiguous option: %(option)s could match %(matches)sr) rxr~r|split_get_option_tuplesr&rrbrrr) r'rr~rr option_tuplesoptionsrbrQrr.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)NrrYr r<zunexpected option string: %s)rxrrr~rr"rbr) r'rrr option_prefixrr~rshort_option_prefixshort_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-*rrer^) rrrr rrrr&rr)r'r~rr r.r.r/r s(  z!ArgumentParser._get_nargs_patterncCs4|||\}}|r0td}||d||Sr)parse_known_intermixed_argsrrbr&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/rc4 sz>ArgumentParser.parse_known_intermixed_args..z3parse_intermixed_args: positional arg with nargs=%srcs&g|]}|jD]}|vr|jqqSr.)rr)r`rr~rr.r/rc: sz;parse_intermixed_args: positional in mutuallyExclusiveGroup)warnzDo not expect %s in %sF)rrrrrsr save_nargsrr save_defaultrNrrrwarningsrrrr save_required) r'rbra save_usager~remaining_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)NrrYcsg|]}|qSr.rr`vr~r'r.r/rc rdz.ArgumentParser._get_values..csg|]}|qSr.r"r#r%r.r/rc rdrcsg|]}|qSr.r"r#r%r.r/rc rd)rrrrrrrr rrrr _check_valuerr|r)r'r~r)r-rr$r.r%r/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)Nrrr rY)rr-z!invalid %(type)s value: %(value)r) rrrrrrrr#rr=rrr)r'r~rrrQrr,rbr.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&r{r#rr)r'r~r-rbrQr.r.r/r& s zArgumentParser._check_valuecCs$|}||j|j|j|Sr1)r;rwrsr}rrj)r'r]r.r.r/r s  zArgumentParser.format_usagecCst|}||j|j|j||j|jD]0}||j ||j| |j | q.||j |Sr1)r;rwrsr}rrqrvrrmrrrrnrrj)r'r] action_groupr.r.r/rj s        zArgumentParser.format_helpcCs|j|jdS)Nr)rrSr5r.r.r/r; szArgumentParser._get_formattercCs"|durtj}|||dSr1)r=r>r<rr'filer.r.r/ print_usage szArgumentParser.print_usagecCs"|durtj}|||dSr1)r=r>r<rjr(r.r.r/r6 szArgumentParser.print_helpcCs |r|durtj}||dSr1)r=stderrwrite)r'r r)r.r.r/r< szArgumentParser._print_messagercCs |r||tjt|dSr1)r<r=r+r7)r'statusr r.r.r/r7 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. )rSr r<z%(prog)s: error: %(message)s N)r*r=r+rSr7r)r'r rbr.r.r/rb s  zArgumentParser.error)NN)NN)NN)NN)N)N)N)rN)#r r6r7r8rrWr$rrrrrrNrrrrrrrr rrrrr&rrjr;r*r6r<r7rbr#r.r.r r/rsXB  'w:-1  M8    r)2r8 __version____all__osrrerNsysr=rrrrrrr rrrPrrr;rrr rr r Exceptionrrr rr$r'r)r-r.r0r1r4r9r?rTrr rorrrr.r.r.r/s`=   ^)#&] 78"