Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call parse_args() twice on the same set of arguments in Python's argparse?

Simplified test case: I have a script that takes two arguments. The first is a list of integers. The second is a single integer that must be contained in the first argument's set of integers.

For example:

$ python argtest.py --valid_nums 1 2 3 --num 2

Should work, but:

$ python argtest.py --valid_nums 1 2 3 --num 4

Should not work, since num is not in valid_nums. However, I have had some difficulty (read: I suspect it is more trouble than it's worth, but I really want it to work) implementing this functionality.

Implementation attempt:

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--valid_nums',
                    type=int,
                    nargs='+')

args = parser.parse_args()
print "Numbers: ", args.valid_nums


parser.add_argument('--num',
                    type=int,
                    choices=args.valid_nums)

args = parser.parse_args()
print args

Actual output:

$ python argtesttest.py --valid_nums 1 2 3 --num 2
usage: argtesttest.py [-h] [--valid_nums VALID_NUMS [VALID_NUMS ...]]
argtesttest.py: error: unrecognized arguments: --num 2

Desired output:

$ python argtesttest.py --valid_nums 1 2 3 --num 2
Namespace(num=2, valid_nums=[1, 2, 3])

Now, (I think) the issue here is that I cannot add a new argument to the parser after I have called parse_args(), which yields the error about an unrecognized argument, but I can't think of a way around it. Is there any way to break up the processing of arguments such that it isn't all at once?

Obviously this would be pretty trivial to do if I only called parse_args() once and handled the container membership checking myself, but I'd like to get it working 'natively' using argparse's built-in error-checking.

Any ideas?

like image 815
Thomas Avatar asked Nov 16 '25 05:11

Thomas


1 Answers

you need to use parser.parse_known_args() as opposed to parser.parse_args(), or add all the arguments to parser before calling parse_args().

parse_args() needs to understand all the current arguments in sys.argv, which already contains --num at the time of first parsing, hence the exception.

like image 91
Bubai Avatar answered Nov 18 '25 20:11

Bubai



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!