Currently I'm creating a directory reader program using Python. I'm using 'argparse' to parse the arguments from command line. I have the following code:
parser = argparse.ArgumentParser(prog = "LS.py",
usage = "%(prog)s [options] [path1 [path2 [...pathN]]]\nThe paths are optional; if not given . is used.")
group = parser.add_argument_group("Options")
group.add_argument("-path", default = ".", help = argparse.SUPPRESS, metavar = "")
group.add_argument("-m", "--modified", default = False,
help = "show last modified date/time [default: off]",
metavar = "")
group.add_argument("-o ORDER", "--order=ORDER", nargs = 2, default = "name",
help = "order by ('name', 'n', 'modified', 'm', 'size', 's')\n[default: name]",
metavar = "")
group.add_argument("-r", "--recursive", default = False,
help = "recurse into subdirectories [default: off]",
metavar = "")
group.add_argument("-s", "--sizes", default = False,
help = "show sizes [default: off]", metavar = "")
args = parser.parse_args()
return args
When called in the following manner "LS.py -h" it produces the following output:
usage: LS.py [options] [path1 [path2 [...pathN]]]
The paths are optional; if not given . is used.
optional arguments:
-h, --help show this help message and exit
Options:
-m , --modified show last modified date/time [default: off]
-o ORDER , --order=ORDER
order by ('name', 'n', 'modified', 'm', 'size', 's')
[default: name]
-r , --recursive recurse into subdirectories [default: off]
-s , --sizes show sizes [default: off]
My question: Is there a way to move the default help argument into a group such as Options? Also, I can't seem to find a way to remove the space before the commas in the Options arguments. The ideal output is:
Usage: ls.py [options] [path1 [path2 [...pathN]]]
The paths are optional; if not given . is used.
Options:
-h, --help show this help message and exit
-m, --modified show last modified date/time [default: off]
-o ORDER, --order=ORDER
order by ('name', 'n', 'modified', 'm', 'size', 's')
[default: name]
-r, --recursive recurse into subdirectories [default: off]
-s, --sizes show sizes [default: off]