a _h~@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.)/opt/imh-python/lib/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_partscs6|durtd}|dur,|t|jd}n|durL|sLdt|jd}n|dur*dt|jd}g}g}|D] }|jr||qr||qr|j} | |||} ddd|| fD}|j|jt |t |kr*d} | ||} | ||} t | | }t | | }d|| ks&Jd|| ks:Jdfdd }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&s\             zHelpFormatter._format_usagec Cst}i}|D]}z||jd}Wnty>YqYq0|t|j}||||jkr|jD]}||qf|js||vr||d7<nd||<||vr||d7<nd||<nF||vr||d7<nd||<||vr||d7<nd||<t|d|D]} d || <qqg} t|D]"\} }|j t ur| d| | d krp| | n"| | dd krV| | dn|js||} ||| } ||vr| ddkr| d dkr| dd } | | nf|jd} |jdkr|} n"||} ||| }d | |f} |jsL||vrLd | } | | q4t|d dD]} || g| | | <qfddd| D}d}d}td|d|}td|d|}td||fd|}tdd|}|}|S)Nrz [[]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 \(([^|]*)\))setindex_group_actions ValueErrorr|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/rsz                     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 Cs:t|jd|j}t|j|d}||jd}||}|jsV|jd|f}d|}n@t||kr~|jd||f}d|}d}n|jd|f}d|}|}|g}|jr| |} | | |} | d|d| df| ddD]} | d|d| fqn| ds| d| |D]} | || q||S) Nr<rrez%*s%s z %*s%-*s rrYrf)rDrJrFrErGrHryrxr| _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}s8          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/rDs 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/rKs0            zHelpFormatter._format_argscCstt||jd}t|D]}||tur||=qt|D] }t||dr:||j||<q:|ddurddd|dD}||d<| ||S)Nrr rrcSsg|] }t|qSr.r)r`cr.r.r/rcprdz.HelpFormatter._expand_help..) rvarsrBr2rhasattrr rr&_get_help_string)r'r~paramsr, choices_strr.r.r/rgs   zHelpFormatter._expand_helpccs>z |j}WntyYn0||EdH|dSr1)_get_subactionsAttributeErrorr[r\)r'r~get_subactionsr.r.r/rzts   z'HelpFormatter._iter_indented_subactionscCs&|jd|}ddl}|||S)Nrr)rQrrtextwrapwrap)r'rprVrr.r.r/r~szHelpFormatter._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>  `g/  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$Fs zAction._get_kwargscCs |jdSr9rr5r.r.r/rTszAction.format_usagecCsttddS)Nz.__call__() not defined)NotImplementedErrorrr'parser namespacevaluesrr.r.r/__call__WszAction.__call__)NNNNNFNN)N)r r6r7r8rWr$rrr.r.r.r/r s5 r cs0eZdZd fdd Zd ddZddZZS) rNFc svg} |D]2} | | | drd| dd} | | q|durT|durT|d7}tj| |d||||||d dS)N----no-r<rr) rrrrrrrrxr)r" startswithsuperrW) r'rrrrrrrxr_option_stringsr __class__r.r/rW[s&    zBooleanOptionalAction.__init__cCs$||jvr t||j|d dS)Nr)rsetattrrrrr.r.r/r{s zBooleanOptionalAction.__call__cCs d|jS)Nz | )r&rr5r.r.r/rsz"BooleanOptionalAction.format_usage)NNNFNN)N)r r6r7rWrr __classcell__r.r.rr/rZs 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#rWr rr.r/rWs   z_StoreAction.__init__cCst||j|dSr1)r!rrr.r.r/rsz_StoreAction.__call__)NNNNNFNN)Nr r6r7rWrr"r.r.rr/r#sr#cs(eZdZdfdd ZdddZZS) _StoreConstActionNFc s"tt|j||d||||ddS)Nr)rrrr rrrx)rr&rWr'rrr rrrxrrr.r/rWs z_StoreConstAction.__init__cCst||j|jdSr1)r!rr rr.r.r/rsz_StoreConstAction.__call__)NFNN)Nr%r.r.rr/r&s r&cseZdZdfdd ZZS)_StoreTrueActionFNcs tt|j||d|||ddS)NTrrr rrrx)rr(rWr'rrrrrxrr.r/rWs z_StoreTrueAction.__init__)FFNr r6r7rWr"r.r.rr/r(sr(cseZdZdfdd ZZS)_StoreFalseActionTFNcs tt|j||d|||ddS)NFr))rr,rWr*rr.r/rWs z_StoreFalseAction.__init__)TFNr+r.r.rr/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-rWr rr.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.rr/r-sr-cs(eZdZdfdd ZdddZZS) _AppendConstActionNFc s$tt|j||d|||||ddS)Nr)rrrr rrrxr)rr/rWr'rr.r/rWs 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.rr/r/s r/cs(eZdZdfdd ZdddZZS) _CountActionNFcs tt|j||d|||ddS)Nr)rrrrrrx)rr0rWr*rr.r/rW"s z_CountAction.__init__cCs0t||jd}|durd}t||j|ddSNrrY)rrr!)r'rrrrcountr.r.r/r0sz_CountAction.__call__)NFN)Nr%r.r.rr/r0 s r0cs.eZdZeedffdd ZdddZZS) _HelpActionNcstt|j|||d|ddSNr)rrrrrx)rr3rW)r'rrrrxrr.r/rW9s z_HelpAction.__init__cCs||dSr1) print_helpexitrr.r.r/rEsz_HelpAction.__call__)Nr r6r7rrWrr"r.r.rr/r37s  r3cs0eZdZdeedffdd ZdddZZS)_VersionActionNz&show program's version number and exitcs$tt|j|||d|d||_dSr4)rr8rWversion)r'rr9rrrxrr.r/rWLs z_VersionAction.__init__cCsD|j}|dur|j}|}||||tj|dSr1)r9_get_formatterrq_print_messagerj_sysstdoutr6)r'rrrrr9r]r.r.r/rZs z_VersionAction.__call__)Nr7r.r.rr/r8Js r8csPeZdZGdddeZedddffdd 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,aliasesrxrrsuprr.r/rWhs  z/_SubParsersAction._ChoicesPseudoAction.__init__r+r.r.rr/r?fsr?FNc s<||_||_i|_g|_tt|j||t|j|||ddS)N)rrrrrrxr) _prog_prefix _parser_class_name_parser_map_choices_actionsrr>rWr)r'rrS parser_classrrrxrrr.r/rWps  z_SubParsersAction.__init__cKs|ddur d|j|f|d<|dd}d|vrX|d}||||}|j||jfi|}||j|<|D]}||j|<qv|S)NrSrr@r.rx)rrBrr?rEr"rCrD)r'r,kwargsr@rx choice_actionraliasr.r.r/ add_parsers     z_SubParsersAction.add_parsercCs|jSr1)rEr5r.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!rDKeyErrorr&rrparse_known_argsrr4 setdefault_UNRECOGNIZED_ARGS_ATTRrr) r'rrrrrKr)rbmsg subnamespacekeyr-r.r.r/rs$      z_SubParsersAction.__call__)N) r r6r7r r?rrWrJrrr"r.r.rr/r>dsr>c@seZdZdddZdS) _ExtendActionNcCs2t||jd}t|}||t||j|dSr1)rrr;rr!r.r.r.r/rs z_ExtendAction.__call__)N)r r6r7rr.r.r.r/rSsrSc@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 Cs|dkr>d|jvrtjSd|jvr(tjStd|j}t|zt||j|j|j|j WSt y}z*||d}td}t ||WYd}~n d}~00dS)N-rTwzargument "-" with mode %r)filenameerrorz$can't open '%(filename)s': %(error)s) rUr<stdinr=rrrrVrWrXOSErrorr)r'stringrPerbr r.r.r/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)rUrVrWrXr&rr )r'rbrGargs_strr.r.r/r0s   zFileType.__repr__)rTrNN)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'rGr,r.r.r/rW szNamespace.__init__cCst|tstSt|t|kSr1)rr NotImplementedr)r'otherr.r.r/__eq__s zNamespace.__eq__cCs ||jvSr1)r3)r'rRr.r.r/ __contains__szNamespace.__contains__N)r r6r7r8rWrirjr.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_constr2rxr9parsersrz^-\d+$|^-\d*\.\d+$)rrkrW descriptionargument_default prefix_charsconflict_handler _registriesregisterr#r&r(r,r-r/r0r3r8r>rS _get_handler_actions_option_string_actions_action_groups_mutually_exclusive_groups _defaultsrNrO_negative_number_matcher_has_negative_number_optionals)r'rrrtrsrurr.r/rWs4 z_ActionsContainer.__init__cCs|j|i}|||<dSr1)rvrN)r' registry_namer-rregistryr.r.r/rwNsz_ActionsContainer.registerNcCs|j|||Sr1)rvr)r'rr-rr.r.r/ _registry_getRsz_ActionsContainer._registry_getcKs2|j||jD]}|j|vr||j|_qdSr1)r}updateryrr)r'rGr~r.r.r/ set_defaultsXs   z_ActionsContainer.set_defaultscCs8|jD]"}|j|kr|jdur|jSq|j|dSr1)ryrrr}r)r'rr~r.r.r/ get_defaultas  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)rtr|r_get_positional_kwargs_get_optional_kwargsr}rs_pop_action_classcallablerrrrr:rr _add_action)r'rbrGcharsr action_classr~ type_funcr.r.r/rks:         z_ActionsContainer.add_argumentcOs&t|g|Ri|}|j||Sr1)_ArgumentGroupr{r")r'rbrGrr.r.r/add_argument_groups z$_ActionsContainer.add_argument_groupcKs t|fi|}|j||Sr1)_MutuallyExclusiveGroupr|r")r'rGrr.r.r/add_mutually_exclusive_groups z._ActionsContainer.add_mutually_exclusive_groupcCs`|||j|||_|jD]}||j|<q"|jD]"}|j|r8|js8|jdq8|S)NT) _check_conflictryr" containerrrzr~matchr)r'r~rr.r.r/rs      z_ActionsContainer._add_actioncCs|j|dSr1)ryremoverr.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)titlerrru)r)r{rrrrrrrurr|rrryrr)r'rtitle_group_maprrP 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'rrGrPr.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)optionrtzNinvalid option string %(option)r: must start with a character %(prefix_chars)rrYrz%dest= is required for options like %rr]rr) rtrrr"r|rlstripreplacer) r'rbrGrlong_option_stringsrrPrdest_option_stringr.r.r/rs.        z&_ActionsContainer._get_optional_kwargscCs|d|}|d||S)Nr~)rr)r'rGrr~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)rurrrr)r'handler_func_namerPr.r.r/rxs    z_ActionsContainer._get_handlercCsLg}|jD]&}||jvr |j|}|||fq |rH|}|||dSr1)rrzr"rx)r'r~confl_optionalsrconfl_optionalrur.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/rc9sz<_ActionsContainer._handle_conflict_error..)rr|r&r)r'r~conflicting_actionsr conflict_stringr.r.r/_handle_conflict_error5s z(_ActionsContainer._handle_conflict_errorcCs>|D]4\}}|j||j|d|js|j|qdSr1)rrrzrrr)r'r~rrr.r.r/_handle_conflict_resolve>s   z*_ActionsContainer._handle_conflict_resolve)N)N)r r6r7rWrwrrrrrrrrrrrrrxrrrr"r.r.rr/rks$ 5   3("   rkcs6eZdZdfdd ZfddZfddZZS) rNc s|j}|d|j|d|j|d|jtt|j}|fd|i|||_g|_|j |_ |j |_ |j |_ |j |_ |j |_ |j|_dS)Nrurtrsrr)rNrurtrsrrrWrrrvryrzr}rr|)r'rrrrrGr super_initrr.r/rWOs    z_ArgumentGroup.__init__cs tt||}|j||Sr1)rrrrr"rrr.r/res z_ArgumentGroup._add_actioncs tt|||j|dSr1)rrrrrrrr.r/rjsz_ArgumentGroup._remove_action)NNr r6r7rWrrr"r.r.rr/rMs rcs.eZdZdfdd ZddZddZZS) rFcs tt||||_||_dSr1)rrrWr _container)r'rrrr.r/rWqsz _MutuallyExclusiveGroup.__init__cCs2|jrtd}t||j|}|j||S)Nz-mutually exclusive arguments must be optional)rrrrrrr")r'r~rPr.r.r/rvs   z#_MutuallyExclusiveGroup._add_actioncCs|j||j|dSr1)rrrrrr.r.r/r~s z&_MutuallyExclusiveGroup._remove_action)Frr.r.rr/rosrc 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]r`Tc 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)rrrtrsrurzpositional argumentszoptional argumentscSs|Sr1r.)rcr.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 _subparsersrwrrrr}rr)r'rSrsrrrparentsrrtrrsrurrr superinit add_grouprdefault_prefixr^defaultsrr.r/rWsH   zArgumentParser.__init__csgd}fdd|DS)N)rSrsrrrrurcsg|]}|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 argumentsrFrrr subcommandsrSrerqr)rr`rrNrrrrrr:_get_positional_actionsr|rwrsrjrrr) r'rGrrrr]rru parsers_classr~r.r.r/add_subparserss$   zArgumentParser.add_subparserscCs$|jr|j|n |j||Sr1)rrrrrr.r.r/rs zArgumentParser._add_actioncCsdd|jDS)NcSsg|]}|jr|qSr.rr`r~r.r.r/rcsz8ArgumentParser._get_optional_actions..ryr5r.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)rMrr`r&r'rbrrrPr.r.r/ parse_argss 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 ryrrrrr!r}r_parse_known_argsrexc_infor`rrOrrdelattr)r'rbrr~rerrr.r.r/rM"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_actionrP action_name)action_conflictsr seen_actionsseen_non_default_actionsr'r.r/ take_actionxs    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|sfJ|D]\}}}|||qj| S)NrYrrzignored explicit argument %r)_match_argumentr"rtrzrr) start_index option_tupler~r explicit_argmatch_argument action_tuples arg_countrcharnew_explicit_arg optionals_maprPstoprbrselected_patterns)r)arg_strings_patternextrasoption_string_indicesr'rr.r/consume_optionalsN        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/rc6s z#one of the arguments %s is requiredr)N)r_read_args_from_filesr|rrrNriterr"_parse_optionalr&rrrErDryrrrrrrrrr! _get_valuer`r)r'r)rrrr mutex_action conflictsarg_string_pattern_partsarg_strings_iter arg_stringrpatternrrmax_option_string_indexnext_option_string_indexpositionals_end_indexstrings stop_indexrequired_actionsr~rrrPr.) rr)rrrrrrrr'rrr/rIs        J                    z ArgumentParser._parse_known_argsc Csg}|D]}|r|d|jvr*||qzxt|ddT}g}|D]}||D]}||q\qN||}||Wdn1s0YWqtyt d}| t |Yq0q|Sr1) rr"rreadrconvert_arg_line_to_argsrrrbr<rr`r)r'r)new_arg_stringsr args_filearg_liner+rr.r.r/r?s   ,  z$ArgumentParser._read_args_from_filescCs|gSr1r.)r'rr.r.r/rYsz'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_errorsrPr.r.r/r\s"    zArgumentParser._match_argumentcsrg}tt|ddD]X}|d|}dfdd|D}t||}|dur|dd|Dqnq|S)Nrrrecsg|]}|qSr.)rrr5r.r/rcxsz;ArgumentParser._match_arguments_partial..cSsg|] }t|qSr.)r|)r`rcr.r.r/rc|rd)rr|r&rNrrru)r'rtrrr actions_slicerrr.r5r/rrs  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) rtrzr|split_get_option_tuplesr&rr`r~rr) r'rr~rr option_tuplesoptionsrbrPrr.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)rtrr rzrr"r`r) r'rrr option_prefixrr~rshort_option_prefixshort_explicit_argr.r.r/r s:             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~rrr.r.r/rs(  z!ArgumentParser._get_nargs_patterncCs4|||\}}|r0td}||d||Sr)parse_known_intermixed_argsrr`r&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/rc/ sz>ArgumentParser.parse_known_intermixed_args..z3parse_intermixed_args: positional arg with nargs=%srcs&g|]}|jD]}|vr|jqqSr.)rr)r`rr~rr.r/rc5 sz;parse_intermixed_args: positional in mutuallyExclusiveGroup)warnzDo not expect %s in %sF)rrrr|rsr save_nargsrr save_defaultrMrrrwarningsrrrr 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.rrr!r.r/rc rdrcsg|]}|qSr.rrr!r.r/rc rd)rrrrrrrr rrrr _check_valuerr|r)r'r~r)r-rr r.r!r/rn 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~rrrPrr,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-rbrPr.r.r/r" s zArgumentParser._check_valuecCs$|}||j|j|j|Sr1)r:rwrsryr|rj)r'r]r.r.r/r s  zArgumentParser.format_usagecCst|}||j|j|j||j|jD]0}||j ||j| |j | q.||j |Sr1)r:rwrsryr|rqrrr{rmrrrrnrrj)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/r5 szArgumentParser.print_helpcCs |r|durtj}||dSr1)r<stderrwrite)r'r r%r.r.r/r; szArgumentParser._print_messagercCs |r||tjt|dSr1)r;r<r'r6)r'statusr r.r.r/r6 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'rSr6r)r'r rbr.r.r/r` s  zArgumentParser.error)NN)NN)NN)NN)N)N)N)rN)#r r6r7r8rrWr$rrrrrrMrrrrrrr rrrrrr"rrjr:r&r5r;r6r`r"r.r.rr/rsXB  'w:-1  M8    r)2r8 __version____all__osrrerNsysr<rrrrrrr rrrOrrr;rrr rr r Exceptionrrr rr#r&r(r,r-r/r0r3r8r>rSrr rkrrrr.r.r.r/s`=   ])#&] 78"