From 82c375a9bdddeddcf7c35d36a6d69e764890e894 Mon Sep 17 00:00:00 2001 From: Jac Fitzgerald Date: Thu, 13 Jun 2024 23:44:35 -0700 Subject: [PATCH 1/3] add space to --version output --- tabcmd/execution/parent_parser.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tabcmd/execution/parent_parser.py b/tabcmd/execution/parent_parser.py index 41362bed..df9b3888 100644 --- a/tabcmd/execution/parent_parser.py +++ b/tabcmd/execution/parent_parser.py @@ -131,7 +131,7 @@ def parent_parser_with_global_options(): "-v", "--version", action="version", - version=strings[6] + "v" + version + "\n \n", + version=strings[6] + " v" + version + "\n \n", help=strings[7], ) From e7f4993ccded39581973cf1b6cc82acb52b67062 Mon Sep 17 00:00:00 2001 From: Jac Fitzgerald Date: Thu, 13 Jun 2024 23:37:19 -0700 Subject: [PATCH 2/3] if no command is given, print help info --- tabcmd/execution/parent_parser.py | 1 + tabcmd/execution/tabcmd_controller.py | 16 ++++++++-------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/tabcmd/execution/parent_parser.py b/tabcmd/execution/parent_parser.py index df9b3888..15216631 100644 --- a/tabcmd/execution/parent_parser.py +++ b/tabcmd/execution/parent_parser.py @@ -182,6 +182,7 @@ def include(self, command): command.define_args(additional_parser) return additional_parser + # Help isn't added like the others because it has to have access to the rest, to get their args def include_help(self): additional_parser = self.subparsers.add_parser("help", help=strings[14], parents=[self.global_options]) additional_parser._optionals.title = strings[1] diff --git a/tabcmd/execution/tabcmd_controller.py b/tabcmd/execution/tabcmd_controller.py index 2fbf270d..5487e536 100644 --- a/tabcmd/execution/tabcmd_controller.py +++ b/tabcmd/execution/tabcmd_controller.py @@ -26,32 +26,32 @@ def run(parser, user_input=None): sys.exit(0) user_input = user_input or sys.argv[1:] namespace = parser.parse_args(user_input) + # if no subcommand was given, call help + if not hasattr(namespace, "func"): + print("No command found.") + parser.print_help() + sys.exit(0) + if hasattr("namespace", "logging_level") and namespace.logging_level != logging.INFO: print("logging:", namespace.logging_level) logger = log(__name__, namespace.logging_level or logging.INFO) logger.info("Tabcmd {}".format(version)) - if (hasattr("namespace", "password") or hasattr("namespace", "token_value")) and hasattr("namespace", "func"): + if hasattr(namespace, "password") or hasattr(namespace, "token_value"): # don't print whole namespace because it has secrets logger.debug(namespace.func) else: logger.debug(namespace) - if namespace.language: + if hasattr(namespace, "language"): set_client_locale(namespace.language, logger) if namespace.query_page_size: os.environ["TSC_PAGE_SIZE"] = str(namespace.query_page_size) try: - func = namespace.func - # if a subcommand was identified, call the function assigned to it - # this is the functional equivalent of the call by reflection in the previous structure # https://stackoverflow.com/questions/49038616/argparse-subparsers-with-functions namespace.func.run_command(namespace) - except AttributeError: - parser.error("No command identified or too few arguments") except Exception as e: # todo: use log_stack here for better presentation? logger.exception(e) - # if no command was given, argparse will just not create the attribute sys.exit(2) return namespace From f2a7b6dfdd1e324d07bbf06e94a8759dbab91277 Mon Sep 17 00:00:00 2001 From: Jac Fitzgerald Date: Thu, 13 Jun 2024 23:37:19 -0700 Subject: [PATCH 3/3] if no command is given, print help info --- tabcmd/execution/tabcmd_controller.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tabcmd/execution/tabcmd_controller.py b/tabcmd/execution/tabcmd_controller.py index 5487e536..684d0495 100644 --- a/tabcmd/execution/tabcmd_controller.py +++ b/tabcmd/execution/tabcmd_controller.py @@ -46,6 +46,7 @@ def run(parser, user_input=None): set_client_locale(namespace.language, logger) if namespace.query_page_size: os.environ["TSC_PAGE_SIZE"] = str(namespace.query_page_size) + try: # https://stackoverflow.com/questions/49038616/argparse-subparsers-with-functions namespace.func.run_command(namespace)