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:
parent
6f8ef85d85
commit
94d6f8f933
|
|
@ -1,24 +1,41 @@
|
||||||
import sys
|
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
|
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:
|
class SoypakCLI:
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
|
# set up the parser and parse the args
|
||||||
_parser = parser.SoypakParser()
|
_parser = parser.SoypakParser()
|
||||||
cmd, args = _parser.parse_args()
|
cmd, args = _parser.parse_args()
|
||||||
|
|
||||||
if len(args) == 0:
|
# check for global optional argument flags
|
||||||
if _parser.namespace.version:
|
if _parser.namespace.version:
|
||||||
print(_parser.VERSION)
|
print(_parser.VERSION)
|
||||||
sys.exit(0)
|
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):
|
def die(self):
|
||||||
# :@TODO: run commands
|
sys.exit(1)
|
||||||
...
|
|
||||||
|
|
||||||
|
def run_command(self):
|
||||||
|
"""Run the soypak command with given arguments"""
|
||||||
|
self.command.run()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user