<s> """<STR_LIT>""" <EOL> import re <EOL> from collections import deque <EOL> from . exceptions import UsageError , NoSuchOption , BadOptionUsage , BadArgumentUsage <EOL> def _unpack_args ( args , nargs_spec ) : <EOL> """<STR_LIT>""" <EOL> args = deque ( args ) <EOL> nargs_spec = deque ( nargs_spec ) <EOL> rv = [ ] <EOL> spos = None <EOL> def _fetch ( c ) : <EOL> try : <EOL> if spos is None : <EOL> return c . popleft ( ) <EOL> else : <EOL> return c . pop ( ) <EOL> except IndexError : <EOL> return None <EOL> while nargs_spec : <EOL> nargs = _fetch ( nargs_spec ) <EOL> if nargs == <NUM_LIT:1> : <EOL> rv . append ( _fetch ( args ) ) <EOL> elif nargs > <NUM_LIT:1> : <EOL> x = [ _fetch ( args ) for _ in range ( nargs ) ] <EOL> if spos is not None : <EOL> x . reverse ( ) <EOL> rv . append ( tuple ( x ) ) <EOL> elif nargs < <NUM_LIT:0> : <EOL> if spos is not None : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> spos = len ( rv ) <EOL> rv . append ( None ) <EOL> if spos is not None : <EOL> rv [ spos ] = tuple ( args ) <EOL> args = [ ] <EOL> rv [ spos + <NUM_LIT:1> : ] = reversed ( rv [ spos + <NUM_LIT:1> : ] ) <EOL> return tuple ( rv ) , list ( args ) <EOL> def _error_opt_args ( nargs , opt ) : <EOL> if nargs == <NUM_LIT:1> : <EOL> raise BadOptionUsage ( opt , '<STR_LIT>' % opt ) <EOL> raise BadOptionUsage ( opt , '<STR_LIT>' % ( opt , nargs ) ) <EOL> def split_opt ( opt ) : <EOL> first = opt [ : <NUM_LIT:1> ] <EOL> if first . isalnum ( ) : <EOL> return '<STR_LIT>' , opt <EOL> if opt [ <NUM_LIT:1> : <NUM_LIT:2> ] == first : <EOL> return opt [ : <NUM_LIT:2> ] , opt [ <NUM_LIT:2> : ] <EOL> return first , opt [ <NUM_LIT:1> : ] <EOL> def normalize_opt ( opt , ctx ) : <EOL> if ctx is None or ctx . token_normalize_func is None : <EOL> return opt <EOL> prefix , opt = split_opt ( opt ) <EOL> return prefix + ctx . token_normalize_func ( opt ) <EOL> def split_arg_string ( string ) : <EOL> """<STR_LIT>""" <EOL> rv = [ ] <EOL> for match in re . finditer ( r"<STR_LIT>" <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' , string , re . S ) : <EOL> arg = match . group ( ) . strip ( ) <EOL> if arg [ : <NUM_LIT:1> ] == arg [ - <NUM_LIT:1> : ] and arg [ : <NUM_LIT:1> ] in '<STR_LIT>' : <EOL> arg = arg [ <NUM_LIT:1> : - <NUM_LIT:1> ] . encode ( '<STR_LIT:ascii>' , '<STR_LIT>' ) . decode ( '<STR_LIT>' ) <EOL> try : <EOL> arg = type ( string ) ( arg ) <EOL> except UnicodeError : <EOL> pass <EOL> rv . append ( arg ) <EOL> return rv <EOL> class Option ( object ) : <EOL> def __init__ ( self , opts , dest , action = None , nargs = <NUM_LIT:1> , const = None , obj = None ) : <EOL> self . _short_opts = [ ] <EOL> self . _long_opts = [ ] <EOL> self . prefixes = set ( ) <EOL> for opt in opts : <EOL> prefix , value = split_opt ( opt ) <EOL> if not prefix : <EOL> raise ValueError ( '<STR_LIT>' <EOL> % opt ) <EOL> self . prefixes . add ( prefix [ <NUM_LIT:0> ] ) <EOL> if len ( prefix ) == <NUM_LIT:1> and len ( value ) == <NUM_LIT:1> : <EOL> self . _short_opts . append ( opt ) <EOL> else : <EOL> self . _long_opts . append ( opt ) <EOL> self . prefixes . add ( prefix ) <EOL> if action is None : <EOL> action = '<STR_LIT:store>' <EOL> self . dest = dest <EOL> self . action = action <EOL> self . nargs = nargs <EOL> self . const = const <EOL> self . obj = obj <EOL> @ property <EOL> def takes_value ( self ) : <EOL> return self . action in ( '<STR_LIT:store>' , '<STR_LIT>' ) <EOL> def process ( self , value , state ) : <EOL> if self . action == '<STR_LIT:store>' : <EOL> state . opts [ self . dest ] = value <EOL> elif self . action == '<STR_LIT>' : <EOL> state . opts [ self . dest ] = self . const <EOL> elif self . action == '<STR_LIT>' : <EOL> state . opts . setdefault ( self . dest , [ ] ) . append ( value ) <EOL> elif self . action == '<STR_LIT>' : <EOL> state . opts . setdefault ( self . dest , [ ] ) . append ( self . const ) <EOL> elif self . action == '<STR_LIT:count>' : <EOL> state . opts [ self . dest ] = state . opts . get ( self . dest , <NUM_LIT:0> ) + <NUM_LIT:1> <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' % self . action ) <EOL> state . order . append ( self . obj ) <EOL> class Argument ( object ) : <EOL> def __init__ ( self , dest , nargs = <NUM_LIT:1> , obj = None ) : <EOL> self . dest = dest <EOL> self . nargs = nargs <EOL> self . obj = obj <EOL> def process ( self , value , state ) : <EOL> if self . nargs > <NUM_LIT:1> : <EOL> holes = sum ( <NUM_LIT:1> for x in value if x is None ) <EOL> if holes == len ( value ) : <EOL> value = None <EOL> elif holes != <NUM_LIT:0> : <EOL> raise BadArgumentUsage ( '<STR_LIT>' <EOL> % ( self . dest , self . nargs ) ) <EOL> state . opts [ self . dest ] = value <EOL> state . order . append ( self . obj ) <EOL> class ParsingState ( object ) : <EOL> def __init__ ( self , rargs ) : <EOL> self . opts = { } <EOL> self . largs = [ ] <EOL> self . rargs = rargs <EOL> self . order = [ ] <EOL> class OptionParser ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , ctx = None ) : <EOL> self . ctx = ctx <EOL> self . allow_interspersed_args = True <EOL> self . ignore_unknown_options = False <EOL> if ctx is not None : <EOL> self . allow_interspersed_args = ctx . allow_interspersed_args <EOL> self . ignore_unknown_options = ctx . ignore_unknown_options <EOL> self . _short_opt = { } <EOL> self . _long_opt = { } <EOL> self . _opt_prefixes = set ( [ '<STR_LIT:->' , '<STR_LIT>' ] ) <EOL> self . _args = [ ] <EOL> def add_option ( self , opts , dest , action = None , nargs = <NUM_LIT:1> , const = None , <EOL> obj = None ) : <EOL> """<STR_LIT>""" <EOL> if obj is None : <EOL> obj = dest <EOL> opts = [ normalize_opt ( opt , self . ctx ) for opt in opts ] <EOL> option = Option ( opts , dest , action = action , nargs = nargs , <EOL> const = const , obj = obj ) <EOL> self . _opt_prefixes . update ( option . prefixes ) <EOL> for opt in option . _short_opts : <EOL> self . _short_opt [ opt ] = option <EOL> for opt in option . _long_opts : <EOL> self . _long_opt [ opt ] = option <EOL> def add_argument ( self , dest , nargs = <NUM_LIT:1> , obj = None ) : <EOL> """<STR_LIT>""" <EOL> if obj is None : <EOL> obj = dest <EOL> self . _args . append ( Argument ( dest = dest , nargs = nargs , obj = obj ) ) <EOL> def parse_args ( self , args ) : <EOL> """<STR_LIT>""" <EOL> state = ParsingState ( args ) <EOL> try : <EOL> self . _process_args_for_options ( state ) <EOL> self . _process_args_for_args ( state ) <EOL> except UsageError : <EOL> if self . ctx is None or not self . ctx . resilient_parsing : <EOL> raise <EOL> return state . opts , state . largs , state . order <EOL> def _process_args_for_args ( self , state ) : <EOL> pargs , args = _unpack_args ( state . largs + state . rargs , <EOL> [ x . nargs for x in self . _args ] ) <EOL> for idx , arg in enumerate ( self . _args ) : <EOL> arg . process ( pargs [ idx ] , state ) <EOL> state . largs = args <EOL> state . rargs = [ ] <EOL> def _process_args_for_options ( self , state ) : <EOL> while state . rargs : <EOL> arg = state . rargs . pop ( <NUM_LIT:0> ) <EOL> arglen = len ( arg ) <EOL> if arg == '<STR_LIT>' : <EOL> return <EOL> elif arg [ : <NUM_LIT:1> ] in self . _opt_prefixes and arglen > <NUM_LIT:1> : <EOL> self . _process_opts ( arg , state ) <EOL> elif self . allow_interspersed_args : <EOL> state . largs . append ( arg ) <EOL> else : <EOL> state . rargs . insert ( <NUM_LIT:0> , arg ) <EOL> return <EOL> def _match_long_opt ( self , opt , explicit_value , state ) : <EOL> if opt not in self . _long_opt : <EOL> possibilities = [ word for word in self . _long_opt <EOL> if word . startswith ( opt ) ] <EOL> raise NoSuchOption ( opt , possibilities = possibilities ) <EOL> option = self . _long_opt [ opt ] <EOL> if option . takes_value : <EOL> if explicit_value is not None : <EOL> state . rargs . insert ( <NUM_LIT:0> , explicit_value ) <EOL> nargs = option . nargs <EOL> if len ( state . rargs ) < nargs : <EOL> _error_opt_args ( nargs , opt ) <EOL> elif nargs == <NUM_LIT:1> : <EOL> value = state . rargs . pop ( <NUM_LIT:0> ) <EOL> else : <EOL> value = tuple ( state . rargs [ : nargs ] ) <EOL> del state . rargs [ : nargs ] <EOL> elif explicit_value is not None : <EOL> raise BadOptionUsage ( opt , '<STR_LIT>' % opt ) <EOL> else : <EOL> value = None <EOL> option . process ( value , state ) <EOL> def _match_short_opt ( self , arg , state ) : <EOL> stop = False <EOL> i = <NUM_LIT:1> <EOL> prefix = arg [ <NUM_LIT:0> ] <EOL> unknown_options = [ ] <EOL> for ch in arg [ <NUM_LIT:1> : ] : <EOL> opt = normalize_opt ( prefix + ch , self . ctx ) <EOL> option = self . _short_opt . get ( opt ) <EOL> i += <NUM_LIT:1> <EOL> if not option : <EOL> if self . ignore_unknown_options : <EOL> unknown_options . append ( ch ) <EOL> continue <EOL> raise NoSuchOption ( opt ) <EOL> if option . takes_value : <EOL> if i < len ( arg ) : <EOL> state . rargs . insert ( <NUM_LIT:0> , arg [ i : ] ) <EOL> stop = True <EOL> nargs = option . nargs <EOL> if len ( state . rargs ) < nargs : <EOL> _error_opt_args ( nargs , opt ) <EOL> elif nargs == <NUM_LIT:1> : <EOL> value = state . rargs . pop ( <NUM_LIT:0> ) <EOL> else : <EOL> value = tuple ( state . rargs [ : nargs ] ) <EOL> del state . rargs [ : nargs ] <EOL> else : <EOL> value = None <EOL> option . process ( value , state ) <EOL> if stop : <EOL> break <EOL> if self . ignore_unknown_options and unknown_options : <EOL> state . largs . append ( prefix + '<STR_LIT>' . join ( unknown_options ) ) <EOL> def _process_opts ( self , arg , state ) : <EOL> explicit_value = None <EOL> if '<STR_LIT:=>' in arg : <EOL> long_opt , explicit_value = arg . split ( '<STR_LIT:=>' , <NUM_LIT:1> ) <EOL> else : <EOL> long_opt = arg <EOL> norm_long_opt = normalize_opt ( long_opt , self . ctx ) <EOL> try : <EOL> self . _match_long_opt ( norm_long_opt , explicit_value , state ) <EOL> except NoSuchOption : <EOL> if arg [ : <NUM_LIT:2> ] not in self . _opt_prefixes : <EOL> return self . _match_short_opt ( arg , state ) <EOL> if not self . ignore_unknown_options : <EOL> raise <EOL> state . largs . append ( arg ) </s>
