o w[e@sBdZdZgdZddlZddlZddlZz ddl m Z m Z Wne y/ddZ dd Z Ynwd Z d Zd Zd ZdZdZdZGdddeZddZGdddeZGdddeZGdddeZGdddeZGdddeZdd ZGd!d"d"eZGd#d$d$eZGd%d&d&eZ Gd'd(d(e Z!Gd)d*d*e Z"Gd+d,d,e Z#Gd-d.d.e#Z$Gd/d0d0e#Z%Gd1d2d2e Z&Gd3d4d4e Z'Gd5d6d6e Z(Gd7d8d8e Z)Gd9d:d:e Z*Gd;d<dd>e&Z,Gd?d@d@eZ-GdAdBdBeZ.GdCdDdDeZ/GdEdFdFe/Z0GdGdHdHe0Z1GdIdJdJee/Z2dS)Ka 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)gettextngettextcC|SN)messagerr/usr/lib/python3.10/argparse.py__rcCs|dkr|S|SNr)singularpluralnrrrrasrz ==SUPPRESS==?*+zA......_unrecognized_argsc@(eZdZdZddZddZddZdS) _AttributeHolderaAbstract base class that provides __repr__. The __repr__ method returns a string in the format:: ClassName(attr=name, attr=name, ...) The attributes are determined either by a class-level attribute, '_kwarg_names', or by inspecting the instance __dict__. cCst|j}g}i}|D] }|t|q |D]\}}|r-|d||fq|||<q|r=|dt|d|d|fS)N%s=%rz**%s%s(%s), )type__name__ _get_argsappendrepr _get_kwargs isidentifierjoin)self type_name arg_strings star_argsargnamevaluerrr__repr__}s   z_AttributeHolder.__repr__cCst|jSr)list__dict__itemsr4rrrr1z_AttributeHolder._get_kwargscCsgSrrr?rrrr.rz_AttributeHolder._get_argsN)r- __module__ __qualname____doc__r;r1r.rrrrr(ts  r(cCs6|durgSt|tur|ddSddl}||SNr)r,r<copy)r>rErrr _copy_itemss    rFc@seZdZdZ   d;ddZddZd d ZGd d d eZd dZ ddZ ddZ ddZ d)r4rirjrkrrrre zHelpFormatter._Section.__init__cCs|jdur |j|jj}|dd|jD}|jdur"|j|s&dS|jtur=|jdur=|jj}d|d|jf}nd}|d||dgS)NcSsg|]\}}||qSrr).0funcargsrrr z6HelpFormatter._Section.format_help..z%*s%s:  ) rjrirg _join_partsr>rhrkrrU)r4r3 item_helpcurrent_indentrkrrr format_helps    z"HelpFormatter._Section.format_helpr)r-rArBrerwrrrrrXs  rXcCs|jj||fdSr)rZr>r/)r4rnrorrr _add_itemszHelpFormatter._add_itemcCs0||||j|}||jg||_dSr)rgrXrZrxrw)r4rksectionrrr start_sections zHelpFormatter.start_sectioncCs|jj|_|dSr)rZrjrhr?rrr end_sections  zHelpFormatter.end_sectioncCs,|tur|dur||j|gdSdSdSr)rrx _format_text)r4textrrradd_textszHelpFormatter.add_textcCs*|tur||||f}||j|dSdSr)rrx _format_usage)r4usageactionsgroupsprefixrorrr add_usages zHelpFormatter.add_usagecCsv|jtur9|j}||g}||D] }|||qttt|}||j}t|j ||_ | |j |gdSdSr) helpr_format_action_invocation_iter_indented_subactionsr/rRmaplenrUrWrx_format_action)r4actionget_invocation invocations subactioninvocation_length action_lengthrrr add_argument s   zHelpFormatter.add_argumentcCs|D]}||qdSr)r)r4rrrrr add_argumentss zHelpFormatter.add_argumentscCs.|j}|r|jd|}|dd}|S)N rs)rYrwr_substrip)r4rrrrrw&s zHelpFormatter.format_helpcCsddd|DS)NrrcSsg|] }|r |tur|qSr)r)rmpartrrrrp.sz-HelpFormatter._join_parts..)r3)r4 part_stringsrrrrt-s zHelpFormatter._join_partscs(|durtd}|dur|t|jd}n|dur$|s$dt|jd}n|durdt|jd}g}g}|D]}|jrB||q7||q7|j} | |||} ddd|| fD}|j|jt |t |krd} | ||} | ||} t | | }t | | }d|| ksJd|| ksJdfdd }t |t |d krdt |t |d }|r||g|||}| |||n=|r||g|||}n1|g}n-dt |}||}|||}t |d krg}| |||| ||||g|}d |}d ||fS)Nzusage: r`z%(prog)s cSsg|]}|r|qSrr)rmsrrrrpNz/HelpFormatter._format_usage..z%\(.*?\)+(?=\s|$)|\[.*?\]+(?=\s|$)|\S+csg}g}|durt|d}nt|d}|D]-}|dt|kr7|r7||d|g}t|d}|||t|d7}q|rQ||d||dura|dt|d|d<|S)Nrrr)rr/r3)partsindentrlineslineline_lenr text_widthrr get_linesbs"   z.HelpFormatter._format_usage..get_linesg?rrsz%s%s r) rdictrOoption_stringsr/_format_actions_usager3rTrUrr[findallextend)r4rrrrr` optionals positionalsrformat action_usage part_regexp opt_usage pos_usage opt_parts pos_partsrrrrrrrr2s\              zHelpFormatter._format_usagec Cs t}i}|D]}|jstd|z ||jd}Wn ty&Yqwt|j}||}||||jkrd} |jD]} || | jturN| d7} q>|| } |jsy||vrc||d7<nd||<||vrt||d7<n+d||<n&| dkr||vr||d7<nd||<||vr||d 7<nd ||<t |d|D]} d || <qqg} t |D]\} } | jtur| d| | d kr| | q| | dd kr| | dq| js|| }|| |}| |vr|ddkr|d dkr|dd }| |q| jd}| jdkr| }n|| }|| |}d ||f}| js<| |vr.z[\[(]z[\])]z(%s) z\1 (%s)z%s *%srr)set_group_actions ValueErrorindexraddrrrequiredrange enumerater/getpopr#_get_default_metavar_for_positional _format_argsnargs format_usage!_get_default_metavar_for_optionalsortedr3r[rr)r4rr group_actionsinsertsgroupstartgroup_action_countendsuppressed_actions_countrexposed_actions_countirdefaultr option_string args_stringr}opencloserrrrs                      z#HelpFormatter._format_actions_usagecCsFd|vr |t|jd}t|j|jd}d|j}||||dS)Nz%(prog)r rr)rrOrRrTrU _fill_text)r4r}rrrrrr|s  zHelpFormatter._format_textc CsDt|jd|j}t|j|d}||jd}||}|js+|jd|f}d|}n t||kr?|jd||f}d|}d}n |jd|f}d|}|}|g}|jr|j r| |} | r| | |} | d|d| df| ddD] } | d|d| fqun | ds| d||D] } | || q||S) NrGrrrz%*s%s z %*s%-*s rrrs)rQrWrSrRrTrUrrrr _expand_help _split_linesr/endswithrrrt) r4r help_position help_width action_width action_headertup indent_firstr help_text help_linesrrrrrr s<           zHelpFormatter._format_actioncCs|js||}|||d\}|Sg}|jdkr!||jn||}|||}|jD] }|d||fq/d|S)Nrrrr+) rr_metavar_formatterrrrrr/r3)r4rrmetavarrrrrrrr:s      z'HelpFormatter._format_action_invocationcsP|jdur |jn|jdurdd|jD}dd|n|fdd}|S)NcSg|]}t|qSrstr)rmchoicerrrrpVrz4HelpFormatter._metavar_formatter..z{%s},csttrSf|Sr) isinstancetuple) tuple_sizeresultrrr[s  z0HelpFormatter._metavar_formatter..format)rchoicesr3)r4rdefault_metavar choice_strsrrrrrRs   z HelpFormatter._metavar_formattercCs|||}|jdurd|d}|S|jtkr d|d}|S|jtkr;|d}t|dkr5d|}|Sd|}|S|jtkrHd|d}|S|jtkrQd}|S|jtkr^d |d}|S|jtkrgd }|Sz d d t |jD}Wn t yt d dwd |||j}|S)N%srrrGz [%s [%s ...]]z[%s ...]z %s [%s ...]r%z%s ...rrcSsg|]}dqS)rr)rmrrrrrpxsz.HelpFormatter._format_args..zinvalid nargs valuer) rrrrrr rrrr TypeErrorrr3)r4rr get_metavarrrformatsrrrrbsB            zHelpFormatter._format_argscCstt||jd}t|D] }||tur||=q t|D]}t||dr-||j||<q|ddurEddd|dD}||d<| ||S)Nrr-rr+cSrrrrmcrrrrprz.HelpFormatter._expand_help..) rvarsrOr<rhasattrr-rr3_get_help_string)r4rparamsr9 choices_strrrrr~s   zHelpFormatter._expand_helpccsBz|j}Wn tyYdSw||EdH|dSr)_get_subactionsAttributeErrorrgrh)r4rget_subactionsrrrrs    z'HelpFormatter._iter_indented_subactionscCs&|jd|}ddl}|||S)Nrr)r^rrtextwrapwrap)r4r}rcrrrrrs zHelpFormatter._split_linescCs,|jd|}ddl}|j||||dS)Nrr)initial_indentsubsequent_indent)r^rrrfill)r4r}rcrrrrrrs zHelpFormatter._fill_textcC|jSr)rr4rrrrrzHelpFormatter._get_help_stringcCs |jSr)destupperr rrrr z/HelpFormatter._get_default_metavar_for_optionalcCr r)r r rrrrr z1HelpFormatter._get_default_metavar_for_positional)rGrHNr) r-rArBrCrergrhobjectrXrxrzr{r~rrrrwrtrrr|rrrrrrrrrrrrrrrrs@ " `q0   rc@eZdZdZddZdS)rzHelp message formatter which retains any formatting in descriptions. Only the name of this class is considered a public API. All the methods provided by the class are considered an implementation detail. cs dfdd|jddDS)Nrrc3s|]}|VqdSrr)rmrrrr sz9RawDescriptionHelpFormatter._fill_text..T)keepends)r3 splitlines)r4r}rcrrrrrs z&RawDescriptionHelpFormatter._fill_textN)r-rArBrCrrrrrr rc@r)r zHelp message formatter which retains formatting of all help text. Only the name of this class is considered a public API. All the methods provided by the class are considered an implementation detail. cCs|Sr)r)r4r}rcrrrrz!RawTextHelpFormatter._split_linesN)r-rArBrCrrrrrr rr c@r)rzHelp message formatter which adds default values to argument help. Only the name of this class is considered a public API. All the methods provided by the class are considered an implementation detail. cCs>|j}d|jvr|jturttg}|js|j|vr|d7}|S)Nz %(default) (default: %(default)s))rrrrrrr)r4rrdefaulting_nargsrrrrs  z.ArgumentDefaultsHelpFormatter._get_help_stringN)r-rArBrCrrrrrrrrc@ eZdZdZddZddZdS)r a Help message formatter which uses the argument 'type' as the default metavar value (instead of the argument 'dest') Only the name of this class is considered a public API. All the methods provided by the class are considered an implementation detail. cC|jjSrr,r-r rrrrrz:MetavarTypeHelpFormatter._get_default_metavar_for_optionalcCrrrr rrrrrz.rr4namesrr?rr1]s zAction._get_kwargscCs |jdSrDrr?rrrrlrzAction.format_usagecCs ttd)Nz.__call__() not defined)NotImplementedErrorrr4parser namespacevaluesrrrr__call__o zAction.__call__NNNNNFNNr)r-rArBrCrer1rr5rrrrr s5 r cs<eZdZ      d fdd Zd ddZddZZS) rNFc s~g} |D]} | | | drd| dd} | | q|dur.|dur.|tur.|d7}tj| |d||||||d dS)N----no-rGrr) rr rrr,rrrr)r/ startswithrsuperre) r4rr rr,rrrr_option_stringsr __class__rrress(     zBooleanOptionalAction.__init__cCs(||jvrt||j|d dSdS)Nr9)rsetattrr r:r1rrrr5s zBooleanOptionalAction.__call__cCs d|jS)Nz | )r3rr?rrrrr6z"BooleanOptionalAction.format_usage)NNNFNNr)r-rArBrer5r __classcell__rrr=rrrs rc8eZdZ        dfdd ZdddZZS) _StoreActionNFc T|dkrtd|dur|tkrtdttt|j||||||||| | d dS)Nrznargs for store actions must be != 0; if you have nothing to store, actions such as store true or store const may be more appropriate nargs must be %r to supply constr$)rrr;rBrer&r=rrre     z_StoreAction.__init__cCst||j|dSr)r?r r1rrrr5z_StoreAction.__call__r7rr-rArBrer5r@rrr=rrBrBc0eZdZ    dfdd ZdddZZS) _StoreConstActionNFc s"tt|j||d||||ddS)Nr)rr rr%rrr)r;rJrer4rr r%rrrrr=rrres  z_StoreConstAction.__init__cCst||j|jdSr)r?r r%r1rrrr5sz_StoreConstAction.__call__NFNNrrGrrr=rrJsrJcs$eZdZ   dfdd ZZS)_StoreTrueActionFNc tt|j||d|||ddS)NTrr r%rrr)r;rMrer4rr rrrr=rrre  z_StoreTrueAction.__init__)FFNr-rArBrer@rrr=rrM rMcs$eZdZ   dfdd ZZS)_StoreFalseActionTFNcrN)NFrO)r;rTrerPr=rrrerQz_StoreFalseAction.__init__)TFNrRrrr=rrTrSrTcrA) _AppendActionNFc rC)Nrznargs for append actions must be != 0; if arg strings are not supplying the value to append, the append const action may be more appropriaterDr$)rrr;rUrer&r=rrrerEz_AppendAction.__init__cC2t||jd}t|}||t||j|dSr)r*r rFr/r?r4r2r3r4rr>rrrr5 z_AppendAction.__call__r7rrGrrr=rrUrHrUcrI) _AppendConstActionNFc s$tt|j||d|||||ddS)Nr)rr rr%rrrr)r;rYrerKr=rrres  z_AppendConstAction.__init__cCs4t||jd}t|}||jt||j|dSr)r*r rFr/r%r?rWrrrr51s z_AppendConstAction.__call__rLrrGrrr=rrYsrYcs.eZdZ   dfdd ZdddZZS) _CountActionNFcrN)Nr)rr rrrr)r;rZrerPr=rrre:rQz_CountAction.__init__cCs0t||jd}|dur d}t||j|ddSNrr)r*r r?)r4r2r3r4rcountrrrr5Hsz_CountAction.__call__)NFNrrGrrr=rrZ8s rZcs.eZdZeedffdd ZdddZZS) _HelpActionNcstt|j|||d|ddSNr)rr rrr)r;r]re)r4rr rrr=rrreQs  z_HelpAction.__init__cCs||dSr) print_helpexitr1rrrr5]s z_HelpAction.__call__rr-rArBrrer5r@rrr=rr]Os  r]cs0eZdZdeedffdd ZdddZZS)_VersionActionNz&show program's version number and exitcs$tt|j|||d|d||_dSr^)r;rbreversion)r4rrcr rrr=rrreds  z_VersionAction.__init__cCsD|j}|dur |j}|}||||tj|dSr)rc_get_formatterr~_print_messagerw_sysstdoutr`)r4r2r3r4rrcrirrrr5rs  z_VersionAction.__call__rrarrr=rrbbsrbcsPeZdZGdddeZedddffdd ZddZd d Zd d d Z Z S)_SubParsersActioncseZdZfddZZS)z&_SubParsersAction._ChoicesPseudoActioncs@|}}|r|dd|7}ttj|}|jg|||ddS)Nrr+)rr rr)r3r;rh_ChoicesPseudoActionre)r4r9aliasesrrr supr=rrres   z/_SubParsersAction._ChoicesPseudoAction.__init__rRrrr=rri~sriFNc s<||_||_i|_g|_tt|j||t|j|||ddS)N)rr rrrrr) _prog_prefix _parser_class_name_parser_map_choices_actionsr;rhrer)r4rr` parser_classr rrrr=rrres   z_SubParsersAction.__init__cKs|ddurd|j|f|d<|dd}d|vr,|d}||||}|j||jdi|}||j|<|D]}||j|<q;|S)Nr`rrjrr)rrlrriror/rmrn)r4r9kwargsrjr choice_actionr2aliasrrr add_parsers     z_SubParsersAction.add_parsercCs|jSr)ror?rrrrr z!_SubParsersAction._get_subactionsc Cs|d}|dd}|jturt||j|z|j|}Wnty8|d|jd}td|}t||w||d\} }t |  D] \} } t|| | qG|rft | t gt |t |dSdS)Nrrr+) parser_namerz5unknown parser %(parser_name)r (choices: %(choices)s))r rr?rnKeyErrorr3rrparse_known_argsrr> setdefault_UNRECOGNIZED_ARGS_ATTRr*r) r4r2r3r4rrur6romsg subnamespacekeyr:rrrr5s(       z_SubParsersAction.__call__r) r-rArBr rirrertrr5r@rrr=rrh|srhc@seZdZdddZdS) _ExtendActionNcCrVr)r*r rFrr?rWrrrr5rXz_ExtendAction.__call__r)r-rArBr5rrrrr}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)r4modebufsizeencodingerrorsrrrrerlzFileType.__init__c s|dkr7djvrdjvrtjjStjStfdddDr,djvr)tjjStjStdj}t|z t|jj j j WSt y_}z||d}td }t ||d}~ww) N-r~bc3s|]}|jvVqdSr)rrr?rrrsz$FileType.__call__..waxzargument "-" with mode %r)filenameerrorz$can't open '%(filename)s': %(error)s)rrfstdinbufferanyrgrrrrrrOSErrorr)r4stringrzerorrr?rr5s"   zFileType.__call__cCsT|j|jf}d|jfd|jfg}ddd|Ddd|D}dt|j|fS)Nrrr+cSsg|] }|dkrt|qS)r)r0)rmr8rrrrpsz%FileType.__repr__..cSs$g|]\}}|durd||fqS)Nr)r)rmkwr8rrrrps  r*)rrrrr3r,r-)r4rorqargs_strrrrr;s  zFileType.__repr__)r~rNN)r-rArBrCrer5r;rrrrrs   rc@r') r zSimple object for storing attributes. Implements equality by attribute names and values, and provides a simple string representation. cKs|D] }t||||qdSr)r?)r4rqr9rrrre"szNamespace.__init__cCst|tstSt|t|kSr)rr NotImplementedr)r4otherrrr__eq__&s zNamespace.__eq__cCs ||jvSr)r=)r4r|rrr __contains__+rzNamespace.__contains__N)r-rArBrCrerrrrrrr s  r cseZdZfddZddZd&ddZdd Zd d Zd d ZddZ ddZ ddZ ddZ ddZ ddZddZd&ddZddZd d!Zd"d#Zd$d%ZZS)'_ActionsContainercstt|||_||_||_||_i|_|ddt |ddt |ddt |ddt |ddt |ddt |ddt|ddt|dd t|dd t|dd t|dd t|g|_i|_g|_g|_i|_td |_g|_dS)Nrstore store_const store_true store_falser/ append_constr\rrcparsersrz^-\d+$|^-\d*\.\d+$)r;rre descriptionargument_default prefix_charsconflict_handler _registriesregisterrBrJrMrTrUrYrZr]rbrhr} _get_handler_actions_option_string_actions_action_groups_mutually_exclusive_groups _defaultsr[r\_negative_number_matcher_has_negative_number_optionals)r4rrrrr=rrre1s4  z_ActionsContainer.__init__cCs|j|i}|||<dSr)rrx)r4 registry_namer:rregistryrrrrfs z_ActionsContainer.registerNcCs|j|||Sr)rr)r4rr:rrrr _registry_getjrFz_ActionsContainer._registry_getcKs2|j||jD] }|j|vr||j|_q dSr)rupdaterr r)r4rqrrrr set_defaultsps    z_ActionsContainer.set_defaultscCs8|jD]}|j|kr|jdur|jSq|j|dSr)rr rrr)r4r rrrr get_defaultys  z_ActionsContainer.get_defaultcOsF|j}|rt|dkr&|dd|vr&|rd|vrtd|j|i|}n|j|i|}d|vrM|d}||jvrC|j||d<n |jdurM|j|d<||}t|s]td|f|d i|}| d|j |j }t|sxtd |f|t urtd |ft |d rz | |dWn tytd w||S)z add_argument(dest, ..., name=value, ...) add_argument(option_string, option_string, ..., name=value, ...) rrr z+dest supplied twice for positional argumentrNzunknown action "%s"r,%r is not callablez<%r is a FileType class object, instance of it must be passedrdz,length of metavar tuple does not match nargsr)rrr_get_positional_kwargs_get_optional_kwargsrr_pop_action_classcallablerr,rrrdrr _add_action)r4rorqcharsr  action_classr type_funcrrrrs<         z_ActionsContainer.add_argumentcOs&t|g|Ri|}|j||Sr)_ArgumentGrouprr/)r4rorqrrrradd_argument_groups z$_ActionsContainer.add_argument_groupcKs t|fi|}|j||Sr)_MutuallyExclusiveGrouprr/)r4rqrrrradd_mutually_exclusive_group z._ActionsContainer.add_mutually_exclusive_groupcCs`|||j|||_|jD]}||j|<q|jD]}|j|r-|js-|jdq|S)NT) _check_conflictrr/ containerrrrmatchr)r4rrrrrrs       z_ActionsContainer._add_actioncCs|j|dSr)rremover rrr_remove_actionsz _ActionsContainer._remove_actioncCsi}|jD]}|j|vrtd}t||j|||j<qi}|jD]"}|j|vr7|j|j|j|jd||j<|jD] }||j||<q:q"|jD]}|j |j d}|jD]}|||<qTqH|j D] }| || |q_dS)Nz.cannot merge actions - two groups are named %r)titlerr)r)rrrrrrrrrrrrrr)r4rtitle_group_maprrz group_mapr mutex_grouprrr_add_container_actionss6          z(_ActionsContainer._add_container_actionscKs^d|vr td}t||dttfvrd|d<|dtkr(d|vr(d|d<t||gdS)Nrz1'required' is an invalid argument for positionalsrTrr r)rrrrrr)r4r rqrzrrrrsz(_ActionsContainer._get_positional_kwargsc Osg}g}|D]0}|d|jvr||jd}td}t||||t|dkr6|d|jvr6||q|dd}|durd|rH|d}n|d}||j}|s^td}t|||dd}t|||d S) Nr)optionrzNinvalid option string %(option)r: must start with a character %(prefix_chars)rrr z%dest= is required for options like %rrrr) rrrr/rrlstripreplacer) r4rorqrlong_option_stringsrrzr dest_option_stringrrrrs0        z&_ActionsContainer._get_optional_kwargscCs|d|}|d||S)Nr)rr)r4rqrrrrrr2s z#_ActionsContainer._pop_action_classcCs<d|j}zt||WStytd}t||jw)Nz_handle_conflict_%sz%invalid conflict_resolution value: %r)rr*rrr)r4handler_func_namerzrrrr6s   z_ActionsContainer._get_handlercCsPg}|jD]}||jvr|j|}|||fq|r&|}|||dSdSr)rrr/r)r4rconfl_optionalsrconfl_optionalrrrrr?s   z!_ActionsContainer._check_conflictcCs2tddt|}ddd|D}t|||)Nzconflicting option string: %szconflicting option strings: %sr+cSsg|]\}}|qSrr)rmrrrrrrpQsz<_ActionsContainer._handle_conflict_error..)rrr3r)r4rconflicting_actionsrconflict_stringrrr_handle_conflict_errorMs z(_ActionsContainer._handle_conflict_errorcCs>|D]\}}|j||j|d|js|j|qdSr)rrrrrr)r4rrrrrr_handle_conflict_resolveVs   z*_ActionsContainer._handle_conflict_resolver)r-rArBrerrrrrrrrrrrrrrrrrr@rrr=rr/s& 5   3( "  rcs6eZdZdfdd ZfddZfddZZS) rNc s|j}|d|j|d|j|d|jtt|j}|dd|i|||_g|_|j |_ |j |_ |j |_ |j |_ |j |_ |j|_dS)Nrrrrr)rxrrrr;rrerrrrrrrr)r4rrrrqr super_initr=rrregs     z_ArgumentGroup.__init__cs tt||}|j||Sr)r;rrrr/r r=rrr}rz_ArgumentGroup._add_actioncs tt|||j|dSr)r;rrrrr r=rrrsz_ArgumentGroup._remove_actionNNr-rArBrerrr@rrr=rres rcs.eZdZdfdd ZddZddZZS) rFcs tt||||_||_dSr)r;rrer _container)r4rrr=rrres z _MutuallyExclusiveGroup.__init__cCs2|jr td}t||j|}|j||S)Nz-mutually exclusive arguments must be optional)rrrrrrr/)r4rrzrrrrs   z#_MutuallyExclusiveGroup._add_actioncCs|j||j|dSr)rrrrr rrrrs z&_MutuallyExclusiveGroup._remove_action)Frrrr=rrsrc s,eZdZdZddddgedddddddf fdd Zdd Zd d Zd d ZddZ ddZ dAddZ dAddZ ddZ ddZddZddZddZd d!Zd"d#Zd$d%ZdAd&d'ZdAd(d)Zd*d+Zd,d-Zd.d/Zd0d1Zd2d3Zd4d5ZdBd6d7ZdBd8d9ZdBd:d;ZdCd=d>Z d?d@Z!Z"S)DraKObject for parsing command line strings into Python objects. Keyword Arguments: - prog -- The name of the program (default: ``os.path.basename(sys.argv[0])``) - usage -- A usage message (default: auto-generated from arguments) - description -- A description of what the program does - epilog -- Text following the argument descriptions - parents -- Parsers whose arguments should be copied into this one - formatter_class -- HelpFormatter class for printing help messages - prefix_chars -- Characters that prefix optional arguments - fromfile_prefix_chars -- Characters that prefix files containing additional arguments - argument_default -- The default value for all arguments - conflict_handler -- String indicating how to handle conflicts - add_help -- Add a -h/-help option - allow_abbrev -- Allow long options to be abbreviated unambiguously - exit_on_error -- Determines whether or not ArgumentParser exits with error info when an error occurs NrrTc s$tt|j}|||| | d|durtjtjd}||_||_ ||_ ||_ ||_ | |_ | |_| |_|j}|td|_|td|_d|_dd}|dd|d|vrXdn|d}|j rq|j|d |d d d ttd d |D]}||z|j}Wn tyYqsw|j|qsdS)N)rrrrrzpositional argumentsoptionscSrrr)rrrridentityrz)ArgumentParser.__init__..identityr,rhrGrzshow this help message and exit)rrr)r;rre_ospathbasenamerfargvr`repilogformatter_classfromfile_prefix_charsadd_help allow_abbrev exit_on_errorrr _positionals _optionals _subparsersrrrrrrr)r4r`rrrparentsrrrrrrrr superinit add_grouprdefault_prefixrjdefaultsr=rrresL    zArgumentParser.__init__cr')N)r`rrrrrcr(rr)r+r?rrrpr,z.ArgumentParser._get_kwargs..rr-rr?rr1szArgumentParser._get_kwargsc Ks|jdur |td|dt|d|vsd|vr4t|dd}t|dd}||||_n|j|_|ddur[| }| }|j }| |j ||d||d<||d}|d d gi|}|j||S) Nz(cannot have multiple subparser argumentsrprr subcommandsr`rrrrr)rrrrxr,rrrrrd_get_positional_actionsrrrrwrrr) r4rqrrrirr parsers_classrrrradd_subparserss$   zArgumentParser.add_subparserscCs&|jr |j||S|j||Sr)rrrrr rrrr s   zArgumentParser._add_actioncCdd|jDS)NcSsg|]}|jr|qSrr/rmrrrrrp( z8ArgumentParser._get_optional_actions..rr?rrr_get_optional_actions'z$ArgumentParser._get_optional_actionscCr)NcSsg|]}|js|qSrr/rrrrrp-rz:ArgumentParser._get_positional_actions..rr?rrrr,rz&ArgumentParser._get_positional_actionscC4|||\}}|rtd}||d||SNzunrecognized arguments: %sr)rwrrr3r4ror3rrzrrr parse_args4 zArgumentParser.parse_argscCs|dur tjdd}nt|}|durt}|jD]}|jtur4t||js4|jtur4t ||j|jq|j D]}t||sHt |||j |q8|j rmz | ||\}}Wnt yltd}|t|Yn w| ||\}}t|tr|t|tt|t||fSr)rfrr<r rr rrrr?rr_parse_known_argsrexc_inforrryrr*delattr)r4ror3rr errrrrrw;s6          zArgumentParser.parse_known_argscs jdur i jD])}|j}t|jD]\}}|g}||d||||ddqqig}t} t| D]/\}} | dkr]|d| D]} |dqTqE | } | durid} n| |<d} || qEd |t t d fdd  fd d }  fd d }gd rt }nd} |krt fddD} |kr| }| kr| q| vr |}|| |  |ks| }|dg} jD]=}|vr9|jr|t|q|jdur9t|jtr9t|jr9|jt|jur9t|j ||jq|rI tdd | jD]+}|jrv|jD] }|vr^nqUdd|jD}td} |d |qLfS)Nrr8rAOrrcs|||}||jur1||gD]}|vr0td}t|}t|||q|tur>|||dSdS)Nznot allowed with argument %s)r _get_valuesrrrr!rr)rargument_stringsrargument_valuesconflict_actionrz action_name)action_conflictsr3 seen_actionsseen_non_default_actionsr4rr take_actions    z5ArgumentParser._parse_known_args..take_actioncs|}|\}}}j}g} |dur||dS|dur||d}j}|dkrk|d|vrk|dkrk||g|f|d} | |d}|ddpQd} j} || vr`| |}| }nLtd} t|| ||dkr|d} |g}||||fn.td} t|| ||d}|d}|||}||} || }||||fnq|sJ|D] \}}}|||q| S)NTrrrrrzignored explicit argument %r)_match_argumentr/rrrr) start_index option_tuplerr explicit_argmatch_argument action_tuples arg_countrcharnew_explicit_arg optionals_maprzstoprorselected_patterns)r6arg_strings_patternextrasoption_string_indicesr4rrrconsume_optionalsV       =z:ArgumentParser._parse_known_args..consume_optionalcsnj}|d}||}t|D]\}}|||}||7}||qt|ddd<|Sr)_match_arguments_partialzipr)r match_partialselected_pattern arg_countsrrro)r6rrr4rrrconsume_positionalss   z=ArgumentParser._parse_known_args..consume_positionalsrrcsg|]}|kr|qSrr)rmr)rrrrps z4ArgumentParser._parse_known_args..z(the following arguments are required: %sr+cSsg|] }|jturt|qSr)rrr!rrrrrpSs  z#one of the arguments %s is requiredrr)r_read_args_from_filesrrrrxriterr/_parse_optionalr3rrrRrQrrr!rrrrr r*r? _get_valuerr)r4r6r3rrr mutex_action conflictsarg_string_pattern_partsarg_strings_iter arg_stringrpatternrr$max_option_string_indexnext_option_string_indexpositionals_end_indexstrings stop_indexrequired_actionsrrr.rzr) r r6rrr3rrr r r4rrrrbs         N              z ArgumentParser._parse_known_argsc Csg}|D]c}|r|d|jvr||qz.cSrr)r)rmrrrrrpr)rrr3r[rrr)r4rrrr actions_slicer.rrr?rrs  z'ArgumentParser._match_arguments_partialc Cs |sdS|d|jvr dS||jvr|j|}||dfSt|dkr$dSd|vr?|dd\}}||jvr?|j|}|||fS||}t|dkreddd|D}||d}td}|||n t|dkrp|\} | S|j |r{|j s{dSd |vrdSd|dfS) Nrr=r+cSsg|]\}}}|qSrr)rmrrrrrrrpsz2ArgumentParser._parse_optional..)rmatchesz4ambiguous option: %(option)s could match %(matches)sr) rrrsplit_get_option_tuplesr3rrrrr) r4r-rrr option_tuplesrrorzrrrrr's>              zArgumentParser._parse_optionalc Cs0g}|j}|d|vrA|d|vrA|jr?d|vr!|dd\}}n|}d}|jD]}||r>|j|}|||f}||q(|S|d|vr|d|vr|}d}|dd}|dd} |jD]*}||krv|j|}||| f}||q`||r|j|}|||f}||q`|S|td||S)Nrrr>rGzunexpected option string: %s)rrr@rr:r/rr) r4rrr option_prefixrrrshort_option_prefixshort_explicit_argrrrrAsB               z!ArgumentParser._get_option_tuplescCs|j}|dur d}n3|tkrd}n,|tkrd}n%|tkrd}n|tkr&d}n|tkr-d}n|tkr4d}n dd d |}|jrL| d d }| d d }|S) Nz(-*A-*)z(-*A?-*)z (-*[A-]*)z (-*A[A-]*)z([-AO]*)z (-*A[-AO]*)z(-*-*)z(-*%s-*)z-*rrrr) rrrr rrrr3rr)r4rrr;rrrr: s(  z!ArgumentParser._get_nargs_patterncCrr)parse_known_intermixed_argsrrr3rrrrparse_intermixed_args7 rz$ArgumentParser.parse_intermixed_argsc s|ddD}|rtd|djfdd|jDr$tdz|j}z`|jdur7|dd|_D]}|j|_t|_|j|_ t|_q9| ||\}}D]%}t ||j ryt ||j gkryddlm}|d |j |ft||j qTWD] }|j|_|j |_q}nD] }|j|_|j |_qw|}z6|D] }|j|_d |_q|jD] } | j| _d | _q| ||\}} W|D]}|j|_q|jD]} | j| _qn|D]}|j|_q|jD]} | j| _qwW||_|| fS||_w) NcSsg|] }|jttfvr|qSr)rrrrrrrrpL sz>ArgumentParser.parse_known_intermixed_args..z3parse_intermixed_args: positional arg with nargs=%srcs&g|]}|jD] }|vr|jqqSr)rr )rmrrrrrrpR s  z;parse_intermixed_args: positional in mutuallyExclusiveGroup)warnzDo not expect %s in %sF)rrrrrr save_nargsrr save_defaultrwrr r*warningsrJrrr save_required) r4ror3a save_usagerremaining_argsrJrrrrrHrrF> sx                 z*ArgumentParser.parse_known_intermixed_argscsjttfvrz|dWn tyYnw|s<jtkr<jr&j}nj}t |t r: |} ||S|sYjt krYjsYjdurOj}n|} ||St|dkrwjdtfvrw|\} |} ||Sjtkrfdd|D}|Sjtkrfdd|D} |d|Sjtkrt}|Sfdd|D}|D]} |q|S)Nr8rcg|]}|qSrr(rmvrr4rrrp rqz.ArgumentParser._get_values..crRrrSrTrVrrrp rqrcrRrrSrTrVrrrp rq)rrrrrrrr%rrrr( _check_valuerrr)r4rr6r:r-rUrrVrr sR    &        zArgumentParser._get_valuesc Cs|d|j|j}t|std}t|||z||}W|Sty=t|jdt|j}tt d}t||t t fy_t|jdt|j}||d}td}t|||w)Nr,rr-r)r,r:z!invalid %(type)s value: %(value)r) rr,rrrrr*r0rrfrrr)r4rr-rrzrr9rorrrr( s"    zArgumentParser._get_valuecCsJ|jdur!||jvr#|dtt|jd}td}t|||dSdS)Nr+)r:rz3invalid choice: %(value)r (choose from %(choices)s))rr3rr0rr)r4rr:rorzrrrrW szArgumentParser._check_valuecCs$|}||j|j|j|Sr)rdrrrrrw)r4rirrrr s  zArgumentParser.format_usagecCst|}||j|j|j||j|jD]}||j ||j| |j | q||j |Sr)rdrrrrr~rrrzrrrr{rrw)r4ri action_grouprrrrw s        zArgumentParser.format_helpcCs|j|jdS)Nr)rr`r?rrrrd r@zArgumentParser._get_formattercC"|durtj}|||dSr)rfrgrerr4filerrr print_usage zArgumentParser.print_usagecCrYr)rfrgrerwrZrrrr_ r]zArgumentParser.print_helpcCs$|r|dur tj}||dSdSr)rfstderrwrite)r4rr[rrrre s zArgumentParser._print_messagercCs |r ||tjt|dSr)rerfr^r`)r4statusrrrrr` 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. )r`rrGz%(prog)s: error: %(message)s N)r\rfr^r`r`r)r4rrorrrr# s zArgumentParser.errorrr)rN)#r-rArBrCrrer1rrrrrrwrr%r6rrr'rAr:rGrFrr(rWrrwrdr\r_rer`rr@rrr=rrsZB  '{:- 1 M8     r)3rC __version____all__osrrer[sysrfrrrrNrrrr rrryrr(rFrrr rr r! Exceptionrrr rrBrJrMrTrUrYrZr]rbrhr}rr rrrrrrrrsl=      ^)#&] 78"