a xa~@sBdZdZgdZddlZddlZddlZzddl m Z m Z Wn"e y^ddZ dd Z Yn0d 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)gettextngettextcCs|SN)messagerr/usr/lib/python3.9/argparse.py__srcCs|dkr |S|SdSNr)ZsingularZpluralnrrrrasrz ==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_argsargnamevaluerrr__repr__}s   z_AttributeHolder.__repr__cCst|jSr)list__dict__itemsr.rrrr+sz_AttributeHolder._get_kwargscCsgSrrr7rrrr(sz_AttributeHolder._get_argsN)r' __module__ __qualname____doc__r3r+r(rrrrr"tsr"cCs6|dur gSt|tur$|ddSddl}||SNr)r&r4copy)r6r<rrr _copy_itemss   r=c@seZdZdZd;ddZddZd d ZGd d d eZd dZ ddZ ddZ ddZ dzddl}|j}|d8}Wnty<d}Yn0||_||_t|t|d|d|_||_ d|_ d|_ d|_ | |d|_|j|_tdtj|_td|_dS)Nrr>Fz\s+z\n\n\n+)Zshutilget_terminal_sizecolumns ImportError_prog_indent_incrementminmax_max_help_position_width_current_indent_level_action_max_length_Section _root_section_current_section_recompileASCII_whitespace_matcher_long_break_matcher)r.progZindent_incrementZmax_help_positionwidthZ_shutilrrr__init__s(    zHelpFormatter.__init__cCs"|j|j7_|jd7_dSrrKrFrLr7rrr_indentszHelpFormatter._indentcCs4|j|j8_|jdks"Jd|jd8_dS)NrzIndent decreased below 0.rrYr7rrr_dedentszHelpFormatter._dedentc@seZdZdddZddZdS)zHelpFormatter._SectionNcCs||_||_||_g|_dSr) formatterparentheadingr6)r.r\r]r^rrrrXszHelpFormatter._Section.__init__cCs|jdur|j|jj}|dd|jD}|jdurD|j|sLdS|jturz|jdurz|jj}d|d|jf}nd}|d||dgS)NcSsg|]\}}||qSrr).0funcargsrrr z6HelpFormatter._Section.format_help..z%*s%s:  ) r]r\rZ _join_partsr6r[r^rrK)r.r-Z item_helpZcurrent_indentr^rrr format_helps    z"HelpFormatter._Section.format_help)N)r'r8r9rXrgrrrrrNs rNcCs|jj||fdSr)rPr6r))r.r`rarrr _add_itemszHelpFormatter._add_itemcCs0||||j|}||jg||_dSr)rZrNrPrhrg)r.r^Zsectionrrr start_sectionszHelpFormatter.start_sectioncCs|jj|_|dSr)rPr]r[r7rrr end_sections zHelpFormatter.end_sectioncCs$|tur |dur ||j|gdSr)rrh _format_text)r.textrrradd_textszHelpFormatter.add_textcCs&|tur"||||f}||j|dSr)rrh _format_usage)r.usageactionsgroupsprefixrarrr add_usages zHelpFormatter.add_usagecCsr|jturn|j}||g}||D]}|||q$ttt|}||j}t|j ||_ | |j |gdSr) helpr_format_action_invocation_iter_indented_subactionsr)rHmaplenrKrMrh_format_action)r.actionZget_invocationZ invocations subactionZinvocation_lengthZ action_lengthrrr add_argument s   zHelpFormatter.add_argumentcCs|D]}||qdSr)r|)r.rprzrrr add_argumentsszHelpFormatter.add_argumentscCs.|j}|r*|jd|}|dd}|S)N re)rOrgrUsubstrip)r.rtrrrrg&s  zHelpFormatter.format_helpcCsddd|DS)NrdcSsg|]}|r|tur|qSr)r)r_partrrrrb.s z-HelpFormatter._join_parts..)r-)r.Z part_stringsrrrrf-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: rVz%(prog)s cSsg|] }|r|qSrr)r_srrrrbNrcz/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)Nrrr)rxr)r-)partsindentrrlineslineZline_lenr text_widthrr get_linesbs"   z.HelpFormatter._format_usage..get_linesg?rrez%s%s )N) rdictrEoption_stringsr)_format_actions_usager-rJrKrxrQfindallextend)r.rorprqrrrV optionals positionalsrzformatZ action_usageZ part_regexpZ opt_usageZ pos_usageZ opt_partsZ pos_partsrrrrrrrrn2s\             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 (()r|%s %s[%s]T)reversercSsg|]}|dur|qSrr)r_itemrrrrbrcz7HelpFormatter._format_actions_usage..z[\[(]z[\])]z(%s) z\1 (%s)z%s *%srdz \(([^|]*)\))setindex_group_actions ValueErrorrxaddrequiredrange enumeratertrr)getpopr#_get_default_metavar_for_positional _format_argsnargs format_usage!_get_default_metavar_for_optionalsortedr-rQrr)r.rprq group_actionsZinsertsgroupstartendrzirdefaultr option_string args_stringrlopencloserrrrsz                     z#HelpFormatter._format_actions_usagecCsFd|vr|t|jd}t|j|jd}d|j}||||dS)Nz%(prog)r rr~)rrErHrJrK _fill_text)r.rlrrrrrrks  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>rrdz%*s%s z %*s%-*s rrre)rGrMrIrHrJrKrurtrx _expand_help _split_linesr)endswithrvryrf) r.rzZ help_positionZ help_widthZ action_widthZ action_headertupZ indent_firstrZ help_textZ help_linesrr{rrrrys8          zHelpFormatter._format_actioncCs|js&||}|||d\}|Sg}|jdkrB||jn4||}|||}|jD]}|d||fq^d|SdS)Nrrrr%) rr_metavar_formatterrrrrr)r-)r.rzrmetavarrrrrrrru/s     z'HelpFormatter._format_action_invocationcsP|jdur|jn.|jdur.z{%s},csttrSf|SdSr) isinstancetuple)Z tuple_sizeresultrrrPs z0HelpFormatter._metavar_formatter..format)rchoicesr-)r.rzdefault_metavarZ choice_strsrrrrrGs   z HelpFormatter._metavar_formattercCs |||}|jdur$d|d}n|jtkrz [%s [%s ...]]z[%s ...]z %s [%s ...]r!z%s ...rdcSsg|]}dqS)rr)r_rrrrrbmrcz.HelpFormatter._format_args..zinvalid nargs valuer) rrrrrxr rrrr TypeErrorrr-)r.rzrZ get_metavarrrZformatsrrrrWs0            zHelpFormatter._format_argscCstt||jd}t|D]}||tur||=qt|D] }t||dr:||j||<q:|ddurddd|dD}||d<| ||S)Nrr'rr%cSsg|] }t|qSrr)r_crrrrb|rcz.HelpFormatter._expand_help..) rvarsrEr4rhasattrr'rr-_get_help_string)r.rzparamsr1Z choices_strrrrrss   zHelpFormatter._expand_helpccs>z |j}WntyYn0||EdH|dSr)_get_subactionsAttributeErrorrZr[)r.rzZget_subactionsrrrrvs   z'HelpFormatter._iter_indented_subactionscCs&|jd|}ddl}|||S)Nrr)rTrrtextwrapZwrap)r.rlrWrrrrrszHelpFormatter._split_linescCs,|jd|}ddl}|j||||dS)Nrr)Zinitial_indentZsubsequent_indent)rTrrrZfill)r.rlrWrrrrrrs zHelpFormatter._fill_textcCs|jSr)rtr.rzrrrrszHelpFormatter._get_help_stringcCs |jSr)destupperrrrrrsz/HelpFormatter._get_default_metavar_for_optionalcCs|jSr)rrrrrrsz1HelpFormatter._get_default_metavar_for_positional)r>r?N)N) r'r8r9r:rXrZr[objectrNrhrirjrmrsr|r}rgrfrnrrkryrurrrrvrrrrrrrrrrs> " `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)Nrdc3s|]}|VqdSrr)r_rrrr rcz9RawDescriptionHelpFormatter._fill_text..T)keepends)r- splitlines)r.rlrWrrrrrsz&RawDescriptionHelpFormatter._fill_textN)r'r8r9r:rrrrrrsrc@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.rlrWrrrrsz!RawTextHelpFormatter._split_linesN)r'r8r9r:rrrrrr 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))rtrrrrrr)r.rzrtZdefaulting_nargsrrrrs  z.ArgumentDefaultsHelpFormatter._get_help_stringN)r'r8r9r:rrrrrrsrc@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|jjSrr&r'rrrrrsz:MetavarTypeHelpFormatter._get_default_metavar_for_optionalcCs|jjSrrrrrrrsz.rr.namesrr7rr+Ps zAction._get_kwargscCs |jdSr;rr7rrrr^szAction.format_usagecCsttddS)Nz.__call__() not defined)NotImplementedErrorrr.parser namespacevaluesrrrr__call__aszAction.__call__)NNNNNFNN)N)r'r8r9r:rXr+rrrrrrr s5 r cs0eZdZd fdd Zd ddZddZZS) rNFc s~g} |D]2} | | | drd| dd} | | q|dur\|dur\|d|d7}tj| |d||||||d dS)N----no-r>z (default: rr) rrrrr&rrrtr)r) startswithsuperrX) r.rrrr&rrrtrZ_option_stringsr __class__rrrXes&    zBooleanOptionalAction.__init__cCs$||jvr t||j|d dS)Nr)rsetattrrrrrrrrs zBooleanOptionalAction.__call__cCs d|jS)Nz | )r-rr7rrrrsz"BooleanOptionalAction.format_usage)NNNFNN)N)r'r8r9rXrr __classcell__rrrrrds 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)rrrrrXrrrrrXs   z_StoreAction.__init__cCst||j|dSr)rrrrrrrsz_StoreAction.__call__)NNNNNFNN)Nr'r8r9rXrrrrrrrsrcs(eZdZdfdd ZdddZZS) _StoreConstActionNFc s"tt|j||d||||ddS)Nr)rrrrrrrt)rrrXr.rrrrrrtrrrrrXs z_StoreConstAction.__init__cCst||j|jdSr)rrrrrrrrsz_StoreConstAction.__call__)NFNN)Nrrrrrrs rcseZdZdfdd ZZS)_StoreTrueActionFNcs tt|j||d|||ddS)NTrrrrrrt)rrrXr.rrrrrtrrrrXs z_StoreTrueAction.__init__)FFNr'r8r9rXrrrrrrsrcseZdZdfdd ZZS)_StoreFalseActionTFNcs tt|j||d|||ddS)NFr)rrrXrrrrrXs z_StoreFalseAction.__init__)TFNrrrrrrsrcs(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)rrrrrXrrrrrXs   z_AppendAction.__init__cCs2t||jd}t|}||t||j|dSr)rrr=r)rr.rrrrr6rrrrs z_AppendAction.__call__)NNNNNFNN)Nrrrrrrsrcs(eZdZdfdd ZdddZZS) _AppendConstActionNFc s$tt|j||d|||||ddS)Nr)rrrrrrrtr)rrrXrrrrrXs z_AppendConstAction.__init__cCs4t||jd}t|}||jt||j|dSr)rrr=r)rrrrrrr#s z_AppendConstAction.__call__)NFNN)Nrrrrrrs rcs(eZdZdfdd ZdddZZS) _CountActionNFcs tt|j||d|||ddS)Nr)rrrrrrt)rrrXrrrrrX,s z_CountAction.__init__cCs0t||jd}|durd}t||j|ddSNrr)rrr)r.rrrrcountrrrr:sz_CountAction.__call__)NFN)Nrrrrrr*s rcs.eZdZeedffdd ZdddZZS) _HelpActionNcstt|j|||d|ddSNr)rrrrrt)rr rX)r.rrrrtrrrrXCs z_HelpAction.__init__cCs||dSr) print_helpexitrrrrrOsz_HelpAction.__call__)Nr'r8r9rrXrrrrrrr As  r cs0eZdZdeedffdd ZdddZZS)_VersionActionNz&show program's version number and exitcs$tt|j|||d|d||_dSr )rrrXversion)r.rrrrrtrrrrXVs z_VersionAction.__init__cCsD|j}|dur|j}|}||||tj|dSr)r_get_formatterrm_print_messagerg_sysstdoutr )r.rrrrrr\rrrrds z_VersionAction.__call__)Nr rrrrrTs rcsPeZdZGdddeZedddffdd ZddZd d Zd d d Z Z S)_SubParsersActioncseZdZfddZZS)z&_SubParsersAction._ChoicesPseudoActioncs@|}}|r|dd|7}ttj|}|jg|||ddS)Nrr%)rrrtr)r-rr_ChoicesPseudoActionrX)r.r1aliasesrtrrZsuprrrrXrs  z/_SubParsersAction._ChoicesPseudoAction.__init__rrrrrrpsrFNc s<||_||_i|_g|_tt|j||t|j|||ddS)N)rrrrrrtr) _prog_prefix _parser_class_name_parser_map_choices_actionsrrrXr)r.rrV parser_classrrrtrrrrrXzs  z_SubParsersAction.__init__cKs|ddur d|j|f|d<|dd}d|vrX|d}||||}|j||jfi|}||j|<|D]}||j|<qv|S)NrVrrrrt)rrrrrr)rr)r.r1kwargsrrtZ choice_actionraliasrrr add_parsers     z_SubParsersAction.add_parsercCs|jSr)rr7rrrrsz!_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)Nrrr%) parser_namerz5unknown parser %(parser_name)r (choices: %(choices)s))rrrrKeyErrorr-rrparse_known_argsrr6 setdefault_UNRECOGNIZED_ARGS_ATTRrr) r.rrrrrr/ramsgZ subnamespacekeyr2rrrrs$      z_SubParsersAction.__call__)N) r'r8r9r rrrXrrrrrrrrrnsrc@seZdZdddZdS) _ExtendActionNcCs2t||jd}t|}||t||j|dSr)rrr=rrrrrrrs z_ExtendAction.__call__)N)r'r8r9rrrrrr&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.modebufsizeencodingerrorsrrrrXszFileType.__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$erarrrrrs   zFileType.__call__cCsT|j|jf}d|jfd|jfg}ddd|Ddd|D}dt|j|fS)Nr.r/r%cSsg|]}|dkrt|qS)r)r*)r_r0rrrrbrcz%FileType.__repr__..cSs$g|]\}}|durd||fqS)Nr#r)r_kwr0rrrrbs r$)r(r)r*r+r-r&r')r.rarZargs_strrrrr3s   zFileType.__repr__)r'rNN)r'r8r9r:rXrr3rrrrrs 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.rr1rrrrXszNamespace.__init__cCst|tstSt|t|kSr)rr NotImplementedr)r.otherrrr__eq__s zNamespace.__eq__cCs ||jvSr)r5)r.r%rrr __contains__szNamespace.__contains__N)r'r8r9r:rXr;r<rrrrr 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)NrzZstoreZ store_const store_trueZ store_falser)Z append_constrrtrparsersrz^-\d+$|^-\d*\.\d+$)rr=rX descriptionargument_default prefix_charsconflict_handler _registriesregisterrrrrrrrr rrr& _get_handler_actions_option_string_actions_action_groups_mutually_exclusive_groups _defaultsrQrR_negative_number_matcher_has_negative_number_optionals)r.r@rBrArCrrrrX#s4 z_ActionsContainer.__init__cCs|j|i}|||<dSr)rDr")r. registry_namer2rregistryrrrrEXsz_ActionsContainer.registerNcCs|j|||Sr)rDr)r.rNr2rrrr _registry_get\sz_ActionsContainer._registry_getcKs2|j||jD]}|j|vr||j|_qdSr)rKupdaterGrr)r.rrzrrr set_defaultsbs   z_ActionsContainer.set_defaultscCs8|jD]"}|j|kr|jdur|jSq|j|dSr)rGrrrKr)r.rrzrrr get_defaultks  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, ...) rrrz+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)rBrxr_get_positional_kwargs_get_optional_kwargsrKrA_pop_action_classcallablerPr&rrrrr _add_action)r.rarcharsrZ action_classrz type_funcrrrr|us:         z_ActionsContainer.add_argumentcOs&t|g|Ri|}|j||Sr)_ArgumentGrouprIr))r.rarrrrradd_argument_groups z$_ActionsContainer.add_argument_groupcKs t|fi|}|j||Sr)_MutuallyExclusiveGrouprJr))r.rrrrradd_mutually_exclusive_groups z._ActionsContainer.add_mutually_exclusive_groupcCs`|||j|||_|jD]}||j|<q"|jD]"}|j|r8|js8|jdq8|S)NT) _check_conflictrGr) containerrrHrLmatchrM)r.rzrrrrrYs      z_ActionsContainer._add_actioncCs|j|dSr)rGremoverrrr_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)titler@rC)r)rIrerrr]r@rCrrJr_rrGrrY)r.raZtitle_group_maprr$Z group_maprz mutex_grouprrr_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$rrrrUsz(_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)optionrBzNinvalid option string %(option)r: must start with a character %(prefix_chars)rrrz%dest= is required for options like %rr0rrh) rBrrr)rxrlstripreplacer) r.rarrZlong_option_stringsrr$rZdest_option_stringrrrrVs.        z&_ActionsContainer._get_optional_kwargscCs|d|}|d||S)Nrz)rrP)r.rrrzrrrrW$s z#_ActionsContainer._pop_action_classcCsDd|j}z t||WSty>td}t||jYn0dS)Nz_handle_conflict_%sz%invalid conflict_resolution value: %r)rCrrrr)r.Zhandler_func_namer$rrrrF(s    z_ActionsContainer._get_handlercCsLg}|jD]&}||jvr |j|}|||fq |rH|}|||dSr)rrHr)rF)r.rzZconfl_optionalsrZconfl_optionalrCrrrr`1s   z!_ActionsContainer._check_conflictcCs6tddt|}ddd|D}t|||dS)Nzconflicting option string: %szconflicting option strings: %sr%cSsg|] \}}|qSrr)r_rrzrrrrbCsz<_ActionsContainer._handle_conflict_error..)rrxr-r)r.rzconflicting_actionsrZconflict_stringrrr_handle_conflict_error?s z(_ActionsContainer._handle_conflict_errorcCs>|D]4\}}|j||j|d|js|j|qdSr)rrcrHrrard)r.rzrlrrrr_handle_conflict_resolveHs   z*_ActionsContainer._handle_conflict_resolve)N)N)r'r8r9rXrErPrRrSr|r]r_rYrdrgrUrVrWrFr`rmrnrrrrrr=!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)NrCrBrAr@)r"rCrBrArr\rXrerrDrGrHrKrMrJ)r.rarer@rrQZ super_initrrrrXYs    z_ArgumentGroup.__init__cs tt||}|j||Sr)rr\rYrr)rrrrrYos z_ArgumentGroup._add_actioncs tt|||j|dSr)rr\rdrrcrrrrrdtsz_ArgumentGroup._remove_action)NNr'r8r9rXrYrdrrrrrr\Ws r\cs.eZdZdfdd ZddZddZZS) r^Fcs tt||||_||_dSr)rr^rXr _container)r.rarrrrrX{sz _MutuallyExclusiveGroup.__init__cCs2|jrtd}t||j|}|j||S)Nz-mutually exclusive arguments must be optional)rrrrprYrr))r.rzr$rrrrYs   z#_MutuallyExclusiveGroup._add_actioncCs|j||j|dSr)rprdrrcrrrrrds z&_MutuallyExclusiveGroup._remove_action)Frorrrrr^ysr^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 Nr0r3Tc 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)r@rBrArCrzpositional argumentszoptional argumentscSs|Srr)r6rrridentitysz)ArgumentParser.__init__..identityr&r0hr>rtzshow this help message and exit)rzrrt)rrrX_ospathbasenamerargvrVroepilogformatter_classfromfile_prefix_charsadd_help allow_abbrev exit_on_errorr]r _positionals _optionals _subparsersrEr|rrgrKrrQ)r.rVror@rwparentsrxrBryrArCrzr{r|Z superinitZ add_grouprqZdefault_prefixr]defaultsrrrrXsH   zArgumentParser.__init__csgd}fdd|DS)N)rVror@rxrCrzcsg|]}|t|fqSrrrr7rrrbrcz.ArgumentParser._get_kwargs..rrrr7rr+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 argumentsrrer@Z subcommandsrVrdr?r)rr3rr"r&rr]r}rr_get_positional_actionsrJrsrorgrrWrY) r.rrer@r\rrqZ parsers_classrzrrradd_subparserss$   zArgumentParser.add_subparserscCs$|jr|j|n |j||Sr)rr~rYr}rrrrrYs zArgumentParser._add_actioncCsdd|jDS)NcSsg|]}|jr|qSrrr_rzrrrrbsz8ArgumentParser._get_optional_actions..rGr7rrr_get_optional_actionssz$ArgumentParser._get_optional_actionscCsdd|jDS)NcSsg|]}|js|qSrrrrrrrbsz:ArgumentParser._get_positional_actions..rr7rrrrsz&ArgumentParser._get_positional_actionscCs4|||\}}|r0td}||d||SNzunrecognized arguments: %sr)r!rr3r-r.rarrvr$rrr 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||fSr)rrvr4r rGrrrrrrKr|_parse_known_argsrexc_infor3rr#rrdelattr)r.rarrzrerrrrrr!,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)Nrrr0AOrdcs||||}||jurb||gD]*}|vr6td}t|}t|||q6|turx|||dS)Nznot allowed with argument %s)r _get_valuesrrrrrr)rzZargument_stringsrZargument_valuesZconflict_actionr$Z action_name)action_conflictsr seen_actionsseen_non_default_actionsr.rr take_actions    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)Nrrrzignored explicit argument %r)_match_argumentr)rBrHrr) start_index option_tuplerzr explicit_argZmatch_argumentZ action_tuples arg_countrZcharZnew_explicit_argZ optionals_mapr$stoprarZselected_patterns)r/arg_strings_patternextrasoption_string_indicesr.rrrconsume_optionalsN        z:ArgumentParser._parse_known_args..consume_optionalcsnj}|d}||}t|D]*\}}|||}||7}||q&t|ddd<|Sr)_match_arguments_partialziprx)rZ match_partialZselected_patternZ arg_countsrzrra)r/rrr.rrrconsume_positionalss   z=ArgumentParser._parse_known_args..consume_positionalsrrcsg|]}|kr|qSrr)r_r)rrrrbsz4ArgumentParser._parse_known_args..z(the following arguments are required: %sr%cSsg|]}|jturt|qSr)rtrrrrrrrb@s z#one of the arguments %s is requiredr)N)ry_read_args_from_filesrJrrr"riterr)_parse_optionalr-rrrHrGrGrrrrrrrrr _get_valuer3r)r.r/rrfrrZ mutex_actionZ conflictsZarg_string_pattern_partsZarg_strings_iter arg_stringrpatternrrZmax_option_string_indexZnext_option_string_indexZpositionals_end_indexZstringsZ stop_indexZrequired_actionsrzrrr$r) rr/rrrrrrrr.rrrrSs        J                    z ArgumentParser._parse_known_argsc Csg}|D]}|r|d|jvr*||qzxt|ddT}g}|D]}||D]}||q\qN||}||Wdn1s0YWqtyt d}| t |Yq0q|Sr) ryr)rreadrconvert_arg_line_to_argsrrr5rrr3r)r.r/Znew_arg_stringsrZ args_filearg_liner0rrrrrIs   ,  z$ArgumentParser._read_args_from_filescCs|gSrr)r.rrrrrcsz'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 argumentsr) _get_nargs_patternrQrbrrr rrrrrxr)r.rzr nargs_patternrbZ nargs_errorsr$rrrrfs"    zArgumentParser._match_argumentcsrg}tt|ddD]X}|d|}dfdd|D}t||}|dur|dd|Dqnq|S)Nrrrdcsg|]}|qSr)rrr7rrrbsz;ArgumentParser._match_arguments_partial..cSsg|] }t|qSr)rx)r_r6rrrrbrc)rrxr-rQrbrrq)r.rprrrZ actions_slicerrbrr7rr|s  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) Nrr=r%cSsg|]\}}}|qSrr)r_rzrrrrrrbsz2ArgumentParser._parse_optional..)riZmatchesz4ambiguous option: %(option)s could match %(matches)sr) rBrHrxsplit_get_option_tuplesr-rr3rLrbrM) r.rrzrrZ option_tuplesZoptionsrar$rrrrrs>              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)Nrrrr>zunexpected option string: %s)rBr{rrHrr)r3r) r.rrrZZ option_prefixrrzrZshort_option_prefixZshort_explicit_argrrrrs:             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-*rrdr0) rrrr rrrr-rrk)r.rzrrrrrrs(  z!ArgumentParser._get_nargs_patterncCs4|||\}}|r0td}||d||Sr)parse_known_intermixed_argsrr3r-rrrrparse_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)rrrrrrrrb9 sz>ArgumentParser.parse_known_intermixed_args..z3parse_intermixed_args: positional arg with nargs=%srcs&g|]}|jD]}|vr|jqqSr)rr)r_rrzrrrrb? sz;parse_intermixed_args: positional in mutuallyExclusiveGroup)warnzDo not expect %s in %sF)rrrrJrorZ save_nargsrrZ save_defaultr!rrrwarningsrrrrZ save_required) r.raraZ save_usagerzZremaining_argsrrrrrrrr+ 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)Nrrcsg|]}|qSrrr_vrzr.rrrb rcz.ArgumentParser._get_values..csg|]}|qSrrrrrrrb rcrcsg|]}|qSrrrrrrrb rc)rrrrcrrrrrrrr _check_valuerrxr)r.rzr/r2rrrrrrx 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)Nr&rTr'r)r&r2z!invalid %(type)s value: %(value)r) rPr&rXrrrrr*rrrrr)r.rzrr[r$rr1rarrrr s    zArgumentParser._get_valuecCsF|jdurB||jvrB|dtt|jd}td}t|||dS)Nr%)r2rz3invalid choice: %(value)r (choose from %(choices)s))rr-rwr*rr)r.rzr2rar$rrrr s zArgumentParser._check_valuecCs$|}||j|j|j|Sr)rrsrorGrJrg)r.r\rrrr s  zArgumentParser.format_usagecCst|}||j|j|j||j|jD]0}||j ||j| |j | q.||j |Sr)rrsrorGrJrmr@rIrirer}rrjrwrg)r.r\Z action_grouprrrrg s        zArgumentParser.format_helpcCs|j|jdS)Nr)rxrVr7rrrr szArgumentParser._get_formattercCs"|durtj}|||dSr)rrrrr.filerrr print_usage szArgumentParser.print_usagecCs"|durtj}|||dSr)rrrrgrrrrr  szArgumentParser.print_helpcCs |r|durtj}||dSr)rstderrwrite)r.rrrrrr szArgumentParser._print_messagercCs |r||tjt|dSr)rrrr )r.Zstatusrrrrr  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. )rVrr>z%(prog)s: error: %(message)s N)rrrrVr r)r.rrarrrr3 s  zArgumentParser.error)NN)NN)NN)NN)N)N)N)rN)#r'r8r9r:rrXr+rrYrrrr!rrrrrrrrrrrrrrrgrrr rr r3rrrrrrsXB  'w:-1  M8    r)3r: __version____all__osrsrerQsysrrrrrDrrrr rrr#rr"r=rrr rr r Exceptionrrr rrrrrrrrr rrr&rr r=r\r^rrrrrsh=     ])#&] 78"