[getopt-sig] The bake-off
Patrick K. O'Brien
pobrien@orbtech.com
Thu, 30 May 2002 10:21:34 -0500
[Greg Ward]
> D'ohh! I originally liked using a list, but I've decided that I prefer
> add_option(). I suppose I will leave the list interface there but
> undocumented. Anyone else care?
I went with add_option() in the application that I used Optik. If it helps
any, I came up with something of an idiom (I think) for the *basic* use of
Optik in an application. I'll post the relevant parts from the application
code below as another example of real world use of Optik.
---
Patrick K. O'Brien
Orbtech
---
#!/usr/bin/env python
class Config:
"""Configuration information for this utility."""
def __init__(self):
"""Create Config instance."""
self.dryrun = 0
self.prefix = 'test_'
self.subdir = 'tests'
self.verbose = 1
self.version = __version__
self.revision = __revision__
self.poundbang = '/usr/bin/env python'
self.author = "Patrick K. O'Brien <pobrien@orbtech.com>"
self.cvsid = '$' + 'Id' + '$' # Broken apart to fool CVS.
self.cvsrev = '$' + 'Revision' + '$' # Ditto.
def _applyOptions(self, options):
"""
Apply overriding options, usually configuration file or
command line options.
"""
for attr, value in vars(options).iteritems():
self._applyOption(attr, value)
def _applyOption(self, attr, value):
"""Apply attribute value."""
if hasattr(self, attr):
if value is not None:
setattr(self, attr, value)
else:
raise AttributeError, 'Config is missing %r' % attr
config = Config()
def parse_args(args=sys.argv[1:]):
"""Parse the command line options."""
from optik import OptionParser
usage = \
'''
usage: %prog [options] file
Create a unit test skeleton module for an existing python code module.
By default, the new file will be created in a "tests" subdirectory.
help: "%prog -h" will display help information for all options.
'''
version = '%prog ' + __version__ + ' (revision %s)' % __revision__
parser = OptionParser(usage=usage, version=version)
parser.add_option('-a', '--author',
type='string',
help='module author')
parser.add_option('-p', '--prefix',
type='string',
default='test_',
help='prefix for new test file, default is "test_"')
parser.add_option('-s', '--subdir',
type='string',
default='tests',
help='subdirectory for new test file, ' + \
'default is "tests"')
parser.add_option('-d', '--dryrun',
action='store_true',
help='do not make any permament changes')
parser.add_option('-v', '--verbose',
action='store_true',
dest='verbose',
help='display progress information')
parser.add_option('-q', '--quiet',
action='store_false',
dest='verbose',
help='suppress progress information')
options, args = parser.parse_args(args)
if len(args) != 1:
parser.error('you must supply the name of a python file')
return (options, args)
def main():
options, args = parse_args()
config._applyOptions(options)
if config.verbose:
print 'Configuration:'
print vars(config)
print 'Command Line Options:'
print vars(options)
print 'Command Line Filespec:'
print args
for filename in args:
process(filename)
def process(filename):
"""Create a unit test skeleton for the python file."""
# [code snipped]
if __name__ == '__main__':
main()