Add command aliases

This commit is contained in:
Tim Van Baak 2020-01-28 13:05:53 -08:00
parent 272ec492f8
commit 5aae0f8326
4 changed files with 24 additions and 3 deletions

View File

@ -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.

View File

@ -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()

View File

@ -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",

View File

@ -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")