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:  ) rTrSrP _join_partsr+rRrUrrA)r!r Z item_helpZcurrent_indentrUr&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!rWrXr&r&r' _add_itemszHelpFormatter._add_itemcCs0||||j|}||jg||_dS)N)rPrDrFr^r])r!rUZsectionr&r&r' start_sectionszHelpFormatter.start_sectioncCs|jj|_|dS)N)rFrTrR)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!usageactionsgroupsprefixrXr&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)rVsr&r&r'rY 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)rr)r!rfrpr&r&r' add_argumentss zHelpFormatter.add_argumentscCs.|j}|r*|jd|}|dd}|S)Nz r[)rEr]rKsubstrip)r!rlr&r&r'r]s  zHelpFormatter.format_helpcCsddd|DS)NrZcSsg|]}|r|tk r|qSr&)r)rVpartr&r&r'rY#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: )rLz%(prog)s cSsg|] }|r|qSr&r&)rVrkr&r&r'rYCsz/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)NrOrwr)rjrr )partsindentrhlineslineZline_lenrv) text_widthr&r' get_linesWs"    z.HelpFormatter._format_usage..get_linesg?rOr[z%s%s )N)_dictr;option_stringsr_format_actions_usager r@rArjrGfindallrQextend)r!rerfrgrhrL optionals positionalsrpformatZ action_usageZ part_regexpZ opt_usageZ pos_usageZ opt_partsZ pos_partsr}ryrzrxr&)r|r'rd'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 (()rO|z%sz%s %sz[%s]T)reverserwcSsg|]}|dk r|qS)Nr&)rVitemr&r&r'rYsz7HelpFormatter._format_actions_usage..z[\[(]z[\])]z(%s) z\1z (%s)z%s *%srZz \(([^|]*)\))setindex_group_actionsr:rjaddrequiredrange enumeraterlrrgetpopr#_get_default_metavar_for_positional _format_argsnargs!_get_default_metavar_for_optionalr)r rGrtru)r!rfrg group_actionsZinsertsgroupstartendrpirxdefaultrv option_string args_stringrbopencloser&r&r'rsz                 z#HelpFormatter._format_actions_usagecCsFd|kr|t|jd}t|j|jd}d|j}||||dS)Nz%(prog))rL rwz )rr;r?r@rA _fill_text)r!rbr|ryr&r&r'ras  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) Nr2rrZz%*s%s z %*s%-*s rrOr[)r>rCr=r?r@rArmrlrj _expand_help _split_linesrendswithrnror\) r!rpZ help_positionZ help_widthZ action_widthZ action_headertupZ indent_firstrxZ help_textZ help_linesr{rqr&r&r'ros6          zHelpFormatter._format_actioncCs|js&||}|||d\}|Sg}|jdkrB||jn8||}|||}x |jD]}|d||fq`Wd|SdS)NrOrz%s %sz, ) rr_metavar_formatterrrrrrr )r!rprmetavarrxrrr&r&r'rm$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!rpdefault_metavarZ choice_strsrr&)rr'r<s   z HelpFormatter._metavar_formattercCs|||}|jdkr$d|d}n|jtkr.rw) rrr rr rrrrr )r!rprZ 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)rVcr&r&r'rYjsz.HelpFormatter._expand_help..) rvarsr;r/rhasattrrrr _get_help_string)r!rpZparamsr$Z choices_strr&r&r'ras  zHelpFormatter._expand_helpccs@y |j}Wntk rYnX||EdH|dS)N)_get_subactionsAttributeErrorrPrR)r!rpZget_subactionsr&r&r'rnns  z'HelpFormatter._iter_indented_subactionscCs&|jd|}ddl}|||S)Nrwr)rJrtrutextwrapZwrap)r!rbrMrr&r&r'rxszHelpFormatter._split_linescCs,|jd|}ddl}|j||||dS)Nrwr)Zinitial_indentZsubsequent_indent)rJrtrurZfill)r!rbrMryrr&r&r'rs zHelpFormatter._fill_textcCs|jS)N)rl)r!rpr&r&r'rszHelpFormatter._get_help_stringcCs |jS)N)destupper)r!rpr&r&r'rsz/HelpFormatter._get_default_metavar_for_optionalcCs|jS)N)r)r!rpr&r&r'rsz1HelpFormatter._get_default_metavar_for_positional)r2r3N)N) rr,r-r.rNrPrRobjectrDr^r_r`rcrirrrsr]r\rdrrarormrrrrnrrrrrr&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)NrZc3s|]}|VqdS)Nr&)rVr{)ryr&r' sz9RawDescriptionHelpFormatter._fill_text..T)keepends)r splitlines)r!rbrMryr&)ryr'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!rbrMr&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))rlrrr rrr)r!rprlZdefaulting_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!rpr&r&r'rsz:MetavarTypeHelpFormatter._get_default_metavar_for_optionalcCs|jjS)N)rr)r!rpr&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) rrrrrrrrrlr)r:r superrrN) r!rrrrrrrrrlr) __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)rrrrrrrl)rrrN)r!rrrrrrlr)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)rrrrrrl)rrrN)r!rrrrrl)rr&r'rNs z_StoreTrueAction.__init__)FFN)rr,r-rNrr&r&)rr'rsrcseZdZdfdd ZZS)_StoreFalseActionTFNcs tt|j||d|||ddS)NF)rrrrrrl)rrrN)r!rrrrrl)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) rrrrrrrrrlr)r:r rrrN) r!rrrrrrrrrlr)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)rrrrrrrlr)rrrN)r!rrrrrrlr)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)rrrrrrl)rrrN)r!rrrrrl)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)rrrrrl)rrrN)r!rrrrl)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)rrrrrl)rrrNversion)r!rrrrrl)rr&r'rNs z_VersionAction.__init__cCsD|j}|dkr|j}|}||||tj|dS)N)r_get_formatterrc_print_messager]_sysstdoutr)r!rrrrrrSr&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, )rrrlr)r rr_ChoicesPseudoActionrN)r!r$aliasesrlrrZsup)rr&r'rN5s   z/_SubParsersAction._ChoicesPseudoAction.__init__)rr,r-rNrr&r&)rr'r3srFNc s<||_||_i|_g|_tt|j||t|j|||ddS)N)rrrrrrlr) _prog_prefix _parser_class_name_parser_map_choices_actionsrrrNr)r!rrL parser_classrrrlr)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&rl)rrrrrrrr)r!r$kwargsrrlZ 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"rXmsgZ 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)rVr#r&r&r'rYsz%FileType.__repr__..cSs$g|]\}}|dk rd||fqS)Nz%s=%rr&)rVkwr#r&r&r'rYsz%s(%s))rrrrr rr)r!rXrZargs_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.rNrr r&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) NrpZstoreZ store_const store_trueZ store_falserZ append_constrrlrparsersz^-\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 rrr)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!rrpr&r&r' set_defaultss   z_ActionsContainer.set_defaultscCs8x(|jD]}|j|kr|jdk r|jSqW|j|dS)N)rrrrr)r!rrpr&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)rrjr:_get_positional_kwargs_get_optional_kwargsrr_pop_action_classcallablerrrrr TypeError _add_action)r!rXrcharsrZ action_classrp type_funcr&r&r'rr0s2          z_ActionsContainer.add_argumentcOs t|f||}|j||S)N)_ArgumentGrouprr)r!rXrrr&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!rprr&r&r'r&is     z_ActionsContainer._add_actioncCs|j|dS)N)rremove)r!rpr&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)rr2r~r:r*r rrrr,rrrr&)r!r.Ztitle_group_maprrZ group_maprp 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:rrjrlstripreplacer) r!rXrrZlong_option_stringsrrrZdest_option_stringr&r&r'r"s0          z&_ActionsContainer._get_optional_kwargscCs|d|}|d||S)Nrp)rr)r!rrrpr&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!rpZconfl_optionalsrZconfl_optionalrr&r&r'r-s   z!_ActionsContainer._check_conflictcCs6tddt|}ddd|D}t|||dS)Nzconflicting option string: %szconflicting option strings: %sz, cSsg|] \}}|qSr&r&)rVrrpr&r&r'rYsz<_ActionsContainer._handle_conflict_error..)rrjr r)r!rpconflicting_actionsrZconflict_stringr&r&r'_handle_conflict_errors    z(_ActionsContainer._handle_conflict_errorcCsBx<|D]4\}}|j||j|d|js|j|qWdS)N)rr0rrr.r1)r!rpr8rr&r&r'_handle_conflict_resolves  z*_ActionsContainer._handle_conflict_resolve)N)N)rr,r-rNrrrr rrr*r,r&r1r4r!r"r#rr-r9r:rr&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)Nrrrr )rrrrrr)rNr2rrrrrrr)r!r.r2r rrZ super_init)rr&r'rNs    z_ArgumentGroup.__init__cs tt||}|j||S)N)rr)r&rr)r!rp)rr&r'r&(s z_ArgumentGroup._add_actioncs tt|||j|dS)N)rr)r1rr0)r!rp)rr&r'r1-sz_ArgumentGroup._remove_action)NN)rr,r-rNr&r1rr&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!rprr&r&r'r&9s   z#_MutuallyExclusiveGroup._add_actioncCs|j||j|dS)N)r;r1rr0)r!rpr&r&r'r1As z&_MutuallyExclusiveGroup._remove_action)F)rr,r-rNr&r1rr&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 rrrrzpositional argumentszoptional argumentscSs|S)Nr&)rr&r&r'identitysz)ArgumentParser.__init__..identityrrhr2rlzshow this help message and exit)rprrl)rrrNr7pathbasenamerargvrLreepilogformatter_classfromfile_prefix_charsadd_help allow_abbrevr*r~ _positionals _optionals _subparsersrrrrr4rrr)r!rLrer rBparentsrCrrDrrrErFZ superinitZ add_groupr=Zdefault_prefixrTdefaults)rr&r'rNYsB     zArgumentParser.__init__cs"ddddddg}fdd|DS) NrLrer rCrrEcsg|]}|t|fqSr&)r)rVr$)r!r&r'rYsz.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 argumentsrr2r Z subcommandsrLrZr r)rIr<r~rrrr*rGrr_get_positional_actionsrrirer]rur#r&) r!rr2r rSrrgZ parsers_classrpr&r&r'add_subparserss$   zArgumentParser.add_subparserscCs$|jr|j|n |j||S)N)rrHr&rG)r!rpr&r&r'r&s zArgumentParser._add_actioncCsdd|jDS)NcSsg|]}|jr|qSr&)r)rVrpr&r&r'rYsz8ArgumentParser._get_optional_actions..)r)r!r&r&r'_get_optional_actionssz$ArgumentParser._get_optional_actionscCsdd|jDS)NcSsg|]}|js|qSr&)r)rVrpr&r&r'rYsz:ArgumentParser._get_positional_actions..)r)r!r&r&r'rLsz&ArgumentParser._get_positional_actionscCs4|||\}}|r0td}||d||S)Nzunrecognized arguments: %srw)rr~r<r )r!rXrrArr&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)rrAr/r rrrrrrr_parse_known_argsrrrdelattrrexc_infor<r)r!rXrrprerrr&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--rAOrZcs|||}||jk rf|x:|gD]*}|kr8td}t|}t|||q8W|tk r||||dS)Nznot allowed with argument %s)r _get_valuesrrr~rrr)rpZargument_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)NrOrTrzignored explicit argument %r)_match_argumentrrrr~rrQ) start_index option_tuplerpr explicit_argZmatch_argumentZ action_tuples arg_countr'charZnew_explicit_argZ optionals_maprstoprXrZselected_patterns)r"arg_strings_patternextrasoption_string_indicesr!rZr&r'consume_optionalHsP        z:ArgumentParser._parse_known_args..consume_optionalcsrj}|d}||}x8t|D]*\}}|||}||7}||q(Wt|ddd<|S)N)_match_arguments_partialziprj)r\Z match_partialZselected_patternZ arg_countsrpr_rX)r"rbrr!rZr&r'consume_positionalss  z=ArgumentParser._parse_known_args..consume_positionalsrrcsg|]}|kr|qSr&r&)rVr)r\r&r'rYsz4ArgumentParser._parse_known_args..z(the following arguments are required: %sz, cSsg|]}|jtk rt|qSr&)rlrr)rVrpr&r&r'rYsz#one of the arguments %s is requiredrw)N)rD_read_args_from_filesrrrrriterr_parse_optionalr rrLr?r>rrrrrrrrrr _get_valuer<r~)r!r"rr3rrZ mutex_actionZ conflictsZarg_string_pattern_partsZarg_strings_iter arg_stringr]patternrerhZmax_option_string_indexZnext_option_string_indexZpositionals_end_indexZstringsZ stop_indexZrequired_actionsrprrrr&) rWr"rbrcrrdrrXrYr!r\rZr'rPs        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) rDrrreadrconvert_arg_line_to_argsrirrrrRr<r)r!r"Znew_arg_stringsrmZ args_filearg_liner#rSr&r&r'ris     z$ArgumentParser._read_args_from_filescCs|gS)Nr&)r!rqr&r&r'rpsz'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 rrrrrjr)r!rprb nargs_patternr/Z nargs_errorsrr&r&r'r[s     zArgumentParser._match_argumentcstg}xjtt|ddD]V}|d|}dfdd|D}t||}|dk r|dd|DPqW|S)NrrrZcsg|]}|qSr&)rr)rVrp)r!r&r'rY3sz;ArgumentParser._match_arguments_partial..cSsg|] }t|qSr&)rj)rVrr&r&r'rY7s)rrjr rGr/rrg)r!rfrbrrZ actions_slicernr/r&)r!r'rf-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&)rVrprr^r&r&r'rY]sz2ArgumentParser._parse_optional..)r5Zmatchesz4ambiguous option: %(option)s could match %(matches)srw) rrrjsplitrF_get_option_tuplesr r~r<rr/r) r!rmrprr^Z option_tuplesZoptionsrXrr]r&r&r'rk=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)NrrOrtr2zunexpected option string: %s)rrur startswithrr<r~) r!rrr'Z option_prefixr^rprZshort_option_prefixZshort_explicit_argr&r&r'rvxs8             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-*rTrZr) rr rr rrrr rr7)r!rprrsr&r&r'rrs(  z!ArgumentParser._get_nargs_patterncCs4|||\}}|r0td}||d||S)Nzunrecognized arguments: %srw)parse_known_intermixed_argsr~r<r )r!rXrrArr&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)rVrpr&r&r'rYsz>ArgumentParser.parse_known_intermixed_args..z3parse_intermixed_args: positional arg with nargs=%srcs&g|]}|jD]}|kr|jqqSr&)rr)rVrrp)rr&r'rYsz;parse_intermixed_args: positional in mutuallyExclusiveGroup)warnzDo not expect %s in %sF)rLr%rrre format_usageZ save_nargsrrZ save_defaultrrrrwarningsr{rQrNrZ save_required) r!rXraZ save_usagerpZremaining_argsr{rrrcr&)rr'rxsX               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&)rl)rVv)rpr!r&r'rYM sz.ArgumentParser._get_values..csg|]}|qSr&)rl)rVr)rpr!r&r'rYQ srcsg|]}|qSr&)rl)rVr)rpr!r&r'rYZ s)rrrr0r:r rrrrrrl _check_valuerrjr)r!rpr"r%rmrr&)rpr!r'rV) 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~rrrrrrrRr%r:)r!rprmr(rrr$rXr&r&r'rla 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!rpr%rXrr&r&r'r{ s zArgumentParser._check_valuecCs$|}||j|j|j|S)N)rrirerrr])r!rSr&r&r'r| s zArgumentParser.format_usagecCsx|}||j|j|j||jx:|jD]0}||j ||j| |j | q0W||j |S)N)rrirerrrcr rr_r2rsrr`rBr])r!rSZ action_groupr&r&r'r] s        zArgumentParser.format_helpcCs|j|jdS)N)rL)rCrL)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!rrXr&r&r'r< s  zArgumentParser.error)NN)NN)NN)NN)N)N)N)rN)#rr,r-r.rrNrrMr&rNrLrOrrPrirpr[rfrkrvrrryrxrVrlrr|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"