| 1 |
"""A cmd2 script that saves the help text for every command, subcommand, and topic to a file. |
| 2 |
This is meant to be run within a cmd2 session using run_pyscript. |
| 3 |
""" |
| 4 |
|
| 5 |
import os |
| 6 |
import sys |
| 7 |
from typing import TextIO |
| 8 |
|
| 9 |
from cmd2 import Cmd2ArgumentParser |
| 10 |
|
| 11 |
ASTERISKS = "********************************************************" |
| 12 |
|
| 13 |
|
| 14 |
def get_sub_commands(parser: Cmd2ArgumentParser) -> list[str]: |
| 15 |
"""Get a list of subcommands for a Cmd2ArgumentParser.""" |
| 16 |
try: |
| 17 |
subparsers_action = parser.get_subparsers_action() |
| 18 |
except ValueError: |
| 19 |
|
| 20 |
return [] |
| 21 |
|
| 22 |
|
| 23 |
checked_parsers: set[Cmd2ArgumentParser] = set() |
| 24 |
|
| 25 |
sub_cmds = [] |
| 26 |
for subcmd, subcmd_parser in subparsers_action.choices.items(): |
| 27 |
if subcmd_parser in checked_parsers: |
| 28 |
continue |
| 29 |
checked_parsers.add(subcmd_parser) |
| 30 |
|
| 31 |
sub_cmds.append(subcmd) |
| 32 |
|
| 33 |
|
| 34 |
sub_cmds.extend(f"{subcmd} {nested_subcmd}" for nested_subcmd in get_sub_commands(subcmd_parser)) |
| 35 |
|
| 36 |
sub_cmds.sort() |
| 37 |
return sub_cmds |
| 38 |
|
| 39 |
|
| 40 |
def add_help_to_file(item: str, outfile: TextIO, is_command: bool) -> None: |
| 41 |
"""Write help text for commands and topics to the output file. |
| 42 |
|
| 43 |
:param item: what is having its help text saved |
| 44 |
:param outfile: file being written to |
| 45 |
:param is_command: tells if the item is a command and not just a help topic. |
| 46 |
""" |
| 47 |
label = "COMMAND" if is_command else "TOPIC" |
| 48 |
|
| 49 |
header = f"{ASTERISKS}\n{label}: {item}\n{ASTERISKS}\n" |
| 50 |
outfile.write(header) |
| 51 |
|
| 52 |
result = app(f"help {item}") |
| 53 |
outfile.write(result.stdout) |
| 54 |
|
| 55 |
|
| 56 |
def main() -> None: |
| 57 |
"""Main function of this script.""" |
| 58 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 106 |
if __name__ == "__main__": |
| 107 |
main() |