I have a CLI argument parser and I'm trying to build the parts that adds the specifications of the arguments dynamically with a pre defined dictionary that contains those details. This is dictionary with the details:
paramsDetails = {
'inputpath': {
'-': 'i',
'--': '--inputpath',
'dest': 'userinputpath',
'type': 'str',
'metavar': 'PATH',
'help': 'REQUIRED, input path of base folder of the files in which to scan. ',
'required': True
},
'depth': {
'-': 'd',
'--': '--depth',
'dest': 'directorydepth',
'type': 'int',
'metavar': 'INT',
'help': 'depth to do down in path. ',
}
}
The "paramsDetails" dictionary details are retrieved from a external source out of my control and it might change without my knowledge at any time so I can't be manually hard coding it in my code. I need to be able to build the "parser.add_argument" call dynamically without knowing the content of "paramsDetails". I don't necessarily need to "parser.add_argument" for this buy DO need to use the "paramsDetails" dict.
This is what I got so far:
from argparse import ArgumentParser
parser = ArgumentParser(description='My test')
for currentParamDetails in paramsDetails:
parser.add_argument(currentParamDetails)
args = parser.parse_args()
the "parser.add_argument(currentParamDetails)" doesn't work. I get:
usage: tests.py [-h] -i PATH [-d INT] inputpath depth
tests.py: error: the following arguments are required: inputpath, depth
which means it all went wrong.
of course typing the arguments manually works as so:
from argparse import ArgumentParser
parser = ArgumentParser(description='My test')
parser.add_argument("-i", "--inputpath", dest="userinputpath", type=str, metavar="PATH", help="REQUIRED, input path of base folder of the files in which to scan. ", required=True)
parser.add_argument("-d", "--depth", dest="directorydepth", type=int, metavar="INT", default=2, help="depth to do down in path. ")
args = parser.parse_args()
but that's not what I want, I want to use the dict.
How can this be done?
The Python version I'm using is 3.6.
'-': 'i',key mean? Both "depth" and "inputpath" have it so there is going to be a collision. Also, is there a reason the'-': 'i',doesn't include the dashed, but'--': '--inputpath',does have them?