summaryrefslogtreecommitdiff
path: root/mp-tool
diff options
context:
space:
mode:
Diffstat (limited to 'mp-tool')
-rwxr-xr-xmp-tool135
1 files changed, 58 insertions, 77 deletions
diff --git a/mp-tool b/mp-tool
index b990e27..1419089 100755
--- a/mp-tool
+++ b/mp-tool
@@ -1,88 +1,69 @@
#!/usr/bin/env python3
-import mp_tool
-
-import argparse
-import sys
-
-
-class HelpAction(argparse._HelpAction):
- def __call__(self, parser, namespace, values, option_string=None):
- formatter = parser._get_formatter()
- formatter.add_usage(parser.usage,
- parser._actions,
- parser._mutually_exclusive_groups)
-
- formatter.start_section(parser._optionals.title)
- formatter.add_text(parser._optionals.description)
- formatter.add_arguments(parser._optionals._group_actions)
- formatter.end_section()
-
- subparsers_actions = [
- action for action in parser._actions
- if isinstance(action, argparse._SubParsersAction)]
-
- for subparsers_action in subparsers_actions:
- # get all subparsers and print help
- subparsers = subparsers_action.choices
- for subaction in subparsers_action._get_subactions():
- subparser = subparsers[subaction.dest]
- usage = formatter._format_actions_usage(subparser._actions, [])
- usage_parent = formatter._format_actions_usage(filter(
- lambda a: not (isinstance(a, HelpAction) or isinstance(a, argparse._SubParsersAction)),
- parser._actions), [])
- formatter.start_section("{} {} {} {}".format(formatter._prog,
- usage_parent,
- subaction.dest,
- usage))
- formatter.add_text(subaction.help)
- formatter.add_arguments(subparser._positionals._group_actions)
- formatter.add_arguments(filter(lambda a: not isinstance(a, argparse._HelpAction),
- subparser._optionals._group_actions))
- formatter.end_section()
-
- print(formatter.format_help())
- parser.exit(0)
+from mp_tool.util import HelpAction, format_subcommands_help, get_default_serial_port
+from argparse import ArgumentParser
+from importlib import import_module
+from sys import argv
+import traceback
if __name__ == '__main__':
- parser = argparse.ArgumentParser(add_help=False)
+ default_port = get_default_serial_port()
+
+ parser = ArgumentParser(add_help=False)
parser.add_argument("--help", action=HelpAction, help="Display full help")
- parser.add_argument("--password", action='store', nargs='?')
- parser.add_argument("WEBSOCKET", action='store', nargs=1,
- help="Websocket address (e.g. ws://ESP_E1278E:8266)")
subparsers = parser.add_subparsers()
- parser_eval = subparsers.add_parser("eval", help="Eval python code remotely")
- parser_eval.set_defaults(func=mp_tool.do_eval)
- parser_eval.add_argument("CODE", action='store', nargs=1, help="Code to execute")
-
- parser_repl = subparsers.add_parser("repl", help="Start interactive REPL")
- parser_repl.set_defaults(func=mp_tool.do_repl)
-
- parser_put = subparsers.add_parser("put", help="Send file to remote")
- parser_put.set_defaults(func=mp_tool.do_put)
-
- parser_get = subparsers.add_parser("get", help="Load file from remote")
- parser_put.set_defaults(func=mp_tool.do_get)
-
- args = parser.parse_args(sys.argv[1:])
+ parser_eval = subparsers.add_parser("eval", help="Eval python code over websocket")
+ parser_eval.set_defaults(func=lambda a: import_module('mp_tool.web').eval(a.WEBSOCKET, a.password, a.CODE))
+ parser_eval.add_argument("WEBSOCKET", help="Websocket address (e.g. ws://ESP_E1278E:8266)")
+ parser_eval.add_argument("CODE", help="Code to execute")
+ parser_eval.add_argument("--password")
+
+ parser_eval_serial = subparsers.add_parser("eval-serial", help="Eval python code over serial")
+ parser_eval_serial.set_defaults(func=lambda a: import_module('mp_tool.serial').eval(a.port, a.CODE))
+ parser_eval_serial.add_argument("--port", default=default_port, help="Serial port, default " + default_port)
+ parser_eval_serial.add_argument("CODE", help="Code to execute")
+
+ parser_repl = subparsers.add_parser("repl", help="Interactive REPL over websocket")
+ parser_repl.set_defaults(func=lambda a: import_module('mp_tool.web').repl(a.WEBSOCKET, a.password))
+ parser_repl.add_argument("WEBSOCKET", help="Websocket address (e.g. ws://ESP_E1278E:8266)")
+ parser_repl.add_argument("--password")
+
+ parser_put = subparsers.add_parser("put", help="Send file over websocket")
+ parser_put.set_defaults(func=lambda a: import_module('mp_tool.web').put(a.WEBSOCKET, a.password, a.CODE))
+ parser_put.add_argument("WEBSOCKET", help="Websocket address (e.g. ws://ESP_E1278E:8266)")
+ parser_put.add_argument("--password")
+
+ parser_put_serial = subparsers.add_parser("put-serial", help="Send file over serial")
+ parser_put_serial.set_defaults(func=lambda a: import_module('mp_tool.serial').put(a.port, a.FILE, a.TARGET))
+ parser_put_serial.add_argument("--port", default=default_port, help="Serial port, default " + default_port)
+ parser_put_serial.add_argument("FILE", help="Filename")
+ parser_put_serial.add_argument("TARGET", nargs='?', help="remote target path/filename")
+
+ parser_get = subparsers.add_parser("get", help="Load file over websocket")
+ parser_get.set_defaults(func=lambda a: import_module('mp_tool.web').get(a.WEBSOCKET, a.password))
+ parser_get.add_argument("WEBSOCKET", help="Websocket address (e.g. ws://ESP_E1278E:8266)")
+ parser_get.add_argument("--password")
+
+ parser_get_serial = subparsers.add_parser("get-serial", help="Get file over serial")
+ parser_get_serial.set_defaults(func=lambda a: import_module('mp_tool.serial').get(a.port, a.FILE, a.TARGET))
+ parser_get_serial.add_argument("--port", default=default_port, help="Serial port, default " + default_port)
+ parser_get_serial.add_argument("FILE", help="Filename")
+ parser_get_serial.add_argument("TARGET", nargs='?', help="local target path/filename")
+
+ parser_ls_serial = subparsers.add_parser("ls-serial", help="List files over serial")
+ parser_ls_serial.set_defaults(func=lambda a: import_module('mp_tool.serial').ls(a.port, a.DIR))
+ parser_ls_serial.add_argument("--port", default=default_port, help="Serial port, default " + default_port)
+ parser_ls_serial.add_argument("DIR", default=".", nargs='?', help="List files in this directory (Default '.')")
+
+ args = parser.parse_args(argv[1:])
if 'func' in args:
- args.func(args)
+ try:
+ args.func(args)
+ except Exception as e:
+ print("Call failed with arguments: " + str(args))
+ parser.exit(1, traceback.format_exc())
else:
- subparsers_actions = [
- action for action in parser._actions
- if isinstance(action, argparse._SubParsersAction)]
-
- formatter = parser._get_formatter()
- formatter.add_usage(parser.usage,
- parser._actions,
- parser._mutually_exclusive_groups)
-
- formatter.start_section("Choose subcommand")
- for subparsers_action in subparsers_actions:
- formatter.add_argument(subparsers_action)
- formatter.end_section()
-
- print(formatter.format_help())
+ print(format_subcommands_help(parser))
parser.exit(0)