Seen bug in argparse module

Peter Otten __peter__ at web.de
Fri Jun 24 16:13:00 EDT 2016


Jagdish Choudhary wrote:

> When argument provided from user which doesn't match to right option which
> is mentioned in help , it runs without issue..let me provide an example
> 
> https://docs.python.org/3.3/library/argparse.html
> 
> import argparse
> parser = argparse.ArgumentParser(description='Process some
> integers.')parser.add_argument('integers', metavar='N', type=int,
> nargs='+',
>                    help='an integer for the
> accumulator')parser.add_argument('--sum', dest='accumulate',
> action='store_const',
>                    const=sum, default=max,
>                    help='sum the integers (default: find the max)')
> args = parser.parse_args()print(args.accumulate(args.integers))
> 
> python prog.py -h
> usage: prog.py [-h] [--sum] N [N ...]
> 
> Process some integers.
> 
> positional arguments:
>  N           an integer for the accumulator
> 
> optional arguments:
>  -h, --help  show this help message and exit
>  --sum       sum the integers (default: find the max)
> 
> If user run it like below-
> 
> JAGDISHs-MacBook-Pro:test_python jagdish$ python prog.py 12 3 4 --sum
> 19
> JAGDISHs-MacBook-Pro:test_python jagdish$ python prog.py 12 3 4 --su
> 19
> JAGDISHs-MacBook-Pro:test_python jag

This works as designed:

https://docs.python.org/3.3/library/argparse.html#prefix-matching

Starting with Python 3.5 you can disable this behaviour with 
allow_abbrev=False, see

https://docs.python.org/3.6/library/argparse.html#allow-abbrev

If you think this should have been the default -- I agree.




More information about the Python-list mailing list