I am using the argparse package here.
There are 4 possible command line arguments in this code. I need to choose any combination of only 2 of them, for example "python script.py -arg1 int1 int2 int3 -arg4 int1 int2 int3" and have those int values assigned to variables in for loops(see below).
How can I make it so that it doesn't matter which of the 4 command line arguments are input, and their int values get assigned to one of the two for loops? It doesn't matter which for loop they get into, as long as all combinations are possible. Does this even make sense? Sorry if it doesn't
import numpy as np
import argparse
parser = argparse.ArgumentParser(description = 'Test')
parser.add_argument('-arg1', nargs =3, required = False, type = int)
parser.add_argument('-arg2', nargs = 3, required = False, type = int)
parser.add_argument('-arg3', nargs = 3, required = False, type = int)
parser.add_argument('-arg4', nargs = 3, required = False, type = int)
args = parser.parse_args()
if arg1:
args.arg1[0] = #start1 or start2
args.arg1[1] = #stop1 or stop2
args.arg1[2] = #num_samples1 or numsamples2
if arg2:
args.arg2[0] = #start1 or start2
args.arg2[1] = #stop1 or stop2
args.arg2[2] = #num_samples1 or numsamples2
if arg3:
args.arg3[0] = #start1 or start2
args.arg3[1] = #stop1 or stop2
args.arg3[2] = #num_samples1 or numsamples2
if arg4:
args.arg4[0] = #start1 or start2
args.arg4[1] = #stop1 or stop2
args.arg4[2] = #num_samples1 or numsamples2
for a in np.linspace(start1, stop1, num_samples1):
for b in np.linspace(start2,stop2,num_samples2):
#do something with these values
First get your two args by iterating over the four possibilities and selecting those that aren't None:
two_args = [a for a in (args.arg1, args.arg2, args.arg3, args.arg4) if a]
if len(two_args) != 2:
print("Exactly two of arg1, arg2, arg3, and/or arg4 must be provided")
exit()
Then you can get your six values like this:
(start1, stop1, num_samples1), (start2, stop2, num_samples2) = two_args
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.