[o`@sCdZdZdddddddd d d d d dddddgZddlZddlZddlZddl Z ddl Z ddl ZyddlmZmZWn*ek rddZddZYnXdZdZdZdZdZdZd ZGd!d"d"eZd#d$ZGd%ddeZGd&ddeZGd'd d eZGd(ddeZ Gd)d d eZ!d*d+Z"Gd,dde#Z$Gd-dde#Z%Gd.d d eZ&Gd/d0d0e&Z'Gd1d2d2e&Z(Gd3d4d4e(Z)Gd5d6d6e(Z*Gd7d8d8e&Z+Gd9d:d:e&Z,Gd;d<d<e&Z-Gd=d>d>e&Z.Gd?d@d@e&Z/GdAdBdBe&Z0GdCddeZ1GdDd d eZ2GdEdFdFeZ3GdGdHdHe3Z4GdIdJdJe4Z5GdKddee3Z6dS)La 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)gettextngettextcCs|S)N)messagerr/usr/lib/python3.4/argparse.py_`srcCs|dkr|S|SdS)Nr)ZsingularZpluralnrrrrbs rz ==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}x'|jD]}|jt|q"Wx1|jD]#\}}|jd||fqLWd|dj|fS)Nz%s=%rz%s(%s)z, )type__name__ _get_argsappendrepr _get_kwargsjoin)selfZ type_name arg_stringsargnamevaluerrr__repr__sz_AttributeHolder.__repr__cCst|jjS)N)sorted__dict__items)r&rrrr$sz_AttributeHolder._get_kwargscCsgS)Nr)r&rrrr!sz_AttributeHolder._get_argsN)r __module__ __qualname____doc__r+r$r!rrrrrvs  rcCs8t||ddkr+t|||nt||S)N)getattrsetattr) namespacer)r*rrr _ensure_valuesr5c@speZdZdZdddddZddZd d ZGd d d eZd dZ ddZ ddZ ddZ dddZ ddZddZddZddZdd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd+d,Zd-d.Zd/d0Zd1d2Zd3d4Zd5d6Zd7d8Zd9d:ZdS);rzFormatter for generating usage messages and argument help strings. Only the name of this class is considered a public API. All the methods provided by the class are considered an implementation detail. NcCs|dkrQyttjd}Wnttfk rCd}YnX|d8}n||_||_||_t|t |d|d|_||_ d|_ d|_ d|_ |j|d|_|j|_tjd|_tjd|_dS)NZCOLUMNSPr6rz\s+z\n\n\n+)int_osenvironKeyError ValueError_prog_indent_increment_max_help_positionminmax_width_current_indent_level_action_max_length_Section _root_section_current_section_recompile_whitespace_matcher_long_break_matcher)r&progZindent_incrementZmax_help_positionwidthrrr__init__s&           zHelpFormatter.__init__cCs%|j|j7_|jd7_dS)Nr)rEr@rF)r&rrr_indentszHelpFormatter._indentcCs@|j|j8_|jdks-td|jd8_dS)NrzIndent decreased below 0.r)rEr@AssertionErrorrF)r&rrr_dedentszHelpFormatter._dedentc@s+eZdZdddZddZdS)zHelpFormatter._SectionNcCs(||_||_||_g|_dS)N) formatterparentheadingr.)r&rUrVrWrrrrQs   zHelpFormatter._Section.__init__cCs|jdk r|jjn|jj}x!|jD]\}}||q5W|dd|jD}|jdk r|jjn|sdS|jtk r|jdk r|jj}d|d|jf}nd}|d||dgS)NcSs"g|]\}}||qSrr).0funcargsrrr s z6HelpFormatter._Section.format_help..z%*s%s:  ) rVrUrR _join_partsr.rTrWrrE)r&r%rYrZZ item_helpZcurrent_indentrWrrr format_helps  z"HelpFormatter._Section.format_help)r r/r0rQr_rrrrrHs rHcCs|jjj||fdS)N)rJr.r")r&rYrZrrr _add_itemszHelpFormatter._add_itemcCsB|j|j||j|}|j|jg||_dS)N)rRrHrJr`r_)r&rWZsectionrrr start_sections zHelpFormatter.start_sectioncCs|jj|_|jdS)N)rJrVrT)r&rrr end_sectionszHelpFormatter.end_sectioncCs5|tk r1|dk r1|j|j|gndS)N)rr` _format_text)r&textrrradd_textszHelpFormatter.add_textcCs8|tk r4||||f}|j|j|ndS)N)rr` _format_usage)r&usageactionsgroupsprefixrZrrr add_usages zHelpFormatter.add_usagecCs|jtk r|j}||g}x*|j|D]}|j||q7Wtdd|D}||j}t|j||_|j|j |gndS)NcSsg|]}t|qSr)len)rXsrrrr[s z.HelpFormatter.add_argument..) helpr_format_action_invocation_iter_indented_subactionsr"rCrErGr`_format_action)r&actionZget_invocationZ invocations subactionZinvocation_lengthZ action_lengthrrr add_arguments    zHelpFormatter.add_argumentcCs"x|D]}|j|qWdS)N)rt)r&rhrrrrr add_argumentss zHelpFormatter.add_argumentscCsD|jj}|r@|jjd|}|jdd}n|S)Nz r])rIr_rNsubstrip)r&rnrrrr_s zHelpFormatter.format_helpcCsdjdd|DS)Nr\cSs(g|]}|r|tk r|qSr)r)rXpartrrrr[&s z-HelpFormatter._join_parts..)r%)r&Z part_stringsrrrr^%szHelpFormatter._join_partscs|dkrtd}n|dk r@|td|j}n|dkrl| rldtd|j}n|dkrdtd|j}g}g}x4|D],}|jr|j|q|j|qW|j} | |||} djdd|| gD}|j|jt |t |krd} | ||} | ||} t j | | }t j | | }dj|| kst dj|| kst dfdd }t |t |d krrdt |t |d }|rD||g|||}|j |||q|rf||g|||}q|g}n}dt |}||}|||}t |d krg}|j ||||j |||n|g|}d j|}qnd ||fS)Nzusage: rOz%(prog)s cSsg|]}|r|qSrr)rXrmrrrr[Fs z/HelpFormatter._format_usage..z\(.*?\)+|\[.*?\]+|\S+csg}g}|dk r+t|d}nt|d}x|D]z}|dt|kr|r|j|dj|g}t|d}n|j||t|d7}qBW|r|j|dj|n|dk r|dt|d|d.get_linesg?rr]z%s%s )rdictr?option_stringsr"_format_actions_usager%rDrErlrKfindallrSextend)r&rgrhrirjrOZ optionals positionalsrrformatZ action_usageZ part_regexpZ opt_usageZ pos_usageZ opt_partsZ pos_partsrr{r|rzr)r~rrf*sZ      "    zHelpFormatter._format_usagec Cst}i}x&|D]}y|j|jd}Wntk rMwYqX|t|j}||||jkrx|jD]}|j|qW|js||kr||d7.z[\[(]z[\])]z(%s) z\1z (%s)z%s *%sr\z \(([^|]*)\)r)setindex_group_actionsr>rladdrequiredrange enumeraternrr"getpopr#_get_default_metavar_for_positional _format_argsnargs!_get_default_metavar_for_optionalr,r%rKrvrw)r&rhri group_actionsZinsertsgroupstartendrrirzdefaultrx option_string args_stringrdopencloserrrrsr                  z#HelpFormatter._format_actions_usagecCsbd|kr%|td|j}nt|j|jd}d|j}|j|||dS)Nz%(prog)rO ryz )rr?rCrDrE _fill_text)r&rdr~r{rrrrcs   zHelpFormatter._format_textc Cst|jd|j}t|j|d}||jd}|j|}|jsw|jd|f}d|}n\t||kr|jd||f}d|}d}n"|jd|f}d|}|}|g}|jr\|j |} |j | |} |j d|d| dfxT| ddD] } |j d|d| fq5Wn|j ds{|j dnx-|j |D]} |j |j| qW|j|S) Nr6rr\z%*s%s z %*s%-*s rrr])rBrGrArCrDrErornrl _expand_help _split_linesr"endswithrprqr^) r&rrZ help_positionZ help_widthZ action_widthZ action_headertupZ indent_firstrzZ help_textZ help_linesr}rsrrrrqs6         !zHelpFormatter._format_actioncCs|js7|j|}|j||d\}|Sg}|jdkr_|j|jnL|j|}|j||}x(|jD]}|jd||fqWdj|SdS)Nrrz%s %sz, ) rr_metavar_formatterrrrrr"r%)r&rrrmetavarrzrrrrrros z'HelpFormatter._format_action_invocationcsr|jdk r|jnA|jdk rVdd|jD}ddj|n|fdd}|S)NcSsg|]}t|qSr)str)rXZchoicerrrr[9s z4HelpFormatter._metavar_formatter..z{%s},cs"ttrSf|SdS)N) isinstancetuple)Z tuple_size)resultrrr>sz0HelpFormatter._metavar_formatter..format)rchoicesr%)r&rrdefault_metavarZ choice_strsrr)rrr5s z HelpFormatter._metavar_formattercCs|j||}|jdkr4d|d}n|jtkrVd|d}n|jtkrxd|d}n|jtkrd|d}nr|jtkrd}nZ|jtkrd|d}n8d d t|jD}d j|||j}|S) Nz%srz[%s]z [%s [%s ...]]r6z %s [%s ...]z...z%s ...cSsg|] }dqS)z%sr)rXrrrrr[Ts z.HelpFormatter._format_args..ry) rrr rr rrrr%)r&rrrZ get_metavarrZformatsrrrrEs  zHelpFormatter._format_argscCstt|d|j}x.t|D] }||tkr(||=q(q(Wx;t|D]-}t||drY||j||.) rvarsr?listrhasattrr rr%_get_help_string)r&rrZparamsr)Z choices_strrrrrXs  zHelpFormatter._expand_helpc csFy |j}Wntk r!Yn!X|j|DdH|jdS)N)_get_subactionsAttributeErrorrRrT)r&rrZget_subactionsrrrrpes    z'HelpFormatter._iter_indented_subactionscCs+|jjd|j}tj||S)Nry)rMrvrw _textwrapZwrap)r&rdrPrrrroszHelpFormatter._split_linescCs7|jjd|j}tj||d|d|S)NryZinitial_indentZsubsequent_indent)rMrvrwrZfill)r&rdrPr{rrrrsszHelpFormatter._fill_textcCs|jS)N)rn)r&rrrrrrxszHelpFormatter._get_help_stringcCs |jjS)N)destupper)r&rrrrrr{sz/HelpFormatter._get_default_metavar_for_optionalcCs|jS)N)r)r&rrrrrr~sz1HelpFormatter._get_default_metavar_for_positional) r r/r0r1rQrRrTobjectrHr`rarbrerkrtrur_r^rfrrcrqrorrrrprrrrrrrrrrs<   !         \ a  /       c@s"eZdZdZddZdS)rzHelp message formatter which retains any formatting in descriptions. Only the name of this class is considered a public API. All the methods provided by the class are considered an implementation detail. cs,djfdd|jddDS)Nr\c3s|]}|VqdS)Nr)rXr})r{rr sz9RawDescriptionHelpFormatter._fill_text..keependsT)r% splitlines)r&rdrPr{r)r{rrsz&RawDescriptionHelpFormatter._fill_textN)r r/r0r1rrrrrrs c@s"eZdZdZddZdS)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 |jS)N)r)r&rdrPrrrrsz!RawTextHelpFormatter._split_linesN)r r/r0r1rrrrrrs c@s"eZdZdZddZdS)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. cCsb|j}d|jkr^|jtk r^ttg}|jsK|j|kr[|d7}q[q^n|S)Nz %(default)z (default: %(default)s))rnrrr rrr)r&rrrnZdefaulting_nargsrrrrs  z.ArgumentDefaultsHelpFormatter._get_help_stringN)r r/r0r1rrrrrrs c@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&rrrrrrsz:MetavarTypeHelpFormatter._get_default_metavar_for_optionalcCs |jjS)N)rr )r&rrrrrrsz.r)r&namesr)r&rr$0s zAction._get_kwargscCsttddS)Nz.__call__() not defined)NotImplementedErrorr)r&parserr4valuesrrrr__call__>szAction.__call__)r r/r0r1rQr$rrrrrr s 1 c sIeZdZddddddddfddZdddZS) _StoreActionNFc s|dkrtdn|dk rF|tkrFtdtntt|jd|d|d|d|d|d |d |d |d | 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 constrrrrrrrrrnr)r>r superrrQ) r&rrrrrrrrrnr) __class__rrrQDs z_StoreAction.__init__cCst||j|dS)N)r3r)r&rr4rrrrrrasz_StoreAction.__call__)r r/r0rQrrr)rrrBs rcs=eZdZddddfddZdddZS)_StoreConstActionNFcsAtt|jd|d|ddd|d|d|d|dS) Nrrrrrrrrn)rrrQ)r&rrrrrrnr)rrrrQgsz_StoreConstAction.__init__cCst||j|jdS)N)r3rr)r&rr4rrrrrrxsz_StoreConstAction.__call__)r r/r0rQrrr)rrres  rcs+eZdZdddfddZS)_StoreTrueActionFNc s;tt|jd|d|ddd|d|d|dS)NrrrTrrrn)rrrQ)r&rrrrrn)rrrrQ~sz_StoreTrueAction.__init__)r r/r0rQrr)rrr|s rcs+eZdZdddfddZS)_StoreFalseActionTFNc s;tt|jd|d|ddd|d|d|dS)NrrrFrrrn)rrrQ)r&rrrrrn)rrrrQsz_StoreFalseAction.__init__)r r/r0rQrr)rrrs rc sIeZdZddddddddfddZdddZS) _AppendActionNFc s|dkrtdn|dk rF|tkrFtdtntt|jd|d|d|d|d|d |d |d |d | 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 constrrrrrrrrrnr)r>r rrrQ) r&rrrrrrrrrnr)rrrrQs z_AppendAction.__init__cCsBtjt||jg}|j|t||j|dS)N)_copycopyr5rr"r3)r&rr4rrr.rrrrs z_AppendAction.__call__)r r/r0rQrrr)rrrs rcs=eZdZddddfddZdddZS)_AppendConstActionNFcsGtt|jd|d|ddd|d|d|d|d |dS) Nrrrrrrrrnr)rrrQ)r&rrrrrrnr)rrrrQsz_AppendConstAction.__init__cCsEtjt||jg}|j|jt||j|dS)N)rrr5rr"rr3)r&rr4rrr.rrrrsz_AppendConstAction.__call__)r r/r0rQrrr)rrrs  rcs:eZdZdddfddZdddZS) _CountActionNFc s;tt|jd|d|ddd|d|d|dS)Nrrrrrrrn)rrrQ)r&rrrrrn)rrrrQsz_CountAction.__init__cCs0t||jdd}t||j|dS)Nrr)r5rr3)r&rr4rrZ new_countrrrrsz_CountAction.__call__)r r/r0rQrrr)rrrs  rcs:eZdZeedfddZdddZS) _HelpActionNc s5tt|jd|d|d|ddd|dS)Nrrrrrrn)rrrQ)r&rrrrn)rrrrQs z_HelpAction.__init__cCs|j|jdS)N) print_helpexit)r&rr4rrrrrrs z_HelpAction.__call__)r r/r0rrQrrr)rrrs rcs=eZdZdeedfddZdddZS)_VersionActionNz&show program's version number and exitc s>tt|jd|d|d|ddd|||_dS)Nrrrrrrn)rrrQversion)r&rrrrrn)rrrrQsz_VersionAction.__init__cCsa|j}|dkr!|j}n|j}|j||j|jtj|jdS)N)r_get_formatterre_print_messager__sysstdoutr)r&rr4rrrrUrrrrs     z_VersionAction.__call__)r r/r0rrQrrr)rrrs  rcsheZdZGdddeZeddfddZddZdd Zdd d Z S) _SubParsersActioncs"eZdZfddZS)z&_SubParsersAction._ChoicesPseudoActionc sb|}}|r*|ddj|7}nttj|}|jdgd|d|d|dS)Nz (%s)z, rrrnr)r%rr_ChoicesPseudoActionrQ)r&r)aliasesrnrrZsup)rrrrQ#s  z/_SubParsersAction._ChoicesPseudoAction.__init__)r r/r0rQrr)rrr!s rNc sh||_||_tj|_g|_tt|jd|d|dt d|jd|d|dS)Nrrrrrnr) _prog_prefix _parser_class _collections OrderedDict_name_parser_map_choices_actionsrrrQr)r&rrO parser_classrrnr)rrrrQ+s    z_SubParsersAction.__init__cKs|jddkr/d|j|f|drrrrOSErrorr)r&stringrerrrrrs  zFileType.__call__cCst|j|jf}d|jfd|jfg}djdd|Ddd|D}dt|j|fS)Nrrz, cSs(g|]}|dkrt|qS)rr)r#)rXr(rrrr[s z%FileType.__repr__..cSs2g|](\}}|dk rd||fqS)Nz%s=%rr)rXkwr(rrrr[s z%s(%s))rrrrr%rr )r&rZrZargs_strrrrr+s zFileType.__repr__r)r r/r0r1rQrr+rrrrrs  c@sFeZdZdZddZddZddZdd Zd S) 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)r3)r&rr)rrrrQs zNamespace.__init__cCs)t|tstSt|t|kS)N)rr NotImplementedr)r&otherrrr__eq__szNamespace.__eq__cCst|tstS||k S)N)rr r)r&rrrr__ne__szNamespace.__ne__cCs ||jkS)N)r-)r&rrrr __contains__szNamespace.__contains__N)r r/r0r1rQrrr rrrrr s    cseZdZfddZddZdddZdd Zd d Zd d ZddZ ddZ ddZ ddZ ddZ ddZddZdddZddZd d!Zd"d#Zd$d%ZS)&_ActionsContainercsgtt|j||_||_||_||_i|_|jddt |jddt |jddt |jddt |jddt |jddt |jddt|jddt|jdd t|jdd t|jdd t|jg|_i|_g|_g|_i|_tjd |_g|_dS) NrrZstoreZ store_const store_trueZ store_falser"Z append_constcountrnrparsersz^-\d+$|^-\d*\.\d+$)rr rQ descriptionargument_default prefix_charsconflict_handler _registriesregisterrrrrrrrrrr _get_handler_actions_option_string_actions_action_groups_mutually_exclusive_groups _defaultsrKrL_negative_number_matcher_has_negative_number_optionals)r&rrrr)rrrrQs2           z_ActionsContainer.__init__cCs#|jj|i}|||_get_positional_kwargs_get_optional_kwargsrr_pop_action_classcallablerrrrr TypeError _add_action)r&rZrcharsrZ action_classrr type_funcrrrrt!s2 -      z_ActionsContainer.add_argumentcOs&t|||}|jj||S)N)_ArgumentGrouprr")r&rZrrrrradd_argument_groupPsz$_ActionsContainer.add_argument_groupcKs#t||}|jj||S)N)_MutuallyExclusiveGrouprr")r&rrrrradd_mutually_exclusive_groupUsz._ActionsContainer.add_mutually_exclusive_groupcCs|j||jj|||_x|jD]}||j|r+rrrrr-rrrr')r&r/Ztitle_group_maprrZ group_maprr mutex_grouprrr_add_container_actionsrs,      z(_ActionsContainer._add_container_actionscKsd|kr'td}t|n|jdttgkrOd|dr"rlrlstripreplacer) r&rZrrZlong_option_stringsrrrZdest_option_stringrrrr#s0         z&_ActionsContainer._get_optional_kwargscCs%|jd|}|jd||S)Nrr)rr)r&rrrrrrrr$sz#_ActionsContainer._pop_action_classc CsVd|j}yt||SWn1tk rQtd}t||jYnXdS)Nz_handle_conflict_%sz%invalid conflict_resolution value: %r)rr2rrr>)r&Zhandler_func_namerrrrrs    z_ActionsContainer._get_handlercCsrg}xC|jD]8}||jkr|j|}|j||fqqW|rn|j}|||ndS)N)rrr"r)r&rrZconfl_optionalsrZconfl_optionalrrrrr.s  z!_ActionsContainer._check_conflictcCsKtddt|}djdd|D}t|||dS)Nzconflicting option string: %szconflicting option strings: %sz, cSsg|]\}}|qSrr)rXrrrrrrr[s z<_ActionsContainer._handle_conflict_error..)rrlr%r)r&rrconflicting_actionsrZconflict_stringrrr_handle_conflict_errors  z(_ActionsContainer._handle_conflict_errorcCsZxS|D]K\}}|jj||jj|d|js|jj|qqWdS)N)rr1rrr/r2)r&rrr9rrrr_handle_conflict_resolves  z*_ActionsContainer._handle_conflict_resolve)r r/r0rQrrr r!rtr+r-r'r2r5r"r#r$rr.r:r;rr)rrr s$ 4  /     (  $  r csLeZdZddfddZfddZfddZS)r*Nc s|j}|d|j|d|j|d|jtt|j}|d||||_g|_|j |_ |j |_ |j |_ |j |_ |j |_ |j|_dS)Nrrrr)rrrrrr*rQr3rrrrrrr)r&r/r3rrrZ super_init)rrrrQs        z_ArgumentGroup.__init__cs,tt|j|}|jj||S)N)rr*r'rr")r&rr)rrrr'sz_ArgumentGroup._add_actioncs*tt|j||jj|dS)N)rr*r2rr1)r&rr)rrrr2sz_ArgumentGroup._remove_action)r r/r0rQr'r2rr)rrr*s r*cs=eZdZdfddZddZddZS)r,Fcs,tt|j|||_||_dS)N)rr,rQr _container)r&r/r)rrrrQ%s z _MutuallyExclusiveGroup.__init__cCsJ|jr$td}t|n|jj|}|jj||S)Nz-mutually exclusive arguments must be optional)rrr>r<r'rr")r&rrrrrrr'*s   z#_MutuallyExclusiveGroup._add_actioncCs$|jj||jj|dS)N)r<r2rr1)r&rrrrrr22sz&_MutuallyExclusiveGroup._remove_action)r r/r0rQr'r2rr)rrr,#s  r,cseZdZdZddddgedddddfdd Zdd Zd d Zd d ZddZ ddZ ddddZ ddddZ ddZ ddZddZddZddZd d!Zd"d#Zd$d%Zd&d'Zd(d)Zd*d+Zd,d-Zd.d/Zd0d1Zdd2d3Zdd4d5Zdd6d7Zd8dd9d:Zd;d<ZS)=raaObject 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 NrerrorTc  stt|j} | d|d|d| d| |dkrYtjjtjd}n||_||_ ||_ ||_ ||_ | |_ |j} | td|_| td|_d|_dd }|jd d|d |krd n|d}|j rF|j|d |d ddddtdtdnxM|D]E}|j|y |j}Wntk rYqMX|jj|qMWdS)Nrrrrrzpositional argumentszoptional argumentscSs|S)Nr)rrrridentitymsz)ArgumentParser.__init__..identityrrhr6rnrrrzshow this help message and exit)rrrQr;pathbasenamerargvrOrgepilogformatter_classfromfile_prefix_charsadd_helpr+r _positionals _optionals _subparsersrrtrr5rrr)r&rOrgrrCparentsrDrrErrrFZ superinitZ add_groupr>Zdefault_prefixrVZdefaults)rrrrQIs@                  zArgumentParser.__init__cs/ddddddg}fdd|DS) NrOrgrrDrrFcs%g|]}|t|fqSr)r2)rXr))r&rrr[s z.ArgumentParser._get_kwargs..r)r&rr)r&rr$s zArgumentParser._get_kwargsc KsG|jdk r%|jtdn|jdt|d|ksSd|krt|jdd}t|jdd}|j|||_n |j|_|jddkr|j }|j }|j }|j |j ||d|jj|d.)r)r&rrr_get_optional_actionss z$ArgumentParser._get_optional_actionscCsdd|jDS)NcSsg|]}|js|qSr)r)rXrrrrrr[s z:ArgumentParser._get_positional_actions..)r)r&rrrrKs z&ArgumentParser._get_positional_actionscCsK|j||\}}|rGtd}|j|dj|n|S)Nzunrecognized arguments: %sry)rrr=r%)r&rZr4rBrrrr parse_argss  zArgumentParser.parse_argsc Cs}|dkr"tjdd}n t|}|dkrFt}nx`|jD]U}|jtk rPt||js|jtk rt ||j|jqqqPqPWx:|j D]/}t||st |||j |qqWy[|j ||\}}t|t r6|j t|t t|t n||fSWn5tk rxtjd}|jt|YnXdS)Nr)rrBrr rrrrrr3r_parse_known_argsrrr2delattrrexc_infor=r)r&rZr4rrrerrrrrrs,    # zArgumentParser.parse_known_argscsF jdk r! jnix~ jD]s}|j}xat|jD]P\}}j|g}|j|d||j||ddqPWq1Wig}t} xt| D]\}} | dkr|jdx_| D]} |jdqWq j | } | dkr7d} n| |.take_actioncs|}|\}}}j}g}x|dkrPj||dS|dk ry||d}j}|dkr|d|kr|j|g|f|d} | |d}|ddpd} j} || kr| |}| }qvtd} t|| |q|dkrW|d} |g}|j|||fPqtd} t|| |q+|d}|d}|||}||} || }|j|||fPq+W|stx'|D]\}}}|||qW| S)NrrSrzignored explicit argument %r)_match_argumentr"rrrrrS) start_index option_tuplerrr explicit_argZmatch_argumentZ action_tuples arg_countr(charZnew_explicit_argZ optionals_maprstoprZrZselected_patterns)r'arg_strings_patternextrasoption_string_indicesr&rYrrconsume_optional6sP                  z:ArgumentParser._parse_known_args..consume_optionalcsj}|d}||}xHt|D]7\}}|||}||7}||q8Wt|ddd<|S)N)_match_arguments_partialziprl)r[Z match_partialZselected_patternZ arg_countsrrr^rZ)r'rarr&rYrrconsume_positionalss   z=ArgumentParser._parse_known_args..consume_positionalsrcs"g|]}|kr|qSrr)rXr)r[rrr[s z4ArgumentParser._parse_known_args..z(the following arguments are required: %sz, cSs+g|]!}|jtk rt|qSr)rnrr)rXrrrrrr[s z#one of the arguments %s is requiredryr)rE_read_args_from_filesrrrrriterr"_parse_optionalr%rrKrCrBrrrrrrrrr2r3 _get_valuer=r)r&r'r4r4rrZ mutex_actionZ conflictsZarg_string_pattern_partsZarg_strings_iter arg_stringr\patternrdrgZmax_option_string_indexZnext_option_string_indexZpositionals_end_indexZstringsZ stop_indexZrequired_actionsrrrrrr) rVr'rarbr4rcrrWrXr&r[rYrrOs #         !!J                   !z ArgumentParser._parse_known_argscCsg}x|D]}| s-|d|jkr=|j|q yt|ddm}g}xA|jjD]-}x$|j|D]}|j|qWqrW|j|}|j|WdQXWq tk rt j d}|j t |Yq Xq W|S)Nrr) rEr"rreadrconvert_arg_line_to_argsrhrrrrQr=r)r&r'Znew_arg_stringsrlZ args_filearg_liner(rRrrrrhs   z$ArgumentParser._read_args_from_filescCs|gS)Nr)r&rprrrrosz'ArgumentParser.convert_arg_line_to_argscCs|j|}tj||}|dkritdd6tdt6tdt6}tdd|j|j}|j|j|}t ||nt |j dS)Nzexpected one argumentzexpected at most one argumentzexpected at least one argumentzexpected %s argumentzexpected %s argumentsr) _get_nargs_patternrKr0rr r rrrrrlr)r&rrra nargs_patternr0Z nargs_errorsrrrrrrZs   zArgumentParser._match_argumentcsg}xtt|ddD]z}|d|}djfdd|D}tj||}|dk r|jdd|jDPqqW|S)Nrrr\csg|]}j|qSr)rq)rXrr)r&rrr[ s z;ArgumentParser._match_arguments_partial..cSsg|]}t|qSr)rl)rXrrrrr[$s r)rrlr%rKr0rri)r&rhrarrZ actions_slicermr0r)r&rres   z'ArgumentParser._match_arguments_partialc Cs|s dS|d|jkr!dS||jkrJ|j|}||dfSt|dkr`dSd|kr|jdd\}}||jkr|j|}|||fSn|j|}t|dkr!djdd|D}i|d6|d6}td }|j||nt|dkr@|\} | S|jj |rb|j sbdSnd |krrdSd|dfS) Nrr=z, cSsg|]\}}}|qSrr)rXrrrr]rrrr[Is z2ArgumentParser._parse_optional..r6Zmatchesz4ambiguous option: %(option)s could match %(matches)sry) rrrlsplit_get_option_tuplesr%rr=rr0r) r&rlrrrr]Z option_tuplesZoptionsrZrr\rrrrj*s<         zArgumentParser._parse_optionalc Csg}|j}|d|kr|d|krd|krV|jdd\}}n |}d}x<|jD]A}|j|rl|j|}|||f}|j|qlqlWn|d|kr|d|kr|}d}|dd}|dd} x|jD]y}||krH|j|}||| f}|j|q |j|r |j|}|||f}|j|q q Wn|jtd||S)Nrrrsr6zunexpected option string: %s)rrtr startswithr"r=r) r&rrr(Z option_prefixr]rrrZshort_option_prefixZshort_explicit_argrrrruds8        z!ArgumentParser._get_option_tuplescCs|j}|dkrd}n|tkr3d}nk|tkrHd}nV|tkr]d}nA|tkrrd}n,|tkrd}nddjd |}|jr|jdd }|jd d }n|S) Nz(-*A-*)z(-*A?-*)z (-*[A-]*)z (-*A[A-]*)z([-AO]*)z (-*A[-AO]*)z(-*%s-*)z-*rSr\r) rr rr rrr%rr8)r&rrrrrrrrrqs$              z!ArgumentParser._get_nargs_patternc sjttgkr>y|jdWq>tk r:Yq>Xn| rjtkrjrij}n j}t |t rj |}j |qnV| rjt krj rjdk rj}n|}j |nt|dkrRjdtgkrR|\}j |}j |njtkrfdd|D}njtkrfdd|D}j |dn=fdd|D}x|D]}j |qW|S)Nz--rcs"g|]}j|qSr)rk)rXv)rrr&rrr[s z.ArgumentParser._get_values..cs"g|]}j|qSr)rk)rXrw)rrr&rrr[s rcs"g|]}j|qSr)rk)rXrw)rrr&rrr[s )rrrr1r>r rrrrrrk _check_valuerrl)r&rrr'r*rlrwr)rrr&rrUs>      '  zArgumentParser._get_valuesc Cs|jd|j|j}t|sItd}t|||ny||}Wntk rt|jdt|j}tt j d}t||Ynit t fk rt|jdt|j}i|d6|d6}td}t|||YnX|S)Nrz%r is not callabler rr*z!invalid %(type)s value: %(value)r) rrr%rrrr2r#rrrQr&r>)r&rrrlr)rrr)rZrrrrks     zArgumentParser._get_valuecCsm|jdk ri||jkrii|d6djtt|jd6}td}t|||ndS)Nr*z, rz3invalid choice: %(value)r (choose from %(choices)s))rr%mapr#rr)r&rrr*rZrrrrrx s   zArgumentParser._check_valuecCs2|j}|j|j|j|j|jS)N)rrkrgrrr_)r&rUrrr format_usage s  zArgumentParser.format_usagecCs|j}|j|j|j|j|j|jxK|jD]@}|j|j |j|j|j |j |j qBW|j|j |jS)N)rrkrgrrrerrrar3rurrbrCr_)r&rUZ action_grouprrrr_ s  zArgumentParser.format_helpcCs|jd|jS)NrO)rDrO)r&rrrr2 szArgumentParser._get_formattercCs2|dkrtj}n|j|j|dS)N)rrrrz)r&filerrr print_usage8 s  zArgumentParser.print_usagecCs2|dkrtj}n|j|j|dS)N)rrrr_)r&r{rrrr= s  zArgumentParser.print_helpcCs2|r.|dkrtj}n|j|ndS)N)rstderrwrite)r&rr{rrrrB s  zArgumentParser._print_messagercCs-|r|j|tjntj|dS)N)rrr}r)r&ZstatusrrrrrK szArgumentParser.exitcCsE|jtji|jd6|d6}|jdtd|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. rOrr6z%(prog)s: error: %(message)s N)r|rr}rOrr)r&rrZrrrr=P s zArgumentParser.error) r r/r0r1rrQr$rLr'rMrKrNrrOrhrorZrerjrurqrUrkrxrzr_rr|rrrr=rr)rrr7sL 3     #     : , , 4     )7r1 __version____all__ collectionsrrrosr;rerKsysrZtextwraprrrr ImportErrorrr rr rrrrrr5rrrrr r Exceptionrrr rrrrrrrrrrrr r r*r,rrrrr>s              [#%`64"