1

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!

AS_Butler
  • 281
  • 2
  • 12
  • 2
    possible duplicate of [this question](https://stackoverflow.com/questions/16174992/cant-get-argparse-to-read-quoted-string-with-dashes-in-it) – Jan Stránský Aug 27 '20 at 23:05
  • 1
    Does this answer your question? [Can't get argparse to read quoted string with dashes in it?](https://stackoverflow.com/questions/16174992/cant-get-argparse-to-read-quoted-string-with-dashes-in-it) Note that there are multiple different approaches – wjandrea Aug 27 '20 at 23:06

0 Answers0