Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

argparse should support multiple types when nargs > 1 #82398

Open
rgov mannequin opened this issue Sep 18, 2019 · 4 comments
Open

argparse should support multiple types when nargs > 1 #82398

rgov mannequin opened this issue Sep 18, 2019 · 4 comments
Labels
3.9 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@rgov
Copy link
Mannequin

rgov mannequin commented Sep 18, 2019

BPO 38217
Nosy @rhettinger

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = None
created_at = <Date 2019-09-18.16:20:02.635>
labels = ['type-feature', 'library', '3.9']
title = 'argparse should support multiple types when nargs > 1'
updated_at = <Date 2019-09-18.16:58:43.613>
user = 'https://bugs.python.org/rgov'

bugs.python.org fields:

activity = <Date 2019-09-18.16:58:43.613>
actor = 'paul.j3'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)']
creation = <Date 2019-09-18.16:20:02.635>
creator = 'rgov'
dependencies = []
files = []
hgrepos = []
issue_num = 38217
keywords = []
message_count = 2.0
messages = ['352741', '352744']
nosy_count = 3.0
nosy_names = ['rhettinger', 'paul.j3', 'rgov']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue38217'
versions = ['Python 3.9']

@rgov
Copy link
Mannequin Author

rgov mannequin commented Sep 18, 2019

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

@rgov rgov mannequin added 3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Sep 18, 2019
@tirkarthi tirkarthi removed 3.7 (EOL) end of life 3.8 only security fixes labels Sep 18, 2019
@paulj3
Copy link
Mannequin

paulj3 mannequin commented Sep 18, 2019

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).

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
@FalcoGer
Copy link

FalcoGer commented Apr 5, 2023

I just ran into this exact use case. I would also appreciate this feature.

@LunarFawn
Copy link

LunarFawn commented Apr 1, 2024

I am running into this now as well and am in need of this feature. The fact that it is not supported already is kinda baffling. To get this same functionality I now need to write up code to process a configuration file, as I need to pass multiple configurations with lot of different data types.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.9 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
Status: Features
Development

No branches or pull requests

3 participants