Onionr/src/onionrcommands/parser/arguments.py

72 lines
2.3 KiB
Python
Raw Normal View History

"""Onionr - Private P2P Communication.
2019-08-29 09:25:21 +00:00
Sets CLI arguments for Onionr
2019-12-19 10:32:40 +00:00
"""
from typing import Callable
2022-01-12 00:13:19 +00:00
from .. import onionrstatistics, version, daemonlaunch
2019-12-19 10:32:40 +00:00
from .. import pubkeymanager # commands to add or change id
from .. import resetplugins # command to reinstall default plugins
2022-01-31 01:39:24 +00:00
2019-12-19 10:32:40 +00:00
import onionrexceptions
"""
2022-01-12 00:13:19 +00:00
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
2019-08-29 09:25:21 +00:00
2022-01-12 00:13:19 +00:00
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
2019-08-29 09:25:21 +00:00
2022-01-12 00:13:19 +00:00
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
2019-12-19 10:32:40 +00:00
"""
2019-12-19 10:32:40 +00:00
def get_arguments() -> dict:
"""Return command argument dict, minus plugin cmds.
This is a function because we need to be able to
dynamically modify them with plugins
"""
2019-07-31 05:10:28 +00:00
args = {
('details', 'info'): onionrstatistics.show_details,
2019-08-05 23:09:04 +00:00
('stats', 'statistics'): onionrstatistics.show_stats,
2019-08-29 22:17:47 +00:00
('version',): version.version,
2019-08-04 04:52:57 +00:00
('start', 'daemon'): daemonlaunch.start,
('stop', 'kill'): daemonlaunch.kill_daemon,
2019-08-05 23:09:04 +00:00
('addid', 'add-id'): pubkeymanager.add_ID,
('changeid', 'change-id'): pubkeymanager.change_ID,
('resetplugins', 'reset-plugins'): resetplugins.reset
2019-07-31 05:10:28 +00:00
}
return args
2019-12-19 10:32:40 +00:00
2019-08-29 22:17:47 +00:00
def get_help(arg: str) -> str:
"""Return the help info string from a given command."""
2019-08-29 22:17:47 +00:00
arguments = get_arguments()
# Iterate the command alias tuples
for argument in arguments:
# Return the help message if its found in a command alias tuple
2019-12-19 10:32:40 +00:00
if arg in argument:
return arguments[argument].onionr_help
2019-08-29 22:17:47 +00:00
raise KeyError
2019-07-31 05:10:28 +00:00
2019-12-19 10:32:40 +00:00
2019-09-14 06:29:31 +00:00
def get_func(argument: str) -> Callable:
"""Return the function for a given command argument."""
2019-07-31 05:10:28 +00:00
argument = argument.lower()
args = get_arguments()
2019-12-19 10:32:40 +00:00
for arg in args.keys(): # Iterate command alias sets
"""
If our argument is in the current alias set,
return the command function
"""
if argument in arg:
2019-07-31 05:10:28 +00:00
return args[arg]
2019-08-29 22:17:47 +00:00
raise onionrexceptions.NotFound