My Python program accepts command line arguments using the argparse module and it works as intended, however, the help text is a bit misleading and I would like to fix it for others using my program.
Currently, I have a positional argument which is a directory and an optional argument, -p
, which accepts an arbitrary number of package names using the nargs=+
argument to the add_argument
function. The DIR positional argument needs to be specified before the optional argument list, otherwise the directory will be wrongly added to the list of package names and it will error out saying that a positional argument was not specified. The help output currently looks like the following:
package_info.py --help
usage: package_info.py [-h] [-v] [--no-cache] [-g FILE] [-p [PKG [PKG ...]]]
DIR
Get information on packages in ros workspace.
positional arguments:
DIR The directory containing rospackages.
optional arguments:
-h, --help show this help message and exit
-v, --verbose Enables verbose mode
--no-cache Do not cache the dependency graph
-g FILE, --graph-file FILE
The graph file to load from or save to.
-p [PKG [PKG ...]], --packages [PKG [PKG ...]]
The packages to provide information on.
I would like it to be formatted with DIR showing up before the -p flag to be more clear to users that this argument must be specified first, like the following:
package_info.py --help
usage: package_info.py DIR [-h] [-v] [--no-cache] [-g FILE] [-p [PKG [PKG ...]]]
.
.
.
or
package_info.py --help
usage: package_info.py DIR
[-h] [-v] [--no-cache] [-g FILE] [-p [PKG [PKG ...]]]
.
.
.
Is there a simple way to format the help message, or will I need to write a custom help message formatter?