chelnak
1/15/2015 - 11:47 AM

OptParse Example

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