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
This commit is contained in:
Ethan Smith-Coss 2023-07-05 20:24:27 +01:00
parent 6f8ef85d85
commit 94d6f8f933
Signed by: TheOnePath
GPG Key ID: 4E7D436CE1A0BAF1

View File

@ -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()