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

Steven Bethard steven.bethard at gmail.com
Mon Sep 28 06:24:54 CEST 2009

On Sun, Sep 27, 2009 at 9:09 PM, "Martin v. Löwis" <martin at v.loewis.de> wrote:
>> If you think getopt and optparse should stick around in 3.X, why is
>> that? If you think there are things that getopt and optparse do better
>> than argparse, could you please give some examples?
> I personally consider getopt superior to both optparse and argparse.
> Those are terribly verbose in specifying arguments, whereas getopt's
> sequence-of-letters is really nice and compact.

Thanks for the concrete example. Although I'm unconvinced that the
characters you save in the sequence of letters in the getopt.getopt
call aren't afterwards wasted on type conversions, if/else statements
and variable assignments in the subsequent loop, it would be pretty
simple to add to argparse something like::

    ArgumentParser.add_getopt_arguments(options[, long_options])

Then you could replace your getopt code::

        opts, args = getopt.getopt(sys.argv[1:], "ho:v", ["help", "output="])
    except getopt.GetoptError, err:
        # print help information and exit:
        print str(err) # will print something like "option -a not recognized"
    output = None
    verbose = False
    for o, a in opts:
        if o == "-v":
            verbose = True
        elif o in ("-h", "--help"):
        elif o in ("-o", "--output"):
            output = a
            assert False, "unhandled option"

with argparse code like::

    parser = argparse.ArgumentParser()
    parser.add_getopt_arguments("ho:v", ["help", "output="])
    args = parser.parse_args()
    verbose = args.v
    output = args.o or args.output

If something like this were available, would you be alright with
deprecating getopt?

Where did you get that preposterous hypothesis?
Did Steve tell you that?
        --- The Hiphopopotamus

More information about the Python-Dev mailing list