Skip to content

Instantly share code, notes, and snippets.

@ssokolow
Last active January 8, 2022 17:43
Show Gist options
  • Star 23 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save ssokolow/151572 to your computer and use it in GitHub Desktop.
Save ssokolow/151572 to your computer and use it in GitHub Desktop.
Python boilerplate from which I start all my projects
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""[application description here]"""
__appname__ = "[application name here]"
__author__ = "Stephan Sokolow (deitarion/SSokolow)"
__version__ = "0.0pre0"
__license__ = "GNU GPL 3.0 or later"
import logging
log = logging.getLogger(__name__)
# -- Code Here --
if __name__ == '__main__':
from optparse import OptionParser
parser = OptionParser(version="%%prog v%s" % __version__,
usage="%prog [options] <argument> ...",
description=__doc__.replace('\r\n', '\n').split('\n--snip--\n')[0])
parser.add_option('-v', '--verbose', action="count", dest="verbose",
default=2, help="Increase the verbosity. Use twice for extra effect")
parser.add_option('-q', '--quiet', action="count", dest="quiet",
default=0, help="Decrease the verbosity. Use twice for extra effect")
#Reminder: %default can be used in help strings.
# Allow pre-formatted descriptions
parser.formatter.format_description = lambda description: description
opts, args = parser.parse_args()
# Set up clean logging to stderr
log_levels = [logging.CRITICAL, logging.ERROR, logging.WARNING,
logging.INFO, logging.DEBUG]
opts.verbose = min(opts.verbose - opts.quiet, len(log_levels) - 1)
opts.verbose = max(opts.verbose, 0)
logging.basicConfig(level=log_levels[opts.verbose],
format='%(levelname)s: %(message)s')
@dotbugfix
Copy link

Any specific reason for using OptionParser instead of the more popular ArgParse?

@MotorCityCobra
Copy link

Must be cause this post is old

@metachris
Copy link

Also take a look at python-boilerplate.com for various up-to-date Python boilerplates!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment