cmds.CmdPkg: Rename pkg -> packages #33
4 changed files with 41 additions and 8 deletions
|
|
@ -6,12 +6,19 @@ from ..App import App as Parent
|
||||||
from ..CmdBase import CmdBase as Base
|
from ..CmdBase import CmdBase as Base
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
|
from typing import Iterable
|
||||||
from ..lib.Distro import Distro
|
from ..lib.Distro import Distro
|
||||||
|
|
||||||
class Cmd(Base): # export
|
class Cmd(Base): # export
|
||||||
|
|
||||||
def __init__(self, parent: Parent, name: str, help: str) -> None:
|
def __init__(
|
||||||
super().__init__(parent, name, help)
|
self,
|
||||||
|
parent: Parent,
|
||||||
|
name: str,
|
||||||
|
help: str,
|
||||||
|
aliases: Iterable[str] | None = None
|
||||||
|
) -> None:
|
||||||
|
super().__init__(parent, name, help, aliases = aliases)
|
||||||
|
|
||||||
async def _run(self, args):
|
async def _run(self, args):
|
||||||
# Missing subcommand
|
# Missing subcommand
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,12 @@ if TYPE_CHECKING:
|
||||||
class CmdPkg(Cmd): # export
|
class CmdPkg(Cmd): # export
|
||||||
|
|
||||||
def __init__(self, parent: Parent) -> None:
|
def __init__(self, parent: Parent) -> None:
|
||||||
super().__init__(parent, 'pkg', help = 'System package manager wrapper')
|
super().__init__(
|
||||||
|
parent,
|
||||||
|
'packages',
|
||||||
|
aliases = ['pkg'],
|
||||||
|
help = 'System package manager wrapper'
|
||||||
|
)
|
||||||
self.load_subcommands()
|
self.load_subcommands()
|
||||||
|
|
||||||
async def _run(self, args):
|
async def _run(self, args):
|
||||||
|
|
|
||||||
|
|
@ -56,6 +56,7 @@ class App: # export
|
||||||
cmd.name,
|
cmd.name,
|
||||||
help = cmd.help,
|
help = cmd.help,
|
||||||
description = cmd.description,
|
description = cmd.description,
|
||||||
|
aliases = cmd.aliases,
|
||||||
formatter_class = ArgumentDefaultsHelpFormatter,
|
formatter_class = ArgumentDefaultsHelpFormatter,
|
||||||
)
|
)
|
||||||
parser.set_defaults(func = cmd.run)
|
parser.set_defaults(func = cmd.run)
|
||||||
|
|
@ -88,13 +89,21 @@ class App: # export
|
||||||
for cmd in cmds:
|
for cmd in cmds:
|
||||||
cmd.set_parent(parent)
|
cmd.set_parent(parent)
|
||||||
scs[cmd.name] = SubCommand(cmd, add_cmd_to_parser(cmd, subparsers))
|
scs[cmd.name] = SubCommand(cmd, add_cmd_to_parser(cmd, subparsers))
|
||||||
|
for alias in cmd.aliases:
|
||||||
|
scs[alias] = scs[cmd.name]
|
||||||
if all:
|
if all:
|
||||||
|
seen: set[int] = set()
|
||||||
for sc in scs.values():
|
for sc in scs.values():
|
||||||
add_cmds_to_parser(sc.cmd, sc.parser, sc.cmd.children, all = all)
|
if id(sc) not in seen:
|
||||||
|
seen.add(id(sc))
|
||||||
|
add_cmds_to_parser(
|
||||||
|
sc.cmd, sc.parser, sc.cmd.children, all = all
|
||||||
|
)
|
||||||
return
|
return
|
||||||
args, unknown = self.__parser.parse_known_args()
|
args, unknown = self.__parser.parse_known_args()
|
||||||
if args.command in scs:
|
cmd_name = getattr(args, 'command', None)
|
||||||
sc = scs[args.command]
|
if cmd_name in scs:
|
||||||
|
sc = scs[cmd_name]
|
||||||
add_cmds_to_parser(sc.cmd, sc.parser, sc.cmd.children, all = all)
|
add_cmds_to_parser(sc.cmd, sc.parser, sc.cmd.children, all = all)
|
||||||
|
|
||||||
from .Cmd import AbstractCmd
|
from .Cmd import AbstractCmd
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ from .Types import LoadTypes, Types
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from argparse import ArgumentParser
|
from argparse import ArgumentParser
|
||||||
from typing import Any
|
from typing import Any, Iterable
|
||||||
|
|
||||||
from .App import App
|
from .App import App
|
||||||
|
|
||||||
|
|
@ -150,6 +150,13 @@ class AbstractCmd(abc.ABC):
|
||||||
def description(self) -> str:
|
def description(self) -> str:
|
||||||
return self._description()
|
return self._description()
|
||||||
|
|
||||||
|
def _aliases(self) -> Iterable[str]:
|
||||||
|
return []
|
||||||
|
|
||||||
|
@property
|
||||||
|
def aliases(self) -> Iterable[str]:
|
||||||
|
return self._aliases()
|
||||||
|
|
||||||
class Cmd(AbstractCmd): # export
|
class Cmd(AbstractCmd): # export
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
|
|
@ -157,12 +164,14 @@ class Cmd(AbstractCmd): # export
|
||||||
parent: App | Cmd,
|
parent: App | Cmd,
|
||||||
name: str,
|
name: str,
|
||||||
help: str,
|
help: str,
|
||||||
description: str | None = None
|
description: str | None = None,
|
||||||
|
aliases: Iterable[str] | None = None
|
||||||
) -> None:
|
) -> None:
|
||||||
super().__init__(parent)
|
super().__init__(parent)
|
||||||
self.__name = name
|
self.__name = name
|
||||||
self.__help = help
|
self.__help = help
|
||||||
self.__description = description if description else help
|
self.__description = description if description else help
|
||||||
|
self.__aliases = aliases if aliases else []
|
||||||
|
|
||||||
def _name(self) -> str:
|
def _name(self) -> str:
|
||||||
return self.__name
|
return self.__name
|
||||||
|
|
@ -172,3 +181,6 @@ class Cmd(AbstractCmd): # export
|
||||||
|
|
||||||
def _description(self) -> str:
|
def _description(self) -> str:
|
||||||
return self.__description
|
return self.__description
|
||||||
|
|
||||||
|
def _aliases(self) -> Iterable[str]:
|
||||||
|
return self.__aliases
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue