[issue38217] argparse should support multiple types when nargs > 1

2019-09-18 Thread paul j3


paul j3  added the comment:

Which is more valuable to you, the string conversion, or the checking?

What's wrong with doing the 'type check' in post parsing code?  (MarSoft's 
answer in the SO link).

To make a better case for this, I'd suggest writing your own fix.  It doesn't 
need to be a polished push, but enough code to show that the change is 
relatively simple (preferably occurring in only one or two functions).

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue38217] argparse should support multiple types when nargs > 1

2019-09-18 Thread Karthikeyan Singaravelan


Change by Karthikeyan Singaravelan :


--
nosy: +paul.j3, rhettinger
versions:  -Python 2.7, Python 3.5, Python 3.6, Python 3.7, Python 3.8

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue38217] argparse should support multiple types when nargs > 1

2019-09-18 Thread Ryan Govostes


New submission from Ryan Govostes :

argparse supports consuming multiple command-line arguments with nargs=2, etc. 
It converts them to the type given in the argument's type parameter.

argparse does not provide a good solution when the input arguments should be 
different data types. For an example, you cannot have an argument that expects 
a str followed by an int, '--set-age Bob 34'.

Ordinarily, the suggestion would be to split it into two arguments, like 
'--set-person Bob --set-age 34'.

However, this becomes awkward with an action such as 'append', where the 
command line arguments become tedious, like '--set-person Bob --set-age 34 
--set-person Alice --set-age 29', or confusing, as in '--set-person Bob 
--set-person Alice --set-age 34 --set-age 29'.

My proposal is to allow the 'type' parameter to accept a tuple of types:

p.add_argument('--set-age', nargs=2, type=(str, int))

Since 'nargs' is redundant, this could even be simplified to just:

p.add_argument('--set-age', type=(str, int))

The resulting parsed argument would then be a tuple of (str, int), as opposed 
to a list. If action='append', the result would be a list of such tuples.

This creates no backwards compatibility issue because tuple instances are not 
callable, so this was never valid code that did something else.

A further enhancement could be that when nargs='+' or '*', and a tuple of types 
is provided, the types are used round robin: '--set-ages Bob 34 Alice 29'. An 
exception would be raised if it would create an incomplete tuple.

See here for other discussion and workarounds: 
https://stackoverflow.com/questions/16959101/python-argparse-how-to-have-nargs-2-with-type-str-and-type-int

--
components: Library (Lib)
messages: 352741
nosy: rgov
priority: normal
severity: normal
status: open
title: argparse should support multiple types when nargs > 1
type: enhancement
versions: Python 2.7, Python 3.5, Python 3.6, Python 3.7, Python 3.8, Python 3.9

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com