简体   繁体   English

python脚本解析多个参数

[英]python script parsing multiple arguments

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? 脚本启动命令行:myscript.py -c Random我在我的代码中使用了getopt(在此处给出),但是此代码未遍历参数,因为稍后在程序中未定义tests_company变量,我在哪里出错了?

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

I think you're missing an equals sign after company in your getopt call. 我认为您的getopt电话中陪伴您的company缺少等号。 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

opts variable may not get initialised and is than called outside the try statement. opts变量可能不会初始化,因此要在try语句之外调用。 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM