Skip to content

Commit 114d9bf

Browse files
committed
[Settable] address typo, docstring format, Added unit test and updated getting_started example
1 parent 1280bb9 commit 114d9bf

4 files changed

Lines changed: 43 additions & 4 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
## (TBA)
22

33
- Enhancements
4-
- Added possibility to use rich Text objects to set the description argument of a `Settanble`
4+
- Added possibility to use rich Text objects to set the description argument of a `Settable`
55

66
## 4.1.1 (July 9, 2026)
77

‎cmd2/utils.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,7 @@ def __init__(
111111
input is a valid integer. Specifying bool automatically provides
112112
completion for 'true' and 'false' and uses a built-in function
113113
for conversion and validation.
114-
:param description: A concise string or rich Text object that describes the purpose of
115-
this setting.
114+
:param description: A concise string or rich Text object that describes the purpose of this setting.
116115
:param settable_object: The object that owns the attribute being made settable (e.g. self).
117116
:param settable_attrib_name: The name of the attribute on the settable_object that
118117
will be modified. This defaults to the value of the name

‎examples/getting_started.py‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from prompt_toolkit.application import get_app
3232
from prompt_toolkit.formatted_text import AnyFormattedText
3333
from rich.style import Style
34+
from rich.text import Text
3435

3536
import cmd2
3637
from cmd2 import (
@@ -94,7 +95,16 @@ def __init__(self) -> None:
9495
cmd2.Settable(
9596
"foreground_color",
9697
str,
97-
"Foreground color to use with echo command",
98+
Text.assemble(
99+
"Foreground color to use with echo command ",
100+
"(Options: ",
101+
Text("Green", Style(color=Color.GREEN)),
102+
", ",
103+
Text("Red", Style(color=Color.RED)),
104+
", ",
105+
Text("Blue", Style(color=Color.BLUE)),
106+
", ...)",
107+
),
98108
self,
99109
choices=fg_colors,
100110
)

‎tests/test_cmd2.py‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from prompt_toolkit.input import DummyInput, create_pipe_input
1919
from prompt_toolkit.output import DummyOutput
2020
from prompt_toolkit.shortcuts import PromptSession
21+
from rich.style import Style
2122
from rich.text import Text
2223

2324
import cmd2
@@ -352,6 +353,31 @@ def test_set_with_choices(base_app) -> None:
352353
assert err[0].startswith("Error setting fake: invalid choice")
353354

354355

356+
def test_set_with_rich_description(base_app) -> None:
357+
"""Test with rich Text description."""
358+
description = "Rich text description"
359+
base_app.rich_settable = "old"
360+
rich_settable = cmd2.Settable(
361+
"rich_settable", type(base_app.rich_settable), Text(description, Style(color="red", bold=True)), base_app
362+
)
363+
base_app.add_settable(rich_settable)
364+
365+
# Try see list settables info
366+
out, err = run_cmd(base_app, "set rich_settable")
367+
assert not err
368+
name, value, text = out[-1].strip().split(maxsplit=2)
369+
assert name == "rich_settable"
370+
assert value == "old"
371+
assert text == description
372+
373+
# Try to set a valid value
374+
out, err = run_cmd(base_app, "set rich_settable new")
375+
assert base_app.last_result is True
376+
assert not err
377+
assert out[0].startswith("rich_settable")
378+
assert out[0].endswith("─> 'new'")
379+
380+
355381
class OnChangeHookApp(cmd2.Cmd):
356382
def __init__(self, *args, **kwargs) -> None:
357383
super().__init__(*args, **kwargs)
@@ -4726,3 +4752,7 @@ def do_base(self, _: argparse.Namespace) -> None:
47264752
root_parser = cast(cmd2.Cmd2ArgumentParser, app.command_parsers.get(app.do_base))
47274753
subparsers_action = root_parser.get_subparsers_action()
47284754
assert not subparsers_action._name_parser_map
4755+
subparsers_action = root_parser.get_subparsers_action()
4756+
assert not subparsers_action._name_parser_map
4757+
subparsers_action = root_parser.get_subparsers_action()
4758+
assert not subparsers_action._name_parser_map

0 commit comments

Comments
 (0)