<s> from exceptions import * <EOL> import command <EOL> import tokenize <EOL> class ParsedArgs ( object ) : <EOL> pass <EOL> class ArgCommand ( command . Command ) : <EOL> def __init__ ( self , args , help ) : <EOL> self . argTypes = args <EOL> self . helpText = help <EOL> def run ( self , cli_obj , name , args , line ) : <EOL> args = args [ <NUM_LIT:1> : ] <EOL> num_args = len ( args ) <EOL> num_arg_types = len ( self . argTypes ) <EOL> if num_args > num_arg_types : <EOL> trailing_args = args [ num_arg_types : ] <EOL> msg = '<STR_LIT>' + tokenize . escape_args ( trailing_args ) <EOL> raise CommandArgumentsError ( msg ) <EOL> parsed_args = ParsedArgs ( ) <EOL> for n in range ( num_args ) : <EOL> arg_type = self . argTypes [ n ] <EOL> value = arg_type . parse ( cli_obj , args [ n ] ) <EOL> setattr ( parsed_args , arg_type . getName ( ) , value ) <EOL> if num_args < num_arg_types : <EOL> arg_type = self . argTypes [ num_args ] <EOL> if not arg_type . isOptional ( ) : <EOL> msg = '<STR_LIT>' % ( arg_type . getHrName ( ) , ) <EOL> raise CommandArgumentsError ( msg ) <EOL> for n in range ( num_args , num_arg_types ) : <EOL> arg_type = self . argTypes [ n ] <EOL> setattr ( parsed_args , arg_type . getName ( ) , arg_type . getDefaultValue ( ) ) <EOL> return self . runParsed ( cli_obj , name , parsed_args ) <EOL> def help ( self , cli_obj , name , args , line ) : <EOL> args = args [ <NUM_LIT:1> : ] <EOL> syntax = name <EOL> end = '<STR_LIT>' <EOL> for arg in self . argTypes : <EOL> if arg . isOptional ( ) : <EOL> syntax += '<STR_LIT>' % ( arg . getName ( ) , ) <EOL> end += '<STR_LIT:]>' <EOL> else : <EOL> syntax += '<STR_LIT>' % ( arg . getName ( ) , ) <EOL> syntax += end <EOL> cli_obj . output ( syntax ) <EOL> if not self . helpText : <EOL> return <EOL> cli_obj . output ( ) <EOL> cli_obj . output ( self . helpText ) <EOL> def complete ( self , cli_obj , name , args , text ) : <EOL> args = args [ <NUM_LIT:1> : ] <EOL> index = len ( args ) <EOL> try : <EOL> arg_type = self . argTypes [ index ] <EOL> except IndexError : <EOL> return [ ] <EOL> return arg_type . complete ( cli_obj , text ) <EOL> class Argument ( object ) : <EOL> def __init__ ( self , name , ** kwargs ) : <EOL> self . name = name <EOL> self . hrName = name <EOL> self . default = None <EOL> self . optional = False <EOL> for ( kwname , kwvalue ) in kwargs . items ( ) : <EOL> if kwname == '<STR_LIT:default>' : <EOL> self . default = kwvalue <EOL> elif kwname == '<STR_LIT>' : <EOL> self . hrName = kwvalue <EOL> elif kwname == '<STR_LIT>' : <EOL> self . optional = kwvalue <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' % ( kwname , ) ) <EOL> def getName ( self ) : <EOL> return self . name <EOL> def getHrName ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . hrName <EOL> def isOptional ( self ) : <EOL> return self . optional <EOL> def getDefaultValue ( self ) : <EOL> return self . default <EOL> def complete ( self , cli_obj , text ) : <EOL> return [ ] <EOL> class StringArgument ( Argument ) : <EOL> def parse ( self , cli_obj , arg ) : <EOL> return arg <EOL> class IntArgument ( Argument ) : <EOL> def __init__ ( self , name , ** kwargs ) : <EOL> self . min = None <EOL> self . max = None <EOL> arg_kwargs = { } <EOL> for ( kwname , kwvalue ) in kwargs . items ( ) : <EOL> if kwname == '<STR_LIT>' : <EOL> self . min = kwvalue <EOL> elif kwname == '<STR_LIT>' : <EOL> self . max = max <EOL> else : <EOL> arg_kwargs [ kwname ] = kwvalue <EOL> Argument . __init__ ( self , name , ** arg_kwargs ) <EOL> def parse ( self , cli_obj , arg ) : <EOL> try : <EOL> value = int ( arg ) <EOL> except ValueError : <EOL> msg = '<STR_LIT>' % ( self . getHrName ( ) , ) <EOL> raise CommandArgumentsError ( msg ) <EOL> if self . min != None and value < self . min : <EOL> msg = '<STR_LIT>' % ( self . getHrName ( ) , self . min ) <EOL> raise CommandArgumentsError ( msg ) <EOL> if self . max != None and value > self . max : <EOL> msg = '<STR_LIT>' % ( self . getHrName ( ) , self . max ) <EOL> raise CommandArgumentsError ( msg ) <EOL> return value </s>
