From c6b18e83a6654c49b6ecf48c5b78a2fdb1f0e350 Mon Sep 17 00:00:00 2001 From: VAN BOSSUYT Nicolas Date: Tue, 20 Feb 2024 07:54:22 +0100 Subject: [PATCH] Cleanup cli api. --- cutekit/__init__.py | 18 +++++++----------- cutekit/cli.py | 26 ++++++++++++++++++++------ 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/cutekit/__init__.py b/cutekit/__init__.py index 16fc89b..9cd817f 100644 --- a/cutekit/__init__.py +++ b/cutekit/__init__.py @@ -1,16 +1,13 @@ -import sys import os import logging -from pathlib import Path - from . import ( - builder, - cli, # noqa: F401 this is imported for side effects + builder, # noqa: F401 this is imported for side effects + cli, const, model, plugins, - pods, # noqa: F401 this is imported for side effects + pods, shell, vt100, ) @@ -68,6 +65,7 @@ class RootArgs( @cli.command(None, "/", const.DESCRIPTION) def _(args: RootArgs): + shell.mkdir(const.GLOBAL_CK_DIR) const.setup() logger.setup(args) plugins.setup(args) @@ -76,7 +74,7 @@ def _(args: RootArgs): @cli.command("u", "usage", "Show usage information") def _(): - print(f"Usage: {const.ARGV0} {cli._root.usage()}") + cli.usage() @cli.command("v", "version", "Show current version") @@ -86,15 +84,13 @@ def _(): def main() -> int: try: - shell.mkdir(const.GLOBAL_CK_DIR) - extra = os.environ.get("CK_EXTRA_ARGS", None) - args = [const.ARGV0] + (extra.split(" ") if extra else []) + sys.argv[1:] - cli._root.eval(args) + cli.exec() return 0 except RuntimeError as e: logging.exception(e) vt100.error(str(e)) + cli.usage() return 1 except KeyboardInterrupt: diff --git a/cutekit/cli.py b/cutekit/cli.py index 72ec95e..6492ef5 100644 --- a/cutekit/cli.py +++ b/cutekit/cli.py @@ -1,14 +1,13 @@ from enum import Enum +import os +import sys from types import GenericAlias import typing as tp import dataclasses as dt import logging from typing import Any, Callable, Optional -from cutekit import vt100, const - - -T = tp.TypeVar("T") +from cutekit import vt100, const, utils _logger = logging.getLogger(__name__) @@ -647,7 +646,7 @@ class Command: curr, rest = self._spliceArgs(args) if "-h" in curr or "--help" in curr: - if len(self.path) == 0: + if len(self.path) == 1: # HACK: This is a special case for the root command # it need to always be run because it might # load some plugins that will register subcommands @@ -657,7 +656,7 @@ class Command: return if "-u" in curr or "--usage" in curr: - if len(self.path) == 0: + if len(self.path) == 1: # HACK: Same as the help flag, the root command needs to be # always run to load plugins self.invoke([]) @@ -724,3 +723,18 @@ def command(shortName: Optional[str], longName: str, description: str = "") -> C return fn return wrap + + +def usage(): + print(f"Usage: {const.ARGV0} {_root.usage()}") + + +def exec(): + extra = os.environ.get("CK_EXTRA_ARGS", None) + args = [const.ARGV0] + (extra.split(" ") if extra else []) + sys.argv[1:] + _root.eval(args) + + +def defaults(typ: type[utils.T]) -> utils.T: + schema = Schema.extract(typ) + return tp.cast(utils.T, schema._instanciate())