Skip to content

Commit 542fa15

Browse files
authored
Merge pull request #615 from python-cmd2/help_script_updates
Added help summary and a separate label for commands and topics
2 parents f3c07a0 + c62a0ac commit 542fa15

File tree

1 file changed

+33
-12
lines changed

1 file changed

+33
-12
lines changed

examples/scripts/save_help_text.py

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# coding=utf-8
22
# flake8: noqa F821
33
"""
4-
A cmd2 script that saves the help text for every command and sub-command to a file.
4+
A cmd2 script that saves the help text for every command, sub-command, and topic to a file.
55
This is meant to be run within a cmd2 session using pyscript.
66
"""
77

@@ -36,12 +36,22 @@ def get_sub_commands(parser: argparse.ArgumentParser) -> List[str]:
3636
return sub_cmds
3737

3838

39-
def add_help_to_file(command: str, outfile: TextIO) -> None:
40-
"""Write a header and help text for a command to the output file"""
41-
header = '{}\nCOMMAND: {}\n{}\n'.format(ASTERISKS, command, ASTERISKS)
39+
def add_help_to_file(item: str, outfile: TextIO, is_command: bool) -> None:
40+
"""
41+
Write help text for commands and topics to the output file
42+
:param item: what is having its help text saved
43+
:param outfile: file being written to
44+
:param is_command: tells if the item is a command and not just a help topic
45+
"""
46+
if is_command:
47+
label = "COMMAND"
48+
else:
49+
label = "TOPIC"
50+
51+
header = '{}\n{}: {}\n{}\n'.format(ASTERISKS, label, item, ASTERISKS)
4252
outfile.write(header)
4353

44-
result = app('help {}'.format(command))
54+
result = app('help {}'.format(item))
4555
outfile.write(result.stdout)
4656

4757

@@ -66,17 +76,28 @@ def main() -> None:
6676
print("Error opening {} because: {}".format(outfile_path, e))
6777
return
6878

79+
# Write the help summary
80+
header = '{0}\nSUMMARY\n{0}\n'.format(ASTERISKS)
81+
outfile.write(header)
82+
83+
result = app('help -v')
84+
outfile.write(result.stdout)
85+
6986
# Get a list of all commands and help topics and then filter out duplicates
70-
to_save = list(set(self.get_all_commands()) | set(self.get_help_topics()))
87+
all_commands = set(self.get_all_commands())
88+
all_topics = set(self.get_help_topics())
89+
to_save = list(all_commands | all_topics)
7190
to_save.sort()
7291

7392
for item in to_save:
74-
add_help_to_file(item, outfile)
75-
76-
# Add any sub-commands
77-
for subcmd in get_sub_commands(getattr(self.cmd_func(item), 'argparser', None)):
78-
full_cmd = '{} {}'.format(item, subcmd)
79-
add_help_to_file(full_cmd, outfile)
93+
is_command = item in all_commands
94+
add_help_to_file(item, outfile, is_command)
95+
96+
if is_command:
97+
# Add any sub-commands
98+
for subcmd in get_sub_commands(getattr(self.cmd_func(item), 'argparser', None)):
99+
full_cmd = '{} {}'.format(item, subcmd)
100+
add_help_to_file(full_cmd, outfile, is_command)
80101

81102
outfile.close()
82103
print("Output written to {}".format(outfile_path))

0 commit comments

Comments
 (0)