Skip to content

Commit 45f17c4

Browse files
committed
Changed set command output unless quiet mode is enabled.
* Updated do_set to use pfeedback for change confirmations, allowing it to be silenced via the quiet setting. * Updated unit tests to match the new output format and verify quiet mode behavior.
1 parent 438ef95 commit 45f17c4

4 files changed

Lines changed: 43 additions & 48 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ prompt is displayed.
131131
specific `cmd2.Cmd` subclass (e.g.,`class MyCommandSet(CommandSet[MyApp]):`). This provides
132132
full type hints and IDE autocompletion for `self._cmd` without needing to override and cast
133133
the property.
134+
- Updated `set` command to consolidate its confirmation output into a single, colorized line.
135+
The confirmation now uses `pfeedback()`, allowing it to be silenced when the `quiet` settable
136+
is enabled.
134137

135138
## 3.5.0 (April 13, 2026)
136139

cmd2/cmd2.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4685,12 +4685,6 @@ def complete_set_value(
46854685
def _build_set_parser(cls) -> Cmd2ArgumentParser:
46864686
# Create the parser for the set command
46874687
set_parser = cls._build_base_set_parser()
4688-
set_parser.add_argument(
4689-
"-v",
4690-
"--verbose",
4691-
action="store_true",
4692-
help="show the change",
4693-
)
46944688
set_parser.add_argument(
46954689
"value",
46964690
nargs=argparse.OPTIONAL,
@@ -4726,15 +4720,15 @@ def do_set(self, args: argparse.Namespace) -> None:
47264720
except ValueError as ex:
47274721
self.perror(f"Error setting {args.param}: {ex}")
47284722
else:
4729-
if args.verbose:
4730-
feedback_msg = Text.assemble(
4731-
args.param,
4732-
": ",
4733-
(f"{orig_value!r}", "red"),
4734-
" -> ",
4735-
(f"{settable.value!r}", "green"),
4736-
)
4737-
self.poutput(feedback_msg)
4723+
# Create the feedback message using Rich Text for color
4724+
feedback_msg = Text.assemble(
4725+
args.param,
4726+
": ",
4727+
(f"{orig_value!r}", "red"),
4728+
" -> ",
4729+
(f"{settable.value!r}", "green"),
4730+
)
4731+
self.pfeedback(feedback_msg)
47384732

47394733
self.last_result = True
47404734
return

tests/test_cmd2.py

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def test_base_argparse_help(base_app) -> None:
121121

122122
def test_base_invalid_option(base_app) -> None:
123123
_out, err = run_cmd(base_app, "set -z")
124-
assert err[0] == "Usage: set [-h] [-v] [param] [value]"
124+
assert err[0] == "Usage: set [-h] [param] [value]"
125125
assert "Error: unrecognized arguments: -z" in err[1]
126126

127127

@@ -161,27 +161,28 @@ def test_base_set(base_app) -> None:
161161

162162

163163
def test_set(base_app) -> None:
164-
# Test silent by default
165-
out, _err = run_cmd(base_app, "set quiet True")
164+
out, err = run_cmd(base_app, "set quiet True")
166165
assert not out
167166
assert base_app.last_result is True
168167

169-
# Test verbose
170-
out, _err = run_cmd(base_app, "set -v quiet False")
171-
expected = ["quiet: True -> False"]
172-
assert out == expected
168+
# Test quiet respect
169+
out, err = run_cmd(base_app, "set timing False")
170+
assert not out
171+
assert not err
173172
assert base_app.last_result is True
174173

174+
# Show one settable (this always goes to out)
175175
line_found = False
176176
out, _err = run_cmd(base_app, "set quiet")
177177
for line in out:
178-
if "quiet" in line and "False" in line and "True" not in line:
178+
if "quiet" in line and "True" in line and "False" not in line:
179179
line_found = True
180180
break
181181

182182
assert line_found
183183
assert len(base_app.last_result) == 1
184-
assert base_app.last_result["quiet"] is False
184+
assert base_app.last_result["quiet"] is True
185+
base_app.quiet = False
185186

186187

187188
def test_set_val_empty(base_app) -> None:
@@ -232,14 +233,14 @@ def test_set_no_settables(base_app) -> None:
232233
@with_ansi_style(ru.AllowStyle.TERMINAL)
233234
def test_set_allow_style(base_app, new_val, is_valid, expected) -> None:
234235
# Use the set command to alter allow_style
235-
out, err = run_cmd(base_app, f"set -v allow_style {new_val}")
236+
out, err = run_cmd(base_app, f"set allow_style {new_val}")
236237
assert base_app.last_result is is_valid
237238

238239
# Verify the results
239240
assert expected == ru.ALLOW_STYLE
240241
if is_valid:
241-
assert not err
242-
assert out
242+
assert err
243+
assert not out
243244

244245

245246
def test_set_with_choices(base_app) -> None:
@@ -251,9 +252,10 @@ def test_set_with_choices(base_app) -> None:
251252
base_app.add_settable(fake_settable)
252253

253254
# Try a valid choice
254-
_out, err = run_cmd(base_app, f"set fake {fake_choices[1]}")
255+
out, err = run_cmd(base_app, f"set fake {fake_choices[1]}")
255256
assert base_app.last_result is True
256-
assert not err
257+
assert not out
258+
assert err == [f"fake: {fake_choices[0]!r} -> {fake_choices[1]!r}"]
257259

258260
# Try an invalid choice
259261
_out, err = run_cmd(base_app, "set fake bad_value")
@@ -277,14 +279,10 @@ def onchange_app():
277279

278280

279281
def test_set_onchange_hook(onchange_app) -> None:
280-
out, _err = run_cmd(onchange_app, "set -v quiet True")
281-
expected = normalize(
282-
"""
283-
You changed quiet
284-
quiet: False -> True
285-
"""
286-
)
287-
assert out == expected
282+
out, err = run_cmd(onchange_app, "set quiet True")
283+
assert out == ["You changed quiet"]
284+
# quiet: False -> True is not shown because quiet is now True
285+
assert not err
288286
assert onchange_app.last_result is True
289287

290288

@@ -874,14 +872,14 @@ def test_allow_clipboard(base_app) -> None:
874872

875873
def test_base_timing(base_app) -> None:
876874
base_app.feedback_to_output = False
877-
out, err = run_cmd(base_app, "set -v timing True")
878-
expected = ["timing: False -> True"]
879-
assert out == expected
875+
out, err = run_cmd(base_app, "set timing True")
876+
assert not out
877+
assert err[0] == "timing: False -> True"
880878

881879
if sys.platform == "win32":
882-
assert err[0].startswith("Elapsed: 0:00:00")
880+
assert err[1].startswith("Elapsed: 0:00:00")
883881
else:
884-
assert err[0].startswith("Elapsed: 0:00:00.0")
882+
assert err[1].startswith("Elapsed: 0:00:00.0")
885883

886884

887885
def test_base_debug(base_app) -> None:
@@ -894,9 +892,9 @@ def test_base_debug(base_app) -> None:
894892
assert "To enable full traceback" in err[3]
895893

896894
# Set debug true
897-
out, err = run_cmd(base_app, "set -v debug True")
898-
expected = ["debug: False -> True"]
899-
assert out == expected
895+
out, err = run_cmd(base_app, "set debug True")
896+
assert not out
897+
assert err == ["debug: False -> True"]
900898

901899
# Verify that we now see the exception traceback
902900
out, err = run_cmd(base_app, "edit")

tests/test_commandset.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,9 +1102,9 @@ def __init__(self) -> None:
11021102
any("arbitrary_value" in line and "5" in line for line in out)
11031103

11041104
# change the value and verify the value changed
1105-
out, err = run_cmd(app, "set -v arbitrary_value 10")
1106-
expected = ["arbitrary_value: 5 -> 10"]
1107-
assert out == expected
1105+
out, err = run_cmd(app, "set arbitrary_value 10")
1106+
assert not out
1107+
assert err == ["arbitrary_value: 5 -> 10"]
11081108
out, err = run_cmd(app, "set arbitrary_value")
11091109
any("arbitrary_value" in line and "10" in line for line in out)
11101110

0 commit comments

Comments
 (0)