Created
January 11, 2011 21:46
-
-
Save andreberg/775214 to your computer and use it in GitHub Desktop.
Module: CLI (optparse)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# encoding: utf-8 | |
''' | |
${module} -- ${shortdesc} | |
${module} is a ${description} | |
It defines ${classes_and_methods} | |
@author: ${user_name} | |
@copyright: ${year} ${organisation_name}. All rights reserved. | |
@license: Licensed under the Apache License, Version 2.0 (the "License");\n | |
you may not use this file except in compliance with the License. | |
You may obtain a copy of the License at | |
U{http://www.apache.org/licenses/LICENSE-2.0} | |
Unless required by applicable law or agreed to in writing, software | |
distributed under the License is distributed on an B{"AS IS"} B{BASIS}, | |
B{WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND}, either express or implied. | |
See the License for the specific language governing permissions and | |
limitations under the License. | |
@contact: ${user_email} | |
@deffield updated: Updated | |
''' | |
import sys | |
import os | |
from optparse import OptionParser | |
__all__ = [] | |
__version__ = 0.1 | |
__date__ = '${isodate}' | |
__updated__ = '${isodate}' | |
DEBUG = 1 | |
TESTRUN = 0 | |
PROFILE = 0 | |
def main(argv=None): | |
'''Command line options.''' | |
program_name = os.path.basename(sys.argv[0]) # IGNORE:W0612 @UnusedVariable | |
program_version = u"v0.1" | |
program_build_date = u"%s" % __updated__ | |
program_version_string = u'%%prog %s (%s)' % (program_version, program_build_date) | |
#program_usage = u'''usage: spam two eggs''' # optional - will be autogenerated by optparse | |
program_longdesc = u'''''' # optional - give further explanation about what the program does | |
program_license = u"Copyright ${year} ${user_name} (${organisation_name}) \ | |
Licensed under the Apache License 2.0\nhttp://www.apache.org/licenses/LICENSE-2.0" | |
if argv is None: | |
argv = sys.argv[1:] | |
try: | |
# setup option parser | |
parser = OptionParser(version=program_version_string, epilog=program_longdesc, description=program_license) | |
parser.add_option("-i", "--in", dest="infile", help="set input path [default: %default]", metavar="FILE") | |
parser.add_option("-o", "--out", dest="outfile", help="set output path [default: %default]", metavar="FILE") | |
parser.add_option("-v", "--verbose", dest="verbose", action="count", help="set verbosity level [default: %default]") | |
# set defaults | |
parser.set_defaults(outfile=u"./out.txt", infile=u"./in.txt") | |
# process options | |
(opts, args) = parser.parse_args(argv) | |
if opts.verbose > 0: | |
print "verbosity level = %d" % opts.verbose | |
if opts.infile: | |
print u"infile = %s" % opts.infile | |
if opts.outfile: | |
print u"outfile = %s" % opts.outfile | |
# MAIN BODY # | |
except Exception, e: | |
print >> sys.stderr, sys.argv[0].split(u"/")[-1] + u": " + unicode(e).decode('unicode_escape') | |
print >> sys.stderr, "\t for help use --help" | |
return 2 | |
if __name__ == "__main__": | |
if DEBUG: | |
sys.argv.append("-h") | |
if TESTRUN: | |
import doctest | |
doctest.testmod() | |
if PROFILE: | |
import cProfile | |
import pstats | |
profile_filename = '${module}_profile.txt' | |
cProfile.run('main()', profile_filename) | |
statsfile = open("profile_stats.txt", "wb") | |
p = pstats.Stats(profile_filename, stream=statsfile) | |
stats = p.strip_dirs().sort_stats('cumulative') | |
print >> statsfile, stats.print_stats() | |
statsfile.close() | |
sys.exit(0) | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment