From 604ce5f012b72585c8f350656051ec6881615c0e Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Thu, 29 Aug 2019 17:17:47 -0500 Subject: [PATCH] readded command help support --- onionr/onionrcommands/banblocks.py | 4 +++- onionr/onionrcommands/parser/__init__.py | 26 ++++++++++++++++++++++- onionr/onionrcommands/parser/arguments.py | 19 +++++++++++------ onionr/onionrcommands/parser/recommend.py | 4 ++-- onionr/onionrcommands/resetplugins.py | 2 +- 5 files changed, 44 insertions(+), 11 deletions(-) diff --git a/onionr/onionrcommands/banblocks.py b/onionr/onionrcommands/banblocks.py index b4d8d82f..ce2f151d 100755 --- a/onionr/onionrcommands/banblocks.py +++ b/onionr/onionrcommands/banblocks.py @@ -40,4 +40,6 @@ def ban_block(): else: logger.warn('That block is already blacklisted', terminal=True) else: - logger.error('Invalid block hash', terminal=True) \ No newline at end of file + logger.error('Invalid block hash', terminal=True) + +ban_block.onionr_help = ": deletes and blacklists a block" \ No newline at end of file diff --git a/onionr/onionrcommands/parser/__init__.py b/onionr/onionrcommands/parser/__init__.py index 6ae81132..212a3017 100644 --- a/onionr/onionrcommands/parser/__init__.py +++ b/onionr/onionrcommands/parser/__init__.py @@ -33,11 +33,35 @@ def register_plugin_commands(cmd): return True def register(): + PROGRAM_NAME = "onionr" + def get_help_message(cmd: str, default: str = 'No help available for this command'): + for i in arguments.get_arguments(): + for alias in i: + try: + return arguments.get_help(cmd) + except AttributeError: + pass + return default + try: cmd = sys.argv[1] except IndexError: cmd = "" - + + if cmd.replace('--', '').lower() == 'help': + try: + sys.argv[2] + except IndexError: + for i in arguments.get_arguments(): + logger.info('%s <%s>: %s' % (PROGRAM_NAME, '/'.join(i), get_help_message(i[0])), terminal=True) + else: + try: + logger.info('%s %s: %s' % (PROGRAM_NAME, sys.argv[2], get_help_message(sys.argv[2])), terminal=True) + except KeyError: + logger.error('%s: command does not exist.' % [sys.argv[2]], terminal=True) + sys.exit(3) + return + try: arguments.get_func(cmd)() except onionrexceptions.NotFound: diff --git a/onionr/onionrcommands/parser/arguments.py b/onionr/onionrcommands/parser/arguments.py index 589a5722..8251f847 100644 --- a/onionr/onionrcommands/parser/arguments.py +++ b/onionr/onionrcommands/parser/arguments.py @@ -28,12 +28,12 @@ import onionrexceptions from onionrutils import importnewblocks # func to import new blocks import onionrevents as events def get_arguments(): - '''This is a function because we need to be able to dynamically modify them with plugins''' + """This is a function because we need to be able to dynamically modify them with plugins""" args = { - ('blacklist', 'blacklist-block', 'remove-block', 'removeblock'): banblocks.ban_block, + ('blacklist', 'blacklist-block', 'remove-block', 'removeblock', 'banblock', 'ban-block'): banblocks.ban_block, ('details', 'info'): onionrstatistics.show_details, ('stats', 'statistics'): onionrstatistics.show_stats, - ('version'): version.version, + ('version',): version.version, ('start', 'daemon'): daemonlaunch.start, ('stop', 'kill'): daemonlaunch.kill_daemon, ('add-address', 'addaddress', 'addadder'): keyadders.add_address, @@ -51,14 +51,21 @@ def get_arguments(): } return args -def get_help(): - return +def get_help(arg: str) -> str: + """Returns the help info string from a given command""" + arguments = get_arguments() + # Iterate the command alias tuples + for argument in arguments: + # Return the help message if its found in a command alias tuple + if arg in argument: return arguments[argument].onionr_help + raise KeyError def get_func(argument): + """Returns the function for a given command argument""" argument = argument.lower() args = get_arguments() for arg in args.keys(): # Iterate command alias sets if argument in arg: # If our argument is in the current alias set, return the command function return args[arg] - raise onionrexceptions.NotFound \ No newline at end of file + raise onionrexceptions.NotFound diff --git a/onionr/onionrcommands/parser/recommend.py b/onionr/onionrcommands/parser/recommend.py index ba745d36..4e8dcc38 100644 --- a/onionr/onionrcommands/parser/recommend.py +++ b/onionr/onionrcommands/parser/recommend.py @@ -3,7 +3,7 @@ from difflib import SequenceMatcher import logger from . import arguments -def recommend(): +def recommend(print_default: bool = True): tried = sys.argv[1] args = arguments.get_arguments() print_message = 'Command not found:' @@ -12,4 +12,4 @@ def recommend(): if SequenceMatcher(None, tried, word).ratio() >= 0.75: logger.warn('%s "%s", did you mean "%s"?' % (print_message, tried, word), terminal=True) return - logger.error('%s "%s"' % (print_message, tried), terminal=True) \ No newline at end of file + if print_default: logger.error('%s "%s"' % (print_message, tried), terminal=True) \ No newline at end of file diff --git a/onionr/onionrcommands/resetplugins.py b/onionr/onionrcommands/resetplugins.py index 09add244..f6c38f4c 100644 --- a/onionr/onionrcommands/resetplugins.py +++ b/onionr/onionrcommands/resetplugins.py @@ -32,4 +32,4 @@ def reset(): if os.path.exists(plugin_dir): shutil.rmtree(plugin_dir) setup_default_plugins() - logger.info('Default plugins have been reset.', terminal=True) \ No newline at end of file + logger.info('Default plugins have been reset.', terminal=True)