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:
logger.warn('That block is already blacklisted', terminal=True)
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
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:

View File

@ -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
raise onionrexceptions.NotFound

View File

@ -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)
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)
setup_default_plugins()
logger.info('Default plugins have been reset.', terminal=True)
logger.info('Default plugins have been reset.', terminal=True)