@@ -39,6 +39,7 @@ def get_sub_commands(parser: Cmd2ArgumentParser) -> list[str]:
3939
4040def add_help_to_file (item : str , outfile : TextIO , is_command : bool ) -> None :
4141 """Write help text for commands and topics to the output file
42+
4243 :param item: what is having its help text saved
4344 :param outfile: file being written to
4445 :param is_command: tells if the item is a command and not just a help topic.
@@ -64,47 +65,43 @@ def main() -> None:
6465 print (f"Usage: { os .path .basename (sys .argv [0 ])} <output_file>" )
6566 return
6667
67- # Open the output file
6868 outfile_path = os .path .expanduser (sys .argv [1 ])
6969 try :
70- outfile = open (outfile_path , 'w' ) # noqa: SIM115
71- except OSError as e :
72- print ( f"Error opening { outfile_path } because: { e } " )
73- return
70+ with open (outfile_path , 'w' ) as outfile :
71+ # Write the help summary
72+ header = f' { ASTERISKS } \n SUMMARY \n { ASTERISKS } \n '
73+ outfile . write ( header )
7474
75- # Write the help summary
76- header = f'{ ASTERISKS } \n SUMMARY\n { ASTERISKS } \n '
77- outfile .write (header )
75+ result = app ('help -v' )
76+ outfile .write (result .stdout )
7877
79- result = app ('help -v' )
80- outfile .write (result .stdout )
78+ # Get a list of all commands and help topics and then filter out duplicates
79+ all_commands = set (self .get_all_commands ())
80+ all_topics = set (self .get_help_topics ())
81+ to_save = sorted (all_commands | all_topics )
8182
82- # Get a list of all commands and help topics and then filter out duplicates
83- all_commands = set (self .get_all_commands ())
84- all_topics = set (self .get_help_topics ())
85- to_save = list (all_commands | all_topics )
86- to_save .sort ()
83+ for item in to_save :
84+ is_command = item in all_commands
85+ add_help_to_file (item , outfile , is_command )
8786
88- for item in to_save :
89- is_command = item in all_commands
90- add_help_to_file (item , outfile , is_command )
87+ if not is_command :
88+ continue
9189
92- if not is_command :
93- continue
90+ cmd_func = self .cmd_func (item )
91+ parser = self ._command_parsers .get (cmd_func )
92+ if parser is None :
93+ continue
9494
95- cmd_func = self . cmd_func ( item )
96- parser = self . _command_parsers . get ( cmd_func )
97- if parser is None :
98- continue
95+ # Add any subcommands
96+ for subcmd in get_sub_commands ( parser ):
97+ full_cmd = f' { item } { subcmd } '
98+ add_help_to_file ( full_cmd , outfile , is_command )
9999
100- # Add any subcommands
101- for subcmd in get_sub_commands (parser ):
102- full_cmd = f'{ item } { subcmd } '
103- add_help_to_file (full_cmd , outfile , is_command )
100+ print (f"Output written to { outfile_path } " )
104101
105- outfile . close ()
106- print (f"Output written to { outfile_path } " )
102+ except OSError as ex :
103+ print (f"Error handling { outfile_path } because: { ex } " )
107104
108105
109- # Run main function
110- main ()
106+ if __name__ == "__main__" :
107+ main ()
0 commit comments