Skip to content

Instantly share code, notes, and snippets.

@chrisma
Forked from ssokolow/boilerplate.py
Created April 29, 2012 08:22
Show Gist options
  • Save chrisma/2545158 to your computer and use it in GitHub Desktop.
Save chrisma/2545158 to your computer and use it in GitHub Desktop.
Python boilerplate
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""[application description]"""
__appname__ = "[application name]"
__author__ = "[author]"
__version__ = "0.0"
__license__ = "GNU GPL 3.0 or later"
import logging, argparse
log = logging.getLogger(__name__)
# -- Code Here --
if __name__ == '__main__':
parser = argparse.ArgumentParser(description=__doc__)
group = parser.add_mutually_exclusive_group()
group.add_argument("-v", "--verbose", action="count", default=0, help="Increase verbosity. Supply twice for increased effect.")
group.add_argument("-q", "--quiet", action="count", default=0, help="Decrease verbosity. Supply twice for increased effect.")
parser.add_argument('--version', action='version', version='%s %s by %s. License: %s' % (__appname__, __version__, __author__, __license__))
# -- more args --
args = parser.parse_args()
log_levels = [logging.CRITICAL, logging.ERROR, logging.WARNING, logging.INFO, logging.DEBUG]
log_index = 2 + (args.verbose if args.verbose<=2 else 2) - (args.quiet if args.quiet<=2 else 2)
logging.basicConfig(level=log_levels[log_index], format='%(asctime)s %(levelname)s %(message)s')
logging.debug('CLI arguments: %s' % args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment