0

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?

retif
  • 1,495
  • 1
  • 22
  • 40
  • 1
    It seems your question duplicates this: https://stackoverflow.com/questions/33900846/disable-unique-prefix-matches-for-argparse-and-optparse/33900999#33900999 – mlabuda2 Aug 31 '21 at 13:08
  • 2
    Does this answer your question? [Disable unique prefix matches for argparse and optparse](https://stackoverflow.com/questions/33900846/disable-unique-prefix-matches-for-argparse-and-optparse) – mlabuda2 Aug 31 '21 at 13:09
  • Absolutely, that was it, should have searched for a similar question more carefully, thank you! – retif Aug 31 '21 at 13:29

0 Answers0