-
-
Notifications
You must be signed in to change notification settings - Fork 30.8k
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 : support "single occurence" arguments #128301
Comments
Why use Essentially, you want a Also, I think usually a single store action would simply keep the last argument that was passed (namely >>> import argparse
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--foo')
_StoreAction(option_strings=['--foo'], dest='foo', nargs=None, const=None, default=None, type=None, choices=None, required=False, help=None, metavar=None)
>>> parser.parse_args('--foo x --foo y'.split())
Namespace(foo='y') |
Yes exactly ! I just copy-pasted the hack that worked for me to show it's not that big of a deal (my program uses only Extend & BooleanOptional, so I subclassed only those).
Yes and that loses the cardinality info. You can retort that an extend action doesn't and you can examine the And this is not only nicer, it can be really important. Case in point : my program is called by someone's automated job that builds an input file with a list of files to move. That job providing two target S3 buckets is a sign there is a bug somewhere, which could extend to the list of files, ultimately leading to the clobbering of production files ! So this feature can provide real protection, and you know people and laziness (the opposite of safety - I want password '12345' everywhere, right). If this protection can come "for free" (just setting |
Yes and that's essentially what I would recommend. Create a dedicated action which raises if the attribute is being added more than once. Note that instead of an action, you can also subclass I would however subclass It would be better to suggest your feature on https://discuss.python.org/c/ideas/6 and if this gathers support, we could implement it but I'm afraid that the standard library is not where it should live. This is more the role of a third-party package.
The standard library makes design choices and may even restrict features on purpose since we weigh the cost of maintenance, use cases and flexibility. So it's not entirely free on our side either. As a user this can be frustrating (and I am one of them sometimes) but we need to estimate how likely this feature is needed (others may say that "the caller is responsible to check that they did not supply the argument twice" since this is not really a arduous task I think). |
I agree that in most cases But I think I make a clear case for the opposite being really useful as well. My argument was that it is not an arduous task as you say, funny how you sent that back :) So in short this boils down to cost on your side. And you're certainly the better judge of that. Thank you for suggesting the ideas page, I'll post there as well asap ! Peace. |
It doesn't mean that it should be part of the standard library. We have a policy that not all one-liners (ok maybe a bit more in this case) should be part of the standard library.
There is a clear case but how many users would use this? how many actually care? I'm not saying that it's useless, I'm just saying that we need to make choices. A good example of this is when users want to add features to Now, one part that will never be free on our side is the fact that we still need to maintain this feature. Even if it's not an arduous task, it would add burden to anyone maintaining the module because it's a feature that may or may not suit each use case in the future . We need to handle corner cases, documentation, and teaching as well. Now, if the Discourse topic gets support and/or a core developper is willing to support this addition, then we wouldn't reject it. (I'm just explaining how the standard library policy for feature requests works.) OTOH, we recently improved examples of argparse (cc @savannahostrowski). We could make a recipe for this kind of action, without adding it to the standard library itself (this is an approach usually taken by |
FTR, I personally think we should make argparse more extensible in general (namely, expose actions classes). I also think that sometimes more semantics for |
Oh ! So Savannah's the better judge, not you ! What you're saying makes total sense. May I plead to Savannah that this is a "harmless" feature, because it is (obviously) opt-in ? A current user of Meanwhile, your suggestion of adding an example use to the documentation is really great ! |
(I tagged Savannah and Serhiy because they recently worked on argparse and I reviewed some of their PRs). Strictly speaking, any core developer willing to support the feature would be sufficient (we actually don't have an active and explicit maintainer for argparse, just some of us work on the module when we can). |
Oh man ! I was trying to court Savannah, and you just explained how the "Blame" button up left works ! |
I agree with @picnixz. The stdlib provides ready solutions for most common use cases. You can create custom actions and type converters for more specific needs, although in many cases you can get by with standard actions and post-processing. I would use |
So be it. |
Don't use |
Feature or enhancement
Proposal:
Hello,
Just finished writing a little gadget to move/rename files inside a given bucket/folder/ on S3 (AWS).
It wouldn't make sense to pass multiple "-b bucket" to my program.
Sure, I can count such args gathered in an Action.dest and emit an error, but it would be nice to express such requirement and have argparse handle it.
Here is an example modification implementing such control in _ExtendAction that worked for me :
Effect :
Also, an other suggestion : added a few line breaks in ArgumentParser.error() to make for a clearer message with prominent error indication :
Peace.
Has this already been discussed elsewhere?
No response given
Links to previous discussion of this feature:
No response
The text was updated successfully, but these errors were encountered: