我正在编写的第一个函数式 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
。关于python - 如果我指定了用户名,为什么我的 getopts 会失败?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30807685/