[Distutils] Creating Dynamic Command Line Options from Python Egg Plugins

Michael Hoffman b3i4old02 at sneakemail.com
Thu Feb 21 02:13:00 CET 2008

Noah Gift wrote:
> Has anyone created a command line tool, using optparse, that  
> dynamically creates options from available eggs/plugins?  I have a  
> need to create such a tool, and was looking for an example.

Yeah, I did something like this, which is somewhat clunky:

def parse_options(args):
     usage = """%prog [OPTION...] MODEL SEQFILE OUTFILE
        %prog [OPTION...] --driver=DRIVER MODEL SEQFILE 
     version = "%%prog %s" % __version__
     parser = OptionParser(usage=usage, version=version)

     with OptionGroup(parser, "Output driver") as group:
         group.add_option("-d", "--driver", default="DEFAULT",
                          help="set output format")

         group.add_option("--driver-help", action="store_true",
                          help="get driver-specific help")

     options, args = parser.parse_args(args)

     if options.driver_help:
         driver_class = driver_load(options.driver)

def driver_load(name):
     return load_entry_point("myproject", "myproject.drivers", name)

And I defined the entry points in my setup.py file:

DEFAULT = myproject.driver.hdf5:HDF5Driver
hdf5 = myproject.driver.hdf5:HDF5Driver
sql = myproject.driver.sql:SQLDriver

At some point I pass unprocessed args to the entry point, which parse 
them with optparse. I could call the program like this:

myproject --driver=sql model seqfile --database=server.example.net

More information about the Distutils-SIG mailing list