Skip to content

Commit 438ef95

Browse files
committed
Silenced the set command on success by default and consolidated its output. (#1627)
* Made the set command silent on success by default. * Added a -v/--verbose flag to the set command to display change confirmations. * Consolidated the verbose output into a single, colorized line (e.g., param: old -> new). * Updated unit tests to reflect the new default behavior and output format.
1 parent 74a2889 commit 438ef95

3 files changed

Lines changed: 36 additions & 33 deletions

File tree

cmd2/cmd2.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4685,6 +4685,12 @@ 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+
)
46884694
set_parser.add_argument(
46894695
"value",
46904696
nargs=argparse.OPTIONAL,
@@ -4720,7 +4726,16 @@ def do_set(self, args: argparse.Namespace) -> None:
47204726
except ValueError as ex:
47214727
self.perror(f"Error setting {args.param}: {ex}")
47224728
else:
4723-
self.poutput(f"{args.param} - was: {orig_value!r}\nnow: {settable.value!r}")
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)
4738+
47244739
self.last_result = True
47254740
return
47264741

tests/test_cmd2.py

Lines changed: 17 additions & 26 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] [param] [value]"
124+
assert err[0] == "Usage: set [-h] [-v] [param] [value]"
125125
assert "Error: unrecognized arguments: -z" in err[1]
126126

127127

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

162162

163163
def test_set(base_app) -> None:
164+
# Test silent by default
164165
out, _err = run_cmd(base_app, "set quiet True")
165-
expected = normalize(
166-
"""
167-
quiet - was: False
168-
now: True
169-
"""
170-
)
166+
assert not out
167+
assert base_app.last_result is True
168+
169+
# Test verbose
170+
out, _err = run_cmd(base_app, "set -v quiet False")
171+
expected = ["quiet: True -> False"]
171172
assert out == expected
172173
assert base_app.last_result is True
173174

174175
line_found = False
175176
out, _err = run_cmd(base_app, "set quiet")
176177
for line in out:
177-
if "quiet" in line and "True" in line and "False" not in line:
178+
if "quiet" in line and "False" in line and "True" not in line:
178179
line_found = True
179180
break
180181

181182
assert line_found
182183
assert len(base_app.last_result) == 1
183-
assert base_app.last_result["quiet"] is True
184+
assert base_app.last_result["quiet"] is False
184185

185186

186187
def test_set_val_empty(base_app) -> None:
@@ -231,7 +232,7 @@ def test_set_no_settables(base_app) -> None:
231232
@with_ansi_style(ru.AllowStyle.TERMINAL)
232233
def test_set_allow_style(base_app, new_val, is_valid, expected) -> None:
233234
# Use the set command to alter allow_style
234-
out, err = run_cmd(base_app, f"set allow_style {new_val}")
235+
out, err = run_cmd(base_app, f"set -v allow_style {new_val}")
235236
assert base_app.last_result is is_valid
236237

237238
# Verify the results
@@ -276,12 +277,11 @@ def onchange_app():
276277

277278

278279
def test_set_onchange_hook(onchange_app) -> None:
279-
out, _err = run_cmd(onchange_app, "set quiet True")
280+
out, _err = run_cmd(onchange_app, "set -v quiet True")
280281
expected = normalize(
281282
"""
282283
You changed quiet
283-
quiet - was: False
284-
now: True
284+
quiet: False -> True
285285
"""
286286
)
287287
assert out == expected
@@ -874,12 +874,8 @@ def test_allow_clipboard(base_app) -> None:
874874

875875
def test_base_timing(base_app) -> None:
876876
base_app.feedback_to_output = False
877-
out, err = run_cmd(base_app, "set timing True")
878-
expected = normalize(
879-
"""timing - was: False
880-
now: True
881-
"""
882-
)
877+
out, err = run_cmd(base_app, "set -v timing True")
878+
expected = ["timing: False -> True"]
883879
assert out == expected
884880

885881
if sys.platform == "win32":
@@ -898,13 +894,8 @@ def test_base_debug(base_app) -> None:
898894
assert "To enable full traceback" in err[3]
899895

900896
# Set debug true
901-
out, err = run_cmd(base_app, "set debug True")
902-
expected = normalize(
903-
"""
904-
debug - was: False
905-
now: True
906-
"""
907-
)
897+
out, err = run_cmd(base_app, "set -v debug True")
898+
expected = ["debug: False -> True"]
908899
assert out == expected
909900

910901
# Verify that we now see the exception traceback

tests/test_commandset.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,12 +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 arbitrary_value 10")
1106-
expected = """
1107-
arbitrary_value - was: 5
1108-
now: 10
1109-
"""
1110-
assert out == normalize(expected)
1105+
out, err = run_cmd(app, "set -v arbitrary_value 10")
1106+
expected = ["arbitrary_value: 5 -> 10"]
1107+
assert out == expected
11111108
out, err = run_cmd(app, "set arbitrary_value")
11121109
any("arbitrary_value" in line and "10" in line for line in out)
11131110

0 commit comments

Comments
 (0)