a |_}@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+$/usr/local/lib/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_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: 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&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 (()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 \(([^|]*)\))setindex_group_actions ValueErrorrtaddrequiredrange 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,rsz                     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 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) Nr9rr`z%*s%s z %*s%-*s rrTra)rArGrCrBrDrErqrprt _expand_help _split_linesr!endswithrrrurb) r&rvZ help_positionZ help_widthZ action_widthZ action_headertupZ indent_firstrZ help_textZ help_linesrrwr+r+r,rus8          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,rDs 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_metavarrrformatsr+r+r,rKs0            zHelpFormatter._format_argscCstt||jd}t|D]}||tur||=qt|D] }t||dr:||j||<q:|ddurddd|dD}||d<| ||S)Nr~rrrcSsg|] }t|qSr+r)r[cr+r+r,r^pr_z.HelpFormatter._expand_help..) rvarsr?r/rhasattrrrr%_get_help_string)r&rvparamsr)Z choices_strr+r+r,rgs   zHelpFormatter._expand_helpccs>z |j}WntyYn0||EdH|dSr.)_get_subactionsAttributeErrorrVrW)r&rvZget_subactionsr+r+r,rrts   z'HelpFormatter._iter_indented_subactionscCs&|jd|}ddl}|||S)Nrr)rNr{r|textwrapZwrap)r&rhrQrr+r+r,r~szHelpFormatter._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>  `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)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)z (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#Ds zAction._get_kwargscCs |jdSr6rr2r+r+r,rRszAction.format_usagecCsttddS)Nz.__call__() not defined)NotImplementedErrorrr&parser namespacevaluesrr+r+r,__call__UszAction.__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\|d|d7}tj| |d||||||d dS)N----no-r9z (default: rr) rrrrrrrrpr)r! startswithsuperrR) r&rrrrrrrprZ_option_stringsr __class__r+r,rRYs&    zBooleanOptionalAction.__init__cCs$||jvr t||j|d dS)Nr)rsetattrrrrr+r+r,rys zBooleanOptionalAction.__call__cCs d|jS)Nz | )r%rr2r+r+r,r}sz"BooleanOptionalAction.format_usage)NNNFNN)N)rr3r4rRrr __classcell__r+r+rr,rXs 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,rRs z_AppendConstAction.__init__cCs4t||jd}t|}||jt||j|dSr.)rrr8r!rrrr+r+r,rs z_AppendConstAction.__call__)NFNN)Nrr+r+rr,rs 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,r.sz_CountAction.__call__)NFN)Nrr+r+rr,rs rcs.eZdZeedffdd ZdddZZS) _HelpActionNcstt|j|||d|ddSNr)rrrrrp)rr rR)r&rrrrprr+r,rR7s z_HelpAction.__init__cCs||dSr.) print_helpexitrr+r+r,rCsz_HelpAction.__call__)Nrr3r4rrRrrr+r+rr,r 5s  r cs0eZdZdeedffdd ZdddZZS)_VersionActionNz&show program's version number and exitcs$tt|j|||d|d||_dSr )rrrRversion)r&rrrrrprr+r,rRJs z_VersionAction.__init__cCsD|j}|dur|j}|}||||tj|dSr.)r_get_formatterri_print_messagerc_sysstdoutr )r&rrrrrrXr+r+r,rXs z_VersionAction.__call__)Nr r+r+rr,rHs 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)aliasesrprrsuprr+r,rRfs  z/_SubParsersAction._ChoicesPseudoAction.__init__rr+r+rr,rdsrFNc s<||_||_i|_g|_tt|j||t|j|||ddS)N)rrrrrrpr) _prog_prefix _parser_class_name_parser_map_choices_actionsrrrRr)r&rrP parser_classrrrprrr+r,rRns  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 rrrRrrrrr+r+rr,rbsrc@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 Cs|dkr>d|jvrtjSd|jvr(tjStd|j}t|zt||j|j|j|j WSt y}z*||d}td}t ||WYd}~n d}~00dS)N-r(wzargument "-" with mode %r)filenameerrorz$can't open '%(filename)s': %(error)s) r)rstdinrrrrr*r+r,OSErrorr)r&stringr%er]rr+r+r,rs   zFileType.__call__cCsT|j|jf}d|jfd|jfg}ddd|Ddd|D}dt|j|fS)Nr/r0rcSsg|]}|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)NrvstoreZ store_const store_trueZ store_falser!Z append_constrrprparsersrz^-\d+$|^-\d*\.\d+$)rr>rR 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&rBrDrCrErr+r,rRs4 z_ActionsContainer.__init__cCs|j|i}|||<dSr.)rFr#)r& registry_namer*rregistryr+r+r,rGLsz_ActionsContainer.registerNcCs|j|||Sr.)rFr)r&rPr*rr+r+r, _registry_getPsz_ActionsContainer._registry_getcKs2|j||jD]}|j|vr||j|_qdSr.)rMupdaterIrr)r&rrvr+r+r, set_defaultsVs   z_ActionsContainer.set_defaultscCs8|jD]"}|j|kr|jdur|jSq|j|dSr.)rIrrrMr)r&rrvr+r+r, get_default_s  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)rDrtr_get_positional_kwargs_get_optional_kwargsrMrC_pop_action_classcallablerRrrrrrr _add_action)r&r]rcharsrZ action_classrv type_funcr+r+r,rxis:         z_ActionsContainer.add_argumentcOs&t|g|Ri|}|j||Sr.)_ArgumentGrouprKr!)r&r]rrr+r+r,add_argument_groups z$_ActionsContainer.add_argument_groupcKs t|fi|}|j||Sr.)_MutuallyExclusiveGrouprLr!)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_conflictrIr! containerrrJrNmatchrO)r&rvrr+r+r,r[s      z_ActionsContainer._add_actioncCs|j|dSr.)rIremoverr+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)titlerBrE)r)rKrgrrr_rBrErrLrarrIrr[)r&rcZtitle_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,rWsz(_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)optionrDzNinvalid option string %(option)r: must start with a character %(prefix_chars)rrTrz%dest= is required for options like %rr1rrj) rDrrr!rtrlstripreplacer) r&r]rrZlong_option_stringsrr%rZdest_option_stringr+r+r,rXs.        z&_ActionsContainer._get_optional_kwargscCs|d|}|d||S)Nrv)rrR)r&rrrvr+r+r,rYs z#_ActionsContainer._pop_action_classcCsDd|j}z t||WSty>td}t||jYn0dS)Nz_handle_conflict_%sz%invalid conflict_resolution value: %r)rErrrr)r&Zhandler_func_namer%r+r+r,rHs    z_ActionsContainer._get_handlercCsLg}|jD]&}||jvr |j|}|||fq |rH|}|||dSr.)rrJr!rH)r&rvZconfl_optionalsrZconfl_optionalrEr+r+r,rb%s   z!_ActionsContainer._check_conflictcCs6tddt|}ddd|D}t|||dS)Nzconflicting option string: %szconflicting option strings: %srcSsg|] \}}|qSr+r+)r[rrvr+r+r,r^7sz<_ActionsContainer._handle_conflict_error..)rrtr%r)r&rvconflicting_actionsrZconflict_stringr+r+r,_handle_conflict_error3s z(_ActionsContainer._handle_conflict_errorcCs>|D]4\}}|j||j|d|js|j|qdSr.)rrerJrrcrf)r&rvrnrr+r+r,_handle_conflict_resolve<s   z*_ActionsContainer._handle_conflict_resolve)N)N)rr3r4rRrGrRrTrUrxr_rar[rfrirWrXrYrHrbrorprr+r+rr,r>s$ 5   3("   r>cs6eZdZdfdd ZfddZfddZZS) r^Nc s|j}|d|j|d|j|d|jtt|j}|fd|i|||_g|_|j |_ |j |_ |j |_ |j |_ |j |_ |j|_dS)NrErDrCrB)r#rErDrCrr^rRrgrrFrIrJrMrOrL)r&rcrgrBrrSZ super_initrr+r,rRMs    z_ArgumentGroup.__init__cs tt||}|j||Sr.)rr^r[rr!rrr+r,r[cs z_ArgumentGroup._add_actioncs tt|||j|dSr.)rr^rfrrerrr+r,rfhsz_ArgumentGroup._remove_action)NNrr3r4rRr[rfrr+r+rr,r^Ks r^cs.eZdZdfdd ZddZddZZS) r`Fcs tt||||_||_dSr.)rr`rRr _container)r&rcrrr+r,rRosz _MutuallyExclusiveGroup.__init__cCs2|jrtd}t||j|}|j||S)Nz-mutually exclusive arguments must be optional)rrrrrr[rr!)r&rvr%r+r+r,r[ts   z#_MutuallyExclusiveGroup._add_actioncCs|j||j|dSr.)rrrfrrerr+r+r,rf|s z&_MutuallyExclusiveGroup._remove_action)Frqr+r+rr,r`msr`c 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 Nr1r4Tc 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)rBrDrCrErzpositional argumentszoptional argumentscSs|Sr.r+)r7r+r+r,identitysz)ArgumentParser.__init__..identityrr1hr9rpzshow this help message and exit)rvrrp)rrrR_ospathbasenamerargvrPrkepilogformatter_classfromfile_prefix_charsadd_help allow_abbrev exit_on_errorr_r _positionals _optionals _subparsersrGrxrrirMrrS)r&rPrkrBryparentsrzrDr{rCrEr|r}r~Z superinitZ add_grouprsZdefault_prefixrYdefaultsrr+r,rRsH   zArgumentParser.__init__csgd}fdd|DS)N)rPrkrBrzrEr|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 argumentsrrgrBZ subcommandsrPr`rAr)rr4rr#rrr_rrr_get_positional_actionsrLrorkrcr|rYr[) r&rrgrBrXrrmZ 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..rIr2r+r+r,_get_optional_actions sz$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"rr4r%r&r]rrxr%r+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||fSrS)rrxr/r rIrrrrrrMr~_parse_known_argsrexc_infor4rr$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)NrTrr1AOr`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_actionvs    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)NrTrrzignored explicit argument %r)_match_argumentr!rDrJrr) 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_optionalsN        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^4s z#one of the arguments %s is requiredr)N)r{_read_args_from_filesrLrrr#riterr!_parse_optionalr%rrrBrArIrrrrrrrrr _get_valuer4r)r&r'rrhrrZ 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,rGs        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_argsrrr6rrr4r)r&r'Znew_arg_stringsrZ args_filearg_liner(rr+r+r,r=s   ,  z$ArgumentParser._read_args_from_filescCs|gSr.r+)r&rr+r+r,rWsz'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_patternrKrdrrr rrrrrtr)r&rvr nargs_patternrdZ nargs_errorsr%r+r+r,rZs"    zArgumentParser._match_argumentcsrg}tt|ddD]X}|d|}dfdd|D}t||}|dur|dd|Dqnq|S)Nrrr`csg|]}|qSr+)rrr2r+r,r^vsz;ArgumentParser._match_arguments_partial..cSsg|] }t|qSr+)rt)r[r7r+r+r,r^zr_)rrtr%rKrdrrm)r&rlrrrZ actions_slicerrdr+r2r,rps  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..)rkZmatchesz4ambiguous option: %(option)s could match %(matches)sr) rDrJrtsplit_get_option_tuplesr%rr4rNrdrO) r&rrvrrZ option_tuplesoptionsr]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)rDr}rrJrr!r4r) 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`r1) rrrr rrrr%rrm)r&rvrrr+r+r,rs(  z!ArgumentParser._get_nargs_patterncCs4|||\}}|r0td}||d||Sr)parse_known_intermixed_argsrr4r%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^- sz>ArgumentParser.parse_known_intermixed_args..z3parse_intermixed_args: positional arg with nargs=%srcs&g|]}|jD]}|vr|jqqSr+)rr)r[rrvrr+r,r^3 sz;parse_intermixed_args: positional in mutuallyExclusiveGroup)warnzDo not expect %s in %sF)rrrrLrkrZ 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_)rrrrerrrrrrrr _check_valuerrtr)r&rvr'r*rrr+rr,rl 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)NrrVrrT)rr*z!invalid %(type)s value: %(value)r) rRrrZrrrrr"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.)rrorkrIrLrc)r&rXr+r+r,r s  zArgumentParser.format_usagecCst|}||j|j|j||j|jD]0}||j ||j| |j | q.||j |Sr.)rrorkrIrLrirBrKrergryrrfryrc)r&rXZ action_groupr+r+r,rc s        zArgumentParser.format_helpcCs|j|jdS)Nr~)rzrPr2r+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&statusrr+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)rrrrPr r)r&rr]r+r+r,r4 s  zArgumentParser.error)NN)NN)NN)NN)N)N)N)rN)#rr3r4r5rrRr#rr[rrrr"rrrrrrrrrrrrrrrcrrr rr r4rr+r+rr,rsXB  'w:-1  M8    r)2r5 __version____all__osrurerKsysrrrrrrrr rrr$rrr8rrr rr r Exceptionrrr rrrrrrrrr rrr'rr r>r^r`rr+r+r+r,s`=    ])#&] 78"