def parse_args(self, args=None, values=None): try: return optparse.OptionParser.parse_args(self, args, values) except gobject.GError, error: if error.domain != gobject.OPTION_ERROR: raise if error.code == gobject.OPTION_ERROR_BAD_VALUE: raise OptionValueError(error.message) elif error.code == gobject.OPTION_ERROR_UNKNOWN_OPTION: raise BadOptionError(error.message) elif error.code == gobject.OPTION_ERROR_FAILED: raise OptParseError(error.message) else: raise
def _process_long_opt(self, args, values): for arg in args.pop(0).split(','): try: opt, value = arg.split('=') except ValueError: opt, value = arg, None try: option = self._long_opt[opt] except KeyError: raise BadOptionError(opt) if option.takes_value(): if option.nargs != 1: raise OptParseError( 'grouped options must only take a single argument') if value is None: self.error('option %s requires an argument' % opt) option.process(opt, value, values, self)
def parse_args(self, args=None, values=None): try: options, args = optparse.OptionParser.parse_args( self, args, values) except GError: error = sys.exc_info()[1] if error.domain != OPTION_CONTEXT_ERROR_QUARK: raise if error.code == GLib.OptionError.BAD_VALUE: raise OptionValueError(error.message) elif error.code == GLib.OptionError.UNKNOWN_OPTION: raise BadOptionError(error.message) elif error.code == GLib.OptionError.FAILED: raise OptParseError(error.message) else: raise for group in self.option_groups: for key, value in group.values.__dict__.items(): options.ensure_value(key, value) return options, args
def _process_args2(self, largs, rargs, values): """_process_args(largs : [string], rargs : [string], values : Values) Process command-line arguments and populate 'values', consuming options and arguments from 'rargs'. If 'allow_interspersed_args' is false, stop at the first non-option argument. If true, accumulate any interspersed non-option arguments in 'largs'. """ while rargs: arg = rargs[0] p = re.match('^-[0-9.]+$', arg) # Identify a numeric argument if p: del rargs[0] raise BadOptionError(arg) #self.error(_("%s unrecognized number in arguments") % arg) # We handle bare "--" explicitly, and bare "-" is handled by the # standard arg handler since the short arg case ensures that the # len of the opt string is greater than 1. if arg == "--": del rargs[0] return elif arg[0:2] == "--": # process a single long option (possibly with value(s)) OptionParser._process_long_opt(self, rargs, values) elif arg[:1] == "-" and len(arg) > 1: # process a cluster of short options (possibly with # value(s) for the last one only) OptionParser._process_short_opts(self, rargs, values) elif self.allow_interspersed_args: largs.append(arg) del rargs[0] else: return # stop now, leave this arg in rargs
def parse_args(self, args=None, values=None): old_args = args or [] try: options, args = optparse.OptionParser.parse_args( self, args, values) except _glib.GError: error = sys.exc_info()[1] if error.domain != _glib.OPTION_ERROR: raise if error.code == _glib.OPTION_ERROR_BAD_VALUE: raise OptionValueError(error.message) elif error.code == _glib.OPTION_ERROR_UNKNOWN_OPTION: raise BadOptionError(error.message) elif error.code == _glib.OPTION_ERROR_FAILED: raise OptParseError(error.message) else: raise for group in self.option_groups: for key, value in group.values.__dict__.items(): options.ensure_value(key, value) args = args[2:-len(old_args)] return options, args
def error(self, msg): """ Explicitly raise BadOptionError so calling program can handle it.""" raise BadOptionError(msg)
def error(self, msg): self.print_usage(sys.stderr) if 'no such option' in msg: raise BadOptionError(msg.replace('no such option: --', '')) self.exit(2, "%s: error: %s\n" % (self.get_prog_name(), msg))
def __init__(self, *args, **kwargs): BadOptionError.__init__(self, *args, **kwargs)