Skip to content

Commit 48cdf7d

Browse files
authored
Merge pull request #882 from python-cmd2/misc
do_set fix
2 parents c3f8160 + 4eca771 commit 48cdf7d

File tree

3 files changed

+25
-19
lines changed

3 files changed

+25
-19
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.10.1 (TBD)
2+
* Bug Fixes
3+
* Corrected issue where the actual new value was not always being printed in do_set. This occurred in cases where
4+
the typed value differed from what the setter had converted it to.
5+
16
## 0.10.0 (February 7, 2020)
27
* Enhancements
38
* Changed the default help text to make `help -v` more discoverable

cmd2/cmd2.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -431,9 +431,8 @@ def allow_style(self, new_val: str) -> None:
431431
if new_val in [ansi.STYLE_TERMINAL, ansi.STYLE_ALWAYS, ansi.STYLE_NEVER]:
432432
ansi.allow_style = new_val
433433
else:
434-
raise ValueError('Invalid value: {} (valid values: {}, {}, {})'.format(new_val, ansi.STYLE_TERMINAL,
435-
ansi.STYLE_ALWAYS,
436-
ansi.STYLE_NEVER))
434+
raise ValueError("must be {}, {}, or {} (case-insensitive)".format(ansi.STYLE_TERMINAL, ansi.STYLE_ALWAYS,
435+
ansi.STYLE_NEVER))
437436

438437
def _completion_supported(self) -> bool:
439438
"""Return whether tab completion is supported"""
@@ -2886,8 +2885,8 @@ def do_set(self, args: argparse.Namespace) -> None:
28862885
# Try to update the settable's value
28872886
try:
28882887
orig_value = getattr(self, args.param)
2889-
new_value = settable.val_type(args.value)
2890-
setattr(self, args.param, new_value)
2888+
setattr(self, args.param, settable.val_type(args.value))
2889+
new_value = getattr(self, args.param)
28912890
# noinspection PyBroadException
28922891
except Exception as e:
28932892
err_msg = "Error setting {}: {}".format(args.param, e)
@@ -3814,9 +3813,6 @@ def async_alert(self, alert_msg: str, new_prompt: Optional[str] = None) -> None:
38143813
# Sanity check that can't fail if self.terminal_lock was acquired before calling this function
38153814
if self.terminal_lock.acquire(blocking=False):
38163815

3817-
# Figure out what prompt is displaying
3818-
current_prompt = self.continuation_prompt if self._at_continuation_prompt else self.prompt
3819-
38203816
# Only update terminal if there are changes
38213817
update_terminal = False
38223818

@@ -3835,6 +3831,8 @@ def async_alert(self, alert_msg: str, new_prompt: Optional[str] = None) -> None:
38353831

38363832
if update_terminal:
38373833
import shutil
3834+
3835+
current_prompt = self.continuation_prompt if self._at_continuation_prompt else self.prompt
38383836
terminal_str = ansi.async_alert_str(terminal_columns=shutil.get_terminal_size().columns,
38393837
prompt=current_prompt, line=readline.get_line_buffer(),
38403838
cursor_offset=rl_get_point(), alert_msg=alert_msg)
@@ -3867,9 +3865,9 @@ def async_update_prompt(self, new_prompt: str) -> None: # pragma: no cover
38673865
a prompt is onscreen. Therefore it is best to acquire the lock before calling this function
38683866
to guarantee the prompt changes.
38693867
3870-
If a continuation prompt is currently being displayed while entering a multiline
3871-
command, the onscreen prompt will not change. However self.prompt will still be updated
3872-
and display immediately after the multiline line command completes.
3868+
If user is at a continuation prompt while entering a multiline command, the onscreen prompt will
3869+
not change. However self.prompt will still be updated and display immediately after the multiline
3870+
line command completes.
38733871
38743872
:param new_prompt: what to change the prompt to
38753873
"""

tests/test_cmd2.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,13 @@ def test_set_no_settables(base_app):
145145

146146

147147
@pytest.mark.parametrize('new_val, is_valid, expected', [
148-
(ansi.STYLE_NEVER, False, ansi.STYLE_NEVER),
149-
('neVeR', False, ansi.STYLE_NEVER),
150-
(ansi.STYLE_TERMINAL, False, ansi.STYLE_TERMINAL),
151-
('TeRMInal', False, ansi.STYLE_TERMINAL),
152-
(ansi.STYLE_ALWAYS, False, ansi.STYLE_ALWAYS),
153-
('AlWaYs', False, ansi.STYLE_ALWAYS),
154-
('invalid', True, ansi.STYLE_TERMINAL),
148+
(ansi.STYLE_NEVER, True, ansi.STYLE_NEVER),
149+
('neVeR', True, ansi.STYLE_NEVER),
150+
(ansi.STYLE_TERMINAL, True, ansi.STYLE_TERMINAL),
151+
('TeRMInal', True, ansi.STYLE_TERMINAL),
152+
(ansi.STYLE_ALWAYS, True, ansi.STYLE_ALWAYS),
153+
('AlWaYs', True, ansi.STYLE_ALWAYS),
154+
('invalid', False, ansi.STYLE_TERMINAL),
155155
])
156156
def test_set_allow_style(base_app, new_val, is_valid, expected):
157157
# Initialize allow_style for this test
@@ -161,14 +161,17 @@ def test_set_allow_style(base_app, new_val, is_valid, expected):
161161
out, err = run_cmd(base_app, 'set allow_style {}'.format(new_val))
162162

163163
# Verify the results
164-
assert bool(err) == is_valid
165164
assert ansi.allow_style == expected
165+
if is_valid:
166+
assert not err
167+
assert "now: {!r}".format(new_val.capitalize()) in out[1]
166168

167169
# Reload ansi module to reset allow_style to its default since it's an
168170
# application-wide setting that can affect other unit tests.
169171
import importlib
170172
importlib.reload(ansi)
171173

174+
172175
class OnChangeHookApp(cmd2.Cmd):
173176
def __init__(self, *args, **kwargs):
174177
super().__init__(*args, **kwargs)

0 commit comments

Comments
 (0)