Skip to content

Commit 0492c6d

Browse files
committed
More code cleanup.
1 parent 653a767 commit 0492c6d

File tree

1 file changed

+60
-56
lines changed

1 file changed

+60
-56
lines changed

cmd2/cmd2.py

Lines changed: 60 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -4170,72 +4170,76 @@ def print_topics(self, header: str, cmds: list[str] | None, cmdlen: int, maxcol:
41704170
:param cmdlen: unused, even by cmd's version
41714171
:param maxcol: max number of display columns to fit into
41724172
"""
4173-
if cmds:
4174-
header_grid = Table.grid()
4175-
header_grid.add_row(header, style=Cmd2Style.HELP_HEADER)
4176-
if self.ruler:
4177-
header_grid.add_row(Rule(characters=self.ruler))
4178-
self.poutput(header_grid)
4179-
self.columnize(cmds, maxcol - 1)
4180-
self.poutput()
4173+
if not cmds:
4174+
return
4175+
4176+
header_grid = Table.grid()
4177+
header_grid.add_row(header, style=Cmd2Style.HELP_HEADER)
4178+
if self.ruler:
4179+
header_grid.add_row(Rule(characters=self.ruler))
4180+
self.poutput(header_grid)
4181+
self.columnize(cmds, maxcol - 1)
4182+
self.poutput()
41814183

41824184
def _print_documented_command_topics(self, header: str, cmds: list[str], verbose: bool) -> None:
41834185
"""Print topics which are documented commands, switching between verbose or traditional output."""
41844186
import io
41854187

4186-
if cmds:
4187-
if not verbose:
4188-
self.print_topics(header, cmds, 15, 80)
4189-
else:
4190-
category_grid = Table.grid()
4191-
category_grid.add_row(header, style=Cmd2Style.HELP_HEADER)
4192-
category_grid.add_row(Rule(characters=self.ruler))
4193-
topics_table = Table(
4194-
Column("Name", no_wrap=True),
4195-
Column("Description", overflow="fold"),
4196-
box=SIMPLE_HEAD,
4197-
border_style=Cmd2Style.RULE_LINE,
4198-
show_edge=False,
4199-
)
4188+
if not cmds:
4189+
return
42004190

4201-
# Try to get the documentation string for each command
4202-
topics = self.get_help_topics()
4203-
for command in cmds:
4204-
if (cmd_func := self.cmd_func(command)) is None:
4205-
continue
4206-
4207-
doc: str | None
4208-
4209-
# Non-argparse commands can have help_functions for their documentation
4210-
if command in topics:
4211-
help_func = getattr(self, constants.HELP_FUNC_PREFIX + command)
4212-
result = io.StringIO()
4213-
4214-
# try to redirect system stdout
4215-
with contextlib.redirect_stdout(result):
4216-
# save our internal stdout
4217-
stdout_orig = self.stdout
4218-
try:
4219-
# redirect our internal stdout
4220-
self.stdout = cast(TextIO, result)
4221-
help_func()
4222-
finally:
4223-
with self.sigint_protection:
4224-
# restore internal stdout
4225-
self.stdout = stdout_orig
4226-
doc = result.getvalue()
4191+
if not verbose:
4192+
self.print_topics(header, cmds, 15, 80)
4193+
else:
4194+
category_grid = Table.grid()
4195+
category_grid.add_row(header, style=Cmd2Style.HELP_HEADER)
4196+
category_grid.add_row(Rule(characters=self.ruler))
4197+
topics_table = Table(
4198+
Column("Name", no_wrap=True),
4199+
Column("Description", overflow="fold"),
4200+
box=SIMPLE_HEAD,
4201+
border_style=Cmd2Style.RULE_LINE,
4202+
show_edge=False,
4203+
)
42274204

4228-
else:
4229-
doc = cmd_func.__doc__
4205+
# Try to get the documentation string for each command
4206+
topics = self.get_help_topics()
4207+
for command in cmds:
4208+
if (cmd_func := self.cmd_func(command)) is None:
4209+
continue
4210+
4211+
doc: str | None
4212+
4213+
# Non-argparse commands can have help_functions for their documentation
4214+
if command in topics:
4215+
help_func = getattr(self, constants.HELP_FUNC_PREFIX + command)
4216+
result = io.StringIO()
4217+
4218+
# try to redirect system stdout
4219+
with contextlib.redirect_stdout(result):
4220+
# save our internal stdout
4221+
stdout_orig = self.stdout
4222+
try:
4223+
# redirect our internal stdout
4224+
self.stdout = cast(TextIO, result)
4225+
help_func()
4226+
finally:
4227+
with self.sigint_protection:
4228+
# restore internal stdout
4229+
self.stdout = stdout_orig
4230+
doc = result.getvalue()
4231+
4232+
else:
4233+
doc = cmd_func.__doc__
42304234

4231-
# Attempt to locate the first documentation block
4232-
cmd_desc = strip_doc_annotations(doc) if doc else ''
4235+
# Attempt to locate the first documentation block
4236+
cmd_desc = strip_doc_annotations(doc) if doc else ''
42334237

4234-
# Add this command to the table
4235-
topics_table.add_row(command, cmd_desc)
4238+
# Add this command to the table
4239+
topics_table.add_row(command, cmd_desc)
42364240

4237-
category_grid.add_row(topics_table)
4238-
self.poutput(category_grid, "")
4241+
category_grid.add_row(topics_table)
4242+
self.poutput(category_grid, "")
42394243

42404244
def columnize(self, str_list: list[str] | None, display_width: int = 80) -> None:
42414245
"""Display a list of single-line strings as a compact set of columns.

0 commit comments

Comments
 (0)