In [1]: import argparse
In [2]: parser = argparse.ArgumentParser()
...: parser.add_argument('arg1')
...: parser.add_argument('arg2')
...: parser.add_argument('arg3')
...: parser.add_argument('arg4')
Since these are all required positionals, we can call parse_args
with:
In [4]: parser.parse_args('1 2 3 4'.split())
Out[4]: Namespace(arg1='1', arg2='2', arg3='3', arg4='4')
The parser maintains a list of the actions (arguments) that you defined. Their dest
can be listed with:
In [5]: [a.dest for a in parser._actions]
Out[5]: ['help', 'arg1', 'arg2', 'arg3', 'arg4']
That list includes the 'help' that was added by default.
Another list, derived from the args
(converted to a dict)
In [6]: list(vars(parser.parse_args('1 2 3 4'.split())).keys())
Out[6]: ['arg1', 'arg2', 'arg3', 'arg4']
You could also collect your own list of actions (some companies don't like you to access the "private" attributes like '_actions', though Python has a loose concept of public v private variables).
In [9]: parser = argparse.ArgumentParser()
...: a1=parser.add_argument('arg1')
...: a2=parser.add_argument('arg2')
...: a3=parser.add_argument('arg3')
...: a4=parser.add_argument('arg4')
In [10]: [a.dest for a in [a1,a2,a3,a4]]
Out[10]: ['arg1', 'arg2', 'arg3', 'arg4']