New submission from Wojciech Muła <wojciech_m...@poczta.onet.pl>:

Hi, sometimes it is needed to grab variable, but limited, number of options.
For example --point could accept 2, 3 or 4 values: (x,y) or (x,y,z) or
(x,y,z,w). Current version of argparse requires postprocessing:

        parser.add_argument('--point', action='append', default=[])
        nmps = parser.parse_args()
        if not (2 <= len(nmsp.point) <= 4):
                raise argparse.ArgumentTypeError("--point expects 2, 3 or 4 
values")

I propose to allow pass range of options count to nargs, including
lower/upper bound. For example:
        
        parser.add_argument('--point', nargs=(2,4) )    # from 2 to 4
        parser.add_argument('--foo', nargs=(9, None) )  # at least 9
        parser.add_argument('--bar', nargs=(None, 7) )  # at most 7
        nmsp = parser.parse_args()

I've attached tests and patch made against Python3.2 lib from Debian.

w.

----------
components: Library (Lib)
files: argparse-nargs.patch
keywords: patch
messages: 129714
nosy: wm
priority: normal
severity: normal
status: open
title: argparse: nargs could accept range of options count
type: feature request
versions: Python 3.2, Python 3.3
Added file: http://bugs.python.org/file20947/argparse-nargs.patch

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue11354>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to