Get a list of subcommands for a Cmd2ArgumentParser.
(parser: Cmd2ArgumentParser)
| 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 | # 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 | |
| 40 | def add_help_to_file(item: str, outfile: TextIO, is_command: bool) -> None: |
no test coverage detected
searching dependent graphs…