python script parsing multiple arguments
Question: 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
    


Answer: 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
    

