Skip to content

Commit aeb517d

Browse files
authored
Merge pull request #817 from python-cmd2/pwarning
Added apply_style to pwarning()
2 parents 3f1b75d + c5632b3 commit aeb517d

File tree

3 files changed

+52
-4
lines changed

3 files changed

+52
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* Added capability to override the argument parser class used by cmd2 built-in commands. See override_parser.py
88
example for more details.
99
* Added `end` argument to `pfeedback()` to be consistent with the other print functions like `poutput()`.
10+
* Added `apply_style` to `pwarning()`.
1011
* Breaking changes
1112
* For consistency between all the print functions:
1213
* Made `end` and `chop` keyword-only arguments of `ppaged()`

cmd2/cmd2.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -467,13 +467,17 @@ def perror(msg: Any, *, end: str = '\n', apply_style: bool = True) -> None:
467467
final_msg = "{}".format(msg)
468468
ansi.ansi_aware_write(sys.stderr, final_msg + end)
469469

470-
def pwarning(self, msg: Any, *, end: str = '\n') -> None:
471-
"""Apply the warning style to a message and print it to sys.stderr
470+
def pwarning(self, msg: Any, *, end: str = '\n', apply_style: bool = True) -> None:
471+
"""Like perror, but applies ansi.style_warning by default
472472
473473
:param msg: message to print (anything convertible to a str with '{}'.format() is OK)
474474
:param end: string appended after the end of the message, default a newline
475+
:param apply_style: If True, then ansi.style_warning will be applied to the message text. Set to False in cases
476+
where the message text already has the desired style. Defaults to True.
475477
"""
476-
self.perror(ansi.style_warning(msg), end=end, apply_style=False)
478+
if apply_style:
479+
msg = ansi.style_warning(msg)
480+
self.perror(msg, end=end, apply_style=False)
477481

478482
def pexcept(self, msg: Any, *, end: str = '\n', apply_style: bool = True) -> None:
479483
"""Print Exception message to sys.stderr. If debug is true, print exception traceback if one exists.
@@ -499,7 +503,6 @@ def pexcept(self, msg: Any, *, end: str = '\n', apply_style: bool = True) -> Non
499503
warning = "\nTo enable full traceback, run the following command: 'set debug true'"
500504
final_msg += ansi.style_warning(warning)
501505

502-
# Set apply_style to False since style has already been applied
503506
self.perror(final_msg, end=end, apply_style=False)
504507

505508
def pfeedback(self, msg: Any, *, end: str = '\n') -> None:

tests/test_cmd2.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1882,13 +1882,57 @@ def test_nonexistent_macro(base_app):
18821882

18831883
assert exception is not None
18841884

1885+
def test_perror_style(base_app, capsys):
1886+
msg = 'testing...'
1887+
end = '\n'
1888+
ansi.allow_ansi = ansi.ANSI_ALWAYS
1889+
base_app.perror(msg)
1890+
out, err = capsys.readouterr()
1891+
assert err == ansi.style_error(msg) + end
1892+
1893+
def test_perror_no_style(base_app, capsys):
1894+
msg = 'testing...'
1895+
end = '\n'
1896+
ansi.allow_ansi = ansi.ANSI_ALWAYS
1897+
base_app.perror(msg, apply_style=False)
1898+
out, err = capsys.readouterr()
1899+
assert err == msg + end
1900+
1901+
def test_pwarning_style(base_app, capsys):
1902+
msg = 'testing...'
1903+
end = '\n'
1904+
ansi.allow_ansi = ansi.ANSI_ALWAYS
1905+
base_app.pwarning(msg)
1906+
out, err = capsys.readouterr()
1907+
assert err == ansi.style_warning(msg) + end
1908+
1909+
def test_pwarning_no_style(base_app, capsys):
1910+
msg = 'testing...'
1911+
end = '\n'
1912+
ansi.allow_ansi = ansi.ANSI_ALWAYS
1913+
base_app.pwarning(msg, apply_style=False)
1914+
out, err = capsys.readouterr()
1915+
assert err == msg + end
1916+
18851917
def test_ppaged(outsim_app):
18861918
msg = 'testing...'
18871919
end = '\n'
18881920
outsim_app.ppaged(msg)
18891921
out = outsim_app.stdout.getvalue()
18901922
assert out == msg + end
18911923

1924+
def test_ppaged_blank(outsim_app):
1925+
msg = ''
1926+
outsim_app.ppaged(msg)
1927+
out = outsim_app.stdout.getvalue()
1928+
assert not out
1929+
1930+
def test_ppaged_none(outsim_app):
1931+
msg = None
1932+
outsim_app.ppaged(msg)
1933+
out = outsim_app.stdout.getvalue()
1934+
assert not out
1935+
18921936
def test_ppaged_strips_ansi_when_redirecting(outsim_app):
18931937
msg = 'testing...'
18941938
end = '\n'

0 commit comments

Comments
 (0)