I find myself in the (unenviable) position of needing to pass a collection of arguments as a single (comma separated) argument to a script, like so:
python example.py --extra_args '--argument1,value1,--argument2,--value2'
It seems that argparse
doesn't like values with leading '--'.
Minimal Working Example
For example, define example.py
as follows
# example.py
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--extra_args', type=str, default=None)
args = parser.parse_args()
print(args.extra_args)
and run the command above and you get the error:
python example.py --extra_args '--argument1,value1,--argument2,--value2'
# usage: example.py [-h] [--extra_args EXTRA_ARGS]
# example.py: error: argument --extra_args: expected one argument
Looking to improve on my current workaround, which is to use a different prefix other than '--'
.
Answered
This related question contained the answer I was looking for:
python example.py --extra_args=--argument1,value1,--argument2,--value2
works!