B -_Ct@sBdZdZdddddddd d d d d dddddgZddlZddlZddlZyddl m Z m Z Wn$e k r~ddZ ddZ YnXdZ dZdZdZdZdZd ZGd!d"d"eZd#d$ZGd%ddeZGd&ddeZGd'd d eZGd(ddeZGd)d d eZd*d+ZGd,ddeZGd-ddeZGd.d d eZ Gd/d0d0e Z!Gd1d2d2e Z"Gd3d4d4e"Z#Gd5d6d6e"Z$Gd7d8d8e Z%Gd9d:d:e Z&Gd;d<dd>e Z(Gd?d@d@e Z)GdAdBdBe Z*GdCddeZ+GdDd d eZ,GdEdFdFeZ-GdGdHdHe-Z.GdIdJdJe.Z/GdKddee-Z0dS)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.7/argparse.py_]srcCs|dkr |S|SdS)Nr)ZsingularZpluralnrrrr_srz ==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:  ) rWrVrS _join_partsr/rUrXrrE)r'r&Z item_helpZcurrent_indentrXrrr format_helps    z"HelpFormatter._Section.format_help)N)r r0r1rRr`rrrrrHs rHcCs|jj||fdS)N)rJr/r")r'rZr[rrr _add_itemszHelpFormatter._add_itemcCs0||||j|}||jg||_dS)N)rSrHrJrar`)r'rXZsectionrrr start_sectionszHelpFormatter.start_sectioncCs|jj|_|dS)N)rJrWrU)r'rrr end_sections zHelpFormatter.end_sectioncCs$|tk r |dk r ||j|gdS)N)rra _format_text)r'textrrradd_textszHelpFormatter.add_textcCs&|tk r"||||f}||j|dS)N)rra _format_usage)r'usageactionsgroupsprefixr[rrr add_usages zHelpFormatter.add_usagecCsz|jtk rv|j}||g}x ||D]}|||q&Wtdd|D}||j}t|j||_||j |gdS)NcSsg|] }t|qSr)len)rYsrrrr\sz.HelpFormatter.add_argument..) helpr_format_action_invocation_iter_indented_subactionsr"rCrErGra_format_action)r'actionZget_invocationZ invocations subactionZinvocation_lengthZ action_lengthrrr add_argument s   zHelpFormatter.add_argumentcCsx|D]}||qWdS)N)ru)r'rirsrrr add_argumentss zHelpFormatter.add_argumentscCs.|j}|r*|jd|}|dd}|S)Nz r^)rIr`rOsubstrip)r'rorrrr`$s  zHelpFormatter.format_helpcCsddd|DS)Nr]cSsg|]}|r|tk r|qSr)r)rYpartrrrr\,sz-HelpFormatter._join_parts..)r&)r'Z part_stringsrrrr_+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: )rPz%(prog)s cSsg|] }|r|qSrr)rYrnrrrr\Lsz/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)Nrrzr)rmr"r&)partsindentrklineslineZline_lenry) text_widthrr get_lines`s"    z.HelpFormatter._format_usage..get_linesg?rr^z%s%s )N)rdictr?option_stringsr"_format_actions_usager&rDrErmrKfindallrTextend)r'rhrirjrkrP optionals positionalsrsformatZ action_usageZ part_regexpZ opt_usageZ pos_usageZ opt_partsZ pos_partsrr|r}r{r)rrrg0sZ             zHelpFormatter._format_usagec Cst}i}x|D]}y||jd}Wntk r>wYqX|t|j}||||jkrx|jD]}||qhW|js||kr||d7<nd||<d||<n*||kr||d7<nd||<d||<xt|d|D] } d || <qWqWg} x0t|D]"\} }|j t krj| d| | d krF| | n"| | dd kr,| | dn|js||} ||| } ||kr| ddkr| d dkr| dd } | | nf|jd} |jdkrd | } n"||} ||| }d | |f} |js"||kr"d | } | | q Wx(t|ddD]} || g| | | <q@Wddd| D}d}d}td|d|}td|d|}td||fd|}tdd|}|}|S)Nrz [[]z (()r|z%sz%s %sz[%s]T)reverserzcSsg|]}|dk r|qS)Nr)rYitemrrrr\sz7HelpFormatter._format_actions_usage..z[\[(]z[\])]z(%s) z\1z (%s)z%s *%sr]z \(([^|]*)\))setindex_group_actionsr>rmaddrequiredrange enumeraterorr"getpopr#_get_default_metavar_for_positional _format_argsnargs!_get_default_metavar_for_optionalr-r&rKrwrx)r'rirj group_actionsZinsertsgroupstartendrsir{defaultry option_string args_stringreopencloserrrrsr                 z#HelpFormatter._format_actions_usagecCsFd|kr|t|jd}t|j|jd}d|j}||||dS)Nz%(prog))rP rzz )rr?rCrDrE _fill_text)r'rerr|rrrrds  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) Nr6rr]z%*s%s z %*s%-*s rrr^)rBrGrArCrDrErprorm _expand_help _split_linesr"endswithrqrrr_) r'rsZ help_positionZ help_widthZ action_widthZ action_headertupZ indent_firstr{Z help_textZ help_linesr~rtrrrrrs6          zHelpFormatter._format_actioncCs|js&||}|||d\}|Sg}|jdkrB||jn8||}|||}x |jD]}|d||fq`Wd|SdS)Nrrz%s %sz, ) rr_metavar_formatterrrrrr"r&)r'rsrmetavarr{rrrrrrp's     z'HelpFormatter._format_action_invocationcsP|jdk r|jn.|jdk r.z{%s},csttrSf|SdS)N) isinstancetuple)Z tuple_size)resultrrrHs z0HelpFormatter._metavar_formatter..format)rchoicesr&)r'rsdefault_metavarZ choice_strsrr)rrr?s   z HelpFormatter._metavar_formattercCs|||}|jdkr$d|d}n|jtkr.rz) rrr rr rrrrr&)r'rsrZ get_metavarrZformatsrrrrOs$        zHelpFormatter._format_argscCstt||jd}x"t|D]}||tkr||=qWx,t|D] }t||dr@||j||<q@W|ddk rddd|dD}||d<| ||S)N)rPr rz, cSsg|] }t|qSr)r)rYcrrrr\msz.HelpFormatter._expand_help..) rvarsr?r3rhasattrr rr&_get_help_string)r'rsZparamsr*Z choices_strrrrrds  zHelpFormatter._expand_helpccs@y |j}Wntk rYnX||EdH|dS)N)_get_subactionsAttributeErrorrSrU)r'rsZget_subactionsrrrrqqs  z'HelpFormatter._iter_indented_subactionscCs&|jd|}ddl}|||S)Nrzr)rNrwrxtextwrapZwrap)r'rerQrrrrr{szHelpFormatter._split_linescCs,|jd|}ddl}|j||||dS)Nrzr)Zinitial_indentZsubsequent_indent)rNrwrxrZfill)r'rerQr|rrrrrs zHelpFormatter._fill_textcCs|jS)N)ro)r'rsrrrrszHelpFormatter._get_help_stringcCs |jS)N)destupper)r'rsrrrrsz/HelpFormatter._get_default_metavar_for_optionalcCs|jS)N)r)r'rsrrrrsz1HelpFormatter._get_default_metavar_for_positional)r6r7N)N) r r0r1r2rRrSrUobjectrHrarbrcrfrlrurvr`r_rgrrdrrrprrrrqrrrrrrrrrrs<  `a/  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)rYr~)r|rr sz9RawDescriptionHelpFormatter._fill_text..T)keepends)r& splitlines)r'rerQr|r)r|rrsz&RawDescriptionHelpFormatter._fill_textN)r r0r1r2rrrrrrsc@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'rerQrrrrsz!RawTextHelpFormatter._split_linesN)r r0r1r2rrrrrrsc@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_nargsrrrrs  z.ArgumentDefaultsHelpFormatter._get_help_stringN)r r0r1r2rrrrrrsc@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'rsrrrrsz:MetavarTypeHelpFormatter._get_default_metavar_for_optionalcCs|jjS)N)rr )r'rsrrrrsz.r)r'namesr)r'rr$AszAction._get_kwargscCsttddS)Nz.__call__() not defined)NotImplementedErrorr)r'parser namespacevaluesrrrr__call__OszAction.__call__)NNNNNFNN)N)r r0r1r2rRr$rrrrrr 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 superrrR) r'rrrrrrrrror) __class__rrrRUs   z_StoreAction.__init__cCst||j|dS)N)setattrr)r'rrrrrrrrrsz_StoreAction.__call__)NNNNNFNN)N)r r0r1rRr __classcell__rr)rrrSsrcs(eZdZdfdd ZdddZZS) _StoreConstActionNFc s"tt|j||d||||ddS)Nr)rrrrrrro)rrrR)r'rrrrrror)rrrrRxs z_StoreConstAction.__init__cCst||j|jdS)N)rrr)r'rrrrrrrrsz_StoreConstAction.__call__)NFNN)N)r r0r1rRrrrr)rrrvs  rcseZdZdfdd ZZS)_StoreTrueActionFNcs tt|j||d|||ddS)NT)rrrrrro)rrrR)r'rrrrro)rrrrRs z_StoreTrueAction.__init__)FFN)r r0r1rRrrr)rrrsrcseZdZdfdd ZZS)_StoreFalseActionTFNcs tt|j||d|||ddS)NF)rrrrrro)rrrR)r'rrrrro)rrrrRs z_StoreFalseAction.__init__)TFN)r r0r1rRrrr)rrrsrcs(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 rrrR) r'rrrrrrrrror)rrrrRs   z_AppendAction.__init__cCs2t||jd}t|}||t||j|dS)N)rrr5r"r)r'rrrrr/rrrrs z_AppendAction.__call__)NNNNNFNN)N)r r0r1rRrrrr)rrrsrcs(eZdZdfdd ZdddZZS) _AppendConstActionNFc s$tt|j||d|||||ddS)Nr)rrrrrrror)rrrR)r'rrrrrror)rrrrRs z_AppendConstAction.__init__cCs4t||jd}t|}||jt||j|dS)N)rrr5r"rr)r'rrrrr/rrrrs z_AppendConstAction.__call__)NFNN)N)r r0r1rRrrrr)rrrs  rcs(eZdZdfdd ZdddZZS) _CountActionNFcs tt|j||d|||ddS)Nr)rrrrrro)rrrR)r'rrrrro)rrrrRs z_CountAction.__init__cCs0t||jd}|dkrd}t||j|ddS)Nrr)rrr)r'rrrrcountrrrrsz_CountAction.__call__)NFN)N)r r0r1rRrrrr)rrrs rcs.eZdZeedffdd ZdddZZS) _HelpActionNcstt|j|||d|ddS)Nr)rrrrro)rrrR)r'rrrro)rrrrR s  z_HelpAction.__init__cCs||dS)N) print_helpexit)r'rrrrrrrrsz_HelpAction.__call__)N)r r0r1rrRrrrr)rrrsrcs0eZdZdeedffdd ZdddZZS)_VersionActionNz&show program's version number and exitcs$tt|j|||d|d||_dS)Nr)rrrrro)rrrRversion)r'rrrrro)rrrrRs z_VersionAction.__init__cCsD|j}|dkr|j}|}||||tj|dS)N)r_get_formatterrf_print_messager`_sysstdoutr)r'rrrrrrVrrrr*s z_VersionAction.__call__)N)r r0r1rrRrrrr)rrrs  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_ChoicesPseudoActionrR)r'r*aliasesrorrZsup)rrrrR8s   z/_SubParsersAction._ChoicesPseudoAction.__init__)r r0r1rRrrr)rrr6srFNc s<||_||_i|_g|_tt|j||t|j|||ddS)N)rrrrrror) _prog_prefix _parser_class_name_parser_map_choices_actionsrrrRr)r'rrP parser_classrrror)rrrrR@s  z_SubParsersAction.__init__cKs|ddkr d|j|f|d<|dd}d|krX|d}||||}|j||jf|}||j|<x|D]}||j|<qtW|S)NrPz%s %srrro)rrrrrr"rr)r'r*kwargsrroZ choice_actionraliasrrr add_parserWs      z_SubParsersAction.add_parsercCs|jS)N)r)r'rrrrnsz!_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)Nrrz, ) parser_namerz5unknown parser %(parser_name)r (choices: %(choices)s))rrrrr=r&rrparse_known_argsrr/ setdefault_UNRECOGNIZED_ARGS_ATTRrr) r'rrrrrr(r[msgZ subnamespacekeyr+rrrrqs"    z_SubParsersAction.__call__)N) r r0r1r rrrRrrrrrr)rrr4src@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'modebufsizeencodingerrorsrrrrRszFileType.__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'stringrerrrrrs  zFileType.__call__cCsT|j|jf}d|jfd|jfg}ddd|Ddd|D}dt|j|fS)Nrrz, cSsg|]}|dkrt|qS)r)r#)rYr)rrrr\sz%FileType.__repr__..cSs$g|]\}}|dk rd||fqS)Nz%s=%rr)rYkwr)rrrr\sz%s(%s))rrrrr&rr )r'r[rZargs_strrrrr,s  zFileType.__repr__)rrNN)r r0r1r2rRrr,rrrrrs 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*rrrrRs zNamespace.__init__cCst|tstSt|t|kS)N)rr NotImplementedr)r'otherrrr__eq__s zNamespace.__eq__cCs ||jkS)N)r.)r'rrrr __contains__szNamespace.__contains__N)r r0r1r2rRr r rrrrr 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) NrsZstoreZ store_const store_trueZ store_falser"Z append_constrrorparsersz^-\d+$|^-\d*\.\d+$)rr rR 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)rrrrRs2 z_ActionsContainer.__init__cCs|j|i}|||<dS)N)rr)r' registry_namer+rregistryrrrrsz_ActionsContainer.registerNcCs|j|||S)N)rr)r'rr+rrrr _registry_getsz_ActionsContainer._registry_getcKs6|j|x$|jD]}|j|kr||j|_qWdS)N)rupdaterrr)r'rrsrrr set_defaults s   z_ActionsContainer.set_defaultscCs8x(|jD]}|j|kr|jdk r|jSqW|j|dS)N)rrrrr)r'rrsrrr 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, ...) rrrz+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_kwargsrr_pop_action_classcallablerrrrr TypeError _add_action)r'r[rcharsrZ action_classrs type_funcrrrru3s2          z_ActionsContainer.add_argumentcOs t|f||}|j||S)N)_ArgumentGrouprr")r'r[rrrrradd_argument_groupbs z$_ActionsContainer.add_argument_groupcKst|f|}|j||S)N)_MutuallyExclusiveGrouprr")r'rrrrradd_mutually_exclusive_groupgs  z._ActionsContainer.add_mutually_exclusive_groupcCsh|||j|||_x|jD]}||j|<q$Wx,|jD]"}|j|r>|js>|jdq>W|S)NT) _check_conflictrr" containerrrrmatchr)r'rsrrrrr'ls     z_ActionsContainer._add_actioncCs|j|dS)N)rremove)r'rsrrr_remove_actionsz _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)rr3rr>r+rrrrr-rrrr')r'r/Ztitle_group_maprrZ group_maprs mutex_grouprrr_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)rr&rr rr)r'rrrrrrr"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)rrrz%dest= is required for options like %rrr)rr) rrr>r"rmrlstripreplacer) r'r[rrZlong_option_stringsrrrZdest_option_stringrrrr#s0          z&_ActionsContainer._get_optional_kwargscCs|d|}|d||S)Nrs)rr)r'rrrsrrrr$s 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_namerrrrrs   z_ActionsContainer._get_handlercCsPg}x0|jD]&}||jkr |j|}|||fq W|rL|}|||dS)N)rrr"r)r'rsZconfl_optionalsrZconfl_optionalrrrrr.s   z!_ActionsContainer._check_conflictcCs6tddt|}ddd|D}t|||dS)Nzconflicting option string: %szconflicting option strings: %sz, cSsg|] \}}|qSrr)rYrrsrrrr\sz<_ActionsContainer._handle_conflict_error..)rrmr&r)r'rsconflicting_actionsrZconflict_stringrrr_handle_conflict_errors    z(_ActionsContainer._handle_conflict_errorcCsBx<|D]4\}}|j||j|d|js|j|qWdS)N)rr1rrr/r2)r'rsr9rrrr_handle_conflict_resolves  z*_ActionsContainer._handle_conflict_resolve)N)N)r r0r1rRrrr r!rur+r-r'r2r5r"r#r$rr.r:r;rrr)rrr 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*rRr3rrrrrrr)r'r/r3rrrZ super_init)rrrrRs    z_ArgumentGroup.__init__cs tt||}|j||S)N)rr*r'rr")r'rs)rrrr'+s z_ArgumentGroup._add_actioncs tt|||j|dS)N)rr*r2rr1)r'rs)rrrr20sz_ArgumentGroup._remove_action)NN)r r0r1rRr'r2rrr)rrr*s r*cs.eZdZdfdd ZddZddZZS) r,Fcs tt||||_||_dS)N)rr,rRr _container)r'r/r)rrrrR7sz _MutuallyExclusiveGroup.__init__cCs2|jrtd}t||j|}|j||S)Nz-mutually exclusive arguments must be optional)rrr>r<r'rr")r'rsrrrrr'<s   z#_MutuallyExclusiveGroup._add_actioncCs|j||j|dS)N)r<r2rr1)r'rsrrrr2Ds z&_MutuallyExclusiveGroup._remove_action)F)r r0r1rRr'r2rrr)rrr,5sr,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)rrrrrzpositional argumentszoptional argumentscSs|S)Nr)rrrridentitysz)ArgumentParser.__init__..identityrrhr6rozshow this help message and exit)rsrro)rrrRr;pathbasenamerargvrPrhepilogformatter_classfromfile_prefix_charsadd_help allow_abbrevr+r _positionals _optionals _subparsersrrurr5rrr)r'rPrhrrCparentsrDrrErrrFrGZ superinitZ add_groupr>Zdefault_prefixrWdefaults)rrrrR\sB     zArgumentParser.__init__cs"ddddddg}fdd|DS) NrPrhrrDrrFcsg|]}|t|fqSr)r)rYr*)r'rrr\sz.ArgumentParser._get_kwargs..r)r'rr)r'rr$szArgumentParser._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 argumentsrr3rZ subcommandsrPr]r r)rJr=rrrrr+rHrr_get_positional_actionsrrlrhr`rxr$r') r'rr3rrVrrjZ parsers_classrsrrradd_subparserss$   zArgumentParser.add_subparserscCs$|jr|j|n |j||S)N)rrIr'rH)r'rsrrrr's zArgumentParser._add_actioncCsdd|jDS)NcSsg|]}|jr|qSr)r)rYrsrrrr\sz8ArgumentParser._get_optional_actions..)r)r'rrr_get_optional_actionssz$ArgumentParser._get_optional_actionscCsdd|jDS)NcSsg|]}|js|qSr)r)rYrsrrrr\sz:ArgumentParser._get_positional_actions..)r)r'rrrrMsz&ArgumentParser._get_positional_actionscCs4|||\}}|r0td}||d||S)Nzunrecognized arguments: %srz)rrr=r&)r'r[rrBrrrr 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)Nr)rrBr3r rrrrrrr_parse_known_argsrrrdelattrrexc_infor=r)r'r[rrsrerrrrrrs,         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)Nrz--rAOr]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'rr take_action6s    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)NrrUrzignored explicit argument %r)_match_argumentr"rrrrrT) start_index option_tuplersr explicit_argZmatch_argumentZ action_tuples arg_countr(charZnew_explicit_argZ optionals_maprstopr[rZselected_patterns)r(arg_strings_patternextrasoption_string_indicesr'r[rrconsume_optionalKsP        z:ArgumentParser._parse_known_args..consume_optionalcsrj}|d}||}x8t|D]*\}}|||}||7}||q(Wt|ddd<|S)N)_match_arguments_partialziprm)r]Z match_partialZselected_patternZ arg_countsrsr`r[)r(rcrr'r[rrconsume_positionalss  z=ArgumentParser._parse_known_args..consume_positionalsrrcsg|]}|kr|qSrr)rYr)r]rrr\sz4ArgumentParser._parse_known_args..z(the following arguments are required: %sz, cSsg|]}|jtk rt|qSr)rorr)rYrsrrrr\sz#one of the arguments %s is requiredrz)N)rE_read_args_from_filesrrrrriterr"_parse_optionalr&rrMrCrBrrrrrrrrrr _get_valuer=r)r'r(rr4rrZ mutex_actionZ conflictsZarg_string_pattern_partsZarg_strings_iter arg_stringr^patternrfriZmax_option_string_indexZnext_option_string_indexZpositionals_end_indexZstringsZ stop_indexZrequired_actionsrsrrrr) rXr(rcrdrrerrYrZr'r]r[rrQs        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)Nrr) rEr"rreadrconvert_arg_line_to_argsrjrrrrSr=r)r'r(Znew_arg_stringsrnZ args_filearg_liner)rTrrrrjs     z$ArgumentParser._read_args_from_filescCs|gS)Nr)r'rrrrrrqsz'ArgumentParser.convert_arg_line_to_argscCst||}t||}|dkrfdtdttdttdi}tdd|j|j}||j|}t ||t | dS)Nzexpected one argumentzexpected at most one argumentzexpected at least one argumentzexpected %s argumentzexpected %s argumentsr) _get_nargs_patternrKr0rr r rrrrrmr)r'rsrc nargs_patternr0Z nargs_errorsrrrrrr\s    zArgumentParser._match_argumentcstg}xjtt|ddD]V}|d|}dfdd|D}t||}|dk r|dd|DPqW|S)Nrrr]csg|]}|qSr)rs)rYrs)r'rrr\5sz;ArgumentParser._match_arguments_partial..cSsg|] }t|qSr)rm)rYrrrrr\9s)rrmr&rKr0rrj)r'rircrrZ actions_sliceror0r)r'rrg/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) Nrr=z, cSsg|]\}}}|qSrr)rYrsrr_rrrr\_sz2ArgumentParser._parse_optional..)r6Zmatchesz4ambiguous option: %(option)s could match %(matches)srz) rrrmsplitrG_get_option_tuplesr&rr=rr0r) r'rnrsrr_Z option_tuplesZoptionsr[rr^rrrrl?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)Nrrrur6zunexpected option string: %s)rrvr startswithr"r=r) r'rrr(Z option_prefixr_rsrZshort_option_prefixZshort_explicit_argrrrrwzs8             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-*rUr]r) rr rr rrrr&rr8)r'rsrrtrrrrss(  z!ArgumentParser._get_nargs_patterncCs4|||\}}|r0td}||d||S)Nzunrecognized arguments: %srz)parse_known_intermixed_argsrr=r&)r'r[rrBrrrrparse_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)rYrsrrrr\sz>ArgumentParser.parse_known_intermixed_args..z3parse_intermixed_args: positional arg with nargs=%srcs&g|]}|jD]}|kr|jqqSr)rr)rYrrs)rrrr\sz;parse_intermixed_args: positional in mutuallyExclusiveGroup)warnzDo not expect %s in %sF)rMr&rrrh format_usageZ save_nargsrrZ save_defaultrrrrwarningsr|rRrOrZ save_required) r'r[raZ save_usagersZremaining_argsr|rrrdr)rrrysX               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--rcsg|]}|qSr)rm)rYv)rsr'rrr\O sz.ArgumentParser._get_values..csg|]}|qSr)rm)rYr)rsr'rrr\S srcsg|]}|qSr)rm)rYr)rsr'rrr\\ s)rrrr1r>r rrrrrrm _check_valuerrmr)r'rsr(r+rnrr)rsr'rrW+ 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 callabler r)rr+z!invalid %(type)s value: %(value)r) rrr%rrrrr#rrrSr&r>)r'rsrnr)rrr*r[rrrrmc s   zArgumentParser._get_valuecCsF|jdk rB||jkrB|dtt|jd}td}t|||dS)Nz, )r+rz3invalid choice: %(value)r (choose from %(choices)s))rr&mapr#rr)r'rsr+r[rrrrr} s zArgumentParser._check_valuecCs$|}||j|j|j|S)N)rrlrhrrr`)r'rVrrrr} s zArgumentParser.format_usagecCsx|}||j|j|j||jx:|jD]0}||j ||j| |j | q0W||j |S)N)rrlrhrrrfrrrbr3rvrrcrCr`)r'rVZ action_grouprrrr` s        zArgumentParser.format_helpcCs|j|jdS)N)rP)rDrP)r'rrrr szArgumentParser._get_formattercCs"|dkrtj}|||dS)N)rrrr})r'filerrr print_usage szArgumentParser.print_usagecCs"|dkrtj}|||dS)N)rrrr`)r'rrrrr szArgumentParser.print_helpcCs |r|dkrtj}||dS)N)rstderrwrite)r'rrrrrr szArgumentParser._print_messagercCs |r||tjt|dS)N)rrrr)r'Zstatusrrrrr szArgumentParser.exitcCs0|tj|j|d}|dtd|dS)zerror(message: string) Prints a usage message incorporating the message to stderr and exits. If you override this in a subclass, it should not return -- it should either exit or raise an exception. )rPrr6z%(prog)s: error: %(message)s N)rrrrPrr)r'rr[rrrr= s  zArgumentParser.error)NN)NN)NN)NN)N)N)N)rN)#r r0r1r2rrRr$rNr'rOrMrPrrQrjrqr\rgrlrwrsrzryrWrmrr}r`rrrrrr=rrr)rrrIsT4  #w;,1  M8    )1r2 __version____all__osr;rerKsysrrrr ImportErrorrr rr rrrrrr5rrrrr r Exceptionrrr rrrrrrrrrrrr r r*r,rrrrr>su    [#&b65"