From 8706bf97f712203dbdac30382c691a0acbbfcd9d Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Tue, 26 Aug 2025 09:46:06 -0400 Subject: [PATCH 1/2] Refactor: Use Python properties for value access in Settable. --- cmd2/cmd2.py | 12 ++++++------ cmd2/rich_utils.py | 4 ++++ cmd2/utils.py | 8 +++++--- tests/test_cmd2.py | 4 ++-- 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index c082ac3c6..d069be7d7 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -2509,7 +2509,7 @@ def _get_settable_completion_items(self) -> list[CompletionItem]: for name, settable in self.settables.items(): descriptive_data = [ - str(settable.get_value()), + str(settable.value), settable.description, ] results.append(CompletionItem(name, descriptive_data)) @@ -4460,12 +4460,12 @@ def do_set(self, args: argparse.Namespace) -> None: if args.value: # Try to update the settable's value try: - orig_value = settable.get_value() - settable.set_value(su.strip_quotes(args.value)) + orig_value = settable.value + settable.value = su.strip_quotes(args.value) except ValueError as ex: self.perror(f"Error setting {args.param}: {ex}") else: - self.poutput(f"{args.param} - was: {orig_value!r}\nnow: {settable.get_value()!r}") + self.poutput(f"{args.param} - was: {orig_value!r}\nnow: {settable.value!r}") self.last_result = True return @@ -4492,10 +4492,10 @@ def do_set(self, args: argparse.Namespace) -> None: settable = self.settables[param] settable_table.add_row( param, - str(settable.get_value()), + str(settable.value), settable.description, ) - self.last_result[param] = settable.get_value() + self.last_result[param] = settable.value self.poutput() self.poutput(settable_table) diff --git a/cmd2/rich_utils.py b/cmd2/rich_utils.py index 3d3873174..477a6ab8e 100644 --- a/cmd2/rich_utils.py +++ b/cmd2/rich_utils.py @@ -40,6 +40,10 @@ def __str__(self) -> str: """Return value instead of enum name for printing in cmd2's set command.""" return str(self.value) + def __repr__(self) -> str: + """Return quoted value instead of enum description for printing in cmd2's set command.""" + return repr(self.value) + # Controls when ANSI style sequences are allowed in output ALLOW_STYLE = AllowStyle.TERMINAL diff --git a/cmd2/utils.py b/cmd2/utils.py index 12ba3c8d5..35a875b5b 100644 --- a/cmd2/utils.py +++ b/cmd2/utils.py @@ -126,11 +126,13 @@ def get_bool_choices(_: str) -> list[str]: self.choices_provider = choices_provider self.completer = completer - def get_value(self) -> Any: + @property + def value(self) -> Any: """Get the value of the settable attribute.""" return getattr(self.settable_obj, self.settable_attrib_name) - def set_value(self, value: Any) -> None: + @value.setter + def value(self, value: Any) -> None: """Set the settable attribute on the specified destination object. :param value: new value to set @@ -144,7 +146,7 @@ def set_value(self, value: Any) -> None: raise ValueError(f"invalid choice: {new_value!r} (choose from {choices_str})") # Try to update the settable's value - orig_value = self.get_value() + orig_value = self.value setattr(self.settable_obj, self.settable_attrib_name, new_value) # Check if we need to call an onchange callback diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py index 295ae7207..f9f511f87 100644 --- a/tests/test_cmd2.py +++ b/tests/test_cmd2.py @@ -153,7 +153,7 @@ def test_base_set(base_app) -> None: # Make sure all settables appear in last_result. assert len(base_app.last_result) == len(base_app.settables) for param in base_app.last_result: - assert base_app.last_result[param] == base_app.settables[param].get_value() + assert base_app.last_result[param] == base_app.settables[param].value def test_set(base_app) -> None: @@ -2279,7 +2279,7 @@ def test_get_settable_completion_items(base_app) -> None: # These CompletionItem descriptions are a two column table (Settable Value and Settable Description) # First check if the description text starts with the value - str_value = str(cur_settable.get_value()) + str_value = str(cur_settable.value) assert cur_res.descriptive_data[0].startswith(str_value) # The second column is likely to have wrapped long text. So we will just examine the From 1c74503122d96218881c95e85053cc9fce91b96d Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Tue, 26 Aug 2025 09:52:54 -0400 Subject: [PATCH 2/2] Updated change log with Settable.value property information. --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 83cdd55d3..2d6d5ea81 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ - Moved all string-related functions from utils.py to string_utils.py. - Removed all text style Enums from ansi.py in favor of `Rich` styles. - Renamed ansi.py to terminal_utils.py to reflect the functions left in it. + - Replaced `utils.Settable.get_value()` and `utils.Settable.set_value()` in favor of a Python + property called `Settable.value`. - Enhancements - Simplified the process to set a custom parser for `cmd2's` built-in commands. See