Display subgroups of commands with the argparse subparameter
I am currently developing a Python program that contains ~ 40 helper commands. The parser is done using argparse. As the number of sub-commands increases, it becomes difficult to find the command of interest. It currently looks like below.
$ pgrm -h
Usage: pgrm [-h] [-v] ...
Blabla bla.
Main command arguments:
-h, --help show this help message and exit
-v, --version show program version number and exit
Available sub-commands:
sub_cmd Some description about the commandβ¦
sub_cmd Some description about the commandβ¦
sub_cmd Some description about the commandβ¦
sub_cmd Some description about the commandβ¦
sub_cmd Some description about the commandβ¦
sub_cmd Some description about the commandβ¦
sub_cmd Some description about the commandβ¦
sub_cmd Some description about the commandβ¦
sub_cmd Some description about the commandβ¦
sub_cmd Some description about the commandβ¦
sub_cmd Some description about the commandβ¦
Usage example:
'pgrm sub-command -h' for more information.
I would like to change the display to show categories (like update / insert / select) and its associated subcommands.
$ pgrm -h
Usage: pgrm [-h] [-v] ...
Blabla bla.
Main command arguments:
-h, --help show this help message and exit
-v, --version show program version number and exit
Available sub-commands:
Updating
sub_cmd Some description about the commandβ¦
sub_cmd Some description about the commandβ¦
sub_cmd Some description about the commandβ¦
sub_cmd Some description about the commandβ¦
Selecting
sub_cmd Some description about the commandβ¦
sub_cmd Some description about the commandβ¦
sub_cmd Some description about the commandβ¦
Inserting
sub_cmd Some description about the commandβ¦
sub_cmd Some description about the commandβ¦
sub_cmd Some description about the commandβ¦
sub_cmd Some description about the commandβ¦
Is there any solution that can be used in argparser to implement such a CLI?
thank
I researched this question a few years ago
http://bugs.python.org/issue9341 allow grouping of argparse subcommands
If I read my suggested patch correctly, you just need to change the class _SubParsersAction
and no further changes to the classes HelpFormatter
or ArgumentParser
.