summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Lin <anselor@gmail.com>2020-09-08 23:06:27 -0400
committerEric Lin <anselor@gmail.com>2020-09-08 23:06:27 -0400
commit5791c153dfdae2f140b6b0d8862b505fc3084112 (patch)
treeb0e2e4239d5dcab1717dd4c04844b176074affe3
parent5bc9b967d347356556d30bb04f0a2a9f7662aa06 (diff)
downloadcmd2-git-default_categories.tar.gz
Start to some improvements to default categories. Next: tag CommandSet classes with category name and search for default categories when a command is added to cmd2.Cmddefault_categories
-rw-r--r--cmd2/command_definition.py4
-rw-r--r--examples/read_input.py13
2 files changed, 10 insertions, 7 deletions
diff --git a/cmd2/command_definition.py b/cmd2/command_definition.py
index 64adaada..96bd9c46 100644
--- a/cmd2/command_definition.py
+++ b/cmd2/command_definition.py
@@ -6,6 +6,7 @@ from typing import Optional, Type
from .constants import COMMAND_FUNC_PREFIX
from .exceptions import CommandSetRegistrationError
+from .utils import get_defining_class
# Allows IDEs to resolve types without impacting imports at runtime, breaking circular dependency issues
try: # pragma: no cover
@@ -32,7 +33,8 @@ def with_default_category(category: str):
from .decorators import with_category
methods = inspect.getmembers(
cls,
- predicate=lambda meth: inspect.isfunction(meth) and meth.__name__.startswith(COMMAND_FUNC_PREFIX))
+ predicate=lambda meth: inspect.isfunction(meth) and meth.__name__.startswith(COMMAND_FUNC_PREFIX)
+ and meth in inspect.getmro(cls)[0].__dict__.values())
category_decorator = with_category(category)
for method in methods:
if not hasattr(method[1], CMD_ATTR_HELP_CATEGORY):
diff --git a/examples/read_input.py b/examples/read_input.py
index e772a106..6fe6b99a 100644
--- a/examples/read_input.py
+++ b/examples/read_input.py
@@ -10,13 +10,14 @@ import cmd2
EXAMPLE_COMMANDS = "Example Commands"
+@cmd2.with_default_category(EXAMPLE_COMMANDS)
class ReadInputApp(cmd2.Cmd):
def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self.prompt = "\n" + self.prompt
self.custom_history = ['history 1', 'history 2']
- @cmd2.with_category(EXAMPLE_COMMANDS)
+ # @cmd2.with_category(EXAMPLE_COMMANDS)
def do_basic(self, _) -> None:
"""Call read_input with no history or tab completion"""
self.poutput("Tab completion and up-arrow history is off")
@@ -36,7 +37,7 @@ class ReadInputApp(cmd2.Cmd):
else:
self.custom_history.append(input_str)
- @cmd2.with_category(EXAMPLE_COMMANDS)
+ # @cmd2.with_category(EXAMPLE_COMMANDS)
def do_commands(self, _) -> None:
"""Call read_input the same way cmd2 prompt does to read commands"""
self.poutput("Tab completing and up-arrow history configured for commands")
@@ -45,7 +46,7 @@ class ReadInputApp(cmd2.Cmd):
except EOFError:
pass
- @cmd2.with_category(EXAMPLE_COMMANDS)
+ # @cmd2.with_category(EXAMPLE_COMMANDS)
def do_custom_choices(self, _) -> None:
"""Call read_input to use custom history and choices"""
self.poutput("Tab completing with static choices list and using custom history")
@@ -62,7 +63,7 @@ class ReadInputApp(cmd2.Cmd):
"""Example choices provider function"""
return ["from_provider_1", "from_provider_2", "from_provider_3"]
- @cmd2.with_category(EXAMPLE_COMMANDS)
+ # @cmd2.with_category(EXAMPLE_COMMANDS)
def do_custom_choices_provider(self, _) -> None:
"""Call read_input to use custom history and choices provider function"""
self.poutput("Tab completing with choices from provider function and using custom history")
@@ -74,7 +75,7 @@ class ReadInputApp(cmd2.Cmd):
else:
self.custom_history.append(input_str)
- @cmd2.with_category(EXAMPLE_COMMANDS)
+ # @cmd2.with_category(EXAMPLE_COMMANDS)
def do_custom_completer(self, _) -> None:
"""all read_input to use custom history and completer function"""
self.poutput("Tab completing paths and using custom history")
@@ -85,7 +86,7 @@ class ReadInputApp(cmd2.Cmd):
except EOFError:
pass
- @cmd2.with_category(EXAMPLE_COMMANDS)
+ # @cmd2.with_category(EXAMPLE_COMMANDS)
def do_custom_parser(self, _) -> None:
"""Call read_input to use a custom history and an argument parser"""
parser = cmd2.Cmd2ArgumentParser(prog='', description="An example parser")