[Distutils] Using getopt for the proposed option processing

Fred L. Drake Fred L. Drake, Jr." <fdrake@acm.org
Mon, 18 Jan 1999 18:14:17 -0500 (EST)


  The option processing Greg proposed in his design doc matches that
of CVS:  there are a bunch of global options, and then each command
has its own options.  The big difference is that cvs commands take
arguments as well (the list of files to operate on), whereas Greg's
proposal is to allow multiple commands, each of which has private
options.  I don't see anywhere in Greg's proposal a requirement that
commands take arguments as well as options, so there's no reason not
to use the getopt module.  The only aspect of it is remotely
interesting is switching in different sets of options for each 
different command class, and that's pretty trivial.
  Here's a little demo code.  It accepts anything as a command, and
just keeps the same options for each, but the comments should make it
more than clear what needs to be done where to get the expected
behaviour.

------------------------------------------------------------------------
"""Example of the command line syntax used by distutils, using getopt."""

import getopt
import sys

LONGOPTS = ["help", "verbose"]
SHORTOPTS = "hv"

def main():
    args = ["<global>"] + sys.argv[1:]
    while args:
        cmd = args[0]
        # switch in different SHORTOPTS and LONGOPTS in this call to
        # support different options for different commands:
        cmdinfo = sys.modules[__name__]
        # cmdinfo = load_command(cmd)
        opts, args = getopt.getopt(args[1:],
                                   cmdinfo.SHORTOPTS, cmdinfo.LONGOPTS)
        # do something with the command here:
        print cmd, opts
        # create_command(opts).run()

if __name__ == "__main__":
    main()
------------------------------------------------------------------------


  -Fred

--
Fred L. Drake, Jr.	     <fdrake@acm.org>
Corporation for National Research Initiatives
1895 Preston White Dr.	    Reston, VA  20191