Cleanup cli api.
This commit is contained in:
		
							parent
							
								
									f783f5607c
								
							
						
					
					
						commit
						c6b18e83a6
					
				
					 2 changed files with 27 additions and 17 deletions
				
			
		|  | @ -1,16 +1,13 @@ | ||||||
| import sys |  | ||||||
| import os | import os | ||||||
| import logging | import logging | ||||||
| 
 | 
 | ||||||
| from pathlib import Path |  | ||||||
| 
 |  | ||||||
| from . import ( | from . import ( | ||||||
|     builder, |     builder,  # noqa: F401 this is imported for side effects | ||||||
|     cli,  # noqa: F401 this is imported for side effects |     cli, | ||||||
|     const, |     const, | ||||||
|     model, |     model, | ||||||
|     plugins, |     plugins, | ||||||
|     pods,  # noqa: F401 this is imported for side effects |     pods, | ||||||
|     shell, |     shell, | ||||||
|     vt100, |     vt100, | ||||||
| ) | ) | ||||||
|  | @ -68,6 +65,7 @@ class RootArgs( | ||||||
| 
 | 
 | ||||||
| @cli.command(None, "/", const.DESCRIPTION) | @cli.command(None, "/", const.DESCRIPTION) | ||||||
| def _(args: RootArgs): | def _(args: RootArgs): | ||||||
|  |     shell.mkdir(const.GLOBAL_CK_DIR) | ||||||
|     const.setup() |     const.setup() | ||||||
|     logger.setup(args) |     logger.setup(args) | ||||||
|     plugins.setup(args) |     plugins.setup(args) | ||||||
|  | @ -76,7 +74,7 @@ def _(args: RootArgs): | ||||||
| 
 | 
 | ||||||
| @cli.command("u", "usage", "Show usage information") | @cli.command("u", "usage", "Show usage information") | ||||||
| def _(): | def _(): | ||||||
|     print(f"Usage: {const.ARGV0} {cli._root.usage()}") |     cli.usage() | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| @cli.command("v", "version", "Show current version") | @cli.command("v", "version", "Show current version") | ||||||
|  | @ -86,15 +84,13 @@ def _(): | ||||||
| 
 | 
 | ||||||
| def main() -> int: | def main() -> int: | ||||||
|     try: |     try: | ||||||
|         shell.mkdir(const.GLOBAL_CK_DIR) |         cli.exec() | ||||||
|         extra = os.environ.get("CK_EXTRA_ARGS", None) |  | ||||||
|         args = [const.ARGV0] + (extra.split(" ") if extra else []) + sys.argv[1:] |  | ||||||
|         cli._root.eval(args) |  | ||||||
|         return 0 |         return 0 | ||||||
| 
 | 
 | ||||||
|     except RuntimeError as e: |     except RuntimeError as e: | ||||||
|         logging.exception(e) |         logging.exception(e) | ||||||
|         vt100.error(str(e)) |         vt100.error(str(e)) | ||||||
|  |         cli.usage() | ||||||
|         return 1 |         return 1 | ||||||
| 
 | 
 | ||||||
|     except KeyboardInterrupt: |     except KeyboardInterrupt: | ||||||
|  |  | ||||||
|  | @ -1,14 +1,13 @@ | ||||||
| from enum import Enum | from enum import Enum | ||||||
|  | import os | ||||||
|  | import sys | ||||||
| from types import GenericAlias | from types import GenericAlias | ||||||
| import typing as tp | import typing as tp | ||||||
| import dataclasses as dt | import dataclasses as dt | ||||||
| import logging | import logging | ||||||
| 
 | 
 | ||||||
| from typing import Any, Callable, Optional | from typing import Any, Callable, Optional | ||||||
| from cutekit import vt100, const | from cutekit import vt100, const, utils | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| T = tp.TypeVar("T") |  | ||||||
| 
 | 
 | ||||||
| _logger = logging.getLogger(__name__) | _logger = logging.getLogger(__name__) | ||||||
| 
 | 
 | ||||||
|  | @ -647,7 +646,7 @@ class Command: | ||||||
|         curr, rest = self._spliceArgs(args) |         curr, rest = self._spliceArgs(args) | ||||||
| 
 | 
 | ||||||
|         if "-h" in curr or "--help" in curr: |         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 |                 # HACK: This is a special case for the root command | ||||||
|                 #       it need to always be run because it might |                 #       it need to always be run because it might | ||||||
|                 #       load some plugins that will register subcommands |                 #       load some plugins that will register subcommands | ||||||
|  | @ -657,7 +656,7 @@ class Command: | ||||||
|             return |             return | ||||||
| 
 | 
 | ||||||
|         if "-u" in curr or "--usage" in curr: |         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 |                 # HACK: Same as the help flag, the root command needs to be | ||||||
|                 #       always run to load plugins |                 #       always run to load plugins | ||||||
|                 self.invoke([]) |                 self.invoke([]) | ||||||
|  | @ -724,3 +723,18 @@ def command(shortName: Optional[str], longName: str, description: str = "") -> C | ||||||
|         return fn |         return fn | ||||||
| 
 | 
 | ||||||
|     return wrap |     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()) | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		
		Reference in a new issue