从命令行选项创建数组 (python::optparse)
有一个 python 脚本,它从命令行读取基准名称,如下所示:
-b benchname1
这样做的代码是:
import optparse
import Mybench
parser = optparse.OptionParser()
# Benchmark options
parser.add_option("-b", "--benchmark", default="", help="The benchmark to be loaded.")
if options.benchmark == 'benchname1':
process = Mybench.b1
elif options.benchmark == 'benchname2':
process = Mybench.b2
else:
print "no such benchmark!"
我想要做的是为此命令行创建一个基准数组:
-b benchname1 benchname2
所以“进程”应该是一个数组那就是:
process[0] = Mybench.b1
process[1] = Mybench.b2
对此有什么建议吗?
谢谢
There is a python script which reads a benchmark name from command line like this:
-b benchname1
The code for this perpose is:
import optparse
import Mybench
parser = optparse.OptionParser()
# Benchmark options
parser.add_option("-b", "--benchmark", default="", help="The benchmark to be loaded.")
if options.benchmark == 'benchname1':
process = Mybench.b1
elif options.benchmark == 'benchname2':
process = Mybench.b2
else:
print "no such benchmark!"
what I want to do is to create a an array of benchmarks for this command line:
-b benchname1 benchname2
So the "process" should be an array that is:
process[0] = Mybench.b1
process[1] = Mybench.b2
Is there any suggestion for that?
Thanx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您有Python 2.7+,则可以使用 argparse 模块而不是 optparse。
脚本的示例运行 -
If you have Python 2.7+, you can use argparse module instead of optparse.
Sample run of the script -
您可以接受像这样的基准名称的逗号分隔列表
然后在代码中处理逗号分隔列表以生成数组 -
You can accept a comma separated list for benchmark names like this
Then process the comma separated list in your code to generate the array -