From f4c29b84895299b61d950480a34bc41ca649f5ea Mon Sep 17 00:00:00 2001 From: Fabio Erculiani Date: Mon, 10 Oct 2011 18:49:57 +0200 Subject: [PATCH] [eit.main] automatically build the list of available commands, add command aliases support --- server/eit/commands/__init__.py | 4 ++++ server/eit/commands/add.py | 1 + server/eit/commands/command.py | 4 ++++ server/eit/commands/commit.py | 1 + server/eit/commands/help.py | 8 +++++++- server/eit/commands/status.py | 1 + server/eit/main.py | 29 ++++++++++++++--------------- 7 files changed, 32 insertions(+), 16 deletions(-) diff --git a/server/eit/commands/__init__.py b/server/eit/commands/__init__.py index b0e064f6f..ef416f5dd 100644 --- a/server/eit/commands/__init__.py +++ b/server/eit/commands/__init__.py @@ -9,3 +9,7 @@ B{Entropy Infrastructure Toolkit}. """ +from eit.commands.help import EitHelp +from eit.commands.status import EitStatus +from eit.commands.commit import EitCommit +from eit.commands.add import EitAdd diff --git a/server/eit/commands/add.py b/server/eit/commands/add.py index 3f1bc2558..81640be2e 100644 --- a/server/eit/commands/add.py +++ b/server/eit/commands/add.py @@ -33,6 +33,7 @@ class EitAdd(EitCommit): """ NAME = "add" + ALIASES = [] def parse(self): """ Overridden from EitCommit """ diff --git a/server/eit/commands/command.py b/server/eit/commands/command.py index 29d07dbb2..68d57f365 100644 --- a/server/eit/commands/command.py +++ b/server/eit/commands/command.py @@ -22,6 +22,10 @@ class EitCommand(object): # gets triggered (for eit help, "help" is the NAME # that should be set). NAME = None + # Set this to a list of aliases for NAME + ALIASES = [] + # Set this to True if command is a catch-all (fallback) + CATCH_ALL = False def __init__(self, args): self._args = args diff --git a/server/eit/commands/commit.py b/server/eit/commands/commit.py index aed58661a..92287f1d5 100644 --- a/server/eit/commands/commit.py +++ b/server/eit/commands/commit.py @@ -33,6 +33,7 @@ class EitCommit(EitCommand): """ NAME = "commit" + ALIASES = ["ci"] def __init__(self, args): EitCommand.__init__(self, args) diff --git a/server/eit/commands/help.py b/server/eit/commands/help.py index 2c8522a86..120f5a3bc 100644 --- a/server/eit/commands/help.py +++ b/server/eit/commands/help.py @@ -22,6 +22,8 @@ class EitHelp(EitCommand): """ NAME = "help" + ALIASES = ["-h", "--help"] + CATCH_ALL = True def parse(self): """ @@ -39,7 +41,11 @@ class EitHelp(EitCommand): descriptors.sort(key = lambda x: x.get_name()) group = parser.add_argument_group("command", "available commands") for descriptor in descriptors: - group.add_argument(descriptor.get_name(), + aliases_str = ", ".join(descriptor.get_class().ALIASES) + if aliases_str: + aliases_str = " [%s]" % (aliases_str,) + name = "%s%s" % (descriptor.get_name(), aliases_str) + group.add_argument(name, help=descriptor.get_description(), action="store_true") parser.print_help() diff --git a/server/eit/commands/status.py b/server/eit/commands/status.py index 5205c07b1..c34d13280 100644 --- a/server/eit/commands/status.py +++ b/server/eit/commands/status.py @@ -33,6 +33,7 @@ class EitStatus(EitCommand): """ NAME = "status" + ALIASES = ["st"] def parse(self): descriptor = EitCommandDescriptor.obtain_descriptor( diff --git a/server/eit/main.py b/server/eit/main.py index c17c198c8..5a7fc2966 100644 --- a/server/eit/main.py +++ b/server/eit/main.py @@ -17,10 +17,7 @@ from entropy.i18n import _ from entropy.output import print_error import entropy.tools -from eit.commands.status import EitStatus -from eit.commands.help import EitHelp -from eit.commands.commit import EitCommit -from eit.commands.add import EitAdd +from eit.commands.descriptor import EitCommandDescriptor def handle_exception(exc_class, exc_instance, exc_tb): @@ -47,14 +44,16 @@ def main(): install_exception_handler() - args_map = { - EitHelp.NAME: EitHelp, - "-h": EitHelp, - "--help": EitHelp, - EitStatus.NAME: EitStatus, - EitCommit.NAME: EitCommit, - EitAdd.NAME: EitAdd, - } + descriptors = EitCommandDescriptor.obtain() + args_map = {} + catch_all = None + for descriptor in descriptors: + klass = descriptor.get_class() + if klass.CATCH_ALL: + catch_all = descriptor + args_map[klass.NAME] = klass + for alias in klass.ALIASES: + args_map[alias] = klass args = sys.argv[1:] cmd = None @@ -64,13 +63,13 @@ def main(): cmd_class = args_map.get(cmd) if cmd_class is None: - cmd_class = args_map.get(EitHelp.NAME) + cmd_class = catch_all # non-root users not allowed allowed = True if os.getuid() != 0 and \ - cmd_class is not EitHelp: - cmd_class = EitHelp + cmd_class is not catch_all: + cmd_class = catch_all allowed = False cmd_obj = cmd_class(args)