<s> """<STR_LIT>""" <EOL> import sys , os , textwrap , getopt , re , struct <EOL> class OptDict : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . _opts = { } <EOL> def __setitem__ ( self , k , v ) : <EOL> if k . startswith ( '<STR_LIT>' ) or k . startswith ( '<STR_LIT>' ) : <EOL> k = k [ <NUM_LIT:3> : ] <EOL> v = not v <EOL> self . _opts [ k ] = v <EOL> def __getitem__ ( self , k ) : <EOL> if k . startswith ( '<STR_LIT>' ) or k . startswith ( '<STR_LIT>' ) : <EOL> return not self . _opts [ k [ <NUM_LIT:3> : ] ] <EOL> return self . _opts [ k ] <EOL> def __getattr__ ( self , k ) : <EOL> return self [ k ] <EOL> def _default_onabort ( msg ) : <EOL> sys . exit ( <NUM_LIT> ) <EOL> def _intify ( v ) : <EOL> try : <EOL> vv = int ( v or '<STR_LIT>' ) <EOL> if str ( vv ) == v : <EOL> return vv <EOL> except ValueError : <EOL> pass <EOL> return v <EOL> def _atoi ( v ) : <EOL> try : <EOL> return int ( v or <NUM_LIT:0> ) <EOL> except ValueError : <EOL> return <NUM_LIT:0> <EOL> def _remove_negative_kv ( k , v ) : <EOL> if k . startswith ( '<STR_LIT>' ) or k . startswith ( '<STR_LIT>' ) : <EOL> return k [ <NUM_LIT:3> : ] , not v <EOL> return k , v <EOL> def _remove_negative_k ( k ) : <EOL> return _remove_negative_kv ( k , None ) [ <NUM_LIT:0> ] <EOL> def _tty_width ( ) : <EOL> s = struct . pack ( "<STR_LIT>" , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> try : <EOL> import fcntl , termios <EOL> s = fcntl . ioctl ( sys . stderr . fileno ( ) , termios . TIOCGWINSZ , s ) <EOL> except ( IOError , ImportError ) : <EOL> return _atoi ( os . environ . get ( '<STR_LIT>' ) ) or <NUM_LIT> <EOL> ( ysize , xsize , ypix , xpix ) = struct . unpack ( '<STR_LIT>' , s ) <EOL> return xsize or <NUM_LIT> <EOL> class Options : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , optspec , optfunc = getopt . gnu_getopt , <EOL> onabort = _default_onabort ) : <EOL> self . optspec = optspec <EOL> self . _onabort = onabort <EOL> self . optfunc = optfunc <EOL> self . _aliases = { } <EOL> self . _shortopts = '<STR_LIT>' <EOL> self . _longopts = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> self . _hasparms = { } <EOL> self . _defaults = { } <EOL> self . _usagestr = self . _gen_usage ( ) <EOL> def _gen_usage ( self ) : <EOL> out = [ ] <EOL> lines = self . optspec . strip ( ) . split ( '<STR_LIT:\n>' ) <EOL> lines . reverse ( ) <EOL> first_syn = True <EOL> while lines : <EOL> l = lines . pop ( ) <EOL> if l == '<STR_LIT>' : break <EOL> out . append ( '<STR_LIT>' % ( first_syn and '<STR_LIT>' or '<STR_LIT>' , l ) ) <EOL> first_syn = False <EOL> out . append ( '<STR_LIT:\n>' ) <EOL> last_was_option = False <EOL> while lines : <EOL> l = lines . pop ( ) <EOL> if l . startswith ( '<STR_LIT:U+0020>' ) : <EOL> out . append ( '<STR_LIT>' % ( last_was_option and '<STR_LIT:\n>' or '<STR_LIT>' , <EOL> l . lstrip ( ) ) ) <EOL> last_was_option = False <EOL> elif l : <EOL> ( flags , extra ) = l . split ( '<STR_LIT:U+0020>' , <NUM_LIT:1> ) <EOL> extra = extra . strip ( ) <EOL> if flags . endswith ( '<STR_LIT:=>' ) : <EOL> flags = flags [ : - <NUM_LIT:1> ] <EOL> has_parm = <NUM_LIT:1> <EOL> else : <EOL> has_parm = <NUM_LIT:0> <EOL> g = re . search ( r'<STR_LIT>' , extra ) <EOL> if g : <EOL> defval = g . group ( <NUM_LIT:1> ) <EOL> else : <EOL> defval = None <EOL> flagl = flags . split ( '<STR_LIT:U+002C>' ) <EOL> flagl_nice = [ ] <EOL> for _f in flagl : <EOL> f , dvi = _remove_negative_kv ( _f , _intify ( defval ) ) <EOL> self . _aliases [ f ] = _remove_negative_k ( flagl [ <NUM_LIT:0> ] ) <EOL> self . _hasparms [ f ] = has_parm <EOL> self . _defaults [ f ] = dvi <EOL> if f == '<STR_LIT:#>' : <EOL> self . _shortopts += '<STR_LIT>' <EOL> flagl_nice . append ( '<STR_LIT>' ) <EOL> elif len ( f ) == <NUM_LIT:1> : <EOL> self . _shortopts += f + ( has_parm and '<STR_LIT::>' or '<STR_LIT>' ) <EOL> flagl_nice . append ( '<STR_LIT:->' + f ) <EOL> else : <EOL> f_nice = re . sub ( r'<STR_LIT>' , '<STR_LIT:_>' , f ) <EOL> self . _aliases [ f_nice ] = _remove_negative_k ( flagl [ <NUM_LIT:0> ] ) <EOL> self . _longopts . append ( f + ( has_parm and '<STR_LIT:=>' or '<STR_LIT>' ) ) <EOL> self . _longopts . append ( '<STR_LIT>' + f ) <EOL> flagl_nice . append ( '<STR_LIT>' + _f ) <EOL> flags_nice = '<STR_LIT:U+002CU+0020>' . join ( flagl_nice ) <EOL> if has_parm : <EOL> flags_nice += '<STR_LIT>' <EOL> prefix = '<STR_LIT>' % flags_nice <EOL> argtext = '<STR_LIT:\n>' . join ( textwrap . wrap ( extra , width = _tty_width ( ) , <EOL> initial_indent = prefix , <EOL> subsequent_indent = '<STR_LIT:U+0020>' * <NUM_LIT> ) ) <EOL> out . append ( argtext + '<STR_LIT:\n>' ) <EOL> last_was_option = True <EOL> else : <EOL> out . append ( '<STR_LIT:\n>' ) <EOL> last_was_option = False <EOL> return '<STR_LIT>' . join ( out ) . rstrip ( ) + '<STR_LIT:\n>' <EOL> def usage ( self , msg = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> sys . stderr . write ( self . _usagestr ) <EOL> if msg : <EOL> sys . stderr . write ( msg ) <EOL> e = self . _onabort and self . _onabort ( msg ) or None <EOL> if e : <EOL> raise e <EOL> def fatal ( self , msg ) : <EOL> """<STR_LIT>""" <EOL> msg = '<STR_LIT>' % msg <EOL> return self . usage ( msg ) <EOL> def parse ( self , args ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> ( flags , extra ) = self . optfunc ( args , self . _shortopts , self . _longopts ) <EOL> except getopt . GetoptError , e : <EOL> self . fatal ( e ) <EOL> opt = OptDict ( ) <EOL> for k , v in self . _defaults . iteritems ( ) : <EOL> k = self . _aliases [ k ] <EOL> opt [ k ] = v <EOL> for ( k , v ) in flags : <EOL> k = k . lstrip ( '<STR_LIT:->' ) <EOL> if k in ( '<STR_LIT:h>' , '<STR_LIT:?>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> self . usage ( ) <EOL> if k . startswith ( '<STR_LIT>' ) : <EOL> k = self . _aliases [ k [ <NUM_LIT:3> : ] ] <EOL> v = <NUM_LIT:0> <EOL> elif ( self . _aliases . get ( '<STR_LIT:#>' ) and <EOL> k in ( '<STR_LIT:0>' , '<STR_LIT:1>' , '<STR_LIT:2>' , '<STR_LIT:3>' , '<STR_LIT:4>' , '<STR_LIT:5>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> v = int ( k ) <EOL> k = self . _aliases [ '<STR_LIT:#>' ] <EOL> opt [ '<STR_LIT:#>' ] = v <EOL> else : <EOL> k = self . _aliases [ k ] <EOL> if not self . _hasparms [ k ] : <EOL> assert ( v == '<STR_LIT>' ) <EOL> v = ( opt . _opts . get ( k ) or <NUM_LIT:0> ) + <NUM_LIT:1> <EOL> else : <EOL> v = _intify ( v ) <EOL> opt [ k ] = v <EOL> for ( f1 , f2 ) in self . _aliases . iteritems ( ) : <EOL> opt [ f1 ] = opt . _opts . get ( f2 ) <EOL> return ( opt , flags , extra ) </s>
