[Python-Dev] argparse ugliness (original) (raw)
David Stanek dstanek at dstanek.com
Mon Mar 8 17:55:09 CET 2010
- Previous message: [Python-Dev] argparse ugliness
- Next message: [Python-Dev] argparse ugliness
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Mon, Mar 8, 2010 at 11:49 AM, Xavier Morel <python-dev at masklinn.net> wrote:
On 8 Mar 2010, at 16:53 , David Stanek wrote:
On Mon, Mar 8, 2010 at 10:40 AM, Steven Bethard <steven.bethard at gmail.com> wrote:
In argparse, unlike optparse, actions are actually defined by objects with a particular API, and the string is just a shorthand for referring to that. So: parser.addargument ('--plot', action='storetrue') is equivalent to: parser.addargument('--plot', argparse.StoreTrueAction) Because the names are so long and you'd have to import them, I've left them as private attributes of the module, but if there's really demand, we could rename them to argparse.StoreTrueAction, etc.
Any reason not to do something like: from argparse import actions ... parser.addargument('--plot', actions.storetrue) Basically a small namespace for the constants. action is taken from **kwargs, not from a positional argument as *args is a sequence of option strings (so you can write addargument('-p', '/p', '--plot', '--land-mass')). So you'd have to write addargument('--plot', action=actions.storetrue) which is straight from the department of redundant redundancies. An option would be parser.add(actions.StoreTrue('--plot')) but I'm not sure this makes any sense API-wise, and it would probably make the code a lot messier as the parser would have to reach into the action to get the information it needs. Either that, or the action would be an *arg and argparse would have to walk all of its *args type-testing each one to find if there's an action anywhere.
You could just change my example to: from argparse.actions import store_true ... parser.add_argument('--plot', action=store_true)
I would probably still import the actions namespace directly, but wouldn't have to.
-- David blog: http://www.traceback.org twitter: http://twitter.com/dstanek
- Previous message: [Python-Dev] argparse ugliness
- Next message: [Python-Dev] argparse ugliness
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]