Skip to content

Commit 8706bf9

Browse files
committed
Refactor: Use Python properties for value access in Settable.
1 parent e9938c2 commit 8706bf9

File tree

4 files changed

+17
-11
lines changed

4 files changed

+17
-11
lines changed

cmd2/cmd2.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2509,7 +2509,7 @@ def _get_settable_completion_items(self) -> list[CompletionItem]:
25092509

25102510
for name, settable in self.settables.items():
25112511
descriptive_data = [
2512-
str(settable.get_value()),
2512+
str(settable.value),
25132513
settable.description,
25142514
]
25152515
results.append(CompletionItem(name, descriptive_data))
@@ -4460,12 +4460,12 @@ def do_set(self, args: argparse.Namespace) -> None:
44604460
if args.value:
44614461
# Try to update the settable's value
44624462
try:
4463-
orig_value = settable.get_value()
4464-
settable.set_value(su.strip_quotes(args.value))
4463+
orig_value = settable.value
4464+
settable.value = su.strip_quotes(args.value)
44654465
except ValueError as ex:
44664466
self.perror(f"Error setting {args.param}: {ex}")
44674467
else:
4468-
self.poutput(f"{args.param} - was: {orig_value!r}\nnow: {settable.get_value()!r}")
4468+
self.poutput(f"{args.param} - was: {orig_value!r}\nnow: {settable.value!r}")
44694469
self.last_result = True
44704470
return
44714471

@@ -4492,10 +4492,10 @@ def do_set(self, args: argparse.Namespace) -> None:
44924492
settable = self.settables[param]
44934493
settable_table.add_row(
44944494
param,
4495-
str(settable.get_value()),
4495+
str(settable.value),
44964496
settable.description,
44974497
)
4498-
self.last_result[param] = settable.get_value()
4498+
self.last_result[param] = settable.value
44994499

45004500
self.poutput()
45014501
self.poutput(settable_table)

cmd2/rich_utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ def __str__(self) -> str:
4040
"""Return value instead of enum name for printing in cmd2's set command."""
4141
return str(self.value)
4242

43+
def __repr__(self) -> str:
44+
"""Return quoted value instead of enum description for printing in cmd2's set command."""
45+
return repr(self.value)
46+
4347

4448
# Controls when ANSI style sequences are allowed in output
4549
ALLOW_STYLE = AllowStyle.TERMINAL

cmd2/utils.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,13 @@ def get_bool_choices(_: str) -> list[str]:
126126
self.choices_provider = choices_provider
127127
self.completer = completer
128128

129-
def get_value(self) -> Any:
129+
@property
130+
def value(self) -> Any:
130131
"""Get the value of the settable attribute."""
131132
return getattr(self.settable_obj, self.settable_attrib_name)
132133

133-
def set_value(self, value: Any) -> None:
134+
@value.setter
135+
def value(self, value: Any) -> None:
134136
"""Set the settable attribute on the specified destination object.
135137
136138
:param value: new value to set
@@ -144,7 +146,7 @@ def set_value(self, value: Any) -> None:
144146
raise ValueError(f"invalid choice: {new_value!r} (choose from {choices_str})")
145147

146148
# Try to update the settable's value
147-
orig_value = self.get_value()
149+
orig_value = self.value
148150
setattr(self.settable_obj, self.settable_attrib_name, new_value)
149151

150152
# Check if we need to call an onchange callback

tests/test_cmd2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def test_base_set(base_app) -> None:
153153
# Make sure all settables appear in last_result.
154154
assert len(base_app.last_result) == len(base_app.settables)
155155
for param in base_app.last_result:
156-
assert base_app.last_result[param] == base_app.settables[param].get_value()
156+
assert base_app.last_result[param] == base_app.settables[param].value
157157

158158

159159
def test_set(base_app) -> None:
@@ -2279,7 +2279,7 @@ def test_get_settable_completion_items(base_app) -> None:
22792279

22802280
# These CompletionItem descriptions are a two column table (Settable Value and Settable Description)
22812281
# First check if the description text starts with the value
2282-
str_value = str(cur_settable.get_value())
2282+
str_value = str(cur_settable.value)
22832283
assert cur_res.descriptive_data[0].startswith(str_value)
22842284

22852285
# The second column is likely to have wrapped long text. So we will just examine the

0 commit comments

Comments
 (0)