From ba2960846e52e9862a7d0aca69c33d29610276bb Mon Sep 17 00:00:00 2001 From: Yves Fischer Date: Sun, 25 Dec 2016 21:46:49 +0100 Subject: first version without put/get --- mp-tool | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100755 mp-tool (limited to 'mp-tool') diff --git a/mp-tool b/mp-tool new file mode 100755 index 0000000..b990e27 --- /dev/null +++ b/mp-tool @@ -0,0 +1,88 @@ +#!/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) + + +if __name__ == '__main__': + parser = argparse.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:]) + if 'func' in args: + args.func(args) + 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()) + parser.exit(0) -- cgit v1.2.1