readded command help support

This commit is contained in:
Kevin Froman 2019-08-29 17:17:47 -05:00
parent 03649756f5
commit 604ce5f012
5 changed files with 44 additions and 11 deletions

View File

@ -40,4 +40,6 @@ def ban_block():
else: else:
logger.warn('That block is already blacklisted', terminal=True) logger.warn('That block is already blacklisted', terminal=True)
else: else:
logger.error('Invalid block hash', terminal=True) logger.error('Invalid block hash', terminal=True)
ban_block.onionr_help = "<block hash>: deletes and blacklists a block"

View File

@ -33,11 +33,35 @@ def register_plugin_commands(cmd):
return True return True
def register(): 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: try:
cmd = sys.argv[1] cmd = sys.argv[1]
except IndexError: except IndexError:
cmd = "" 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: try:
arguments.get_func(cmd)() arguments.get_func(cmd)()
except onionrexceptions.NotFound: except onionrexceptions.NotFound:

View File

@ -28,12 +28,12 @@ import onionrexceptions
from onionrutils import importnewblocks # func to import new blocks from onionrutils import importnewblocks # func to import new blocks
import onionrevents as events import onionrevents as events
def get_arguments(): 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 = { 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, ('details', 'info'): onionrstatistics.show_details,
('stats', 'statistics'): onionrstatistics.show_stats, ('stats', 'statistics'): onionrstatistics.show_stats,
('version'): version.version, ('version',): version.version,
('start', 'daemon'): daemonlaunch.start, ('start', 'daemon'): daemonlaunch.start,
('stop', 'kill'): daemonlaunch.kill_daemon, ('stop', 'kill'): daemonlaunch.kill_daemon,
('add-address', 'addaddress', 'addadder'): keyadders.add_address, ('add-address', 'addaddress', 'addadder'): keyadders.add_address,
@ -51,14 +51,21 @@ def get_arguments():
} }
return args return args
def get_help(): def get_help(arg: str) -> str:
return """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): def get_func(argument):
"""Returns the function for a given command argument"""
argument = argument.lower() argument = argument.lower()
args = get_arguments() args = get_arguments()
for arg in args.keys(): # Iterate command alias sets 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 if argument in arg: # If our argument is in the current alias set, return the command function
return args[arg] return args[arg]
raise onionrexceptions.NotFound raise onionrexceptions.NotFound

View File

@ -3,7 +3,7 @@ from difflib import SequenceMatcher
import logger import logger
from . import arguments from . import arguments
def recommend(): def recommend(print_default: bool = True):
tried = sys.argv[1] tried = sys.argv[1]
args = arguments.get_arguments() args = arguments.get_arguments()
print_message = 'Command not found:' print_message = 'Command not found:'
@ -12,4 +12,4 @@ def recommend():
if SequenceMatcher(None, tried, word).ratio() >= 0.75: if SequenceMatcher(None, tried, word).ratio() >= 0.75:
logger.warn('%s "%s", did you mean "%s"?' % (print_message, tried, word), terminal=True) logger.warn('%s "%s", did you mean "%s"?' % (print_message, tried, word), terminal=True)
return return
logger.error('%s "%s"' % (print_message, tried), terminal=True) if print_default: logger.error('%s "%s"' % (print_message, tried), terminal=True)

View File

@ -32,4 +32,4 @@ def reset():
if os.path.exists(plugin_dir): shutil.rmtree(plugin_dir) if os.path.exists(plugin_dir): shutil.rmtree(plugin_dir)
setup_default_plugins() setup_default_plugins()
logger.info('Default plugins have been reset.', terminal=True) logger.info('Default plugins have been reset.', terminal=True)