aboutsummaryrefslogtreecommitdiffstats
path: root/libbe/command
diff options
context:
space:
mode:
authorW. Trevor King <wking@drexel.edu>2011-05-02 17:48:18 -0400
committerW. Trevor King <wking@drexel.edu>2011-05-02 17:48:24 -0400
commit1fcb1dd9114fba59a5d75570b9acc3b87d575821 (patch)
treeaaed83677449589e1ef149a22d5bda1dac3ed9d8 /libbe/command
parent741abdd7453d400049a089c22686af9000a91db5 (diff)
downloadbugseverywhere-1fcb1dd9114fba59a5d75570b9acc3b87d575821.tar.gz
Revive the UserError/UsageError distinction
UsageError was removed back in commit bf3d434b244c57556bec979acbc658c30eb58221 Author: W. Trevor King <wking@drexel.edu> Date: Sat Dec 12 00:31:55 2009 -0500 Added libbe.command.base (with Command class)... because the distinction between UsageError and UserError was unclear. I've brought it back to satisfy a request by Christian Heinrich: On Sun, May 01, 2011 at 02:52:13AM +0200, Christian Heinrich wrote: > 3.) Using wrong syntax should receive better help messages. > > Current: > > "be new" -> ERROR: > Missing required argument SUMMARY > > Should be: > > "be new" -> usage: be new [options] SUMMARY > ... He suggested we print the full option list as well, but I've decided to just print the usage summary and remind the user how to get the full help message if they want it.
Diffstat (limited to 'libbe/command')
-rw-r--r--libbe/command/__init__.py3
-rw-r--r--libbe/command/base.py33
2 files changed, 30 insertions, 6 deletions
diff --git a/libbe/command/__init__.py b/libbe/command/__init__.py
index 1cad096..832218c 100644
--- a/libbe/command/__init__.py
+++ b/libbe/command/__init__.py
@@ -19,6 +19,7 @@
import base
UserError = base.UserError
+UsageError = base.UsageError
UnknownCommand = base.UnknownCommand
get_command = base.get_command
get_command_class = base.get_command_class
@@ -33,7 +34,7 @@ UnconnectedStorageGetter = base.UnconnectedStorageGetter
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,
diff --git a/libbe/command/base.py b/libbe/command/base.py
index b5f5a22..48bb7ab 100644
--- a/libbe/command/base.py
+++ b/libbe/command/base.py
@@ -27,13 +27,36 @@ import libbe.ui.util.user
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