如果指定用户名,为什么我的 getopts 会失败?

Why does my getopts fail if I specify a username?

我正在编写的第一个功能性 python 脚本有问题,下面列出了完整的工作摘录。

我的问题是,如果我包含用户名命令行选项(-u--username),它总是空白并且会弄乱其他命令行参数。

例如,如果我 运行 使用 -n tdelane -s 10.1.213.226 -p xxx -v 的命令行参数,我将得到这样的输出:

cliloc = /usr/local/bin/cli
server = 
username = Administrator
password = 
verbose = True
o = -n
a = 
cliloc = /usr/local/bin/cli
server = 
username = 
password = 
verbose = True

如您所见,一旦达到 -n,它不仅将其设置为空,而且也不会处理任何其他内容。如果我把 -n 拿出来,它就起作用了。 运行 -s 10.1.213.226 -p xxx -v 这是输出:

cliloc = /usr/local/bin/cli
server = 
username = Administrator
password = 
verbose = True
o = -s
a = 10.1.213.226
o = -p
a = xxx
o = -v
a = 
cliloc = /usr/local/bin/cli
server = 10.1.213.226
username = Administrator
password = xxx
verbose = True

这是该脚本的相关部分:

import subprocess, getopt, sys, re

try:
    opts, args = getopt.getopt(sys.argv[1:], "cs:np:hv",                 
        ["cli", "server", "username", "password", "help", "verbose"])

except getopt.GetoptError as err:
    print str(err)
    usage()
    sys.exit(2)

cliloc = '/usr/local/bin/cli'
server = ''
username = 'Administrator'
password = ''
verbose = True

if verbose:
    print "cliloc = %s" % (cliloc)
    print "server = %s" % (server)
    print "username = %s" % (username)
    print "password = %s" % (password)
    print "verbose = %s" % (verbose)

for o, a in opts:
    if verbose:
        print 'o = ' + o
        print 'a = ' + a
    if o == "-v":
        verbose = True
    elif o in ("-h", "--help"):
        usage()
        sys.exit()
    elif o in ("-c", "--cli"):
        cliloc = a
    elif o in ("-s", "--server"):
        server = a
    elif o in ("-n", "--username"):
        username = a
    elif o in ("-p", "--password"):
        password = a
    else:
        assert False, "unhandled option"

if verbose:
    print "cliloc = %s" % (cliloc)
    print "server = %s" % (server)
    print "username = %s" % (username)
    print "password = %s" % (password)
    print "verbose = %s" % (verbose)

提前致谢!

您将 -n 配置为不接受任何参数;后面没有::

opts, args = getopt.getopt(sys.argv[1:], "cs:np:hv", 
#                                            ^ no colon after this option                
    ["cli", "server", "username", "password", "help", "verbose"])

添加冒号:

opts, args = getopt.getopt(sys.argv[1:], "cs:n:p:hv",                 
#                                             ^ insert colon here
    ["cli", "server", "username", "password", "help", "verbose"])

有了冒号,库知道要查找参数,tdelane 不会最终将所有内容都推入未解析的选项列表:

>>> import getopt
>>> import shlex
>>> args = shlex.split('-n tdelane -s 10.1.213.226 -p xxx -v')
>>> getopt.getopt(args, "cs:np:hv")
([('-n', '')], ['tdelane', '-s', '10.1.213.226', '-p', 'xxx', '-v'])
>>> getopt.getopt(args, "cs:n:p:hv")
([('-n', 'tdelane'), ('-s', '10.1.213.226'), ('-p', 'xxx'), ('-v', '')], [])

请注意,getopt 模块相当..陈旧和过时。我会在这里改用 argparse