diff options
Diffstat (limited to 'libbe/ui/command_line.py')
-rw-r--r-- | libbe/ui/command_line.py | 75 |
1 files changed, 53 insertions, 22 deletions
diff --git a/libbe/ui/command_line.py b/libbe/ui/command_line.py index 5f42147..d5719a6 100644 --- a/libbe/ui/command_line.py +++ b/libbe/ui/command_line.py @@ -1,4 +1,5 @@ -# Copyright (C) 2009-2011 W. Trevor King <wking@drexel.edu> +# Copyright (C) 2009-2011 Chris Ball <cjb@laptop.org> +# W. Trevor King <wking@drexel.edu> # # This file is part of Bugs Everywhere. # @@ -29,6 +30,8 @@ import libbe.command import libbe.command.util import libbe.version import libbe.ui.util.pager +import libbe.util.encoding + if libbe.TESTING == True: import doctest @@ -86,11 +89,11 @@ class CmdOptionParser(optparse.OptionParser): if '_' in name: # reconstruct original option name options[name.replace('_', '-')] = options.pop(name) for name,value in options.items(): + argument = None + option = self._option_by_name[name] + if option.arg != None: + argument = option.arg if value == '--complete': - argument = None - option = self._option_by_name[name] - if option.arg != None: - argument = option.arg fragment = None indices = [i for i,arg in enumerate(args) if arg == '--complete'] @@ -108,29 +111,36 @@ class CmdOptionParser(optparse.OptionParser): if i+1 < len(args): fragment = args[i+1] self.complete(argument, fragment) + elif argument is not None: + value = self.process_raw_argument(argument=argument, value=value) + options[name] = value for i,arg in enumerate(parsed_args): + if i > 0 and self.command.name == 'be': + break # let this pass through for the command parser to handle + elif i < len(self.command.args): + argument = self.command.args[i] + elif len(self.command.args) == 0: + break # command doesn't take arguments + else: + argument = self.command.args[-1] + if argument.repeatable == False: + raise libbe.command.UserError('Too many arguments') if arg == '--complete': - if i > 0 and self.command.name == 'be': - break # let this pass through for the command parser to handle - elif i < len(self.command.args): - argument = self.command.args[i] - elif len(self.command.args) == 0: - break # command doesn't take arguments - else: - argument = self.command.args[-1] - if argument.repeatable == False: - raise libbe.command.UserError('Too many arguments') fragment = None if i < len(parsed_args) - 1: fragment = parsed_args[i+1] self.complete(argument, fragment) + else: + value = self.process_raw_argument(argument=argument, value=arg) + parsed_args[i] = value if len(parsed_args) > len(self.command.args) \ and self.command.args[-1].repeatable == False: raise libbe.command.UserError('Too many arguments') for arg in self.command.args[len(parsed_args):]: if arg.optional == False: - raise libbe.command.UserError( - 'Missing required argument %s' % arg.metavar) + raise libbe.command.UsageError( + command=self.command, + message='Missing required argument %s' % arg.metavar) return (options, parsed_args) def callback(self, option, opt, value, parser): @@ -154,6 +164,16 @@ class CmdOptionParser(optparse.OptionParser): print >> self.command.stdout, '\n'.join(comps) raise CallbackExit + def process_raw_argument(self, argument, value): + if value == argument.default: + return value + if argument.type == 'string': + if not hasattr(self, 'argv_encoding'): + self.argv_encoding = libbe.util.encoding.get_argv_encoding() + return unicode(value, self.argv_encoding) + return value + + class BE (libbe.command.Command): """Class for parsing the command line arguments for `be`. This class does not contain a useful _run() method. Call this @@ -271,6 +291,13 @@ def dispatch(ui, command, args): 'See http://docs.python.org/library/locale.html for details', ]) return 1 + except libbe.command.UsageError, e: + print >> ui.io.stdout, 'Usage Error:\n', e + if e.command: + print >> ui.io.stdout, e.command.usage() + print >> ui.io.stdout, 'For usage information, try' + print >> ui.io.stdout, ' be help %s' % e.command_name + return 1 except libbe.command.UserError, e: print >> ui.io.stdout, 'ERROR:\n', e return 1 @@ -296,15 +323,19 @@ def main(): options,args = parser.parse_args() except CallbackExit: return 0 - except libbe.command.UserError, e: - if str(e).endswith('COMMAND'): + except libbe.command.UsageError, e: + if isinstance(e.command, BE): # no command given, print usage string - print >> ui.io.stdout, 'ERROR:' - print >> ui.io.stdout, be.usage(), '\n', e + print >> ui.io.stdout, 'Usage Error:\n', e + print >> ui.io.stdout, be.usage() print >> ui.io.stdout, 'For example, try' print >> ui.io.stdout, ' be help' else: - print >> ui.io.stdout, 'ERROR:\n', e + print >> ui.io.stdout, 'Usage Error:\n', e + if e.command: + print >> ui.io.stdout, e.command.usage() + print >> ui.io.stdout, 'For usage information, try' + print >> ui.io.stdout, ' be help %s' % e.command_name return 1 command_name = args.pop(0) |