From 696c62292b06bb3ceafb150d70057b7b5e31a5c0 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Mon, 12 Apr 2021 19:43:55 +0200 Subject: [PATCH] Added logic to cli to use --help option for help command --- src/cpl_cli/cli.py | 19 ++++++++++++++++--- src/cpl_cli/startup.py | 3 +++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/cpl_cli/cli.py b/src/cpl_cli/cli.py index 145abc12..28f06cf4 100644 --- a/src/cpl_cli/cli.py +++ b/src/cpl_cli/cli.py @@ -30,6 +30,7 @@ class CLI(ApplicationABC): ApplicationABC.__init__(self, config, services) self._command_handler: Optional[CommandHandler] = None + self._options: list[str] = [] def configure(self): self._command_handler: CommandHandler = self._services.get_service(CommandHandler) @@ -47,6 +48,9 @@ class CLI(ApplicationABC): self._command_handler.add_command(CommandModel('update', ['u', 'U'], UpdateService, False, True, True)) self._command_handler.add_command(CommandModel('version', ['v', 'V'], VersionService, False, False, False)) + self._command_handler.add_command(CommandModel('--help', ['-h', '-H'], HelpService, False, False, False)) + self._options.append('--help') + def main(self): """ Entry point of the CPL CLI @@ -56,9 +60,18 @@ class CLI(ApplicationABC): command = None args = [] if len(self._configuration.additional_arguments) > 0: - command = self._configuration.additional_arguments[0] - if len(self._configuration.additional_arguments) > 1: - args = self._configuration.additional_arguments[1:] + is_option = False + for opt in self._options: + if opt in self._configuration.additional_arguments: + is_option = True + command = opt + args = self._configuration.additional_arguments + args.remove(opt) + + if not is_option: + command = self._configuration.additional_arguments[0] + if len(self._configuration.additional_arguments) > 1: + args = self._configuration.additional_arguments[1:] else: for cmd in self._command_handler.commands: result = self._configuration.get_configuration(cmd.name) diff --git a/src/cpl_cli/startup.py b/src/cpl_cli/startup.py index f380b050..3e0cf9c9 100644 --- a/src/cpl_cli/startup.py +++ b/src/cpl_cli/startup.py @@ -69,6 +69,9 @@ class Startup(StartupABC): self._configuration.add_console_argument(ConsoleArgument('', 'uninstall', ['ui', 'UI'], ' ')) self._configuration.add_console_argument(ConsoleArgument('', 'update', ['u', 'U'], '')) self._configuration.add_console_argument(ConsoleArgument('', 'version', ['v', 'V'], '')) + + self._configuration.add_console_argument(ConsoleArgument('', '--help', ['-h', '-H'], '')) + self._configuration.add_console_arguments(error=False) return self._configuration