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

Function test_nested_subcommands

tests/test_commandset.py:673–715  ·  view source on GitHub ↗
(manual_command_sets_app)

Source from the content-addressed store, hash-verified

671
672
673def test_nested_subcommands(manual_command_sets_app) -> None:
674 base_cmds = LoadableBase(1)
675 pasta_cmds = LoadablePastaStir(1)
676
677 with pytest.raises(CommandSetRegistrationError):
678 manual_command_sets_app.register_command_set(pasta_cmds)
679
680 manual_command_sets_app.register_command_set(base_cmds)
681
682 manual_command_sets_app.register_command_set(pasta_cmds)
683
684 with pytest.raises(CommandSetRegistrationError):
685 manual_command_sets_app.unregister_command_set(base_cmds)
686
687 class BadNestedSubcommands(cmd2.CommandSet):
688 def __init__(self, dummy) -> None:
689 super().__init__()
690 self._dummy = dummy # prevents autoload
691
692 stir_pasta_vigor_parser = cmd2.Cmd2ArgumentParser()
693 stir_pasta_vigor_parser.add_argument("frequency")
694
695 # stir sauce doesn't exist anywhere, this should fail
696 @cmd2.as_subcommand_to("stir sauce", "vigorously", stir_pasta_vigor_parser)
697 def stir_pasta_vigorously(self, ns: argparse.Namespace) -> None:
698 self._cmd.poutput("stir the pasta vigorously")
699
700 with pytest.raises(CommandSetRegistrationError):
701 manual_command_sets_app.register_command_set(BadNestedSubcommands(1))
702
703 fruit_cmds = LoadableFruits(1)
704 manual_command_sets_app.register_command_set(fruit_cmds)
705
706 # validates custom namespace provider works correctly. Stir command will fail until
707 # the cut command is called
708 result = manual_command_sets_app.app_cmd("stir pasta vigorously everyminute")
709 assert "Need to cut before stirring" in result.stdout
710
711 result = manual_command_sets_app.app_cmd("cut banana discs")
712 assert "cutting banana: discs" in result.stdout
713
714 result = manual_command_sets_app.app_cmd("stir pasta vigorously everyminute")
715 assert "stir the pasta vigorously" in result.stdout
716
717
718class AppWithSubCommands(cmd2.Cmd):

Callers

nothing calls this directly

Calls 7

LoadableBaseClass · 0.85
LoadablePastaStirClass · 0.85
register_command_setMethod · 0.80
app_cmdMethod · 0.80
LoadableFruitsClass · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…