Skip to content

Instantly share code, notes, and snippets.

@chelnak
Created January 15, 2015 11:47
Show Gist options
  • Save chelnak/2b091350e12b28a9fc90 to your computer and use it in GitHub Desktop.
Save chelnak/2b091350e12b28a9fc90 to your computer and use it in GitHub Desktop.
OptParse Example
#!/usr/bin/python
from optparse import OptionParser
#Setup OptionParser
parser = OptionParser()
parser.add_option("-n", "--host",dest="hostname",help="Hostname of server.")
parser.add_option("-g", "--hostgroups",dest="hostgroups",help="Hostgroup of a server")
(options,args) = parser.parse_args()
required = ["hostname", "hostgroups"]
for i in required:
if options.__dict__[i] is None:
print "Mandetory arguments are missing\n"
parser.print_help()
exit(-1)
#Get at arguments
print options.hostname
print options.hostgroups
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment