diff options
author | Doug Hellmann <doug.hellmann@dreamhost.com> | 2012-04-26 16:18:07 -0400 |
---|---|---|
committer | Doug Hellmann <doug.hellmann@dreamhost.com> | 2012-04-26 16:18:07 -0400 |
commit | b5c97668034e455e6e56f037473c7b9da8d8f08a (patch) | |
tree | 23f01d512ed5ae628573102e5807fd5b7f36403b | |
parent | 2db12c2e90620d17b3d81da52a71443a4ea4a728 (diff) | |
download | cliff-tablib-b5c97668034e455e6e56f037473c7b9da8d8f08a.tar.gz |
provide an internal API for applications to register commands without going through setuptools (used for help handler)
-rw-r--r-- | cliff/commandmanager.py | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/cliff/commandmanager.py b/cliff/commandmanager.py index 65af9ed..bb8bfe2 100644 --- a/cliff/commandmanager.py +++ b/cliff/commandmanager.py @@ -9,6 +9,18 @@ import pkg_resources LOG = logging.getLogger(__name__) +class EntryPointWrapper(object): + """Wrap up a command class already imported to make it look like a plugin. + """ + + def __init__(self, name, command_class): + self.name = name + self.command_class = command_class + + def load(self): + return self.command_class + + class CommandManager(object): """Discovers commands and handles lookup based on argv data. """ @@ -26,6 +38,9 @@ class CommandManager(object): def __iter__(self): return iter(self.commands.items()) + def add_command(self, name, command_class): + self.commands[name] = EntryPointWrapper(name, command_class) + def find_command(self, argv): """Given an argument list, find a command and return the processor and any remaining arguments. @@ -42,5 +57,5 @@ class CommandManager(object): cmd_factory = cmd_ep.load() return (cmd_factory, name, search_args) else: - raise ValueError('Did not find command processor for %r' % + raise ValueError('Unknown command %r' % (argv,)) |