MCPcopy Index your code
hub / github.com/python-cmd2/cmd2 / test_subcommand_attachment

Function test_subcommand_attachment

tests/test_argparse_utils.py:337–410  ·  view source on GitHub ↗

Test Cmd2ArgumentParser convenience methods for attaching and detaching subcommands.

()

Source from the content-addressed store, hash-verified

335
336
337def test_subcommand_attachment() -> None:
338 """Test Cmd2ArgumentParser convenience methods for attaching and detaching subcommands."""
339
340 ###############################
341 # Set up parsers
342 ###############################
343 root_parser = Cmd2ArgumentParser(prog="root", description="root command")
344 root_subparsers = root_parser.add_subparsers()
345
346 child_parser = Cmd2ArgumentParser(prog="child", description="child command")
347 child_subparsers = child_parser.add_subparsers() # Must have subparsers to host grandchild
348
349 grandchild_parser = Cmd2ArgumentParser(prog="grandchild", description="grandchild command")
350
351 ###############################
352 # Attach subcommands
353 ###############################
354
355 # Attach child to root
356 root_parser.attach_subcommand(
357 [],
358 "child",
359 child_parser,
360 help="a child command",
361 aliases=["child_alias"],
362 )
363
364 # Attach grandchild to child
365 root_parser.attach_subcommand(
366 ["child"],
367 "grandchild",
368 grandchild_parser,
369 help="a grandchild command",
370 )
371
372 ###############################
373 # Verify hierarchy navigation
374 ###############################
375
376 assert root_parser.find_parser(["child", "grandchild"]) is grandchild_parser
377 assert root_parser.find_parser(["child"]) is child_parser
378 assert root_parser.find_parser([]) is root_parser
379
380 ###############################
381 # Verify attachments
382 ###############################
383
384 # Verify child attachment and aliases
385 assert root_subparsers._name_parser_map["child"] is child_parser
386 assert root_subparsers._name_parser_map["child_alias"] is child_parser
387
388 # Verify grandchild attachment
389 assert child_subparsers._name_parser_map["grandchild"] is grandchild_parser
390
391 # Verify help entries are present
392 assert any(action.dest == "child" for action in root_subparsers._choices_actions)
393 assert any(action.dest == "grandchild" for action in child_subparsers._choices_actions)
394

Callers

nothing calls this directly

Calls 4

attach_subcommandMethod · 0.95
find_parserMethod · 0.95
detach_subcommandMethod · 0.95
Cmd2ArgumentParserClass · 0.90

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…