Can’t get argparse to read quoted string with dashes in it?

Updated answer:

You can put an equals sign when you call it:

python Application.py -env="-env"

Original answer:

I too have had troubles doing what you are trying to do, but there is a workaround build into argparse, which is the parse_known_args method. This will let all arguments that you haven’t defined pass through the parser with the assumption that you would use them for a subprocess. The drawbacks are that you won’t get error reporting with bad arguments, and you will have to make sure that there is no collision between your options and your subprocess’s options.

Another option could be to force the user’s to use a plus instead of a minus:

python Application.py -e "+s WHATEVER +e COOL STUFF"

and then you change the ‘+’ to ‘-‘ in post processing before passing to your subprocess.

Leave a Comment