[eit.main] automatically build the list of available commands, add command aliases support
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -33,6 +33,7 @@ class EitAdd(EitCommit):
|
||||
"""
|
||||
|
||||
NAME = "add"
|
||||
ALIASES = []
|
||||
|
||||
def parse(self):
|
||||
""" Overridden from EitCommit """
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -33,6 +33,7 @@ class EitCommit(EitCommand):
|
||||
"""
|
||||
|
||||
NAME = "commit"
|
||||
ALIASES = ["ci"]
|
||||
|
||||
def __init__(self, args):
|
||||
EitCommand.__init__(self, args)
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -33,6 +33,7 @@ class EitStatus(EitCommand):
|
||||
"""
|
||||
|
||||
NAME = "status"
|
||||
ALIASES = ["st"]
|
||||
|
||||
def parse(self):
|
||||
descriptor = EitCommandDescriptor.obtain_descriptor(
|
||||
|
||||
+14
-15
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user