3 \xEã@sädZddlZddlZddlZddlZddlTdZejdeƒZejdeefƒZ e j ddƒZ Gd d „d ƒZ d d „Zd d„ejDƒZdd„Zdd„ZGdd„dƒZedkràdZx2dD]*Zedeƒedjeeeƒƒƒeƒq²WdS)a6distutils.fancy_getopt Wrapper around the standard getopt module that provides the following additional features: * short and long options are tied together * options have help strings, so fancy_getopt could potentially create a complete usage summary * options set attributes of a passed-in object éN)Ú*z[a-zA-Z](?:[a-zA-Z0-9-]*)z^%s$z ^(%s)=!(%s)$ú-Ú_c@sŠeZdZdZddd„Zdd„Zdd„Zd d d „Zd d „Zd d„Z dd„Z dd„Z dd„Z dd„Z d!dd„Zdd„Zd"dd„Zd#dd„ZdS)$Ú FancyGetoptaÕWrapper around the standard 'getopt()' module that provides some handy extra functionality: * short and long options are tied together * options have help strings, and help text can be assembled from them * options set attributes of a passed-in object * boolean options can have "negative aliases" -- eg. if --quiet is the "negative alias" of --verbose, then "--quiet" on the command line sets 'verbose' to false NcCsN||_i|_|jr|jƒi|_i|_g|_g|_i|_i|_i|_ g|_ dS)N) Ú option_tableÚ option_indexÚ _build_indexÚaliasÚnegative_aliasÚ short_optsÚ long_optsÚ short2longÚ attr_nameÚ takes_argÚ option_order)Úselfr©rú./usr/lib64/python3.6/distutils/fancy_getopt.pyÚ__init__)s zFancyGetopt.__init__cCs,|jjƒx|jD]}||j|d<qWdS)Nr)rÚclearr)rÚoptionrrrrQs  zFancyGetopt._build_indexcCs||_|jƒdS)N)rr)rrrrrÚset_option_tableVszFancyGetopt.set_option_tablecCs<||jkrtd|ƒ‚n |||f}|jj|ƒ||j|<dS)Nz'option conflict: already an option '%s')rÚDistutilsGetoptErrorrÚappend)rÚ long_optionZ short_optionZ help_stringrrrrÚ add_optionZs     zFancyGetopt.add_optioncCs ||jkS)zcReturn true if the option table for this parser has an option with long name 'long_option'.)r)rrrrrÚ has_optioncszFancyGetopt.has_optioncCs |jtƒS)z’Translate long option name 'long_option' to the form it has as an attribute of some object: ie., translate hyphens to underscores.)Ú translateÚ longopt_xlate)rrrrrÚ get_attr_namehszFancyGetopt.get_attr_namecCsRxL|jƒD]@\}}||jkr.td|||fƒ‚||jkr td|||fƒ‚q WdS)Nz(invalid %s '%s': option '%s' not definedz0invalid %s '%s': aliased option '%s' not defined)Úitemsrr)rÚaliasesZwhatr ÚoptrrrÚ_check_alias_dictns  zFancyGetopt._check_alias_dictcCs|j|dƒ||_dS)z'Set the aliases for this option parser.r N)r#r )rr rrrÚ set_aliasesxs zFancyGetopt.set_aliasescCs|j|dƒ||_dS)zÚSet the negative aliases for this option parser. 'negative_alias' should be a dictionary mapping option names to option names, both the key and value must already be defined in the option table.znegative aliasN)r#r )rr rrrÚset_negative_aliases}s z FancyGetopt.set_negative_aliasescCsÖg|_g|_|jjƒi|_�x²|jD�]¦}t|ƒdkrH|\}}}d}n(t|ƒdkrb|\}}}}ntd|fƒ‚t|t ƒ sˆt|ƒdkr”t d|ƒ‚|dkp°t|t ƒo°t|ƒdks¾t d |ƒ‚||j|<|jj |ƒ|dd k�r|rî|d }|dd…}d|j |<nF|j j|ƒ}|dk �rB|j |�r8t d ||fƒ‚||jd<d|j |<|jj|ƒ}|dk �rˆ|j ||j |k�rˆt d ||fƒ‚tj|ƒ�s t d|ƒ‚|j|ƒ|j|<|r&|jj |ƒ||j|d<q&WdS)z�Populate the various data structures that keep tabs on the option table. Called by 'getopt()' before it can do anything worthwhile. érézinvalid option tuple: %réz9invalid long option '%s': must be a string of length >= 2Néz:invalid short option '%s': must a single character or Noneú=ú:z>invalid negative alias '%s': aliased option '%s' takes a valuezginvalid alias '%s': inconsistent with aliased option '%s' (one of them takes a value, the other doesn'tzEinvalid long option name '%s' (must be letters, numbers, hyphens onlyéÿÿÿÿr,r,)r r r rÚrepeatrÚlenÚ ValueErrorÚ isinstanceÚstrrrrr Úgetr Ú longopt_reÚmatchrr)rrÚlongÚshortÚhelpr-Zalias_torrrÚ_grok_option_table…s^                   zFancyGetopt._grok_option_tablec Csr|dkrtjdd…}|dkr*tƒ}d}nd}|jƒdj|jƒ}ytj|||jƒ\}}Wn,tjk r†}zt |ƒ‚WYdd}~XnXxÒ|D]Ê\}}t |ƒdkr¾|ddkr¾|j |d}n |dd…}|j j |ƒ} | rÞ| }|j|�s |jj |ƒ} | �r| }d}nd}|j|} |�r<|jj | ƒdk �rÒsB        zFancyGetopt.getoptcCs|jdkrtdƒ‚n|jSdS)z«Returns the list of (option, value) tuples processed by the previous run of 'getopt()'. Raises RuntimeError if 'getopt()' hasn't been called yet. Nz!'getopt()' hasn't been called yet)rÚ RuntimeError)rrrrÚget_option_orders  zFancyGetopt.get_option_ordercCsxd}xV|jD]L}|d}|d}t|ƒ}|ddkr<|d}|dk rL|d}||kr |}q W|ddd}d}||} d|} |rŒ|g} nd g} xà|jD]Ö}|dd …\}}} t| | ƒ} |ddkrÒ|dd…}|dk�r| �rü| jd ||| dfƒn| jd ||fƒn:d ||f}| �r<| jd ||| dfƒn| jd|ƒx$| dd…D]}| j| |ƒ�qXWqšW| S)zŒGenerate help text (a list of strings, one per suggested line of output) from the option table for this FancyGetopt object. rr)r*Nér(éNr9zOption summary:r&z --%-*s %sz --%-*s z%s (-%s)z --%-*sr,r,r,)rr.Ú wrap_textr)rÚheaderZmax_optrr5r6ÚlZ opt_widthZ line_widthZ text_widthZ big_indentÚlinesr7ÚtextZ opt_namesrrrÚ generate_helpsF        zFancyGetopt.generate_helpcCs4|dkrtj}x |j|ƒD]}|j|dƒqWdS)NÚ )r:ÚstdoutrPÚwrite)rrLÚfileÚlinerrrÚ print_helphszFancyGetopt.print_help)N)NN)NN)N)NN)Ú__name__Ú __module__Ú __qualname__Ú__doc__rrrrrrr#r$r%r8r>rHrPrVrrrrrs  (  M = OrcCst|ƒ}|j|ƒ|j||ƒS)N)rr%r>)ÚoptionsZ negative_optrCrBÚparserrrrÚ fancy_getoptos r]cCsi|]}dt|ƒ“qS)r9)Úord)Ú.0Z_wscharrrrú usr`cCs"|dkr gSt|ƒ|kr|gS|jƒ}|jtƒ}tjd|ƒ}dd„|Dƒ}g}xÎ|�rg}d}xZ|r¸t|dƒ}|||krš|j|dƒ|d=||}q`|r´|d ddkr´|d =Pq`W|�r |dkrò|j|dd|…ƒ|d|d…|d<|dddk�r |d=|jdj|ƒƒqPW|S) z¶wrap_text(text : string, width : int) -> [string] Split 'text' into multiple lines of no more than 'width' characters each, and return the list of strings that results. Nz( +|-+)cSsg|] }|r|‘qSrr)r_Zchrrrú …szwrap_text..rr)r9Úr,r,)r.Ú expandtabsrÚWS_TRANSÚreÚsplitrr=)rOÚwidthZchunksrNZcur_lineZcur_lenrMrrrrKws:      rKcCs |jtƒS)zXConvert a long option name to a valid Python identifier by changing "-" to "_". )rr)r"rrrÚtranslate_longopt­srhc@seZdZdZgfdd„ZdS)r<z_Dummy class just used as a place to hold command-line option values as instance attributes.cCsx|D]}t||dƒqWdS)zkCreate a new OptionDummy instance. The attributes listed in 'options' will be initialized to None.N)rA)rr[r"rrrr¸s zOptionDummy.__init__N)rWrXrYrZrrrrrr<´sr<Ú__main__z¢Tra-la-la, supercalifragilisticexpialidocious. How *do* you spell that odd word, anyways? (Someone ask Mary -- she'll know [or she'll say, "How should I know?"].)é ééé(z width: %drQ)rjrkrlrm)rZr:Ústringrer>Zdistutils.errorsZ longopt_patÚcompiler3Z neg_alias_rer1Ú maketransrrr]Z whitespacerdrKrhr<rWrOÚwÚprintr=rrrrÚ s* T6