[Python-Dev] PEP 389: argparse - new command line parsing module

Yuvgoog Greenle ubershmekel at gmail.com
Mon Sep 28 23:14:59 CEST 2009


for a live demo of how getopt is useful and flexible, I like how Audacity
uses it:
http://www.google.com/codesearch/p?hl=en&sa=N&cd=6&ct=rc#_hWFOhGz9lE/mezzo/scons/sconsign.py&q=getopt%20%22import%20getopt%22%20file:%5C.py$&l=264

To answer your question, it goes like this:
    options, args = getopt.getopt(sys.argv[1:], "a:b", ["alpha=", "beta"])
    for o, a in options:
        if o in ('-a', '--alpha'):
            alpha = a
        elif o in ('-b', '--beta'):
            beta = a
    main(alpha, beta, args)

Notice a few things:
1. There is no chance of the script killing itself. In argparse and optparse
exit() is called on every parsing error (btw because of this it sucks to
debug parse_args in an interpreter).
2. There is no chance the parser will print things I don't want it to print.
3. You can do whatever you want using this flow - call a function, a binary
operator (ie | is used in audacity scons example above)
4. in argparse this flow can be emulated and would be nicer, in ('-a',
'--alpha') becomes == 'alpha'

In a perfect world, getopt would be the low level parser that argparse and
optparse rely on. This is not too far fetched btw, all that needs to be done
is add another optional argument to getopt that would allow '-' and '--' to
be replaced with arbitrary signs, OR moving the parsing code from argparse
into getopt.

On Mon, Sep 28, 2009 at 10:57 PM, Steven Bethard
<steven.bethard at gmail.com>wrote:

> On Mon, Sep 28, 2009 at 12:22 PM, Brett Cannon <brett at python.org> wrote:
> > On Mon, Sep 28, 2009 at 08:49, Steven Bethard <steven.bethard at gmail.com>
> wrote:
> >> On Mon, Sep 28, 2009 at 8:27 AM, Steven D'Aprano <steve at pearwood.info>
> wrote:
> >>> On Tue, 29 Sep 2009 12:28:39 am Steven Bethard wrote:
> >>>> * Would you like argparse to grow an add_getopt_arguments method (as
> >>>> in my other post)?
> >>>
> >>> 0
> >>>
> >>>> * If argparse grew an add_getopt_arguments, would you still want to
> >>>> keep getopt around? And if so, why?
> >>>
> >>> Simplicity of the learning curve. Using it is as simple as:
> >>>
> >>> getopt.getopt(sys.argv[1:], "a:b", ["alpha=", "beta"])
> >>
> >> You forgot the for-loop, nested if/else statements and type conversions.
> ;-)
> >>
> >
> > =) I do wonder if people who are advocating for getopt sticking around
> > realize how much extra code must be written to make sure what it gives
> > back to you is in some sane manner.
> >
> > Let's take ``getopt.getopt(sys.argv[1:], "a:b", ["alpha=", "beta"])``
> > as an example and simply assume that 'alpha' takes a string as an
> > argument and that it's required and that 'beta' is a boolean flag. To
> > pull everything out you could do::
> >
> >  options, args = getopt.getopt(sys.argv[1:], "a:b", ["alpha=", "beta"])
> >  options_dict = dict(options)
> >  alpha = options_dict.get('-a', options_dict.get('--alpha', ''))
> >  beta = '-b' in options_dict or '--beta' in options_dict
> >
> >  main(alpha, beta, args)
> >
> > Obviously if one of the getopt supporters has a better way of doing
> > this then please speak up.
> >
> > Now, Steven, can you show how best to do this in argparse?
>
> Here's the same option parsing in argparse:
>
>    parser = argparse.ArgumentParser()
>    parser.add_argument('-a', '--alpha')
>    parser.add_argument('-b', '--beta', action='store_true')
>    args = parser.parse_args()
>
>    main(args.alpha, args.beta)
>
> Or if those final positional arguments were actually meaningful, then
> you would add one more argument like this::
>
>    parser = argparse.ArgumentParser()
>    parser.add_argument('-a', '--alpha')
>    parser.add_argument('-b', '--beta', action='store_true')
>    parser.add_argument('gammas', nargs='*')
>    args = parser.parse_args()
>
>    main(args.alpha, args.beta, args.gammas)
>
> Steve
> --
> Where did you get that preposterous hypothesis?
> Did Steve tell you that?
>        --- The Hiphopopotamus
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe:
> http://mail.python.org/mailman/options/python-dev/ubershmekel%40gmail.com
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20090929/0a77206a/attachment.htm>


More information about the Python-Dev mailing list