readded help support including for plugins

This commit is contained in:
Kevin Froman 2019-08-29 18:26:43 -05:00
parent 604ce5f012
commit e02e7dba87
2 changed files with 42 additions and 19 deletions

View File

@ -24,17 +24,28 @@ import onionrplugins
import onionrpluginapi
from . import arguments, recommend
plugin_command = lambda cmd: 'on_%s_cmd' % (cmd,)
def register_plugin_commands(cmd):
cmd = 'on_%s_cmd' % (cmd,)
plugin_cmd = plugin_command(cmd)
for pl in onionrplugins.get_enabled_plugins():
pl = onionrplugins.get_plugin(pl)
if hasattr(pl, cmd):
getattr(pl, cmd)(onionrpluginapi.PluginAPI)
if hasattr(pl, plugin_cmd):
getattr(pl, plugin_cmd)(onionrpluginapi.PluginAPI)
return True
def register():
PROGRAM_NAME = "onionr"
def get_help_message(cmd: str, default: str = 'No help available for this command'):
pl_cmd = plugin_command(cmd)
for pl in onionrplugins.get_enabled_plugins():
pl = onionrplugins.get_plugin(pl)
if hasattr(pl, pl_cmd):
try:
return getattr(pl, pl_cmd).onionr_help
except AttributeError:
pass
for i in arguments.get_arguments():
for alias in i:
try:
@ -43,17 +54,40 @@ def register():
pass
return default
PROGRAM_NAME = "onionr"
try:
cmd = sys.argv[1]
except IndexError:
cmd = ""
if cmd.replace('--', '').lower() == 'help':
is_help_cmd = False
if cmd.replace('--', '').lower() == 'help': is_help_cmd = True
try:
arguments.get_func(cmd)()
except onionrexceptions.NotFound:
if not register_plugin_commands(cmd) and not is_help_cmd:
recommend.recommend()
sys.exit(3)
if is_help_cmd:
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)
for pl in onionrplugins.get_enabled_plugins():
pl = onionrplugins.get_plugin(pl)
if hasattr(pl, 'ONIONR_COMMANDS'):
print('')
try:
logger.info('%s commands:' % (pl.plugin_name,), terminal=True)
except AttributeError:
logger.info('%s commands:' % (pl.__name__,), terminal=True)
for plugin_cmd in pl.ONIONR_COMMANDS:
logger.info('%s %s: %s' % (PROGRAM_NAME, plugin_cmd, get_help_message(plugin_cmd)), terminal=True)
print('')
else:
try:
logger.info('%s %s: %s' % (PROGRAM_NAME, sys.argv[2], get_help_message(sys.argv[2])), terminal=True)
@ -62,9 +96,3 @@ def register():
sys.exit(3)
return
try:
arguments.get_func(cmd)()
except onionrexceptions.NotFound:
if not register_plugin_commands(cmd):
recommend.recommend()
sys.exit(3)

View File

@ -113,11 +113,6 @@ def on_decrypt_cmd(api, data=None):
def on_encrypt_cmd(api, data=None):
PlainEncryption(api).encrypt()
def on_init(api, data = None):
'''
This event is called after Onionr is initialized, but before the command
inputted is executed. Could be called when daemon is starting or when
just the client is running.
'''
pluginapi = api
encrypt = PlainEncryption(pluginapi)
on_encrypt_cmd.onionr_help = """encrypt <user_key>\nEncrypt text data to an Onionr user key. Similar to PGP"""
on_decrypt_cmd.onionr_help = """decrypt\nDecrypt text data with your Onionr key. Similar to PGP"""
ONIONR_COMMANDS = ['encrypt', 'decrypt']