RegisS
RegisS

Reputation: 3

Python Argparse strict variable name

I try to understand a functionality (or bug ?) of python's argparse.

Here, my simple code:

import argparse

parser = argparse.ArgumentParser(usage="%(prog)s [--start]", add_help=False)
parser.add_argument("--start", help="Start prog", action="store_true")

arguments  = parser.parse_args()
start_fpc  = arguments.start

print arguments

When I execute this script, both start and star arguments are accepted:

[ rsenet  2015-08-28 12:39:50] /tmp
$ python test.py --star
Namespace(start=True)

[ rsenet  2015-08-28 13:59:16] /tmp
$ python test.py --start
Namespace(start=True)

Any idea why? If yes, is it possible to disable this function?

Upvotes: 0

Views: 1248

Answers (1)

Andrejs Cainikovs
Andrejs Cainikovs

Reputation: 28474

You need to disable allow_abbrev option, which was introduced in v3.5.

Excerpt from argparse doc:

  • allow_abbrev_ - Allows long options to be abbreviated if the abbreviation is unambiguous. (default: True)

This should help:

parser = argparse.ArgumentParser(usage="%(prog)s [--start]",
                                 allow_abbrev=False,
                                 add_help=False)

Upvotes: 4

Related Questions