MCPcopy Index your code
hub / github.com/python-cmd2/cmd2 / main

Function main

examples/scripts/save_help_text.py:56–103  ·  view source on GitHub ↗

Main function of this script.

()

Source from the content-addressed store, hash-verified

54
55
56def main() -> None:
57 """Main function of this script."""
58 # Make sure we have access to self
59 if "self" not in globals():
60 print("Re-run this script from a cmd2 application where self_in_py is True")
61 return
62
63 # Make sure the user passed in an output file
64 if len(sys.argv) != 2:
65 print(f"Usage: {os.path.basename(sys.argv[0])} <output_file>")
66 return
67
68 outfile_path = os.path.expanduser(sys.argv[1])
69 try:
70 with open(outfile_path, "w") as outfile:
71 # Write the help summary
72 header = f"{ASTERISKS}\nSUMMARY\n{ASTERISKS}\n"
73 outfile.write(header)
74
75 result = app("help -v")
76 outfile.write(result.stdout)
77
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)
82
83 for item in to_save:
84 is_command = item in all_commands
85 add_help_to_file(item, outfile, is_command)
86
87 if not is_command:
88 continue
89
90 cmd_func = self.get_command_func(item)
91 parser = self.command_parsers.get(cmd_func)
92 if parser is None:
93 continue
94
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)
99
100 print(f"Output written to {outfile_path}")
101
102 except OSError as ex:
103 print(f"Error handling {outfile_path} because: {ex}")
104
105
106if __name__ == "__main__":

Callers 1

save_help_text.pyFile · 0.70

Calls 8

appFunction · 0.85
add_help_to_fileFunction · 0.85
get_sub_commandsFunction · 0.85
get_help_topicsMethod · 0.80
get_command_funcMethod · 0.80
writeMethod · 0.45
get_all_commandsMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…