cmdline args

Pass command line arguments to script

import getopt

import sys

#python foo.py --dc="dd" --env="meooow" --zone="int" --user="admin" --pass="password" --pool="pool" --monitor="monitor" --verbose

#https://pymotw.com/2/getopt/

version = '1.0'

verbose = False

output_filename = 'default.out'

print 'ARGV      :', sys.argv[1:]

options, remainder = getopt.getopt(sys.argv[1:], 'd:e:z:u:p:g:m:vh', ['dc=',

                                                                      'env=',

                                                                      'zone=',

                                                                      'user=',

                                                                      'pass=',

                                                                      'pool=',

                                                                      'monitor=',

                                                                      'verbose',

                                                                      'help'

                                                                      ])

cmdargs = {}

for opt, arg in options:

    if opt in ('-h', '--help'):

        help_message()

        exit()

    elif opt in ('-d', '--dc'):

        cmdargs['datacenter'] = arg

    elif opt in ('-e', '--env'):

        cmdargs['environment'] = arg

    elif opt in ('-z', '--zone'):

        cmdargs['zone'] = arg

    elif opt in ('-u', '--user'):

        cmdargs['user'] = arg

    elif opt in ('-p', '--pass'):

        cmdargs['password'] = arg

    elif opt in ('-g', '--pool'):

        cmdargs['pool'] = arg

    elif opt in ('-m', '--monitor'):

        cmdargs['monitor'] = arg

    elif opt in ('-v', '--verbose'):

        cmdargs['verbose'] = True

        log_level = logging.DEBUG

print cmdargs

if ('datacenter' in cmdargs):

    print 'datacenter    :', cmdargs['datacenter']

if ('environment' in cmdargs):

    print 'environment   :', cmdargs['environment']

if ('zone' in cmdargs):

    print 'zone          :', cmdargs['zone']

if ('user' in cmdargs):

    print 'user          :', cmdargs['user']

if ('password' in cmdargs):

    print 'password      :', cmdargs['password']

if ('pool' in cmdargs):

    print 'pool     :', cmdargs['pool']

if ('monitor' in cmdargs):

    print 'monitor  :', cmdargs['monitor']

if ('VERBOSE' in cmdargs):

    print 'VERBOSE       :', cmdargs['verbose']

if ('REMAINING' in cmdargs):

    print 'REMAINING     :', cmdargs['remainder']