lib.Cmd: Add single Cmd to add_subcommands()
All checks were successful
CI / Packaging - Kali Linux (pull_request) Successful in 4m32s
CI / Packaging - OpenSUSE Tumbleweed (pull_request) Successful in 4m30s
CI / Packaging test (pull_request) Successful in 0s
CI / Packaging - Kali Linux (push) Successful in 4m18s
CI / Packaging - OpenSUSE Tumbleweed (push) Successful in 3m59s
CI / Packaging test (push) Successful in 0s
All checks were successful
CI / Packaging - Kali Linux (pull_request) Successful in 4m32s
CI / Packaging - OpenSUSE Tumbleweed (pull_request) Successful in 4m30s
CI / Packaging test (pull_request) Successful in 0s
CI / Packaging - Kali Linux (push) Successful in 4m18s
CI / Packaging - OpenSUSE Tumbleweed (push) Successful in 3m59s
CI / Packaging test (push) Successful in 0s
add_subcommands() advertises Cmd and list[Cmd] in its signature, but a single Cmd instance raises NotImplementedError, and since the list branch handles every element through the same method, a list of Cmd instances is broken as well. The only working forms are Types and lists of Types. Handle a single Cmd instance by reparenting it to the caller and appending it to the children, tracking its class like the class-based path does. Instances whose name is already taken by a child are rejected, mirroring the duplicate-class handling, because argparse cannot register two subparsers under the same name. Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v0.84.2 Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
d9cc2f3012
commit
75b6603a3f
1 changed files with 9 additions and 1 deletions
|
|
@ -88,7 +88,15 @@ class AbstractCmd(abc.ABC):
|
|||
self, cmds: Cmd | list[Cmd] | Types[Any] | list[Types[Any]]
|
||||
) -> None:
|
||||
if isinstance(cmds, Cmd):
|
||||
raise NotImplementedError('Single Cmd should be handled elsewhere')
|
||||
if any(child.name == cmds.name for child in self.__children):
|
||||
raise Exception(
|
||||
f'Can\'t register subcommand with already taken name "{cmds.name}"'
|
||||
)
|
||||
self.__child_classes.append(type(cmds))
|
||||
cmds.set_parent(self)
|
||||
self.__children.append(cmds)
|
||||
assert len(self.__children) == len(self.__child_classes)
|
||||
return
|
||||
if isinstance(cmds, list):
|
||||
for cmd in cmds:
|
||||
self.add_subcommands(cmd)
|
||||
|
|
|
|||
Loading…
Reference in a new issue