Commit cd1bda40 authored by Steve Goering's avatar Steve Goering

add aliases and command help functions

parent 9a50ed75
......@@ -5,6 +5,15 @@
"update": "core/",
"version": "core/",
"commands_help": {
"core" :"Core functions of webis command.",
"core": "Core functions of webis command.",
"test": "A few unimportant test scripts for developing purpose.",
"betamng": "Low level scripts for managing betaweb.",
"betaweb": "High level scripts for betaweb, e.g. hadoop management."
"commands_aliases": {
"core": ["co"],
"betamng": ["bm"],
"betaweb": ["bw"]
\ No newline at end of file
......@@ -6,9 +6,39 @@
import re
import os
import argparse
class AliasedSubParsersAction(argparse._SubParsersAction):
""" Based on : """
class _AliasedPseudoAction(argparse.Action):
def __init__(self, name, aliases, help):
dest = name
if aliases:
dest += ' (%s)' % ','.join(aliases)
sup = super(AliasedSubParsersAction._AliasedPseudoAction, self)
sup.__init__(option_strings=[], dest=dest, help=help)
def add_parser(self, name, **kwargs):
if 'aliases' in kwargs:
aliases = kwargs['aliases']
del kwargs['aliases']
aliases = []
parser = super(AliasedSubParsersAction, self).add_parser(name, **kwargs)
# Make the aliases work.
for alias in aliases:
self._name_parser_map[alias] = parser
# Make the help text reflect them, first removing old help entry.
if 'help' in kwargs:
help = kwargs.pop('help')
pseudo_action = self._AliasedPseudoAction(name, aliases, help)
return parser
if __name__ == "__main__":
print("\033[91m[ERROR]\033[0m lib is not a standalone module")
......@@ -18,6 +18,18 @@ from log import *
from system import *
def check_commands_alias_definition(config):
"""Checks if the command alias definition has no collisions."""
aliashisto = {}
for command in config["commands_aliases"]:
for alias in config["commands_aliases"][command]:
aliashisto[alias] = aliashisto.get(alias, 0) + 1
if len(config["commands_aliases"].keys()) != len(aliashisto.keys()):
lError("Commands alias definition has collisions, please fix it.")
def load_config():
"""Returns the configuration, based on the JSON configuration file."""
......@@ -86,22 +98,36 @@ def run_subcommand(subcommand, subcommandpath, params=[]):
lError("An error occurred while executing " + cmd)
return return_value
def translate_alias_to_command(config, alias):
"""Transforms a given alias to the unique command."""
for command in config["commands_aliases"]:
if alias in config["commands_aliases"][cmd]:
return command
def main(args):
config = load_config()
commands = get_commands(config)
parser = argparse.ArgumentParser()
parser.register('action', 'parsers', AliasedSubParsersAction)
subparsers = {}
allcommands = {}
commandparsers = parser.add_subparsers(
metavar='<command>', dest='<command>', help='Available commands:')
for command in commands:
# TODO: It is unclear where to obtain the help messages for commands.
# TODO: It is conceivable to introduce aliases for each command, but it
# is unclear where to obtain them. Simply using a prefix does not
# work for betamng and betaweb, since they share a long prefix.
help_msg = config["commands_help"].get(
"Please add a short description of {} "
"in config.json[commands_help].".format(command)))
commandparser = commandparsers.add_parser(
command, help='TODO: Help message for commands.')
command, aliases=config["commands_aliases"].get(command, []),
subparsers[command] = commandparser
subcommandparsers = commandparser.add_subparsers(
metavar='<subcommand>', dest='<subcommand>',
......@@ -124,7 +150,11 @@ def main(args):
parameter = parseresults[1]
if len(argsdict) == 2 and argsdict['<subcommand>'] is None:
command = argsdict['<command>']
if command not in subparsers:
command = translate_alias_to_command(command)
return 0
return run_subcommand(
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment