This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: argparse nargs should support string wrapped integers too
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.7
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: asvetlov, eric.smith, paul.j3, r.david.murray, shubham1172, steven.daprano
Priority: normal Keywords: patch

Created on 2018-01-01 06:47 by shubham1172, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 5070 closed shubham1172, 2018-01-01 09:00
PR 5075 closed shubham1172, 2018-01-01 18:31
Messages (10)
msg309322 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2018-01-01 07:02
What do you mean by "string wrapped integers", and how should it support them?
msg309323 - (view) Author: Shubham Sharma (shubham1172) * Date: 2018-01-01 07:10
Values like "1", "2", "3", should be supported.
msg309324 - (view) Author: Shubham Sharma (shubham1172) * Date: 2018-01-01 07:11
A simple int(nargs) would be sufficient. I am getting ready with a PR in some time.
msg309325 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2018-01-01 07:19
> Values like "1", "2", "3", should be supported.

You mean you want to call parser.add_argument('--foo', nargs="2") 
instead of parser.add_argument('--foo', nargs=2)?

Why?
msg309326 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2018-01-01 07:22
Why?  What's the motivation for supporting this?  There's no reason that I can think of, so I'm curious what your use case is.
msg309327 - (view) Author: Shubham Sharma (shubham1172) * Date: 2018-01-01 07:34
nargs can take various values like "*", "+", etc. but doesn't support integers which are wrapped around in strings. Say, I want to take a user input for the nargs value (just a hypothetical situation); now I'll have to check additionally whether there's a numeric value inside the input char and then cast it to integer manually. An enhancement like this would eliminate this hassle and support for these will be helpful.
msg309331 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2018-01-01 14:33
Fixed possible versions. 

I’m firmly -1 on this. It’s not at all common, and would allow code to pass that I’d prefer to cause an error. For the rare case where it’s desired, it can be moved in to the client code. 

If I were doing this in client code, I’d try to convert to an int, and if an exception use the string version. It’s only a few lines of code:

    try:
        val = int(val)
    except ValueError:
        pass

(I think it’s a ValueError, but I’m on my phone and can’t easily verify.)
msg309345 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2018-01-01 19:49
-1
msg309350 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2018-01-01 21:27
Agreed.  I don't think there is sufficient motivation for doing this, and there are downsides as Eric has pointed out.  Rejecting.
msg309686 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2018-01-09 07:00
In https://bugs.python.org/issue11354

'argparse: nargs could accept range of options count'

I explored the option allowing regex style range arguments, such as nargs='{2,4}' or an equivalent tuple nargs=(2,4).

But that builds on https://bugs.python.org/issue9849, which seeks better error checking of the `nargs` parameter.  In the current implementation, there is no checking or parsing of the value. The full range of allowable values is determined by its use in 2 methods:

_get_nargs_pattern
_format_args
History
Date User Action Args
2022-04-11 14:58:56adminsetgithub: 76655
2018-01-09 07:00:18paul.j3setnosy: + paul.j3
messages: + msg309686
2018-01-01 21:27:22r.david.murraysetstatus: open -> closed
resolution: rejected
messages: + msg309350

stage: patch review -> resolved
2018-01-01 19:49:09asvetlovsetnosy: + asvetlov
messages: + msg309345
2018-01-01 18:31:16shubham1172setpull_requests: + pull_request4949
2018-01-01 17:16:23eric.smithsetversions: - Python 3.8
2018-01-01 14:33:45eric.smithsetnosy: + eric.smith

messages: + msg309331
versions: - Python 2.7, Python 3.4, Python 3.5, Python 3.6
2018-01-01 09:00:43shubham1172setkeywords: + patch
stage: patch review
pull_requests: + pull_request4944
2018-01-01 07:34:17shubham1172setmessages: + msg309327
2018-01-01 07:22:14r.david.murraysetnosy: + r.david.murray
messages: + msg309326
2018-01-01 07:19:41steven.dapranosetmessages: + msg309325
2018-01-01 07:11:39shubham1172setmessages: + msg309324
2018-01-01 07:10:37shubham1172setmessages: + msg309323
2018-01-01 07:02:42steven.dapranosetnosy: + steven.daprano
messages: + msg309322
2018-01-01 06:47:30shubham1172create