[docs] Seen bug in argparse module

Jagdish Choudhary jagdish1287 at gmail.com
Fri Jun 24 14:10:23 EDT 2016


Hi All,

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 jagdish$ python prog.py 12 3 4 --s
19
JAGDISHs-MacBook-Pro:test_python jagdish$


If users provide s or su in place of sum it still works . Now consider the
case when some code may have few starting characters same , it will get be
confused . I think we should have proper check if user doesn't provide same
parameter as per help , it should throw error . thanks in advance !

-- 
Thanks and Regards,
Jagdish Choudhary
IBM India Pvt Ltd, Bangalore
M.No-8971011661
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/docs/attachments/20160624/01951de2/attachment-0001.html>


More information about the docs mailing list