Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions cmd2/cmd2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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

Expand All @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions cmd2/rich_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions cmd2/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/test_cmd2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
Loading