Source

pymotw-ja / motw

Full commit
#!/usr/bin/env python
# encoding: utf-8
#
# Copyright (c) 2009 Doug Hellmann All rights reserved.
#
"""
"""
#end_pymotw_header

import optparse
import sys

import PyMOTW

def main(args):
    try:
        _main(args)
    except Exception, err:
        sys.stderr.write(str(err))
        sys.stderr.write('\n')
        return 1
    return 0
    

OUTPUT_HANDLERS = {
    'text':PyMOTW.show_text,
    'html':PyMOTW.show_html,
    'web':PyMOTW.show_webpage,
    }

def _main(args):
    parser = optparse.OptionParser(
        usage='usage: %prog [options] <module_name>',
        )
    parser.add_option('-t', '--text', 
                      help="Print plain-text version of help to stdout", 
                      action='store_const', 
                      const='text',
                      dest='mode', 
                      )
    parser.add_option('-w', '--web', 
                      help="Open HTML version of help from web", 
                      action='store_const', 
                      const='web',
                      dest='mode', 
                      )
    parser.add_option('--html', 
                      help="Open HTML version of help from installed file", 
                      action='store_const', 
                      const='html',
                      dest='mode',
                      default='html', 
                      )
    options, args = parser.parse_args(args)
    if len(args) != 1:
        raise ValueError('Please provide the name of exactly one module from the Python standard library.')
    module_name = args[0]
    
    handler = OUTPUT_HANDLERS[options.mode]
    handler(module_name)
    
    return 0

if __name__ == '__main__':
    sys.exit(main(sys.argv[1:]))