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

Function get_sub_commands

examples/scripts/save_help_text.py:14–37  ·  view source on GitHub ↗

Get a list of subcommands for a Cmd2ArgumentParser.

(parser: Cmd2ArgumentParser)

Source from the content-addressed store, hash-verified

12
13
14def 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 # No subcommands
20 return []
21
22 # Prevent redundant traversal of parser aliases
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 # Look for nested subcommands
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
40def add_help_to_file(item: str, outfile: TextIO, is_command: bool) -> None:

Callers 1

mainFunction · 0.85

Calls 3

get_subparsers_actionMethod · 0.80
addMethod · 0.80
appendMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…