Moved more stuff to the cli module.

This commit is contained in:
Sleepy Monax 2023-11-11 17:02:56 +01:00
parent 5f7a12e2e2
commit c637675a6f
3 changed files with 71 additions and 59 deletions

View file

@ -2,7 +2,14 @@ import sys
import os import os
import logging import logging
from . import const, model, vt100, plugins, cmds, cli from . import (
cli,
const,
model,
plugins,
vt100,
cmds, # noqa: F401
)
def setupLogger(verbose: bool): def setupLogger(verbose: bool):
@ -37,13 +44,13 @@ def main() -> int:
a = cli.parse(sys.argv[1:]) a = cli.parse(sys.argv[1:])
setupLogger(a.consumeOpt("verbose", False) is True) setupLogger(a.consumeOpt("verbose", False) is True)
plugins.loadAll() plugins.loadAll()
cmds.exec(a) cli.exec(a)
print() print()
return 0 return 0
except RuntimeError as e: except RuntimeError as e:
logging.exception(e) logging.exception(e)
cmds.error(str(e)) cli.error(str(e))
cmds.usage() cli.usage()
print() print()
return 1 return 1
except KeyboardInterrupt: except KeyboardInterrupt:

View file

@ -1,8 +1,10 @@
import inspect import inspect
import sys
from typing import Optional, Union, Callable from typing import Optional, Union, Callable
from dataclasses import dataclass from dataclasses import dataclass
from . import const, vt100
Value = Union[str, bool, int] Value = Union[str, bool, int]
@ -95,3 +97,61 @@ def command(shortName: str, longName: str, helpText: str):
return fn return fn
return wrap return wrap
# --- Builtins Commands ------------------------------------------------------ #
@command("u", "usage", "Show usage information")
def usage(args: Args | None = None):
print(f"Usage: {const.ARGV0} <command> [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}")

View file

@ -1,6 +1,5 @@
import logging import logging
import os import os
import sys
from . import ( from . import (
@ -118,38 +117,6 @@ def nukeCmd(args: cli.Args):
shell.rmrf(const.PROJECT_CK_DIR) 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]): def grabExtern(extern: dict[str, model.Extern]):
for extSpec, ext in extern.items(): for extSpec, ext in extern.items():
extPath = os.path.join(const.EXTERN_DIR, extSpec) extPath = os.path.join(const.EXTERN_DIR, extSpec)
@ -227,25 +194,3 @@ def initCmd(args: cli.Args):
print( print(
f" {vt100.GREEN}cutekit build{vt100.BRIGHT_BLACK} # Build the project{vt100.RESET}" f" {vt100.GREEN}cutekit build{vt100.BRIGHT_BLACK} # Build the project{vt100.RESET}"
) )
def usage():
print(f"Usage: {const.ARGV0} <command> [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}")