it seems this question has been asked before very often, but i cant find the right answer anyway. so here it goes again...
example code: one optional arg, with args converted to dict.
import argparse
epilog = '''usage example:'''
parser = argparse.ArgumentParser(description="add watchit hosts", epilog=epilog)
parser.add_argument('--add', nargs='*', default="-1", metavar="key 'value'")
args = vars(parser.parse_args())
print args
print args['name']
python argparse_test.py --add name 'aixbuildhost' spits out the following:
{'add': ['name', 'aixbuildhost']}
Traceback (most recent call last):
File "argparse_test.py", line 9, in <module>
print args['name']
KeyError: 'name'
so the big question is, how to i get the "name'?
args['add'][1]argparseor almost any debugging with it, it's a good idea to look atargswithout further processing,print(args). That will show you the values AND their names.