mah*_*ood 4 python arrays optparse command-line-arguments
有一个python脚本从命令行读取基准名称,如下所示:
-b benchname1
Run Code Online (Sandbox Code Playgroud)
这个代码的代码是:
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!"
Run Code Online (Sandbox Code Playgroud)
我想要做的是为这个命令行创建一个基准数组:
-b benchname1 benchname2
Run Code Online (Sandbox Code Playgroud)
所以"进程"应该是一个数组:
process[0] = Mybench.b1
process[1] = Mybench.b2
Run Code Online (Sandbox Code Playgroud)
对此有什么建议吗?
感谢名单
如果你有Python 2.7+,你可以使用argparse模块而不是optparse.
import argparse
parser = argparse.ArgumentParser(description='Process benchmarks.')
parser.add_argument("-b", "--benchmark", default=[], type=str, nargs='+',
help="The benchmark to be loaded.")
args = parser.parse_args()
print args.benchmark
Run Code Online (Sandbox Code Playgroud)
示例运行脚本 -
$ python sample.py -h
usage: sample.py [-h] [-b BENCHMARK [BENCHMARK ...]]
Process benchmarks.
optional arguments:
-h, --help show this help message and exit
-b BENCHMARK [BENCHMARK ...], --benchmark BENCHMARK [BENCHMARK ...]
The benchmark to be loaded.
$ python sample.py -b bench1 bench2 bench3
['bench1', 'bench2', 'bench3']
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7298 次 |
| 最近记录: |