MCPcopy Create free account
hub / github.com/python-cmd2/cmd2 / test_subcommand_attachment

Function test_subcommand_attachment

tests/test_cmd2.py:4521–4567  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

4519
4520
4521def test_subcommand_attachment() -> None:
4522 import argparse
4523
4524 class SubcmdApp(cmd2.Cmd):
4525 def __init__(self) -> None:
4526 super().__init__()
4527
4528 root_parser = cmd2.Cmd2ArgumentParser()
4529 root_parser.add_subparsers()
4530
4531 @cmd2.with_argparser(root_parser)
4532 def do_root(self, _args: argparse.Namespace) -> None:
4533 pass
4534
4535 app = SubcmdApp()
4536
4537 # Verify root exists and uses argparse
4538 root_parser = app.command_parsers.get(app.do_root)
4539 assert root_parser is not None
4540
4541 # Attach child to root
4542 child_parser = cmd2.Cmd2ArgumentParser(prog="child")
4543 child_parser.add_subparsers()
4544 app.attach_subcommand("root", "child", child_parser, help="child help")
4545
4546 # Verify child was attached
4547 root_subparsers_action = root_parser.get_subparsers_action()
4548 assert "child" in root_subparsers_action._name_parser_map
4549 assert root_subparsers_action._name_parser_map["child"] is child_parser
4550
4551 # Attach grandchild to child
4552 grandchild_parser = cmd2.Cmd2ArgumentParser(prog="grandchild")
4553 app.attach_subcommand("root child", "grandchild", grandchild_parser)
4554
4555 # Verify grandchild was attached
4556 child_subparsers_action = child_parser.get_subparsers_action()
4557 assert "grandchild" in child_subparsers_action._name_parser_map
4558
4559 # Detach grandchild
4560 detached_grandchild = app.detach_subcommand("root child", "grandchild")
4561 assert detached_grandchild is grandchild_parser
4562 assert "grandchild" not in child_subparsers_action._name_parser_map
4563
4564 # Detach child
4565 detached_child = app.detach_subcommand("root", "child")
4566 assert detached_child is child_parser
4567 assert "child" not in root_subparsers_action._name_parser_map
4568
4569
4570def test_subcommand_attachment_errors() -> None:

Callers

nothing calls this directly

Calls 5

get_subparsers_actionMethod · 0.95
SubcmdAppClass · 0.85
getMethod · 0.45
attach_subcommandMethod · 0.45
detach_subcommandMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…