|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# coding=utf-8 |
| 3 | +""" |
| 4 | +Simple example demonstrating basic CommandSet usage. |
| 5 | +""" |
| 6 | +from typing import Any |
| 7 | + |
| 8 | +import cmd2 |
| 9 | +from cmd2 import CommandSet, with_default_category |
| 10 | + |
| 11 | + |
| 12 | +@with_default_category('Default Category') |
| 13 | +class MyBaseCommandSet(CommandSet): |
| 14 | + """Defines a default category for all sub-class CommandSets""" |
| 15 | + def __init__(self, _: Any): |
| 16 | + super(MyBaseCommandSet, self).__init__() |
| 17 | + |
| 18 | + |
| 19 | +class ChildInheritsParentCategories(MyBaseCommandSet): |
| 20 | + """ |
| 21 | + This subclass doesn't declare any categories so all commands here are also categorized under 'Default Category' |
| 22 | + """ |
| 23 | + def do_hello(self, _: cmd2.Statement): |
| 24 | + self._cmd.poutput('Hello') |
| 25 | + |
| 26 | + def do_world(self, _: cmd2.Statement): |
| 27 | + self._cmd.poutput('World') |
| 28 | + |
| 29 | + |
| 30 | +@with_default_category('Non-Heritable Category', heritable=False) |
| 31 | +class ChildOverridesParentCategoriesNonHeritable(MyBaseCommandSet): |
| 32 | + """ |
| 33 | + This subclass overrides the 'Default Category' from the parent, but in a non-heritable fashion. Sub-classes of this |
| 34 | + CommandSet will not inherit this category and will, instead, inherit 'Default Category' |
| 35 | + """ |
| 36 | + def do_goodbye(self, _: cmd2.Statement): |
| 37 | + self._cmd.poutput('Goodbye') |
| 38 | + |
| 39 | + |
| 40 | +class GrandchildInheritsGrandparentCategory(ChildOverridesParentCategoriesNonHeritable): |
| 41 | + """ |
| 42 | + This subclass's parent class declared its default category non-heritable. Instead, it inherits the category defined |
| 43 | + by the grandparent class. |
| 44 | + """ |
| 45 | + def do_aloha(self, _: cmd2.Statement): |
| 46 | + self._cmd.poutput('Aloha') |
| 47 | + |
| 48 | + |
| 49 | +@with_default_category('Heritable Category') |
| 50 | +class ChildOverridesParentCategories(MyBaseCommandSet): |
| 51 | + """ |
| 52 | + This subclass is decorated with a default category that is heritable. This overrides the parent class's default |
| 53 | + category declaration. |
| 54 | + """ |
| 55 | + def do_bonjour(self, _: cmd2.Statement): |
| 56 | + self._cmd.poutput('Bonjour') |
| 57 | + |
| 58 | + |
| 59 | +class GrandchildInheritsHeritable(ChildOverridesParentCategories): |
| 60 | + """ |
| 61 | + This subclass's parent declares a default category that overrides its parent. As a result, commands in this |
| 62 | + CommandSet will be categorized under 'Heritable Category' |
| 63 | + """ |
| 64 | + def do_monde(self, _: cmd2.Statement): |
| 65 | + self._cmd.poutput('Monde') |
| 66 | + |
| 67 | + |
| 68 | +class ExampleApp(cmd2.Cmd): |
| 69 | + """ |
| 70 | + Example to demonstrate heritable default categories |
| 71 | + """ |
| 72 | + |
| 73 | + def __init__(self): |
| 74 | + super(ExampleApp, self).__init__(auto_load_commands=False) |
| 75 | + |
| 76 | + def do_something(self, arg): |
| 77 | + self.poutput('this is the something command') |
| 78 | + |
| 79 | + |
| 80 | +def test_heritable_categories(): |
| 81 | + app = ExampleApp() |
| 82 | + |
| 83 | + base_cs = MyBaseCommandSet(0) |
| 84 | + assert getattr(base_cs, cmd2.constants.CLASS_ATTR_DEFAULT_HELP_CATEGORY, None) == 'Default Category' |
| 85 | + |
| 86 | + child1 = ChildInheritsParentCategories(1) |
| 87 | + assert getattr(child1, cmd2.constants.CLASS_ATTR_DEFAULT_HELP_CATEGORY, None) == 'Default Category' |
| 88 | + app.register_command_set(child1) |
| 89 | + assert getattr(app.cmd_func('hello').__func__, cmd2.constants.CMD_ATTR_HELP_CATEGORY, None) == 'Default Category' |
| 90 | + app.unregister_command_set(child1) |
| 91 | + |
| 92 | + child_nonheritable = ChildOverridesParentCategoriesNonHeritable(2) |
| 93 | + assert getattr(child_nonheritable, cmd2.constants.CLASS_ATTR_DEFAULT_HELP_CATEGORY, None) != 'Non-Heritable Category' |
| 94 | + app.register_command_set(child_nonheritable) |
| 95 | + assert getattr(app.cmd_func('goodbye').__func__, cmd2.constants.CMD_ATTR_HELP_CATEGORY, None) == 'Non-Heritable Category' |
| 96 | + app.unregister_command_set(child_nonheritable) |
| 97 | + |
| 98 | + grandchild1 = GrandchildInheritsGrandparentCategory(3) |
| 99 | + assert getattr(grandchild1, cmd2.constants.CLASS_ATTR_DEFAULT_HELP_CATEGORY, None) == 'Default Category' |
| 100 | + app.register_command_set(grandchild1) |
| 101 | + assert getattr(app.cmd_func('aloha').__func__, cmd2.constants.CMD_ATTR_HELP_CATEGORY, None) == 'Default Category' |
| 102 | + app.unregister_command_set(grandchild1) |
| 103 | + |
| 104 | + child_overrides = ChildOverridesParentCategories(4) |
| 105 | + assert getattr(child_overrides, cmd2.constants.CLASS_ATTR_DEFAULT_HELP_CATEGORY, None) == 'Heritable Category' |
| 106 | + app.register_command_set(child_overrides) |
| 107 | + assert getattr(app.cmd_func('bonjour').__func__, cmd2.constants.CMD_ATTR_HELP_CATEGORY, None) == 'Heritable Category' |
| 108 | + app.unregister_command_set(child_overrides) |
| 109 | + |
| 110 | + grandchild2 = GrandchildInheritsHeritable(5) |
| 111 | + assert getattr(grandchild2, cmd2.constants.CLASS_ATTR_DEFAULT_HELP_CATEGORY, None) == 'Heritable Category' |
0 commit comments