î
]á[\xE  ã               @   sK  d  Z  d d l Z d d l Z d d l Z d d l Z d d l Td Z e j d e ƒ Z e j d e e f ƒ Z	 e
 j d d ƒ Z Gd	 d
 „  d
 ƒ Z d d „  Z d d „  e j Dƒ Z d d „  Z d d „  Z Gd d „  d ƒ Z e d k rGd Z xK d d d d f D]7 Z e d e ƒ e d j e e e ƒ ƒ ƒ e ƒ  q	Wn  d S)a6  distutils.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Ö   e  Z d  Z d Z d d d „ Z d d „  Z d d „  Z d d d	 d
 „ Z d d „  Z d d „  Z	 d d „  Z
 d d „  Z d d „  Z d d „  Z d d d d „ Z d d „  Z d d d „ Z d d d d „ Z d S)Ú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
    Nc             C   st   | |  _  i  |  _ |  j  r( |  j ƒ  n  i  |  _ i  |  _ g  |  _ g  |  _ i  |  _ i  |  _ i  |  _	 g  |  _
 d  S)N)Úoption_tableÚoption_indexÚ_build_indexÚaliasÚnegative_aliasÚ
short_optsÚ	long_optsÚ
short2longÚ	attr_nameÚ	takes_argÚoption_order)Úselfr   © r   ú,/usr/lib/python3.4/distutils/fancy_getopt.pyÚ__init__)   s    											zFancyGetopt.__init__c             C   s6   |  j  j ƒ  x" |  j D] } | |  j  | d <q Wd  S)Nr   )r   Úclearr   )r   Úoptionr   r   r   r   Q   s    zFancyGetopt._build_indexc             C   s   | |  _  |  j ƒ  d  S)N)r   r   )r   r   r   r   r   Úset_option_tableV   s    	zFancyGetopt.set_option_tablec             C   sR   | |  j  k r" t d | ƒ ‚ n, | | | f } |  j j | ƒ | |  j  | <d  S)Nz'option conflict: already an option '%s')r   ÚDistutilsGetoptErrorr   Úappend)r   Úlong_optionZshort_optionZhelp_stringr   r   r   r   Ú
add_optionZ   s    zFancyGetopt.add_optionc             C   s   | |  j  k S)zcReturn true if the option table for this parser has an
        option with long name 'long_option'.)r   )r   r   r   r   r   Ú
has_optionc   s    zFancyGetopt.has_optionc             C   s   | j  t ƒ 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)r   r   r   r   r   Úget_attr_nameh   s    zFancyGetopt.get_attr_namec             C   sŒ   t  | t ƒ s t ‚ xp | j ƒ  D]b \ } } | |  j k rY t d | | | f ƒ ‚ n  | |  j k r" t d | | | f ƒ ‚ q" q" Wd  S)Nz(invalid %s '%s': option '%s' not definedz0invalid %s '%s': aliased option '%s' not defined)Ú
isinstanceÚdictÚAssertionErrorÚitemsr   r   )r   ÚaliasesZwhatr	   Úoptr   r   r   Ú_check_alias_dictn   s    zFancyGetopt._check_alias_dictc             C   s   |  j  | d ƒ | |  _ d S)z'Set the aliases for this option parser.r	   N)r&   r	   )r   r	   r   r   r   Úset_aliasesx   s    zFancyGetopt.set_aliasesc             C   s   |  j  | d ƒ | |  _ d S)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.znegative aliasN)r&   r
   )r   r
   r   r   r   Úset_negative_aliases}   s    z FancyGetopt.set_negative_aliasesc             C   s  g  |  _  g  |  _ |  j j ƒ  i  |  _ xa|  j D]V} t | ƒ d k rb | \ } } } d } n: t | ƒ d k r‰ | \ } } } } n t d | f ƒ ‚ t | t	 ƒ s¾ t | ƒ d k  rÑ t
 d | ƒ ‚ n  | d k pû t | t	 ƒ oû t | ƒ d k st
 d	 | ƒ ‚ n  | |  j | <|  j  j | ƒ | d d
 k rq| rQ| d } n  | d d … } d |  j | <na |  j j | ƒ } | d k	 rÅ|  j | rµt
 d | | f ƒ ‚ n  | |  j  d <n  d |  j | <|  j j | ƒ } | d k	 r&|  j | |  j | k r&t
 d | | f ƒ ‚ q&n  t j | ƒ sHt
 d | ƒ ‚ n  |  j | ƒ |  j | <| r2 |  j j | ƒ | |  j | d <q2 q2 Wd S)zPopulate the various data structures that keep tabs on the
        option table.  Called by 'getopt()' before it can do anything
        worthwhile.
        é   r   é   zinvalid 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Ú
ValueErrorr    Ústrr   r   r   r
   Úgetr	   Ú
longopt_reÚmatchr   r   )r   r   ÚlongÚshortÚhelpr0   Zalias_tor   r   r   Ú_grok_option_table…   s^    				"! zFancyGetopt._grok_option_tablec             C   s>  | d k r" t  j d d … } n  | d k r@ t ƒ  } d } n d } |  j ƒ  d j |  j ƒ } y" t j | | |  j ƒ \ } } Wn4 t j k
 rº } z t	 | ƒ ‚ WYd d } ~ Xn Xxh| D]`\ } } t
 | ƒ d k r| d d k r|  j | d } n> t
 | ƒ d k r,| d d … d	 k s2t ‚ | d d … } |  j j | ƒ }	 |	 rc|	 } n  |  j | s¸| d
 k sˆt d ƒ ‚ |  j j | ƒ }	 |	 r¯|	 } d } q¸d } n  |  j | }
 | rü|  j j |
 ƒ d k	 rüt | |
 d ƒ d } n  t | |
 | ƒ |  j j | | f ƒ qÂ W| r6| | f S| Sd S)a  Parse command-line options in args. Store as attributes on object.

        If 'args' is None or not supplied, uses 'sys.argv[1:]'.  If
        'object' is None or not supplied, creates a new OptionDummy
        object, stores option values there, and returns a tuple (args,
        object).  If 'object' is supplied, it is modified in place and
        'getopt()' just returns 'args'; in both cases, the returned
        'args' is a modified copy of the passed-in 'args' list, which
        is left untouched.
        Nr,   TFú r+   r   r   z--Ú zboolean option can't have value)ÚsysÚargvÚOptionDummyr:   Újoinr   Úgetoptr   ÚerrorZDistutilsArgErrorr1   r   r"   r	   r4   r   r
   r   r0   ÚgetattrÚsetattrr   r   )r   ÚargsÚobjectZcreated_objectr   ZoptsÚmsgr%   Úvalr	   Úattrr   r   r   rA   Ò   sF    		
"".			
zFancyGetopt.getoptc             C   s)   |  j  d k r t d ƒ ‚ n |  j  Sd S)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)r   r   r   r   Úget_option_order  s    zFancyGetopt.get_option_orderc             C   sð  d } x| |  j  D]q } | d } | d } t | ƒ } | d d k rS | d } n  | d k	 rl | d } n  | | k r | } q q W| d d d } d } | | }	 d | }
 | rÃ | g } n	 d	 g } x|  j  D]} | d d
 … \ } } } t | |	 ƒ } | d d k r'| d d … } n  | d k rt| rZ| j d | | | d f ƒ q¼| j d | | f ƒ nH d | | f } | r«| j d | | | d f ƒ n | j d | ƒ x) | d d … D] } | j |
 | ƒ qÍWqÖ W| S)zŒGenerate help text (a list of strings, one per suggested line of
        output) from the option table for this FancyGetopt object.
        r   r,   r-   Né   r+   éN   r;   zOption summary:r)   z  --%-*s  %sz
  --%-*s  z%s (-%s)z  --%-*sr/   r/   r/   )r   r1   Ú	wrap_textr   )r   ÚheaderZmax_optr   r7   r8   ÚlZ	opt_widthZ
line_widthZ
text_widthZ
big_indentÚlinesr9   ÚtextZ	opt_namesr   r   r   Úgenerate_help  sF    



	!	zFancyGetopt.generate_helpc             C   sG   | d  k r t  j } n  x( |  j | ƒ D] } | j | d ƒ q( Wd  S)NÚ
)r=   ÚstdoutrS   Úwrite)r   rO   ÚfileÚliner   r   r   Ú
print_helph  s    zFancyGetopt.print_help)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r&   r'   r(   r:   rA   rK   rS   rY   r   r   r   r   r      s   
(	
M=
Or   c             C   s)   t  |  ƒ } | j | ƒ | j | | ƒ S)N)r   r(   rA   )ÚoptionsZnegative_optrF   rE   Úparserr   r   r   Úfancy_getopto  s    r`   c             C   s   i  |  ] } d  t  | ƒ “ q S)r;   )Úord)Ú.0Z_wscharr   r   r   ú
<dictcomp>u  s   	 rc   c             C   s‚  |  d k r g  St  |  ƒ | k r) |  g S|  j ƒ  }  |  j t ƒ }  t j d |  ƒ } d d „  | Dƒ } g  } x| r}g  } d } xt | rú t  | d ƒ } | | | k rÒ | j | d ƒ | d =| | } q‡ | rö | d	 d d k rö | d
 =n  Pq‡ W| rd| d k rC| j | d d | … ƒ | d | d … | d <n  | d d d k rd| d =qdn  | j d j | ƒ ƒ qr W| 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( +|-+)c             S   s   g  |  ] } | r | ‘ q Sr   r   )rb   Zchr   r   r   ú
<listcomp>…  s   	 zwrap_text.<locals>.<listcomp>r   r,   r;   r<   r/   r/   )r1   Ú
expandtabsr   ÚWS_TRANSÚreÚsplitr   r@   )rR   ÚwidthZchunksrQ   Zcur_lineZcur_lenrP   r   r   r   rN   w  s:    		
rN   c             C   s   |  j  t ƒ S)zXConvert a long option name to a valid Python identifier by
    changing "-" to "_".
    )r   r   )r%   r   r   r   Útranslate_longopt­  s    rj   c               @   s%   e  Z d  Z d Z g  d d „ Z d S)r?   z_Dummy class just used as a place to hold command-line option
    values as instance attributes.c             C   s%   x | D] } t  |  | d ƒ q Wd S)zkCreate a new OptionDummy instance.  The attributes listed in
        'options' will be initialized to None.N)rD   )r   r^   r%   r   r   r   r   ¸  s    zOptionDummy.__init__N)rZ   r[   r\   r]   r   r   r   r   r   r?   ´  s   r?   Ú__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: %drT   )r]   r=   Ústringrg   rA   Zdistutils.errorsZlongopt_patÚcompiler5   Zneg_alias_rer3   Ú	maketransr   r   r`   Z
whitespacerf   rN   rj   r?   rZ   rR   ÚwÚprintr@   r   r   r   r   Ú<module>	   s(   $
ÿ S6