From 94d6f8f933cec87d80494b97962c575bd6542eb4 Mon Sep 17 00:00:00 2001 From: TheOnePath Date: Wed, 5 Jul 2023 20:24:27 +0100 Subject: [PATCH] Updated soypakcli.py soypakcli is responsible for registering all commands, parsing and invoking the command to run. - global optional flags are explicitly check first, regardless of the number of args. - if there is no command, the program displays the help message and dies. - fetches the command class for the identified soypak command and initialised the class command. - SoypakCLI@run_command: invokes the `run()` method of a command class --- soypak/cli/soypakcli.py | 41 +++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/soypak/cli/soypakcli.py b/soypak/cli/soypakcli.py index f6bfd2c..8c8bdcc 100644 --- a/soypak/cli/soypakcli.py +++ b/soypak/cli/soypakcli.py @@ -1,24 +1,41 @@ import sys + +import soypak.cli.command as command +# import all the commands to auto-register +from soypak.cli.commands import ( + help +) + import soypak.cli.parser as parser -# :@TODO: remove this and implement the following: -# - a class which will auto-add commands to the CLI which can be ran (eopkg autocommand). -# - this will inherit `type` and be inherited as a metaclass `class ...(metaclass=...)` - class SoypakCLI: def __init__(self) -> None: + # set up the parser and parse the args _parser = parser.SoypakParser() cmd, args = _parser.parse_args() - if len(args) == 0: - if _parser.namespace.version: - print(_parser.VERSION) - sys.exit(0) + # check for global optional argument flags + if _parser.namespace.version: + print(_parser.VERSION) + sys.exit(0) - print(cmd, args) + # if we haven't been provided a command + if cmd is None: + _parser.print_help() + self.die() + + # fetch the command and pass arguments for initialisation + self.command = command.Command.get_command(cmd, args=args) + if not self.command: + print("Unrecognised command: %s" % cmd) + self.die() - def run(self): - # :@TODO: run commands - ... + def die(self): + sys.exit(1) + + + def run_command(self): + """Run the soypak command with given arguments""" + self.command.run()