我正在编写我正在编写的第一个函数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)
提前致谢!
答案 0 :(得分:3)
您将-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
。