Skip to content

Commit ac4c904

Browse files
committed
Updated documentation.
1 parent 322b2e4 commit ac4c904

File tree

2 files changed

+42
-19
lines changed

2 files changed

+42
-19
lines changed

cmd2/cmd2.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@
170170

171171
# Set up readline
172172
if rl_type == RlType.NONE: # pragma: no cover
173-
Cmd2GeneralConsole(sys.stderr).print(rl_warning, style=Cmd2Style.WARNING)
173+
Cmd2GeneralConsole(file=sys.stderr).print(rl_warning, style=Cmd2Style.WARNING)
174174
else:
175175
from .rl_utils import (
176176
readline,
@@ -1351,7 +1351,7 @@ def print_to(
13511351
console = destination
13521352
else:
13531353
# It's a file-like object (e.g., sys.stdout, StringIO)
1354-
console = Cmd2GeneralConsole(destination)
1354+
console = Cmd2GeneralConsole(file=destination)
13551355

13561356
prepared_objects = ru.prepare_objects_for_rendering(*objects)
13571357

@@ -1374,7 +1374,7 @@ def print_to(
13741374
# warning message, then set the broken_pipe_warning attribute
13751375
# to the message you want printed.
13761376
if self.broken_pipe_warning and console.file != sys.stderr:
1377-
Cmd2GeneralConsole(sys.stderr).print(self.broken_pipe_warning)
1377+
Cmd2GeneralConsole(file=sys.stderr).print(self.broken_pipe_warning)
13781378

13791379
def poutput(
13801380
self,
@@ -1507,7 +1507,7 @@ def pexcept(
15071507
:param kwargs: Arbitrary keyword arguments. This allows subclasses to extend the signature of this
15081508
method and still call `super()` without encountering unexpected keyword argument errors.
15091509
"""
1510-
console = Cmd2ExceptionConsole(sys.stderr)
1510+
console = Cmd2ExceptionConsole()
15111511

15121512
# Only print a traceback if we're in debug mode and one exists.
15131513
if self.debug and sys.exc_info() != (None, None, None):
@@ -1646,7 +1646,7 @@ def ppaged(
16461646
soft_wrap = True
16471647

16481648
# Generate the bytes to send to the pager
1649-
console = Cmd2GeneralConsole(self.stdout)
1649+
console = Cmd2GeneralConsole(file=self.stdout)
16501650
with console.capture() as capture:
16511651
self.print_to(
16521652
console,

cmd2/rich_utils.py

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ class Cmd2BaseConsole(Console):
124124

125125
def __init__(
126126
self,
127+
*,
127128
file: IO[str] | None = None,
128129
**kwargs: Any,
129130
) -> None:
@@ -180,17 +181,19 @@ def on_broken_pipe(self) -> None:
180181

181182

182183
class Cmd2GeneralConsole(Cmd2BaseConsole):
183-
"""Rich console for general-purpose printing."""
184+
"""Rich console for general-purpose printing.
184185
185-
def __init__(self, file: IO[str] | None = None) -> None:
186+
It enables soft wrap and disables Rich's automatic detection for markup,
187+
emoji, and highlighting. These defaults can be overridden in calls to the
188+
console's or cmd2's print methods.
189+
"""
190+
191+
def __init__(self, *, file: IO[str] | None = None) -> None:
186192
"""Cmd2GeneralConsole initializer.
187193
188194
:param file: optional file object where the console should write to.
189195
Defaults to sys.stdout.
190196
"""
191-
# This console is configured for general-purpose printing. It enables soft wrap
192-
# and disables Rich's automatic detection for markup, emoji, and highlighting.
193-
# These defaults can be overridden in calls to the console's or cmd2's print methods.
194197
super().__init__(
195198
file=file,
196199
soft_wrap=True,
@@ -203,35 +206,55 @@ def __init__(self, file: IO[str] | None = None) -> None:
203206
class Cmd2RichArgparseConsole(Cmd2BaseConsole):
204207
"""Rich console for rich-argparse output.
205208
206-
This class ensures long lines in help text are not truncated by avoiding soft_wrap,
209+
Ensures long lines in help text are not truncated by disabling soft_wrap,
207210
which conflicts with rich-argparse's explicit no_wrap and overflow settings.
211+
212+
Since this console is used to print error messages which may not be intended
213+
for Rich formatting, it disables Rich's automatic detection for markup, emoji,
214+
and highlighting. Because rich-argparse does markup and highlighting without
215+
involving the console, disabling these settings does not affect the library's
216+
internal functionality.
208217
"""
209218

210-
def __init__(self, file: IO[str] | None = None) -> None:
219+
def __init__(self, *, file: IO[str] | None = None) -> None:
211220
"""Cmd2RichArgparseConsole initializer.
212221
213222
:param file: optional file object where the console should write to.
214223
Defaults to sys.stdout.
215224
"""
216-
# Since this console is used to print error messages which may not have
217-
# been pre-formatted by rich-argparse, disable Rich's automatic detection
218-
# for markup, emoji, and highlighting. rich-argparse does markup and
219-
# highlighting without involving the console so these won't affect its
220-
# internal functionality.
221225
super().__init__(
222226
file=file,
227+
soft_wrap=False,
223228
markup=False,
224229
emoji=False,
225230
highlight=False,
226231
)
227232

228233

229234
class Cmd2ExceptionConsole(Cmd2BaseConsole):
230-
"""Rich console for printing exceptions.
235+
"""Rich console for printing exceptions and Rich Tracebacks.
231236
232-
Ensures that long exception messages word wrap for readability by keeping soft_wrap disabled.
237+
Ensures that output is always word-wrapped for readability and disables
238+
Rich's automatic detection for markup, emoji, and highlighting to prevent
239+
interference with raw error data.
233240
"""
234241

242+
def __init__(self, *, file: IO[str] | None = None) -> None:
243+
"""Cmd2ExceptionConsole initializer.
244+
245+
:param file: optional file object where the console should write to.
246+
If None, output defaults to sys.stderr.
247+
"""
248+
# Use stderr=True so that Rich defaults to sys.stderr if file is None.
249+
super().__init__(
250+
file=file,
251+
stderr=True,
252+
soft_wrap=False,
253+
markup=False,
254+
emoji=False,
255+
highlight=False,
256+
)
257+
235258

236259
def console_width() -> int:
237260
"""Return the width of the console."""

0 commit comments

Comments
 (0)