import base
UserError = base.UserError
+UsageError = base.UsageError
UnknownCommand = base.UnknownCommand
get_command = base.get_command
get_command_class = base.get_command_class
StorageCallbacks = base.StorageCallbacks
UserInterface = base.UserInterface
-__all__ = [UserError, UnknownCommand,
+__all__ = [UserError, UsageError, UnknownCommand,
get_command, get_command_class, commands,
Option, Argument, Command,
InputOutput, StdInputOutput, StringInputOutput,
import libbe.util.encoding
import libbe.util.plugin
-class UserError(Exception):
+
+class UserError (Exception):
+ "An error due to improper BE usage."
pass
-class UnknownCommand(UserError):
- def __init__(self, cmd):
- Exception.__init__(self, "Unknown command '%s'" % cmd)
- self.cmd = cmd
+
+class UsageError (UserError):
+ """A serious parsing error due to invalid BE command construction.
+
+ The distinction between `UserError`\s and the more specific
+ `UsageError`\s is that when displaying a `UsageError` to the user,
+ the user is pointed towards the command usage information. Use
+ the more general `UserError` if you feel that usage information
+ would not be particularly enlightening.
+ """
+ def __init__(self, command=None, command_name=None, message=None):
+ super(UsageError, self).__init__(message)
+ self.command = command
+ if command_name is None and command is not None:
+ command_name = command.name
+ self.command_name = command_name
+ self.message = message
+
+
+class UnknownCommand (UsageError):
+ def __init__(self, command_name):
+ super(UnknownCommand, self).__init__(
+ command_name=command_name,
+ message="Unknown command '%s'" % command_name)
+
def get_command(command_name):
"""Retrieves the module for a user command
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):
'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
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)