Skip to content

Instantly share code, notes, and snippets.

@ckolumbus
Created April 9, 2014 15:03
Show Gist options
  • Save ckolumbus/10280681 to your computer and use it in GitHub Desktop.
Save ckolumbus/10280681 to your computer and use it in GitHub Desktop.
Template for a main function in Python including optparse command line parsing
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 20xx CKolumbus
# All rights reserved.
#
# This file is part of FOOBAR.
#
# FOOBAR is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# FOOBAR is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with FOOBAR. If not, see <http://www.gnu.org/licenses/>.
#
#
# Author : Chris <ckolumbus@ac-drexler.de>
# Date : yyyy-mm-dd
__version__ = "0.1"
def main(argv=None):
# not using argparser to support Python<2.7
from optparse import OptionParser
usage = "usage: %prog [options] arg1 arg2 "
parser = OptionParser(usage)
parser.set_defaults(verbose=False)
parser.add_option("-v", "--verbose", dest="verbose", action="store_true",
help="verbose output")
parser.add_option("-q", "--quite", dest="verbose", action="store_false",
help="supress all output (quite)")
(options, args) = parser.parse_args(argv[1:])
if len(args) != 2:
parser.print_help()
return 2
if __name__ == "__main__":
import sys
sys.exit(main(sys.argv))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment