88import argparse
99import os
1010import sys
11- import tempfile
1211from typing import List , TextIO
1312
1413ASTERISKS = "********************************************************"
@@ -33,20 +32,22 @@ def get_sub_commands(parser: argparse.ArgumentParser) -> List[str]:
3332
3433 break
3534
35+ sub_cmds .sort ()
3636 return sub_cmds
3737
3838
39- def add_command_to_script (command : str , temp_file : TextIO , outfile_path : str ) -> None :
39+ def add_help_to_file (command : str , outfile : TextIO ) -> None :
4040 """
41- Write to the script the commands necessary to write both a
42- header and help text for a command to the output file
41+ Write a header and help text for a command to the output file
4342 """
44- header = '{}\\ nCOMMAND: {}\\ n{}\\ n' .format (ASTERISKS , command , ASTERISKS )
45- temp_file .write ('py print("{}") >> {}\n ' .format (header , outfile_path ))
46- temp_file .write ('help {} >> {}\n ' .format (command , outfile_path ))
43+ header = '{}\n COMMAND: {}\n {}\n ' .format (ASTERISKS , command , ASTERISKS )
44+ outfile .write (header )
4745
46+ result = app ('help {}' .format (command ))
47+ outfile .write (result .stdout )
4848
49- def main ():
49+
50+ def main () -> None :
5051 """Main function of this script"""
5152
5253 # Make sure we have access to self
@@ -59,34 +60,28 @@ def main():
5960 print ("Usage: {} <output_file>" .format (os .path .basename (sys .argv [0 ])))
6061 return
6162
62- # Get output file path
63- outfile = os .path .expanduser (sys .argv [1 ])
64-
65- # Create a script that will call help on each command and topic and write its output to a file
66- with tempfile .NamedTemporaryFile (mode = 'w+t' , delete = False ) as temp_file :
67-
68- # First delete any existing output file
69- temp_file .write ('py if os.path.exists("{0}"): os.remove("{0}")\n ' .format (outfile ))
70-
71- # Get a list of all commands and help topics and then filter out duplicates
72- to_save = set (self .get_all_commands ()) | set (self .get_help_topics ())
73-
74- for item in to_save :
75- add_command_to_script (item , temp_file , outfile )
63+ # Open the output file
64+ outfile_path = os .path .expanduser (sys .argv [1 ])
65+ try :
66+ outfile = open (outfile_path , 'w' )
67+ except OSError as e :
68+ print ("Error opening {} because: {}" .format (outfile_path , e ))
69+ return
7670
77- # Add any sub-commands
78- for subcmd in get_sub_commands (getattr (self .cmd_func (item ), 'argparser' , None )):
79- full_cmd = '{} {}' .format (item , subcmd )
80- add_command_to_script (full_cmd , temp_file , outfile )
71+ # Get a list of all commands and help topics and then filter out duplicates
72+ to_save = list (set (self .get_all_commands ()) | set (self .get_help_topics ()))
73+ to_save .sort ()
8174
82- # Inform the user where output was written
83- temp_file . write ( 'py print("Output written to {}") \n ' . format ( outfile ) )
75+ for item in to_save :
76+ add_help_to_file ( item , outfile )
8477
85- # Have the script delete itself as its last step
86- temp_file .write ('py os.remove("{}")\n ' .format (temp_file .name ))
78+ # Add any sub-commands
79+ for subcmd in get_sub_commands (getattr (self .cmd_func (item ), 'argparser' , None )):
80+ full_cmd = '{} {}' .format (item , subcmd )
81+ add_help_to_file (full_cmd , outfile )
8782
88- # Tell cmd2 to run the script as its next command
89- self . cmdqueue . insert ( 0 , 'load " {}"' .format (temp_file . name ))
83+ outfile . close ()
84+ print ( "Output written to {}" .format (outfile_path ))
9085
9186
9287# Run main function
0 commit comments