I'm using argparse in my Python script:
import argparse
argParser = argparse.ArgumentParser(description="Testing argparse.")
argParser.add_argument(
"--use-something",
action="store_true",
help="use something in addition (default: %(default)s)"
)
cliArgs = argParser.parse_args()
print(cliArgs)
So I would expect the script to accept only --use-something
option, but actually it accepts any incomplete variant of it:
$ python --version
Python 3.9.6
$ python ./testing-argparse.py --help
usage: testing-argparse.py [-h] [--use-something]
Testing argparse.
optional arguments:
-h, --help show this help message and exit
--use-something use something in addition (default: False)
$ python ./testing-argparse.py
Namespace(use_something=False)
$ python ./testing-argparse.py --use-something
Namespace(use_something=True)
$ python ./testing-argparse.py --use-some
Namespace(use_something=True)
$ python ./testing-argparse.py --use
Namespace(use_something=True)
$ python ./testing-argparse.py --u
Namespace(use_something=True)
$ python ./testing-argparse.py -u
usage: testing-argparse.py [-h] [--use-something]
testing-argparse.py: error: unrecognized arguments: -u
$ python ./testing-argparse.py --use-somethings
usage: testing-argparse.py [-h] [--use-something]
testing-argparse.py: error: unrecognized arguments: --use-somethings
Not sure, if it is a bug, or am I missing some configuration option for a "more strict" parsing?