1
1
# coding=utf-8
2
2
# flake8: noqa F821
3
3
"""
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.
5
5
This is meant to be run within a cmd2 session using pyscript.
6
6
"""
7
7
@@ -36,12 +36,22 @@ def get_sub_commands(parser: argparse.ArgumentParser) -> List[str]:
36
36
return sub_cmds
37
37
38
38
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 = '{}\n COMMAND: {}\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 )
42
52
outfile .write (header )
43
53
44
- result = app ('help {}' .format (command ))
54
+ result = app ('help {}' .format (item ))
45
55
outfile .write (result .stdout )
46
56
47
57
@@ -66,17 +76,28 @@ def main() -> None:
66
76
print ("Error opening {} because: {}" .format (outfile_path , e ))
67
77
return
68
78
79
+ # Write the help summary
80
+ header = '{0}\n SUMMARY\n {0}\n ' .format (ASTERISKS )
81
+ outfile .write (header )
82
+
83
+ result = app ('help -v' )
84
+ outfile .write (result .stdout )
85
+
69
86
# 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 )
71
90
to_save .sort ()
72
91
73
92
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 )
80
101
81
102
outfile .close ()
82
103
print ("Output written to {}" .format (outfile_path ))
0 commit comments