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

Michael Hoffman b3i4old02 at sneakemail.com
Thu Feb 21 16:19:22 CET 2008


Michael Hoffman wrote:
>      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")

I forgot that this bit requires making an OptionGroup wrapper.

from optparse import OptionGroup as _OptionGroup

class OptionGroup(_OptionGroup):
     def __enter__(self):
         return self

     def __exit__(self, *exc_info):
         self.parser.add_option_group(self)

Also I have been thinking about this and I think the way I did it is 
clunkier than necessary. Better would be to do two passes of 
optparse--one to figure out what plugins to load, then load their 
options into the OptionParser, and parse the options again. This would 
require special handling for help and option conflicts, but would be a 
better user experience.



More information about the Distutils-SIG mailing list