Consider following MCVE:
import sys
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-t", "--test")
parser.add_argument("-o", "--other")
assert sys.argv == ['mcve.py', '-o', '-t 123']
parsed = parser.parse_args()
assert parsed.other == "-t 123"
And its' invocation:
lrogalsk-mac01:src lrogalsk$ python mcve.py -o "-t 123"
usage: mcve.py [-h] [-t TEST] [-o OTHER]
mcve.py: error: argument -o/--other: expected one argument
As you can see, values in shell are interpreted correctly and passed to Python process as separate command line arguments. Nevertheless, argparse
parsing mechanism still rejects this value (likely because it begins with match of another parameter, -t
).
Changing invocation to space-prepended value (see below) and adjusting assertions in MCVE script makes it work, but this for me is merely workaround and not an actual solution.
lrogalsk-mac01:src lrogalsk$ python mcve.py -o " -t 123"
Is there any known way of fixing this behaviour in argparse?
Set-up details:
lrogalsk-mac01:src lrogalsk$ python --version
Python 2.7.10