I'm writing a script to call a program with all possible command-line arguments. Some of these arguments can be not present. Attempt #1:
args1 = ['program.exe']
args2 = ['1', '5', '']
args3 = ['--foo', '']
for args in itertools.product(args1, args2, args3):
args = [arg for arg in args if arg != '']
subprocess.call(list(args))
Is there another way to get these permutations without having to manually filter out the empty string? Things break if I just leave it in the argument list. I mean, my approach works, but I feel like there would be some more built-in way to handle this. Although reading this question is making me think that's less likely.
If you want it to look a bit cleaner you could use the built-in filter() function.
args = filter(lambda x: not x, args)
Or if you don't like the lambda
args = filter(not_empty, args)
----
def not_empty(x):
return not x
I don't know of a way to do this with just itertools
functions.