Skip to content

Instantly share code, notes, and snippets.

@packetchef
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save packetchef/7ba77dcd2375b212dcf5 to your computer and use it in GitHub Desktop.
Save packetchef/7ba77dcd2375b212dcf5 to your computer and use it in GitHub Desktop.
Demonstrate handling of command line options using optparse
import optparse
import sys
global debug
progDesc = (
'Welcome to %prog. It\'s meant for frobbing foozles, and otherwise'
'making for better frognosticators.'
)
op = optparse.OptionParser(usage='Usage: %prog [options]',
description=progDesc, version='%prog v0.99')
op.add_option('-d', '--debug', action='store_true', dest='debug',
help='Enable debug mode', default=False)
op.add_option('-u', '--user', action='store', help='User ID goes here')
op.add_option('-f', '--file', action='store',
help='File name for the stuff', default='frood.conf')
(options, args) = op.parse_args()
if options.debug:
print('[*] Debug mode enabled')
debug = True
else:
debug = False
if not options.user:
op.print_help()
sys.exit(1)
print('[*] User: {user}'.format(user=options.user))
print('[*] File: {file}'.format(file=options.file))
print('[*] Debug: {debug}'.format(debug=options.debug))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment