Skip to content

Instantly share code, notes, and snippets.

@pestilence669
Last active April 10, 2017 23:51
Show Gist options
  • Save pestilence669/f1e92d037920a33485f8 to your computer and use it in GitHub Desktop.
Save pestilence669/f1e92d037920a33485f8 to your computer and use it in GitHub Desktop.
Boiler-plate Python 2.7 quick CLI project file
#!/usr/bin/env python
# vim: ts=4 sw=4 noet fileencoding=utf-8:
"""Boiler-plate Python 2.7 quick CLI project file"""
__author__ = 'pestilence669'
from argparse import ArgumentParser
import sys
def run_tests_fully():
"""Execute the doctest suite for this module and return success.
:rtype: bool"""
import doctest
failure_count, unused_tests_run = doctest.testmod()
return failure_count == 0 # success?
def parse_options(argv=None):
"""Parses a list of command-line options and returns them in an object.
:param list[string] argv: Command-line arguments
:rtype: object"""
parser = ArgumentParser(description='Do something')
parser.add_argument(
'-v', '--verbose',
action='store_true', dest='verbose', default=False,
help='show extended output')
parser.add_argument(
'-q', '--quiet',
action='store_true', dest='quiet', default=False,
help='suppress all output')
parser.add_argument(
'-s', '--skip_tests',
action='store_true', dest='skip_tests', default=False,
help='skip tests before running')
return parser.parse_args(argv)
def main(options=object()):
"""The main entry point of the script's main operations.
:param object options: Options (parsed)
:rtype: none"""
print 'Done\n'
################################################################################
if __name__ == '__main__':
command_line_options = parse_options()
if command_line_options.quiet:
sys.stdout.close()
sys.stdout = open('/dev/null', 'w')
if not command_line_options.skip_tests and not run_tests_fully():
sys.exit(-1)
else:
main(command_line_options)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment