|
14 | 14 | ) |
15 | 15 | from cmd2.argparse_custom import ( |
16 | 16 | Cmd2HelpFormatter, |
17 | | - Cmd2RichArgparseConsole, |
18 | 17 | generate_range_error, |
19 | 18 | register_argparse_argument_parameter, |
20 | 19 | ) |
| 20 | +from cmd2.rich_utils import Cmd2RichArgparseConsole |
21 | 21 |
|
22 | 22 | from .conftest import run_cmd |
23 | 23 |
|
@@ -476,3 +476,59 @@ def side_effect(color, **kwargs): |
476 | 476 | assert mock_set_color.call_count == 2 |
477 | 477 | mock_set_color.assert_any_call(True, file=sys.stdout) |
478 | 478 | mock_set_color.assert_any_call(True) |
| 479 | + |
| 480 | + |
| 481 | +def test_update_prog() -> None: |
| 482 | + """Test Cmd2ArgumentParser.update_prog() across various scenarios.""" |
| 483 | + |
| 484 | + # Set up a complex parser hierarchy |
| 485 | + old_root = 'old_app' |
| 486 | + parser = Cmd2ArgumentParser(prog=old_root) |
| 487 | + |
| 488 | + # Positionals before subcommand |
| 489 | + parser.add_argument('pos1') |
| 490 | + |
| 491 | + # Mutually exclusive group with positionals |
| 492 | + group = parser.add_mutually_exclusive_group(required=True) |
| 493 | + group.add_argument('posA', nargs='?') |
| 494 | + group.add_argument('posB', nargs='?') |
| 495 | + |
| 496 | + # Subparsers with aliases and no help text |
| 497 | + subparsers = parser.add_subparsers(dest='cmd') |
| 498 | + |
| 499 | + # Subcommand with aliases |
| 500 | + sub1 = subparsers.add_parser('sub1', aliases=['s1', 'alias1'], help='help for sub1') |
| 501 | + |
| 502 | + # Subcommand with no help text |
| 503 | + sub2 = subparsers.add_parser('sub2') |
| 504 | + |
| 505 | + # Nested subparser |
| 506 | + sub2.add_argument('inner_pos') |
| 507 | + sub2_subparsers = sub2.add_subparsers(dest='sub2_cmd') |
| 508 | + leaf = sub2_subparsers.add_parser('leaf', help='leaf help') |
| 509 | + |
| 510 | + # Verify initial progs look correct |
| 511 | + assert parser.prog == 'old_app' |
| 512 | + assert sub1.prog == 'old_app pos1 (posA | posB) sub1' |
| 513 | + assert sub2.prog == 'old_app pos1 (posA | posB) sub2' |
| 514 | + assert leaf.prog == 'old_app pos1 (posA | posB) sub2 inner_pos leaf' |
| 515 | + |
| 516 | + # Perform update |
| 517 | + new_root = 'new_app' |
| 518 | + parser.update_prog(new_root) |
| 519 | + |
| 520 | + # Verify new progs look correct |
| 521 | + assert parser.prog == 'new_app' |
| 522 | + assert sub1.prog == 'new_app pos1 (posA | posB) sub1' |
| 523 | + assert sub2.prog == 'new_app pos1 (posA | posB) sub2' |
| 524 | + assert leaf.prog == 'new_app pos1 (posA | posB) sub2 inner_pos leaf' |
| 525 | + |
| 526 | + # Verify that action._prog_prefix was updated by adding a new subparser |
| 527 | + sub3 = subparsers.add_parser('sub3') |
| 528 | + assert sub3.prog == 'new_app pos1 (posA | posB) sub3' |
| 529 | + |
| 530 | + # Verify aliases still point to the correct parser |
| 531 | + for action in parser._actions: |
| 532 | + if isinstance(action, argparse._SubParsersAction): |
| 533 | + assert action.choices['s1'].prog == sub1.prog |
| 534 | + assert action.choices['alias1'].prog == sub1.prog |
0 commit comments