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 nargs should support string wrapped integers too #76655
Comments
What do you mean by "string wrapped integers", and how should it support them? |
Values like "1", "2", "3", should be supported. |
A simple int(nargs) would be sufficient. I am getting ready with a PR in some time. |
You mean you want to call parser.add_argument('--foo', nargs="2") Why? |
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. |
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. |
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:
(I think it’s a ValueError, but I’m on my phone and can’t easily verify.) |
-1 |
Agreed. I don't think there is sufficient motivation for doing this, and there are downsides as Eric has pointed out. Rejecting. |
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 _get_nargs_pattern |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: