From 5aae0f8326c9618841152b1b6a5d26715fecd210 Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Tue, 28 Jan 2020 13:05:53 -0800 Subject: [PATCH] Add command aliases --- amanuensis/__main__.py | 3 ++- amanuensis/cli/helpers.py | 11 +++++++++++ amanuensis/cli/server.py | 6 +++++- amanuensis/cli/user.py | 7 ++++++- 4 files changed, 24 insertions(+), 3 deletions(-) diff --git a/amanuensis/__main__.py b/amanuensis/__main__.py index 18734ee..e88eea4 100644 --- a/amanuensis/__main__.py +++ b/amanuensis/__main__.py @@ -101,7 +101,8 @@ def get_parser(valid_commands): # Create the subparser, set the docstring as the description. cmd = subp.add_parser(name, description=process_doc(func.__doc__), - formatter_class=argparse.RawDescriptionHelpFormatter) + formatter_class=argparse.RawDescriptionHelpFormatter, + aliases=func.__dict__.get("aliases", [])) # Delegate subparser setup to the command. func(cmd) # Store function for later execution. diff --git a/amanuensis/cli/helpers.py b/amanuensis/cli/helpers.py index f4121c9..508e58d 100644 --- a/amanuensis/cli/helpers.py +++ b/amanuensis/cli/helpers.py @@ -118,6 +118,17 @@ def requires_user(command): return augmented_command +# Wrapper for aliasing commands +def alias(cmd_alias): + """Adds an alias to the function dictionary""" + def aliaser(command): + aliases = command.__dict__.get('aliases', []) + aliases.append(cmd_alias) + command.__dict__['aliases'] = aliases + return command + return aliaser + + # Helpers for common command tasks CONFIG_GET_ROOT_VALUE = object() diff --git a/amanuensis/cli/server.py b/amanuensis/cli/server.py index 5eb2467..3eae23f 100644 --- a/amanuensis/cli/server.py +++ b/amanuensis/cli/server.py @@ -1,9 +1,10 @@ import os from amanuensis.cli.helpers import ( - add_argument, no_argument, + add_argument, no_argument, alias, config_get, config_set, CONFIG_GET_ROOT_VALUE) +@alias('i') @add_argument( "--refresh", action="store_true", help="Refresh an existing config directory") @@ -31,6 +32,7 @@ def command_init(args): return 0 +@alias('gs') @no_argument def command_generate_secret(args): """ @@ -49,6 +51,7 @@ def command_generate_secret(args): return 0 +@alias('r') @add_argument("-a", "--address", default="127.0.0.1") @add_argument("-p", "--port", default="5000") @add_argument("--debug", action="store_true") @@ -70,6 +73,7 @@ def command_run(args): return 0 +@alias('n') @add_argument("--get", metavar="PATHSPEC", dest="get", nargs="?", const=CONFIG_GET_ROOT_VALUE, help="Get the value of a config key") @add_argument("--set", metavar=("PATHSPEC", "VALUE"), dest="set", diff --git a/amanuensis/cli/user.py b/amanuensis/cli/user.py index f86e3b7..689493c 100644 --- a/amanuensis/cli/user.py +++ b/amanuensis/cli/user.py @@ -6,10 +6,11 @@ import shutil # Module imports from amanuensis.cli.helpers import ( - add_argument, no_argument, requires_user, + add_argument, no_argument, requires_user, alias, config_get, config_set, CONFIG_GET_ROOT_VALUE) +@alias('uc') @add_argument("--username", required=True, help="Name of user to create") @add_argument("--email", help="User's email") @add_argument("--displayname", help="User's publicly displayed name") @@ -44,6 +45,7 @@ def command_create(args): return 0 +@alias('ud') @requires_user def command_delete(args): """ @@ -65,6 +67,7 @@ def command_delete(args): return 0 +@alias('ul') @no_argument def command_list(args): """List all users""" @@ -88,6 +91,7 @@ def command_list(args): return 0 +@alias('un') @requires_user @add_argument( "--get", metavar="PATHSPEC", dest="get", @@ -120,6 +124,7 @@ def command_config(args): return 0 +@alias('up') @requires_user @add_argument("--password", help="The password to set. Used for scripting; " "not recommended for general use")