I would like to get the dest
values for all the arguments defined for the parser. I.e. in the case of:
parser = argparse.ArgumentParser()
parser.add_argument('arg1')
parser.add_argument('arg2')
parser.add_argument('arg3')
parser.add_argument('arg4')
I would like to return ['arg1', 'arg2', 'arg3', 'arg4']
.
parser.parse_args()
takes stuff from sys.argv which is not what I'm looking for. How can I achieve this?
Though you don't typically need it, add_argument
returns an instance of the Action
subclass used to implement the argument. Among other things, these objects have a dest
attribute containing the string you want.
For example,
import argparse
parser = argparse.ArgumentParser()
a1 = parser.add_argument('arg1')
a2 = parser.add_argument('arg2')
a3 = parser.add_argument('arg3')
a4 = parser.add_argument('arg4')
destinations = [x.dest for x in [a1, a2, a3, a4]])
parser._actions
contains a list of all arguments defined for the parser, including things like --help
, so that may also be of use.