Python , getopt , 如何获取列表
Python , getopt , how to get a list
我正在尝试使用库 getopt
来捕获从命令行给出的路径列表,如下所示:
python script.py -l ["a","b","c","d","e"] -p 80 ....
我写的是这样的:
def getValue(self):
'''
get value from command line and initialize
variable !
'''
try:
opts,args = getopt.getopt( self.args ,
"hl:us:r:p:" ,
['help','local_path','update',"remote_host","remote_path","parameter"])
except getopt.GetoptError as err:
print(str(err))
self.usage()
## ----------------- SETTING VARIABLE PASSED BY COMMAND LINE ------------------ ##
for opt,arg in opts:
#----------------------------------------------------------
if opt in ("-l","--local_path"):
self.local_path = arg
if DEBUG:
print('local path: ',self.local_path)
#-----------------------------------------------------------
elif opt in ("-h", "--help"):
self.usage()
#-----------------------------------------------------------
elif opt in ("-s", "--remote_host"):
self.remote_host = arg
if DEBUG:
print('Simulation host: ', self.remote_host)
#-----------------------------------------------------------
elif opt in ("-r", "--remote_path"):
self.remote_path = arg
if DEBUG:
print('Simulation path: ', self.remote_path)
#-----------------------------------------------------------
elif opt in ("-p", "--parameter"):
self.parameter = arg
if DEBUG:
print('Simulation parameter: ',self.parameter)
#-----------------------------------------------------------
elif opt in ("-u","--update"):
#if self.remote_host and self.remote_path:
self.update()
#-----------------------------------------------------------
else:
assert False, "Unhandled Option"
#-----------------------------------------------------------
但不幸的是,这对每个选项(-l、-p ....)只取一个值
怎样才能达到我的目的?
提前致谢!!
我稍微简化了您的脚本以专门解决您关于从命令行传递参数列表的问题。
您有一个选择是多次指定相同的标志以将多个参数发送到您的程序中。例如:
import getopt
import sys
opts, args = getopt.getopt(
sys.argv[1:],
'hl:p:',
['help', 'local_path', 'parameter'],
)
local_paths = []
for opt, arg in opts:
if opt in ('-l', '--local_path'):
local_paths.append(arg)
print(opt + ': ' + arg)
if opt in ('-p', '--parameter'):
print(opt + ': ' + arg)
print('local_paths: ' + str(local_paths))
使用如下:
$ python script.py -la -lb -p 80
-l: a
-l: b
-p: 80
local_paths: ['a', 'b']
另一个选项(如果您必须通过命令行将列表本身传递给标志的单个实例)是使用某种序列化。 JSON 可以完成任务,但您也可以使用 csv 或其他格式。示例:
import getopt
import json
import sys
opts, args = getopt.getopt(
sys.argv[1:],
'hl:p:',
['help', 'local_path', 'parameter'],
)
for opt, arg in opts:
if opt in ('-l', '--local_path'):
list_arg = json.loads(arg)
print(opt + ': ' + str(list_arg))
if opt in ('-p', '--parameter'):
print(opt + ': ' + arg)
$ python script.py -l '["a","b"]' -p 80
-l: ['a', 'b']
-p: 80
请注意 -l
标志 ('["a","b"]'
) 之后 JSON 周围的引号 ('
)。此符号“保护”参数不被 bash.
评估
如果您按照示例中的方式传递参数,python 仍会收到一个参数,但它并不像我认为的那样有效:
import getopt
import sys
opts, args = getopt.getopt(
sys.argv[1:],
'hl:',
['help', 'local_path'],
)
for opt, arg in opts:
if opt in ('-l', '--local_path'):
print(opt + ': ' + arg)
print('type: ' + str(type(opt)))
$ python script.py -l ["a","b"]
-l: [a,b]
type: <class 'str'>
-l
标志的参数实际上是 python 中的字符串 "[a,b]"
。发生这种情况是因为 bash 在 运行 脚本之前评估了表达式 ["a","b"]
,所以现在 python 中的反序列化有点棘手。避免这种方式并坚持使用标准序列化模式可能是值得的。
从the docs澄清上面的符号:
shortopts
is the string of option letters that the script wants to recognize, with options that require an argument followed by a colon (':')
所以 hl:
意味着我们接受 -h
和 -l
,但是 -l
必须有一个参数,否则我们会得到这样的东西:
getopt.GetoptError: option -l requires argument
我正在尝试使用库 getopt
来捕获从命令行给出的路径列表,如下所示:
python script.py -l ["a","b","c","d","e"] -p 80 ....
我写的是这样的:
def getValue(self):
'''
get value from command line and initialize
variable !
'''
try:
opts,args = getopt.getopt( self.args ,
"hl:us:r:p:" ,
['help','local_path','update',"remote_host","remote_path","parameter"])
except getopt.GetoptError as err:
print(str(err))
self.usage()
## ----------------- SETTING VARIABLE PASSED BY COMMAND LINE ------------------ ##
for opt,arg in opts:
#----------------------------------------------------------
if opt in ("-l","--local_path"):
self.local_path = arg
if DEBUG:
print('local path: ',self.local_path)
#-----------------------------------------------------------
elif opt in ("-h", "--help"):
self.usage()
#-----------------------------------------------------------
elif opt in ("-s", "--remote_host"):
self.remote_host = arg
if DEBUG:
print('Simulation host: ', self.remote_host)
#-----------------------------------------------------------
elif opt in ("-r", "--remote_path"):
self.remote_path = arg
if DEBUG:
print('Simulation path: ', self.remote_path)
#-----------------------------------------------------------
elif opt in ("-p", "--parameter"):
self.parameter = arg
if DEBUG:
print('Simulation parameter: ',self.parameter)
#-----------------------------------------------------------
elif opt in ("-u","--update"):
#if self.remote_host and self.remote_path:
self.update()
#-----------------------------------------------------------
else:
assert False, "Unhandled Option"
#-----------------------------------------------------------
但不幸的是,这对每个选项(-l、-p ....)只取一个值 怎样才能达到我的目的? 提前致谢!!
我稍微简化了您的脚本以专门解决您关于从命令行传递参数列表的问题。
您有一个选择是多次指定相同的标志以将多个参数发送到您的程序中。例如:
import getopt
import sys
opts, args = getopt.getopt(
sys.argv[1:],
'hl:p:',
['help', 'local_path', 'parameter'],
)
local_paths = []
for opt, arg in opts:
if opt in ('-l', '--local_path'):
local_paths.append(arg)
print(opt + ': ' + arg)
if opt in ('-p', '--parameter'):
print(opt + ': ' + arg)
print('local_paths: ' + str(local_paths))
使用如下:
$ python script.py -la -lb -p 80
-l: a
-l: b
-p: 80
local_paths: ['a', 'b']
另一个选项(如果您必须通过命令行将列表本身传递给标志的单个实例)是使用某种序列化。 JSON 可以完成任务,但您也可以使用 csv 或其他格式。示例:
import getopt
import json
import sys
opts, args = getopt.getopt(
sys.argv[1:],
'hl:p:',
['help', 'local_path', 'parameter'],
)
for opt, arg in opts:
if opt in ('-l', '--local_path'):
list_arg = json.loads(arg)
print(opt + ': ' + str(list_arg))
if opt in ('-p', '--parameter'):
print(opt + ': ' + arg)
$ python script.py -l '["a","b"]' -p 80
-l: ['a', 'b']
-p: 80
请注意 -l
标志 ('["a","b"]'
) 之后 JSON 周围的引号 ('
)。此符号“保护”参数不被 bash.
如果您按照示例中的方式传递参数,python 仍会收到一个参数,但它并不像我认为的那样有效:
import getopt
import sys
opts, args = getopt.getopt(
sys.argv[1:],
'hl:',
['help', 'local_path'],
)
for opt, arg in opts:
if opt in ('-l', '--local_path'):
print(opt + ': ' + arg)
print('type: ' + str(type(opt)))
$ python script.py -l ["a","b"]
-l: [a,b]
type: <class 'str'>
-l
标志的参数实际上是 python 中的字符串 "[a,b]"
。发生这种情况是因为 bash 在 运行 脚本之前评估了表达式 ["a","b"]
,所以现在 python 中的反序列化有点棘手。避免这种方式并坚持使用标准序列化模式可能是值得的。
从the docs澄清上面的符号:
shortopts
is the string of option letters that the script wants to recognize, with options that require an argument followed by a colon (':')
所以 hl:
意味着我们接受 -h
和 -l
,但是 -l
必须有一个参数,否则我们会得到这样的东西:
getopt.GetoptError: option -l requires argument