B Y6dt@sdZdZdddddddd d d d d dddddgZddlZddlZddlZddl m Z m Z dZ dZ dZdZdZdZdZGdddeZdd ZGd!ddeZGd"ddeZGd#d d eZGd$ddeZGd%d d eZd&d'ZGd(ddeZGd)ddeZGd*d d eZGd+d,d,eZ Gd-d.d.eZ!Gd/d0d0e!Z"Gd1d2d2e!Z#Gd3d4d4eZ$Gd5d6d6eZ%Gd7d8d8eZ&Gd9d:d:eZ'Gd;d<dd>eZ)Gd?ddeZ*Gd@d d eZ+GdAdBdBeZ,GdCdDdDe,Z-GdEdFdFe-Z.GdGddee,Z/dS)Ha 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.1ArgumentParser ArgumentErrorArgumentTypeErrorFileType HelpFormatterArgumentDefaultsHelpFormatterRawDescriptionHelpFormatterRawTextHelpFormatterMetavarTypeHelpFormatter NamespaceAction ONE_OR_MOREOPTIONALPARSER REMAINDERSUPPRESS ZERO_OR_MOREN)gettextngettextz ==SUPPRESS==?*+zA...z...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}x|D]}|t|qWx8|D],\}}|r`|d||fq<|||<qrD)r!r'r'r(_indentszHelpFormatter._indentcCs4|j|j8_|jdks"td|jd8_dS)NrzIndent decreased below 0.rQ)rCr>AssertionErrorrD)r!r'r'r(_dedentszHelpFormatter._dedentc@seZdZdddZddZdS)zHelpFormatter._SectionNcCs||_||_||_g|_dS)N) formatterparentheadingr,)r!rUrVrWr'r'r(rPszHelpFormatter._Section.__init__cCs|jdk r|j|jj}|dd|jD}|jdk rD|j|sLdS|jtk rz|jdk rz|jj}d|d|jf}nd}|d||dgS)NcSsg|]\}}||qSr'r').0funcargsr'r'r( sz6HelpFormatter._Section.format_help..z%*s%s:  ) rVrUrR _join_partsr,rTrWrrC)r!r Z item_helpZcurrent_indentrWr'r'r( format_helps    z"HelpFormatter._Section.format_help)N)rr-r.rPr_r'r'r'r(rFs rFcCs|jj||fdS)N)rHr,r)r!rYrZr'r'r( _add_itemszHelpFormatter._add_itemcCs0||||j|}||jg||_dS)N)rRrFrHr`r_)r!rWsectionr'r'r( start_sectionszHelpFormatter.start_sectioncCs|jj|_|dS)N)rHrVrT)r!r'r'r( end_sections zHelpFormatter.end_sectioncCs$|tk r |dk r ||j|gdS)N)rr` _format_text)r!textr'r'r(add_textszHelpFormatter.add_textcCs&|tk r"||||f}||j|dS)N)rr` _format_usage)r!usageactionsgroupsprefixrZr'r'r( add_usages zHelpFormatter.add_usagecCsz|jtk rv|j}||g}x ||D]}|||q&Wtdd|D}||j}t|j||_||j |gdS)NcSsg|] }t|qSr')len)rXsr'r'r(r[ sz.HelpFormatter.add_argument..) helpr_format_action_invocation_iter_indented_subactionsrrArCrEr`_format_action)r!actionZget_invocationZ invocations subactionZinvocation_lengthZ action_lengthr'r'r( add_arguments   zHelpFormatter.add_argumentcCsx|D]}||qWdS)N)ru)r!rirsr'r'r( add_argumentss zHelpFormatter.add_argumentscCs.|j}|r*|jd|}|dd}|S)Nz r])rGr_rMsubstrip)r!ror'r'r(r_s  zHelpFormatter.format_helpcCsddd|DS)Nr\cSsg|]}|r|tk r|qSr')r)rXpartr'r'r(r[#sz-HelpFormatter._join_parts..)r )r!Z part_stringsr'r'r(r^"s zHelpFormatter._join_partscs:|dkrtd}|dk r,|t|jd}n|dkrL|sLdt|jd}n|dkr.dt|jd}g}g}x(|D] }|jr||qt||qtW|j} | |||} ddd|| gD}|j|jt |t |kr.d} | ||} | ||} t | | }t | | }d|| ks*t d|| ks>t dfdd }t |t |d krdt |t |d }|r||g|||}| |||n |r||g|||}n|g}nZdt |}||}|||}t |d krg}| |||| ||||g|}d |}d ||fS)Nzusage: )rNz%(prog)s cSsg|] }|r|qSr'r')rXrnr'r'r(r[Csz/HelpFormatter._format_usage..z%\(.*?\)+(?=\s|$)|\[.*?\]+(?=\s|$)|\S+csg}g}|dk rt|d}n t|d}xb|D]Z}|dt|krp|rp||d|g}t|d}|||t|d7}q0W|r||d||dk r|dt|d|d<|S)NrQrzr)rmrr )partsindentrklineslineZline_lenry) text_widthr'r( get_linesWs"    z.HelpFormatter._format_usage..get_linesg?rQr]z%s%s )N)_dictr=option_stringsr_format_actions_usager rBrCrmrIfindallrSextend)r!rhrirjrkrN optionals positionalsrsformatZ action_usageZ part_regexpZ opt_usageZ pos_usageZ opt_partsZ pos_partsrr|r}r{r')rr(rg'sZ             zHelpFormatter._format_usagec Cst}i}x(|D]}y||jd}Wntk rBwYqX|t|j}||||jkrx|jD]}||qlW|js||kr||d7<nd||<||kr||d7<nd||<nF||kr||d7<nd||<||kr||d7<nd||<x t|d|D]} d || <q WqWg} x0t|D]"\} }|j t kr| d| | d kr| | n"| | dd krh| | dn|js||} ||| } ||kr| ddkr| d dkr| dd } | | nf|jd} |jdkr"d | } n"||} ||| }d | |f} |js^||kr^d | } | | qFWx(t|ddD]} || g| | | <q|Wddd| D}d}d}td|d|}td|d|}td||fd|}tdd|}|}|S)Nrz [[]z (()rQ|z%sz%s %sz[%s]T)reverserzcSsg|]}|dk r|qS)Nr')rXitemr'r'r(r[sz7HelpFormatter._format_actions_usage..z[\[(]z[\])]z(%s) z\1z (%s)z%s *%sr\z \(([^|]*)\))setindex_group_actionsr<rmaddrequiredrange enumeraterorrgetpopr#_get_default_metavar_for_positional _format_argsnargs!_get_default_metavar_for_optionalr*r rIrwrx)r!rirj group_actionsZinsertsgroupstartendrsir{defaultry option_string args_stringreopencloser'r'r(rsz                 z#HelpFormatter._format_actions_usagecCsFd|kr|t|jd}t|j|jd}d|j}||||dS)Nz%(prog))rN rzz )rr=rArBrC _fill_text)r!rerr|r'r'r(rds  zHelpFormatter._format_textc CsBt|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| dfx@| ddD]} | d|d| fqWn| ds| dx$| |D]} | || qW||S) Nr3rr\z%*s%s z %*s%-*s rrQr])r@rEr?rArBrCrprorm _expand_help _split_linesrendswithrqrrr^) r!rsZ help_positionZ help_widthZ action_widthZ action_headertupZ indent_firstr{Z help_textZ help_linesr~rtr'r'r(rrs6          zHelpFormatter._format_actioncCs|js&||}|||d\}|Sg}|jdkrB||jn8||}|||}x |jD]}|d||fq`Wd|SdS)NrQrz%s %sz, ) rr_metavar_formatterrrrrrr )r!rsrmetavarr{rrr'r'r(rp$s     z'HelpFormatter._format_action_invocationcsP|jdk r|jn.|jdk r.z{%s},csttrSf|SdS)N) isinstancetuple)Z tuple_size)resultr'r(rEs z0HelpFormatter._metavar_formatter..format)rchoicesr )r!rsdefault_metavarZ choice_strsrr')rr(r<s   z HelpFormatter._metavar_formattercCs|||}|jdkr$d|d}n|jtkr.rz) rrr rr rrrrr )r!rsrZ get_metavarrformatsr'r'r(rLs$        zHelpFormatter._format_argscCstt||jd}x"t|D]}||tkr||=qWx,t|D] }t||dr@||j||<q@W|ddk rddd|dD}||d<| ||S)N)rNrrz, cSsg|] }t|qSr')r)rXcr'r'r(r[jsz.HelpFormatter._expand_help..) rvarsr=r0rhasattrrrr _get_help_string)r!rsparamsr%Z choices_strr'r'r(ras  zHelpFormatter._expand_helpccs@y |j}Wntk rYnX||EdH|dS)N)_get_subactionsAttributeErrorrRrT)r!rsZget_subactionsr'r'r(rqns  z'HelpFormatter._iter_indented_subactionscCs&|jd|}ddl}|||S)Nrzr)rLrwrxtextwrapwrap)r!rerOrr'r'r(rxszHelpFormatter._split_linescCs,|jd|}ddl}|j||||dS)Nrzr)initial_indentsubsequent_indent)rLrwrxrfill)r!rerOr|rr'r'r(rs zHelpFormatter._fill_textcCs|jS)N)ro)r!rsr'r'r(rszHelpFormatter._get_help_stringcCs |jS)N)destupper)r!rsr'r'r(rsz/HelpFormatter._get_default_metavar_for_optionalcCs|jS)N)r)r!rsr'r'r(rsz1HelpFormatter._get_default_metavar_for_positional)r3r4N)N) rr-r.r/rPrRrTobjectrFr`rbrcrfrlrurvr_r^rgrrdrrrprrrrqrrrrrr'r'r'r(rs<  `g/  c@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|]}|VqdS)Nr')rXr~)r|r'r( sz9RawDescriptionHelpFormatter._fill_text..T)keepends)r splitlines)r!rerOr|r')r|r(rsz&RawDescriptionHelpFormatter._fill_textN)rr-r.r/rr'r'r'r(rsc@seZdZdZddZdS)rzHelp 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|S)N)r)r!rerOr'r'r(rsz!RawTextHelpFormatter._split_linesN)rr-r.r/rr'r'r'r(rsc@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|jkr:|jtk r:ttg}|js2|j|kr:|d7}|S)Nz %(default)z (default: %(default)s))rorrr rrr)r!rsroZdefaulting_nargsr'r'r(rs  z.ArgumentDefaultsHelpFormatter._get_help_stringN)rr-r.r/rr'r'r'r(rsc@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|jjS)N)rr)r!rsr'r'r(rsz:MetavarTypeHelpFormatter._get_default_metavar_for_optionalcCs|jjS)N)rr)r!rsr'r'r(rsz.r')r!namesr')r!r(r>szAction._get_kwargscCsttddS)Nz.__call__() not defined)NotImplementedErrorr)r!parser namespacevaluesrr'r'r(__call__LszAction.__call__)NNNNNFNN)N)rr-r.r/rPrrr'r'r'r(r s1 cs(eZdZdfdd ZdddZZS) _StoreActionNFc sT|dkrtd|dk r,|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 appropriatez nargs must be %r to supply const) rrrrrrrrror)r<r superrrP) r!rrrrrrrrror) __class__r'r(rPRs   z_StoreAction.__init__cCst||j|dS)N)setattrr)r!rrrrr'r'r(rosz_StoreAction.__call__)NNNNNFNN)N)rr-r.rPr __classcell__r'r')rr(rPsrcs(eZdZdfdd ZdddZZS) _StoreConstActionNFc s"tt|j||d||||ddS)Nr)rrrrrrro)rrrP)r!rrrrrror)rr'r(rPus z_StoreConstAction.__init__cCst||j|jdS)N)rrr)r!rrrrr'r'r(rsz_StoreConstAction.__call__)NFNN)N)rr-r.rPrrr'r')rr(rss  rcseZdZdfdd ZZS)_StoreTrueActionFNcs tt|j||d|||ddS)NT)rrrrrro)rrrP)r!rrrrro)rr'r(rPs z_StoreTrueAction.__init__)FFN)rr-r.rPrr'r')rr(rsrcseZdZdfdd ZZS)_StoreFalseActionTFNcs tt|j||d|||ddS)NF)rrrrrro)rrrP)r!rrrrro)rr'r(rPs z_StoreFalseAction.__init__)TFN)rr-r.rPrr'r')rr(rsrcs(eZdZdfdd ZdddZZS) _AppendActionNFc sT|dkrtd|dk r,|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 appropriatez nargs must be %r to supply const) rrrrrrrrror)r<r rrrP) r!rrrrrrrrror)rr'r(rPs   z_AppendAction.__init__cCs2t||jd}t|}||t||j|dS)N)rrr2rr)r!rrrrr,r'r'r(rs z_AppendAction.__call__)NNNNNFNN)N)rr-r.rPrrr'r')rr(rsrcs(eZdZdfdd ZdddZZS) _AppendConstActionNFc s$tt|j||d|||||ddS)Nr)rrrrrrror)rrrP)r!rrrrrror)rr'r(rPs z_AppendConstAction.__init__cCs4t||jd}t|}||jt||j|dS)N)rrr2rrr)r!rrrrr,r'r'r(rs z_AppendConstAction.__call__)NFNN)N)rr-r.rPrrr'r')rr(rs  rcs(eZdZdfdd ZdddZZS) _CountActionNFcs tt|j||d|||ddS)Nr)rrrrrro)rrrP)r!rrrrro)rr'r(rPs z_CountAction.__init__cCs0t||jd}|dkrd}t||j|ddS)NrrQ)rrr)r!rrrrcountr'r'r(rsz_CountAction.__call__)NFN)N)rr-r.rPrrr'r')rr(rs rcs.eZdZeedffdd ZdddZZS) _HelpActionNcstt|j|||d|ddS)Nr)rrrrro)rrrP)r!rrrro)rr'r(rPs  z_HelpAction.__init__cCs||dS)N) print_helpexit)r!rrrrr'r'r(rsz_HelpAction.__call__)N)rr-r.rrPrrr'r')rr(rsrcs0eZdZdeedffdd ZdddZZS)_VersionActionNz&show program's version number and exitcs$tt|j|||d|d||_dS)Nr)rrrrro)rrrPversion)r!rrrrro)rr'r(rPs z_VersionAction.__init__cCsD|j}|dkr|j}|}||||tj|dS)N)r_get_formatterrf_print_messager__sysstdoutr)r!rrrrrrUr'r'r(r's z_VersionAction.__call__)N)rr-r.rrPrrr'r')rr(rs  rcsPeZdZGdddeZedddffdd ZddZd d Zd d d Z Z S)_SubParsersActioncseZdZfddZZS)z&_SubParsersAction._ChoicesPseudoActioncs@|}}|r|dd|7}ttj|}|jg|||ddS)Nz (%s)z, )rrror)r rr_ChoicesPseudoActionrP)r!r%aliasesrorrsup)rr'r(rP5s   z/_SubParsersAction._ChoicesPseudoAction.__init__)rr-r.rPrr'r')rr(r3srFNc s<||_||_i|_g|_tt|j||t|j|||ddS)N)rrrrrror) _prog_prefix _parser_class_name_parser_map_choices_actionsrrrPr)r!rrN parser_classrrror)rr'r(rP=s  z_SubParsersAction.__init__cKs|ddkr d|j|f|d<|dd}d|krX|d}||||}|j||jf|}||j|<x|D]}||j|<qtW|S)NrNz%s %srr'ro)rrrrrrrr)r!r%kwargsrroZ choice_actionraliasr'r'r( add_parserTs      z_SubParsersAction.add_parsercCs|jS)N)r)r!r'r'r(rksz!_SubParsersAction._get_subactionsc Cs|d}|dd}|jtk r,t||j|y|j|}Wn<tk rv|d|jd}td|}t||YnX||d\} }x$t |  D]\} } t|| | qW|rt | t gt |t |dS)NrrQz, ) parser_namerz5unknown parser %(parser_name)r (choices: %(choices)s))rrrrr;r rrparse_known_argsrr, setdefault_UNRECOGNIZED_ARGS_ATTRrr) r!rrrrrr#rZmsgZ subnamespacekeyr&r'r'r(rns"    z_SubParsersAction.__call__)N) rr-r.r rrrPrrrrr'r')rr(r1src@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||_||_||_||_dS)N)_mode_bufsize _encoding_errors)r!modebufsizeencodingerrorsr'r'r(rPszFileType.__init__c Cs|dkr>d|jkrtjSd|jkr(tjStd|j}t|yt||j|j|j|j St k r}ztd}t |||fWdd}~XYnXdS)N-rwzargument "-" with mode %rzcan't open '%s': %s) rrstdinrrr<rrrrOSErrorr)r!stringrerr'r'r(rs  zFileType.__call__cCsT|j|jf}d|jfd|jfg}ddd|Ddd|D}dt|j|fS)Nrr z, cSsg|]}|dkrt|qS)r)r)rXr$r'r'r(r[sz%FileType.__repr__..cSs$g|]\}}|dk rd||fqS)Nz%s=%rr')rXkwr$r'r'r(r[sz%s(%s))rrrrr rr)r!rZrargs_strr'r'r(r)s  zFileType.__repr__)rrNN)rr-r.r/rPrr)r'r'r'r(rs c@s(eZdZdZddZddZddZdS) r zSimple object for storing attributes. Implements equality by attribute names and values, and provides a simple string representation. cKs"x|D]}t||||qWdS)N)r)r!rr%r'r'r(rPs zNamespace.__init__cCst|tstSt|t|kS)N)rr NotImplementedr)r!otherr'r'r(__eq__s zNamespace.__eq__cCs ||jkS)N)r+)r!rr'r'r( __contains__szNamespace.__contains__N)rr-r.r/rPrrr'r'r'r(r scseZdZfddZddZd&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|g|_i|_g|_g|_i|_td |_g|_dS) NrsstoreZ store_const store_trueZ store_falserZ append_constrrorparsersz^-\d+$|^-\d*\.\d+$)rrrP descriptionargument_default prefix_charsconflict_handler _registriesregisterrrrrrrrrrr _get_handler_actions_option_string_actions_action_groups_mutually_exclusive_groups _defaultsrIrJ_negative_number_matcher_has_negative_number_optionals)r!rrrr)rr'r(rPs2 z_ActionsContainer.__init__cCs|j|i}|||<dS)N)rr)r! registry_namer&rregistryr'r'r(rsz_ActionsContainer.registerNcCs|j|||S)N)rr)r!r(r&rr'r'r( _registry_getsz_ActionsContainer._registry_getcKs6|j|x$|jD]}|j|kr||j|_qWdS)N)r%updater!rr)r!rrsr'r'r( set_defaultss   z_ActionsContainer.set_defaultscCs8x(|jD]}|j|kr|jdk r|jSqW|j|dS)N)r!rrr%r)r!rrsr'r'r( get_default&s  z_ActionsContainer.get_defaultcOs.|j}|r&t|dkrH|dd|krH|r:d|kr:td|j||}n |j||}d|kr|d}||jkr~|j||d<n|jdk r|j|d<||}t|std|f|f|}| d|j |j }t|std |ft |d r$y| |dWntk r"td YnX||S) z add_argument(dest, ..., name=value, ...) add_argument(option_string, option_string, ..., name=value, ...) rQrrz+dest supplied twice for positional argumentrNzunknown action "%s"rz%r is not callablerz,length of metavar tuple does not match nargs)rrmr<_get_positional_kwargs_get_optional_kwargsr%r_pop_action_classcallabler*rrrr TypeError _add_action)r!rZrcharsrZ action_classrs type_funcr'r'r(ru0s2          z_ActionsContainer.add_argumentcOs t|f||}|j||S)N)_ArgumentGroupr#r)r!rZrrr'r'r(add_argument_group_s z$_ActionsContainer.add_argument_groupcKst|f|}|j||S)N)_MutuallyExclusiveGroupr$r)r!rrr'r'r(add_mutually_exclusive_groupds  z._ActionsContainer.add_mutually_exclusive_groupcCsh|||j|||_x|jD]}||j|<q$Wx,|jD]"}|j|r>|js>|jdq>W|S)NT) _check_conflictr!r containerrr"r&matchr')r!rsrr'r'r(r3is     z_ActionsContainer._add_actioncCs|j|dS)N)r!remove)r!rsr'r'r(_remove_action~sz _ActionsContainer._remove_actioncCsi}x8|jD].}|j|kr0td}t||j|||j<q Wi}xR|jD]H}|j|krt|j|j|j|jd||j<x|jD]}||j||<q|WqJWx4|jD]*}|j |j d}x|jD] }|||<qWqWx |j D]}| || |qWdS)Nz.cannot merge actions - two groups are named %r)titlerr)r)r#r?rr<r7rrrr$r9rr!rr3)r!r;Ztitle_group_maprrZ group_maprs mutex_groupr'r'r(_add_container_actionss,         z(_ActionsContainer._add_container_actionscKs^d|krtd}t||dttgkr2d|d<|dtkrPd|krPd|d<t||gdS)Nrz1'required' is an invalid argument for positionalsrTr)rr)rr2rr rr)r!rrrr'r'r(r.sz(_ActionsContainer._get_positional_kwargsc Osg}g}xv|D]n}|d|jkr@||jd}td}t|||||d|jkrt|dkr|d|jkr||qW|dd}|dkr|r|d}n|d}||j}|std}t|||dd}t|||d S) Nr)optionrzNinvalid option string %(option)r: must start with a character %(prefix_chars)rrQrz%dest= is required for options like %rr r)rr) rrr<rrmrlstripreplacer) r!rZrrZlong_option_stringsrrrZdest_option_stringr'r'r(r/s0          z&_ActionsContainer._get_optional_kwargscCs|d|}|d||S)Nrs)rr*)r!rrrsr'r'r(r0s z#_ActionsContainer._pop_action_classcCsDd|j}y t||Stk r>td}t||jYnXdS)Nz_handle_conflict_%sz%invalid conflict_resolution value: %r)rrrrr<)r!Zhandler_func_namerr'r'r(r s   z_ActionsContainer._get_handlercCsPg}x0|jD]&}||jkr |j|}|||fq W|rL|}|||dS)N)rr"rr )r!rsZconfl_optionalsrZconfl_optionalrr'r'r(r:s   z!_ActionsContainer._check_conflictcCs6tddt|}ddd|D}t|||dS)Nzconflicting option string: %szconflicting option strings: %sz, cSsg|] \}}|qSr'r')rXrrsr'r'r(r[sz<_ActionsContainer._handle_conflict_error..)rrmr r)r!rsconflicting_actionsrZconflict_stringr'r'r(_handle_conflict_errors    z(_ActionsContainer._handle_conflict_errorcCsBx<|D]4\}}|j||j|d|js|j|qWdS)N)rr=r"rr;r>)r!rsrErr'r'r(_handle_conflict_resolves  z*_ActionsContainer._handle_conflict_resolve)N)N)rr-r.rPrr*r,r-rur7r9r3r>rAr.r/r0r r:rFrGrr'r')rr(rs$ 4   /($   rcs6eZdZdfdd ZfddZfddZZS) r6Nc s|j}|d|j|d|j|d|jtt|j}|fd|i|||_g|_|j |_ |j |_ |j |_ |j |_ |j |_ |j|_dS)Nrrrr)rrrrrr6rPr?rrr!r"r%r'r$)r!r;r?rrr+Z super_init)rr'r(rPs    z_ArgumentGroup.__init__cs tt||}|j||S)N)rr6r3rr)r!rs)rr'r(r3(s z_ArgumentGroup._add_actioncs tt|||j|dS)N)rr6r>rr=)r!rs)rr'r(r>-sz_ArgumentGroup._remove_action)NN)rr-r.rPr3r>rr'r')rr(r6s r6cs.eZdZdfdd ZddZddZZS) r8Fcs tt||||_||_dS)N)rr8rPr _container)r!r;r)rr'r(rP4sz _MutuallyExclusiveGroup.__init__cCs2|jrtd}t||j|}|j||S)Nz-mutually exclusive arguments must be optional)rrr<rHr3rr)r!rsrr'r'r(r39s   z#_MutuallyExclusiveGroup._add_actioncCs|j||j|dS)N)rHr>rr=)r!rsr'r'r(r>As z&_MutuallyExclusiveGroup._remove_action)F)rr-r.rPr3r>rr'r')rr(r82sr8c s*eZdZdZddddgeddddddf 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)IraObject 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 Nr errorTc  s&tt|j} | ||| | d|dkr6tjtjd}||_||_ ||_ ||_ ||_ | |_ | |_|j}|td|_|td|_d|_dd}|dd|d|krdn|d}|j r|j|d |d d d ttd d xD|D]<}||y |j}Wntk rYqX|j|qWdS)N)rrrrrzpositional argumentszoptional argumentscSs|S)Nr')rr'r'r(identitysz)ArgumentParser.__init__..identityrr hr3rozshow this help message and exit)rsrro)rrrPr9pathbasenamerargvrNrhepilogformatter_classfromfile_prefix_charsadd_help allow_abbrevr7r _positionals _optionals _subparsersrrurrAr%rr+)r!rNrhrrOparentsrPrrQrrrRrSZ superinitZ add_grouprJZdefault_prefixrVdefaults)rr'r(rPYsB     zArgumentParser.__init__cs"ddddddg}fdd|DS) NrNrhrrPrrRcsg|]}|t|fqSr')r)rXr%)r!r'r(r[sz.ArgumentParser._get_kwargs..r')r!rr')r!r(rszArgumentParser._get_kwargsc Ks|jdk r|td|dt|d|ks8d|krht|dd}t|dd}||||_n|j|_|ddkr| }| }|j }| |j ||d||d<||d}|fd gi|}|j||S) Nz(cannot have multiple subparser argumentsrr?rZ subcommandsrNr\rr)rVrIrrrrr7rTrr_get_positional_actionsr$rlrhr_rxr0r3) r!rr?rrUrrjZ parsers_classrsr'r'r(add_subparserss$   zArgumentParser.add_subparserscCs$|jr|j|n |j||S)N)rrUr3rT)r!rsr'r'r(r3s zArgumentParser._add_actioncCsdd|jDS)NcSsg|]}|jr|qSr')r)rXrsr'r'r(r[sz8ArgumentParser._get_optional_actions..)r!)r!r'r'r(_get_optional_actionssz$ArgumentParser._get_optional_actionscCsdd|jDS)NcSsg|]}|js|qSr')r)rXrsr'r'r(r[sz:ArgumentParser._get_positional_actions..)r!)r!r'r'r(rYsz&ArgumentParser._get_positional_actionscCs4|||\}}|r0td}||d||S)Nzunrecognized arguments: %srz)rrrIr )r!rZrrNrr'r'r( parse_argss zArgumentParser.parse_argscCs |dkrtjdd}nt|}|dkr.t}x>|jD]4}|jtk r6t||js6|jtk r6t ||j|jq6Wx*|j D] }t||svt |||j |qvWy<| ||\}}t|t r| t|t t|t ||fStk rtd}|t|YnXdS)NrQ)rrNr0r r!rrrrrr%_parse_known_argsrrrdelattrrexc_inforIr)r!rZrrsrerrr'r'r(rs,         zArgumentParser.parse_known_argscs" jdk r ix` jD]V}|j}xJt|jD]<\}}|g}||d||||ddq6Wq Wig}t} xnt| D]b\}} | dkr|dxF| D]} |dqWq | } | dkrd} n | |<d} || qWd |t t d fdd  fd d }  fd d }gd rpt }nd}x| |krt fddD} |kr| }| kr| qvn| kr |}|| |  qvW| }|dg}x jD]|}|kr|jr>|t|nT|jdk rt|jtrt|jr|jt|jkrt|j ||jqW|r tdd |xb jD]X}|jrxH|jD]}|krPqWdd|jD}td} |d |qWfS)NrQz--r AOr\cs|||}||jk rf|x:|gD]*}|kr8td}t|}t|||q8W|tk r||||dS)Nznot allowed with argument %s)r _get_valuesrrrrrr)rsZargument_stringsrZargument_valuesZconflict_actionrZ action_name)action_conflictsr seen_actionsseen_non_default_actionsr!r'r( take_action3s    z5ArgumentParser._parse_known_args..take_actioncs|}|\}}}j}g}x>|dkr>||dS|dk r||d}j}|dkr|d|kr||g|f|d} | |d}|ddpd} j} || kr| |}| }ntd} t|| |n@|dkr|d} |g}||||fPntd} t|| |q |d}|d}|||}||} || }||||fPq W|shtx |D]\}}}|||qnW| S)NrQrarzignored explicit argument %r)_match_argumentrrr"rrrS) start_index option_tuplersr explicit_argZmatch_argumentZ action_tuples arg_countr4charZnew_explicit_argZ optionals_maprstoprZrZselected_patterns)r#arg_strings_patternextrasoption_string_indicesr!rgr'r(consume_optionalHsP        z:ArgumentParser._parse_known_args..consume_optionalcsrj}|d}||}x8t|D]*\}}|||}||7}||q(Wt|ddd<|S)N)_match_arguments_partialziprm)riZ match_partialZselected_patternZ arg_countsrsrlrZ)r#rorr!rgr'r(consume_positionalss  z=ArgumentParser._parse_known_args..consume_positionalsrrcsg|]}|kr|qSr'r')rXr)rir'r(r[sz4ArgumentParser._parse_known_args..z(the following arguments are required: %sz, cSsg|]}|jtk rt|qSr')rorr)rXrsr'r'r(r[sz#one of the arguments %s is requiredrz)N)rQ_read_args_from_filesr$rrrriterr_parse_optionalr rrYrAr@r!rrrrrrrrr _get_valuerIr)r!r#rr@rrZ mutex_actionZ conflictsZarg_string_pattern_partsZarg_strings_iter arg_stringrjpatternrrruZmax_option_string_indexZnext_option_string_indexZpositionals_end_indexstringsZ stop_indexZrequired_actionsrsrrrr') rdr#rorprrqrrerfr!rirgr(r]s        J                 z ArgumentParser._parse_known_argsc Csg}x|D]}|r |d|jkr,||q ylt|ddR}g}x2|D]"}x||D]}||qbWqRW||}||WdQRXWq tk rt d}| t |Yq Xq W|S)NrrQ) rQrrreadrconvert_arg_line_to_argsrvrr rr_rIr)r!r#Znew_arg_stringsrzZ args_filearg_liner$r`r'r'r(rvs     z$ArgumentParser._read_args_from_filescCs|gS)Nr')r!rr'r'r(r~sz'ArgumentParser.convert_arg_line_to_argscCsz||}t||}|dkrldtdttdttdi}||j}|dkrbtdd|j|j}t ||t | dS)Nzexpected one argumentzexpected at most one argumentzexpected at least one argumentzexpected %s argumentzexpected %s argumentsrQ) _get_nargs_patternrIr<rr r rrrrrmr)r!rsro nargs_patternr<Z nargs_errorsrr'r'r(rhs     zArgumentParser._match_argumentcstg}xjtt|ddD]V}|d|}dfdd|D}t||}|dk r|dd|DPqW|S)Nrrr\csg|]}|qSr')r)rXrs)r!r'r(r[3sz;ArgumentParser._match_arguments_partial..cSsg|] }t|qSr')rm)rXrr'r'r(r[7s)rrmr rIr<rrj)r!rirorrZ actions_slicer{r<r')r!r(rs-s   z'ArgumentParser._match_arguments_partialc Cs|sdS|d|jkrdS||jkr8|j|}||dfSt|dkrHdSd|kr~|dd\}}||jkr~|j|}|||fS|jr||}t|dkrddd|D}||d}td}|||nt|dkr|\} | S|j |r|j sdSd |kr dSd|dfS) NrrQ=z, cSsg|]\}}}|qSr'r')rXrsrrkr'r'r(r[]sz2ArgumentParser._parse_optional..)rBmatchesz4ambiguous option: %(option)s could match %(matches)srz) rr"rmsplitrS_get_option_tuplesr rrIr&r<r') r!rzrsrrkZ option_tuplesoptionsrZrrjr'r'r(rx=s>               zArgumentParser._parse_optionalc Cs2g}|j}|d|kr~|d|kr~d|kr<|dd\}}n|}d}x|jD],}||rL|j|}|||f}||qLWn|d|kr|d|kr|}d}|dd}|dd} xr|jD]T}||kr|j|}||| f}||q||r|j|}|||f}||qWn|td||S)NrrQrr3zunexpected option string: %s)rrr" startswithrrIr) r!rrr4Z option_prefixrkrsrZshort_option_prefixZshort_explicit_argr'r'r(rxs8             z!ArgumentParser._get_option_tuplescCs|j}|dkrd}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-*rar\r ) rr rr rrrr rrD)r!rsrrr'r'r(rs(  z!ArgumentParser._get_nargs_patterncCs4|||\}}|r0td}||d||S)Nzunrecognized arguments: %srz)parse_known_intermixed_argsrrIr )r!rZrrNrr'r'r(parse_intermixed_argss z$ArgumentParser.parse_intermixed_argsc s|ddD}|r,td|djfdd|jDrHtdzl|j}z|jdkrp|dd|_x(D] }|j|_t|_|j|_ t|_qvW| ||\}}xRD]J}t ||j rt ||j gkrddlm}|d |j |ft||j qWWdxD]}|j|_|j |_qWX|}zRx|D]}|j|_d |_q4Wx|jD]} | j| _d | _qTW| ||\}} Wdx|D]}|j|_qWx|jD]} | j| _qWXWd||_X|| fS) NcSsg|]}|jttgkr|qSr')rrr)rXrsr'r'r(r[sz>ArgumentParser.parse_known_intermixed_args..z3parse_intermixed_args: positional arg with nargs=%srcs&g|]}|jD]}|kr|jqqSr')rr)rXrrs)rr'r(r[sz;parse_intermixed_args: positional in mutuallyExclusiveGroup)warnzDo not expect %s in %sF)rYr2rr$rh format_usageZ save_nargsrrZ save_defaultrrrrwarningsrr^r[rZ save_required) r!rZraZ save_usagersZremaining_argsrrrrpr')rr(rsX               z*ArgumentParser.parse_known_intermixed_argscsjttgkr2y|dWntk r0YnX|szjtkrzjrNj}nj}t |t rv |} |n|sjt krjsjdk rj}n|} |nt|dkrjdtgkr|\} |} |njtkrfdd|D}ntjtkr@fdd|D} |dnBjtkrRt}n0fdd|D}x|D]} |qlW|S)Nz--rQcsg|]}|qSr')ry)rXv)rsr!r'r(r[M sz.ArgumentParser._get_values..csg|]}|qSr')ry)rXr)rsr!r'r(r[Q srcsg|]}|qSr')ry)rXr)rsr!r'r(r[Z s)rrrr=r<r rrrrrry _check_valuerrmr)r!rsr#r&rzrr')rsr!r(rc) sB        zArgumentParser._get_valuesc Cs|d|j|j}t|s0td}t|||y ||}Wntk r~t|jdt|j}tt d}t||YnLt t fk rt|jdt|j}||d}td}t|||YnX|S)Nrz%r is not callablerrQ)rr&z!invalid %(type)s value: %(value)r) r*rr1rrrrrrrr_r2r<)r!rsrzr5rrr%rZr'r'r(rya s   zArgumentParser._get_valuecCsF|jdk rB||jkrB|dtt|jd}td}t|||dS)Nz, )r&rz3invalid choice: %(value)r (choose from %(choices)s))rr maprrr)r!rsr&rZrr'r'r(r{ s zArgumentParser._check_valuecCs$|}||j|j|j|S)N)rrlrhr!r$r_)r!rUr'r'r(r s zArgumentParser.format_usagecCsx|}||j|j|j||jx:|jD]0}||j ||j| |j | q0W||j |S)N)rrlrhr!r$rfrr#rbr?rvrrcrOr_)r!rUZ action_groupr'r'r(r_ s        zArgumentParser.format_helpcCs|j|jdS)N)rN)rPrN)r!r'r'r(r szArgumentParser._get_formattercCs"|dkrtj}|||dS)N)rrrr)r!filer'r'r( print_usage szArgumentParser.print_usagecCs"|dkrtj}|||dS)N)rrrr_)r!rr'r'r(r szArgumentParser.print_helpcCs |r|dkrtj}||dS)N)rstderrwrite)r!rrr'r'r(r szArgumentParser._print_messagercCs |r||tjt|dS)N)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. )rNrr3z%(prog)s: error: %(message)s N)rrrrNrr)r!rrZr'r'r(rI s  zArgumentParser.error)NN)NN)NN)NN)N)N)N)rN)#rr-r.r/rrPrrZr3r[rYr\rr]rvr~rhrsrxrrrrrcryrrr_rrrrrrIrr'r')rr(rFsT4  #w;,1  M8    )0r/ __version____all__osr9rerIsysrrrrrr rr rrrrrr2rrrrr r Exceptionrrr rrrrrrrrrrrr rr6r8rr'r'r'r(>sz{    [#&b65"