B YN^@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<|||<qsz6HelpFormatter._Section.format_help..z%*s%s:  ) rSrRrP _join_partsr+rQrTrrA)r!r Z item_helpZcurrent_indentrTr&r&r' format_helps    z"HelpFormatter._Section.format_help)N)rr,r-rNr\r&r&r&r'rDs rDcCs|jj||fdS)N)rFr+r)r!rVrWr&r&r' _add_itemszHelpFormatter._add_itemcCs0||||j|}||jg||_dS)N)rPrDrFr]r\)r!rTZsectionr&r&r' start_sectionszHelpFormatter.start_sectioncCs|jj|_|dS)N)rFrSrQ)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!usageactionsgroupsprefixrWr&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)rUsr&r&r'rX sz.HelpFormatter.add_argument..) helpr_format_action_invocation_iter_indented_subactionsrr?rArCr]_format_action)r!actionZget_invocationZ invocations subactionZinvocation_lengthZ action_lengthr&r&r' add_arguments   zHelpFormatter.add_argumentcCsx|D]}||qWdS)N)rq)r!reror&r&r' add_argumentss zHelpFormatter.add_argumentscCs.|j}|r*|jd|}|dd}|S)Nz rZ)rEr\rKsubstrip)r!rkr&r&r'r\s  zHelpFormatter.format_helpcCsddd|DS)NrYcSsg|]}|r|tk r|qSr&)r)rUpartr&r&r'rX#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|dkrdt|jd}g}g}x(|D] }|jr||qt||qtW|j} | |||} ddd|| gD}|j|jt |t |krd} | ||} | ||} t | | }t | | }dfdd }t |t |d krdt |t |d }|r||g|||}| |||n |r||g|||}n|g}nZdt |}||}|||}t |d krg}| |||| ||||g|}d |}d ||fS)Nzusage: )rLz%(prog)s cSsg|] }|r|qSr&r&)rUrjr&r&r'rXCsz/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)NrOrvr)rirr )partsindentrglineslineZline_lenru) text_widthr&r' get_linesWs"    z.HelpFormatter._format_usage..get_linesg?rOrZz%s%s )N) _dictr;option_stringsr_format_actions_usager r@rArirGfindallextend)r!rdrerfrgrL optionals positionalsroformatZ action_usageZ part_regexpZ opt_usageZ pos_usageZ opt_partsZ pos_partsr|rxryrwr&)r{r'rc'sV             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 (()rO|z%sz%s %sz[%s]T)reverservcSsg|]}|dk r|qS)Nr&)rUitemr&r&r'rXsz7HelpFormatter._format_actions_usage..z[\[(]z[\])]z(%s) z\1z (%s)z%s *%srYz \(([^|]*)\))setindex_group_actionsr:riaddrequiredrange enumeraterkrrgetpopr#_get_default_metavar_for_positional _format_argsnargs!_get_default_metavar_for_optionalr)r rGrsrt)r!rerf group_actionsZinsertsgroupstartendroirwdefaultru option_string args_stringraopencloser&r&r'rsz                 z#HelpFormatter._format_actions_usagecCsFd|kr|t|jd}t|j|jd}d|j}||||dS)Nz%(prog))rL rvz )r~r;r?r@rA _fill_text)r!rar{rxr&r&r'r`s  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) Nr2rrYz%*s%s z %*s%-*s rrOrZ)r>rCr=r?r@rArlrkri _expand_help _split_linesrendswithrmrnr[) r!roZ help_positionZ help_widthZ action_widthZ action_headertupZ indent_firstrwZ help_textZ help_linesrzrpr&r&r'rns6          zHelpFormatter._format_actioncCs|js&||}|||d\}|Sg}|jdkrB||jn8||}|||}x |jD]}|d||fq`Wd|SdS)NrOrz%s %sz, ) rr_metavar_formatterrrrrrr )r!rormetavarrwrrr&r&r'rl$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!rodefault_metavarZ choice_strsrr&)rr'r<s   z HelpFormatter._metavar_formattercCs|||}|jdkr$d|d}n|jtkr.rv) rrr rr rrrrr )r!rorZ get_metavarrZformatsr&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)rLrrz, cSsg|] }t|qSr&)r)rUcr&r&r'rXjsz.HelpFormatter._expand_help..) r~varsr;r/rhasattrrrr _get_help_string)r!roZparamsr$Z choices_strr&r&r'ras  zHelpFormatter._expand_helpccs@y |j}Wntk rYnX||EdH|dS)N)_get_subactionsAttributeErrorrPrQ)r!roZget_subactionsr&r&r'rmns  z'HelpFormatter._iter_indented_subactionscCs&|jd|}ddl}|||S)Nrvr)rJrsrttextwrapZwrap)r!rarMrr&r&r'rxszHelpFormatter._split_linescCs,|jd|}ddl}|j||||dS)Nrvr)Zinitial_indentZsubsequent_indent)rJrsrtrZfill)r!rarMrxrr&r&r'rs zHelpFormatter._fill_textcCs|jS)N)rk)r!ror&r&r'rszHelpFormatter._get_help_stringcCs |jS)N)destupper)r!ror&r&r'rsz/HelpFormatter._get_default_metavar_for_optionalcCs|jS)N)r)r!ror&r&r'rsz1HelpFormatter._get_default_metavar_for_positional)r2r3N)N) rr,r-r.rNrPrQobjectrDr]r^r_rbrhrqrrr\r[rcrr`rnrlrrrrmrrrrrr&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)NrYc3s|]}|VqdS)Nr&)rUrz)rxr&r' sz9RawDescriptionHelpFormatter._fill_text..T)keepends)r splitlines)r!rarMrxr&)rxr'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!rarMr&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))rkrrr rrr)r!rorkZdefaulting_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!ror&r&r'rsz:MetavarTypeHelpFormatter._get_default_metavar_for_optionalcCs|jjS)N)rr)r!ror&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.rNrrr&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) rrrrrrrrrkr)r:r superrrN) r!rrrrrrrrrkr) __class__r&r'rNRs   z_StoreAction.__init__cCst||j|dS)N)setattrr)r!rrrrr&r&r'rosz_StoreAction.__call__)NNNNNFNN)N)rr,r-rNr __classcell__r&r&)rr'rPsrcs(eZdZdfdd ZdddZZS) _StoreConstActionNFc s"tt|j||d||||ddS)Nr)rrrrrrrk)rrrN)r!rrrrrrkr)rr&r'rNus z_StoreConstAction.__init__cCst||j|jdS)N)rrr)r!rrrrr&r&r'rsz_StoreConstAction.__call__)NFNN)N)rr,r-rNrrr&r&)rr'rss  rcseZdZdfdd ZZS)_StoreTrueActionFNcs tt|j||d|||ddS)NT)rrrrrrk)rrrN)r!rrrrrk)rr&r'rNs z_StoreTrueAction.__init__)FFN)rr,r-rNrr&r&)rr'rsrcseZdZdfdd ZZS)_StoreFalseActionTFNcs tt|j||d|||ddS)NF)rrrrrrk)rrrN)r!rrrrrk)rr&r'rNs z_StoreFalseAction.__init__)TFN)rr,r-rNrr&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) rrrrrrrrrkr)r:r rrrN) r!rrrrrrrrrkr)rr&r'rNs   z_AppendAction.__init__cCs2t||jd}t|}||t||j|dS)N)rrr1rr)r!rrrrr+r&r&r'rs z_AppendAction.__call__)NNNNNFNN)N)rr,r-rNrrr&r&)rr'rsrcs(eZdZdfdd ZdddZZS) _AppendConstActionNFc s$tt|j||d|||||ddS)Nr)rrrrrrrkr)rrrN)r!rrrrrrkr)rr&r'rNs z_AppendConstAction.__init__cCs4t||jd}t|}||jt||j|dS)N)rrr1rrr)r!rrrrr+r&r&r'rs z_AppendConstAction.__call__)NFNN)N)rr,r-rNrrr&r&)rr'rs  rcs(eZdZdfdd ZdddZZS) _CountActionNFcs tt|j||d|||ddS)Nr)rrrrrrk)rrrN)r!rrrrrk)rr&r'rNs z_CountAction.__init__cCs0t||jd}|dkrd}t||j|ddS)NrrO)rrr)r!rrrrcountr&r&r'rsz_CountAction.__call__)NFN)N)rr,r-rNrrr&r&)rr'rs rcs.eZdZeedffdd ZdddZZS) _HelpActionNcstt|j|||d|ddS)Nr)rrrrrk)rrrN)r!rrrrk)rr&r'rNs  z_HelpAction.__init__cCs||dS)N) print_helpexit)r!rrrrr&r&r'rsz_HelpAction.__call__)N)rr,r-rrNrrr&r&)rr'rsrcs0eZdZdeedffdd ZdddZZS)_VersionActionNz&show program's version number and exitcs$tt|j|||d|d||_dS)Nr)rrrrrk)rrrNversion)r!rrrrrk)rr&r'rNs z_VersionAction.__init__cCsD|j}|dkr|j}|}||||tj|dS)N)r_get_formatterrb_print_messager\_sysstdoutr)r!rrrrrrRr&r&r'r's z_VersionAction.__call__)N)rr,r-rrNrrr&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, )rrrkr)r rr_ChoicesPseudoActionrN)r!r$aliasesrkrrZsup)rr&r'rN5s   z/_SubParsersAction._ChoicesPseudoAction.__init__)rr,r-rNrr&r&)rr'r3srFNc s<||_||_i|_g|_tt|j||t|j|||ddS)N)rrrrrrkr) _prog_prefix _parser_class_name_parser_map_choices_actionsrrrNr)r!rrL parser_classrrrkr)rr&r'rN=s  z_SubParsersAction.__init__cKs|ddkr d|j|f|d<|dd}d|krX|d}||||}|j||jf|}||j|<x|D]}||j|<qtW|S)NrLz%s %srr&rk)rrrrrrrr)r!r$kwargsrrkZ 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)NrrOz, ) parser_namerz5unknown parser %(parser_name)r (choices: %(choices)s))rrrrr9r r}rparse_known_argsrr+ setdefault_UNRECOGNIZED_ARGS_ATTRrr) r!rrrrrr"rWmsgZ subnamespacekeyr%r&r&r'rns"    z_SubParsersAction.__call__)N) rr,r-r rrrNrrrrr&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'rNszFileType.__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) rrstdinrr}r:rrrrOSErrorr)r!stringrerr&r&r'rs  zFileType.__call__cCsT|j|jf}d|jfd|jfg}ddd|Ddd|D}dt|j|fS)Nrrz, cSsg|]}|dkrt|qS)r)r)rUr#r&r&r'rXsz%FileType.__repr__..cSs$g|]\}}|dk rd||fqS)Nz%s=%rr&)rUkwr#r&r&r'rXsz%s(%s))rrrrr rr)r!rWrZargs_strr&r&r'r(s  zFileType.__repr__)rrNN)rr,r-r.rNrr(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'rNs 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.rNrrr&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) NroZstoreZ store_const store_trueZ store_falserZ append_constrrkrparsersz^-\d+$|^-\d*\.\d+$)rr rN descriptionargument_default prefix_charsconflict_handler _registriesregisterrrrrrrrrrr _get_handler_actions_option_string_actions_action_groups_mutually_exclusive_groups _defaultsrGrH_negative_number_matcher_has_negative_number_optionals)r!r rr r)rr&r'rNs2 z_ActionsContainer.__init__cCs|j|i}|||<dS)N)rr)r! registry_namer%rregistryr&r&r'rsz_ActionsContainer.registerNcCs|j|||S)N)rr)r!rr%rr&r&r' _registry_getsz_ActionsContainer._registry_getcKs6|j|x$|jD]}|j|kr||j|_qWdS)N)rupdaterrr)r!rror&r&r' set_defaultss   z_ActionsContainer.set_defaultscCs8x(|jD]}|j|kr|jdk r|jSqW|j|dS)N)rrrrr)r!rror&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, ...) rOrrz+dest supplied twice for positional argumentrNzunknown action "%s"rz%r is not callablerz,length of metavar tuple does not match nargs)rrir:_get_positional_kwargs_get_optional_kwargsrr _pop_action_classcallablerrrrr TypeError _add_action)r!rWrcharsrZ action_classro type_funcr&r&r'rq0s2          z_ActionsContainer.add_argumentcOs t|f||}|j||S)N)_ArgumentGrouprr)r!rWrrr&r&r'add_argument_group_s z$_ActionsContainer.add_argument_groupcKst|f|}|j||S)N)_MutuallyExclusiveGrouprr)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_conflictrr containerrrrmatchr)r!rorr&r&r'r%is     z_ActionsContainer._add_actioncCs|j|dS)N)rremove)r!ror&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)titler r)r)rr1r}r:r)r rrrr+rrrr%)r!r-Ztitle_group_maprrZ group_mapro 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)r}r$rr 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)rrOrz%dest= is required for options like %rrr})rr) rr}r:rrirlstripreplacer~) r!rWrrZlong_option_stringsrrrZdest_option_stringr&r&r'r!s0          z&_ActionsContainer._get_optional_kwargscCs|d|}|d||S)Nro)rr)r!rrror&r&r'r"s z#_ActionsContainer._pop_action_classcCsDd|j}y t||Stk r>td}t||jYnXdS)Nz_handle_conflict_%sz%invalid conflict_resolution value: %r)rrrr}r:)r!Zhandler_func_namerr&r&r'rs   z_ActionsContainer._get_handlercCsPg}x0|jD]&}||jkr |j|}|||fq W|rL|}|||dS)N)rrrr)r!roZconfl_optionalsrZconfl_optionalrr&r&r'r,s   z!_ActionsContainer._check_conflictcCs6tddt|}ddd|D}t|||dS)Nzconflicting option string: %szconflicting option strings: %sz, cSsg|] \}}|qSr&r&)rUrror&r&r'rXsz<_ActionsContainer._handle_conflict_error..)rrir r)r!roconflicting_actionsrZconflict_stringr&r&r'_handle_conflict_errors    z(_ActionsContainer._handle_conflict_errorcCsBx<|D]4\}}|j||j|d|js|j|qWdS)N)rr/rrr-r0)r!ror7rr&r&r'_handle_conflict_resolves  z*_ActionsContainer._handle_conflict_resolve)N)N)rr,r-rNrrrrrqr)r+r%r0r3r r!r"rr,r8r9rr&r&)rr'r s$ 4   /($   r cs6eZdZdfdd ZfddZfddZZS) r(Nc s|j}|d|j|d|j|d|jtt|j}|fd|i|||_g|_|j |_ |j |_ |j |_ |j |_ |j |_ |j|_dS)Nrrr r )rrrr rr(rNr1rrrrrrr)r!r-r1r rrZ super_init)rr&r'rNs    z_ArgumentGroup.__init__cs tt||}|j||S)N)rr(r%rr)r!ro)rr&r'r%(s z_ArgumentGroup._add_actioncs tt|||j|dS)N)rr(r0rr/)r!ro)rr&r'r0-sz_ArgumentGroup._remove_action)NN)rr,r-rNr%r0rr&r&)rr'r(s r(cs.eZdZdfdd ZddZddZZS) r*Fcs tt||||_||_dS)N)rr*rNr _container)r!r-r)rr&r'rN4sz _MutuallyExclusiveGroup.__init__cCs2|jrtd}t||j|}|j||S)Nz-mutually exclusive arguments must be optional)rr}r:r:r%rr)r!rorr&r&r'r%9s   z#_MutuallyExclusiveGroup._add_actioncCs|j||j|dS)N)r:r0rr/)r!ror&r&r'r0As z&_MutuallyExclusiveGroup._remove_action)F)rr,r-rNr%r0rr&r&)rr'r*2sr*c 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 NrerrorTc  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)r rr rrzpositional argumentszoptional argumentscSs|S)Nr&)rr&r&r'identitysz)ArgumentParser.__init__..identityrrhr2rkzshow this help message and exit)rorrk)rrrNr7pathbasenamerargvrLrdepilogformatter_classfromfile_prefix_charsadd_help allow_abbrevr)r} _positionals _optionals _subparsersrrqrr3rrr)r!rLrdr rAparentsrBrrCr rrDrEZ superinitZ add_groupr<Zdefault_prefixrSdefaults)rr&r'rNYsB     zArgumentParser.__init__cs"ddddddg}fdd|DS) NrLrdr rBrrDcsg|]}|t|fqSr&)r)rUr$)r!r&r'rXsz.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 argumentsrr1r Z subcommandsrLrYr r)rHr;r}rrrr)rFrr_get_positional_actionsrrhrdr\rtr"r%) r!rr1r rRrrfZ parsers_classror&r&r'add_subparserss$   zArgumentParser.add_subparserscCs$|jr|j|n |j||S)N)rrGr%rF)r!ror&r&r'r%s zArgumentParser._add_actioncCsdd|jDS)NcSsg|]}|jr|qSr&)r)rUror&r&r'rXsz8ArgumentParser._get_optional_actions..)r)r!r&r&r'_get_optional_actionssz$ArgumentParser._get_optional_actionscCsdd|jDS)NcSsg|]}|js|qSr&)r)rUror&r&r'rXsz:ArgumentParser._get_positional_actions..)r)r!r&r&r'rKsz&ArgumentParser._get_positional_actionscCs4|||\}}|r0td}||d||S)Nzunrecognized arguments: %srv)rr}r;r )r!rWrr@rr&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)NrO)rr@r/r rrrrrrr_parse_known_argsrrrdelattrrexc_infor;r)r!rWrrorerrr&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)NrOz--rAOrYcs|||}||jk rf|x:|gD]*}|kr8td}t|}t|||q8W|tk r||||dS)Nznot allowed with argument %s)r _get_valuesrrr}rrr)roZargument_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 Wx |D]\}}}|||qdW| S)NrOrSrzignored explicit argument %r)_match_argumentrrrr}r) start_index option_tupleror explicit_argZmatch_argumentZ action_tuples arg_countr&charZnew_explicit_argZ optionals_maprstoprWrZselected_patterns)r"arg_strings_patternextrasoption_string_indicesr!rYr&r'consume_optionalHsN       z:ArgumentParser._parse_known_args..consume_optionalcsrj}|d}||}x8t|D]*\}}|||}||7}||q(Wt|ddd<|S)N)_match_arguments_partialzipri)r[Z match_partialZselected_patternZ arg_countsror^rW)r"rarr!rYr&r'consume_positionalss  z=ArgumentParser._parse_known_args..consume_positionalsrrcsg|]}|kr|qSr&r&)rUr)r[r&r'rXsz4ArgumentParser._parse_known_args..z(the following arguments are required: %sz, cSsg|]}|jtk rt|qSr&)rkrr)rUror&r&r'rXsz#one of the arguments %s is requiredrv)N)rC_read_args_from_filesrrrrriterr_parse_optionalr rrKr?r>rrrrrrrrrr _get_valuer;r})r!r"rr2rrZ mutex_actionZ conflictsZarg_string_pattern_partsZarg_strings_iter arg_stringr\patternrdrgZmax_option_string_indexZnext_option_string_indexZpositionals_end_indexZstringsZ stop_indexZrequired_actionsrorrrr&) rVr"rarbrrcrrWrXr!r[rYr'rOs        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)NrrO) rCrrreadrconvert_arg_line_to_argsrhrrrrQr;r)r!r"Znew_arg_stringsrlZ args_filearg_liner#rRr&r&r'rhs     z$ArgumentParser._read_args_from_filescCs|gS)Nr&)r!rpr&r&r'rosz'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 argumentsrO) _get_nargs_patternrGr.r}r r rrrrrir)r!rora nargs_patternr.Z nargs_errorsrr&r&r'rZs     zArgumentParser._match_argumentcstg}xjtt|ddD]V}|d|}dfdd|D}t||}|dk r|dd|DPqW|S)NrrrYcsg|]}|qSr&)rq)rUro)r!r&r'rX3sz;ArgumentParser._match_arguments_partial..cSsg|] }t|qSr&)ri)rUrr&r&r'rX7s)rrir rGr.rrf)r!rerarrZ actions_slicermr.r&)r!r're-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) NrrO=z, cSsg|]\}}}|qSr&r&)rUrorr]r&r&r'rX]sz2ArgumentParser._parse_optional..)r4Zmatchesz4ambiguous option: %(option)s could match %(matches)srv) rrrisplitrE_get_option_tuplesr r}r;rr.r) r!rlrorr]Z option_tuplesZoptionsrWrr\r&r&r'rj=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)NrrOrsr2zunexpected option string: %s)rrtr startswithrr;r}) r!rrr&Z option_prefixr]rorZshort_option_prefixZshort_explicit_argr&r&r'ruxs8             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-*rSrYr) rr rr rrrr rr6)r!rorrrr&r&r'rqs(  z!ArgumentParser._get_nargs_patterncCs4|||\}}|r0td}||d||S)Nzunrecognized arguments: %srv)parse_known_intermixed_argsr}r;r )r!rWrr@rr&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)rUror&r&r'rXsz>ArgumentParser.parse_known_intermixed_args..z3parse_intermixed_args: positional arg with nargs=%srcs&g|]}|jD]}|kr|jqqSr&)rr)rUrro)rr&r'rXsz;parse_intermixed_args: positional in mutuallyExclusiveGroup)warnzDo not expect %s in %sF)rKr$rrrd format_usageZ save_nargsrrZ save_defaultrrrrwarningsrzrPrMrZ save_required) r!rWraZ save_usageroZremaining_argsrzrrrbr&)rr'rwsX               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--rOcsg|]}|qSr&)rk)rUv)ror!r&r'rXM sz.ArgumentParser._get_values..csg|]}|qSr&)rk)rUr~)ror!r&r'rXQ srcsg|]}|qSr&)rk)rUr~)ror!r&r'rXZ s)rrrr/r:r rrrrrrk _check_valuerrir)r!ror"r%rlr~r&)ror!r'rU) 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 callablerrO)rr%z!invalid %(type)s value: %(value)r) rrr#r}rrrrrrrQr$r:)r!rorlr'rrr$rWr&r&r'rka s   zArgumentParser._get_valuecCsF|jdk rB||jkrB|dtt|jd}td}t|||dS)Nz, )r%rz3invalid choice: %(value)r (choose from %(choices)s))rr maprr}r)r!ror%rWrr&r&r'r{ s zArgumentParser._check_valuecCs$|}||j|j|j|S)N)rrhrdrrr\)r!rRr&r&r'r{ s zArgumentParser.format_usagecCsx|}||j|j|j||jx:|jD]0}||j ||j| |j | q0W||j |S)N)rrhrdrrrbr rr^r1rrrr_rAr\)r!rRZ action_groupr&r&r'r\ s        zArgumentParser.format_helpcCs|j|jdS)N)rL)rBrL)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!Zstatusrr&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. )rLrr2z%(prog)s: error: %(message)s N)rrrrLrr})r!rrWr&r&r'r; s  zArgumentParser.error)NN)NN)NN)NN)N)N)N)rN)#rr,r-r.rrNrrLr%rMrKrNrrOrhrorZrerjrurqrxrwrUrkrr{r\rrrrrr;rr&r&)rr'rFsT4  #w;,1  M8    )0r. __version____all__osr7rerGsysrrr}rrr rr rrrrrr1rrrrr r Exceptionrrr rrrrrrrrrrrr r r(r*rr&r&r&r'>sz{    [#&b65"