
(hxE                 @   sH  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 rDd Z xK d d d d f D]7 Z e d e  e d j e e e    e   q	Wd 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   sq   | |  _  i  |  _ |  j  r% |  j   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   H/tmp/pip-build-kmztzv_d/setuptools/setuptools/_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_optionshort_option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)zTranslate 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  xj | j   D]\ \ } } | |  j k rV t d | | | f   | |  j k r" t d | | | f   q" Wd  S)Nz(invalid %s '%s': option '%s' not definedz0invalid %s '%s': aliased option '%s' not defined)
isinstancedictAssertionErroritemsr   r   )r   aliases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)zSet 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   su  g  |  _  g  |  _ |  j j   i  |  _ xF|  j D];} t |  d k rb | \ } } } d } n: t |  d k r | \ } } } } n t d | f   t | t	  s t |  d k  r t
 d |   | d k p t | t	  o t |  d k st
 d	 |   | |  j | <|  j  j |  | d d
 k rh| rH| d } | d d  } d |  j | <n[ |  j j |  } | d k	 r|  j | rt
 d | | f   | |  j  d <d |  j | <|  j j |  } | d k	 r|  j | |  j | k rt
 d | | f   t j |  s0t
 d |   |  j |  |  j | <| r2 |  j j |  | |  j | d <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 onlyr2   r2   )r   r   r   r   repeatr   len
ValueErrorr"   strr   r   r   r
   getr	   
longopt_rematchr!   r   )r   r   longshorthelpr3   alias_tor   r   r   _grok_option_table   s^    				"
!
 

zFancyGetopt._grok_option_tablec             C   s2  | d k r t  j d d  } | 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 Xx_| D]W\ } } t
 |  d k r| d d k r|  j | d } n> t
 |  d k r)| d d  d	 k s/t  | d d  } |  j j |  }	 |	 r]|	 } |  j | s| d
 k st d   |  j j |  }	 |	 r|	 } d } n d } |  j | }
 | r|  j j |
  d k	 rt | |
 d  d } t | |
 |  |  j j | | f  q W| r*| | 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   errorDistutilsArgErrorr4   r   r$   r	   r7   r   r
   r   r3   getattrsetattrr   r   )r   argsobjectcreated_objectr   optsmsgr(   valr	   attrr   r   r   rE      sF    		
"".	
zFancyGetopt.getoptc             C   s)   |  j  d k r t d   n |  j  Sd S)zReturns 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 } xs |  j  D]h } | d } | d } t |  } | d d k rP | d } | d k	 rf | d } | | k r | } q W| d d d } d } | | }	 d | }
 | r | g } n	 d	 g } x|  j  D]} | d d
  \ } } } t | |	  } | d d k r| d d  } | d k rh| rN| 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)zGenerate help text (a list of strings, one per suggested line of
        output) from the option table for this FancyGetopt object.
        r   r/   r0   N   r.   N   r?   zOption summary:r,   z  --%-*s  %sz
  --%-*s  z%s (-%s)z  --%-*sr2   r2   r2   )r   r4   	wrap_textr   )r   headermax_optr   r:   r;   l	opt_width
line_width
text_width
big_indentlinesr<   text	opt_namesr   r   r   generate_help  sF    






	!	zFancyGetopt.generate_helpc             C   sD   | d  k r t  j } x( |  j |  D] } | j | d  q% Wd  S)N
)rA   stdoutr`   write)r   rV   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>   rE   rR   r`   rf   r   r   r   r   r      s   
(	
M=
Or   c             C   s)   t  |   } | j |  | j | |  S)N)r   r+   rE   )optionsnegative_optrK   rJ   parserr   r   r   fancy_getopto  s    rn   c             C   s   i  |  ] } d  t  |   q S)r?   )ord).0Z_wscharr   r   r   
<dictcomp>u  s   	 rq   c             C   sv  |  d k r g  St  |   | k r) |  g S|  j   }  |  j t  }  t j d |   } d d   | D } g  } x | rqg  } d } xq | r t  | d  } | | | k r | j | d  | d =| | } q | r | d	 d d k r | d
 =Pq W| rX| d k r=| j | d d |   | d | d  | d <| d d d k rX| d =| j d j |   qr W| S)zwrap_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   )rp   chr   r   r   
<listcomp>  s   	 zwrap_text.<locals>.<listcomp>r   r/   r?   r@   r2   r2   )r4   
expandtabsr   WS_TRANSresplitr   rD   )r^   widthchunksr]   cur_linecur_lenrX   r   r   r   rU   w  s:    		rU   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    r|   c               @   s%   e  Z d  Z d Z g  d d  Z d S)rC   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)rI   )r   rk   r(   r   r   r   r     s    zOptionDummy.__init__N)rg   rh   ri   rj   r   r   r   r   r   rC     s   rC   __main__zTra-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: %dra   )rj   rA   stringrv   rE   Zdistutils.errorslongopt_patcompiler8   neg_alias_rer6   	maketransr    r   rn   
whitespaceru   rU   r|   rC   rg   r^   wprintrD   r   r   r   r   <module>	   s(   $
 S6