From c637675a6f1095736c59665fe4543370b7ec013a Mon Sep 17 00:00:00 2001 From: VAN BOSSUYT Nicolas Date: Sat, 11 Nov 2023 17:02:56 +0100 Subject: [PATCH] Moved more stuff to the cli module. --- cutekit/__init__.py | 15 +++++++++--- cutekit/cli.py | 60 +++++++++++++++++++++++++++++++++++++++++++++ cutekit/cmds.py | 55 ----------------------------------------- 3 files changed, 71 insertions(+), 59 deletions(-) diff --git a/cutekit/__init__.py b/cutekit/__init__.py index f0d4909..84df82e 100644 --- a/cutekit/__init__.py +++ b/cutekit/__init__.py @@ -2,7 +2,14 @@ import sys import os import logging -from . import const, model, vt100, plugins, cmds, cli +from . import ( + cli, + const, + model, + plugins, + vt100, + cmds, # noqa: F401 +) def setupLogger(verbose: bool): @@ -37,13 +44,13 @@ def main() -> int: a = cli.parse(sys.argv[1:]) setupLogger(a.consumeOpt("verbose", False) is True) plugins.loadAll() - cmds.exec(a) + cli.exec(a) print() return 0 except RuntimeError as e: logging.exception(e) - cmds.error(str(e)) - cmds.usage() + cli.error(str(e)) + cli.usage() print() return 1 except KeyboardInterrupt: diff --git a/cutekit/cli.py b/cutekit/cli.py index 17b6e79..94df42c 100644 --- a/cutekit/cli.py +++ b/cutekit/cli.py @@ -1,8 +1,10 @@ import inspect +import sys from typing import Optional, Union, Callable from dataclasses import dataclass +from . import const, vt100 Value = Union[str, bool, int] @@ -95,3 +97,61 @@ def command(shortName: str, longName: str, helpText: str): return fn return wrap + + +# --- Builtins Commands ------------------------------------------------------ # + + +@command("u", "usage", "Show usage information") +def usage(args: Args | None = None): + print(f"Usage: {const.ARGV0} [args...]") + + +def error(msg: str) -> None: + print(f"{vt100.RED}Error:{vt100.RESET} {msg}\n", file=sys.stderr) + + +@command("h", "help", "Show this help message") +def helpCmd(args: Args): + usage() + + print() + + vt100.title("Description") + print(f" {const.DESCRIPTION}") + + print() + vt100.title("Commands") + for cmd in commands: + pluginText = "" + if cmd.isPlugin: + pluginText = f"{vt100.CYAN}(plugin){vt100.RESET}" + + print( + f" {vt100.GREEN}{cmd.shortName or ' '}{vt100.RESET} {cmd.longName} - {cmd.helpText} {pluginText}" + ) + + print() + vt100.title("Logging") + print(" Logs are stored in:") + print(f" - {const.PROJECT_LOG_FILE}") + print(f" - {const.GLOBAL_LOG_FILE}") + + +@command("v", "version", "Show current version") +def versionCmd(args: Args): + print(f"CuteKit v{const.VERSION_STR}\n") + + +def exec(args: Args): + cmd = args.consumeArg() + + if cmd is None: + raise RuntimeError("No command specified") + + for c in commands: + if c.shortName == cmd or c.longName == cmd: + c.callback(args) + return + + raise RuntimeError(f"Unknown command {cmd}") diff --git a/cutekit/cmds.py b/cutekit/cmds.py index 1b2b857..c302492 100644 --- a/cutekit/cmds.py +++ b/cutekit/cmds.py @@ -1,6 +1,5 @@ import logging import os -import sys from . import ( @@ -118,38 +117,6 @@ def nukeCmd(args: cli.Args): shell.rmrf(const.PROJECT_CK_DIR) -@cli.command("h", "help", "Show this help message") -def helpCmd(args: cli.Args): - usage() - - print() - - vt100.title("Description") - print(f" {const.DESCRIPTION}") - - print() - vt100.title("Commands") - for cmd in cli.commands: - pluginText = "" - if cmd.isPlugin: - pluginText = f"{vt100.CYAN}(plugin){vt100.RESET}" - - print( - f" {vt100.GREEN}{cmd.shortName or ' '}{vt100.RESET} {cmd.longName} - {cmd.helpText} {pluginText}" - ) - - print() - vt100.title("Logging") - print(" Logs are stored in:") - print(f" - {const.PROJECT_LOG_FILE}") - print(f" - {const.GLOBAL_LOG_FILE}") - - -@cmd("v", "version", "Show current version") -def versionCmd(args: cli.Args): - print(f"CuteKit v{const.VERSION_STR}") - - def grabExtern(extern: dict[str, model.Extern]): for extSpec, ext in extern.items(): extPath = os.path.join(const.EXTERN_DIR, extSpec) @@ -227,25 +194,3 @@ def initCmd(args: cli.Args): print( f" {vt100.GREEN}cutekit build{vt100.BRIGHT_BLACK} # Build the project{vt100.RESET}" ) - - -def usage(): - print(f"Usage: {const.ARGV0} [args...]") - - -def error(msg: str) -> None: - print(f"{vt100.RED}Error:{vt100.RESET} {msg}\n", file=sys.stderr) - - -def exec(args: cli.Args): - cmd = args.consumeArg() - - if cmd is None: - raise RuntimeError("No command specified") - - for c in cli.commands: - if c.shortName == cmd or c.longName == cmd: - c.callback(args) - return - - raise RuntimeError(f"Unknown command {cmd}")