Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterating over accepted args of argparse

Tags:

python

I cant see m to figure out how to iterate over the accepted args of argparse. I get I can iterate over the parsed_args result, but what I want is to iterate over the arguments the parser is configured with ( ie with optparse you can iterate over the args ).

for example:

parser = argparse.ArgumentParser( prog = 'myapp' )
parser.add_argument( '--a',  .. )
parser.add_argument( '--b',  ...) 
parser.add_argument( '--c',  ... )

for arg in parser.args():
    print arg

would result in

--a
--b
--c
like image 596
ByteMe95 Avatar asked Jan 31 '26 10:01

ByteMe95


1 Answers

You'll probably want to getattr from the args:

args = parser.parse_args()
for arg in vars(args):
     print arg, getattr(args, arg)

Result:

a None
c None
b None
like image 191
l'L'l Avatar answered Feb 03 '26 00:02

l'L'l



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!