0

I have trouble passing parameters to my script. Script launch command line : myscript.py -c Random I'm using getopt in my code (given down there) but this code is not looping through the arguments because later on the program the tested_company varible is not defined, where did I go wrong?

tested_company=None
try:
    opts, args = getopt.getopt(sys.argv[1:], "hc:i", ['help', 'company', 'info']) #first argument ignored because zabbix giving it and being useless
except getopt.GetoptError as e:
    print (e)
    usage()
    sys.exit(3)
if not opts:
    #print ('No options supplied, only updating the database')
    print("3")
    sys.exit(3)
else:
    for opt, arg in opts:
        if opt in ('-h', '--help'):
            usage()
            sys.exit(0)
        elif opt in ('-c', '--company'):
            tested_company = arg
        elif opt == '-i':
            displayInfos=1
3
  • What do you expect tested_company to be? It's set to None on the very first line and then not reassigned at any point. Commented Jan 6, 2016 at 15:40
  • it is in the "elif opt in ('-c', '--company'):" part, when I'm specifying myscript.py -c XXXXXX , the tested_company should be XXXXXX but it's not Commented Jan 6, 2016 at 15:41
  • What is the actual error? Is it coming from this code or something else? Commented Jan 6, 2016 at 15:43

2 Answers 2

2

I think you're missing an equals sign after company in your getopt call. This code works for me:

import getopt
import sys

tested_company=None
try:
    opts, args = getopt.getopt(sys.argv[1:], "hc:i", ['help', 'company=', 'info']) #first argument ignored because zabbix giving it and being useless
    print(opts)
except getopt.GetoptError as e:
    print (e)
    usage()
    sys.exit(3)
if not opts:
    #print ('No options supplied, only updating the database')
    print("3")
    sys.exit(3)
else:
    for opt, arg in opts:
        if opt in ('-h', '--help'):
            usage()
            sys.exit(0)
        elif opt in ('-c', '--company'):
            tested_company = arg
        elif opt == '-i':
            displayInfos=1

print(tested_company)

Calling this with

> python .\script.py -c xxxx

gives

[('-c', 'xxxx')]
xxxx

Calling with

> python .\script.py --company xxxx

gives

[('--company', 'xxxx')]
xxxx
Sign up to request clarification or add additional context in comments.

Comments

1

opts variable may not get initialised and is than called outside the try statement. Any particular reason why you can't do the following?

tested_company=None
try:
    opts, args = getopt.getopt(sys.argv[1:], "hc:i", ['help', 'company', 'info']) #first argument ignored because zabbix giving it and being useless
    if not opts:
        #print ('No options supplied, only updating the database')
        print("3")
        sys.exit(3)
    else:
        for opt, arg in opts:
            if opt in ('-h', '--help'):
                usage()
                sys.exit(0)
            elif opt in ('-c', '--company'):
                tested_company = arg
            elif opt == '-i':
                displayInfos=1
except getopt.GetoptError as e:
    print (e)
    usage()
    sys.exit(3)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.