Last active
August 29, 2015 14:24
-
-
Save packetchef/7ba77dcd2375b212dcf5 to your computer and use it in GitHub Desktop.
Demonstrate handling of command line options using optparse
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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