从命令行选项创建数组 (python::optparse)

发布于 2024-12-08 08:13:01 字数 660 浏览 1 评论 0原文

有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蝶…霜飞 2024-12-15 08:13:01

如果您有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

脚本的示例运行 -

$ 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']

If you have Python 2.7+, you can use argparse module instead of 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

Sample run of the script -

$ 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']
风柔一江水 2024-12-15 08:13:01
    self.opt_parser.add_argument('-s', '--skip',
        default=[],
        type=str,
        help='A name of a project or build group to skip. Can be repeated to skip multiple projects.',
        dest='skip',
        action='append')
    self.opt_parser.add_argument('-s', '--skip',
        default=[],
        type=str,
        help='A name of a project or build group to skip. Can be repeated to skip multiple projects.',
        dest='skip',
        action='append')
情愿 2024-12-15 08:13:01

您可以接受像这样的基准名称的逗号分隔列表

-b benchname1,benchname2

然后在代码中处理逗号分隔列表以生成数组 -

bench_map = {'benchname1': Mybench.b1,
             'benchname2': Mybench.b2,
            }
process = []

# Create a list of benchmark names of the form ['benchname1', benchname2']
benchmarks = options.benchmark.split(',')

for bench_name in benchmarks:
    process.append(bench_map[bench_name])

You can accept a comma separated list for benchmark names like this

-b benchname1,benchname2

Then process the comma separated list in your code to generate the array -

bench_map = {'benchname1': Mybench.b1,
             'benchname2': Mybench.b2,
            }
process = []

# Create a list of benchmark names of the form ['benchname1', benchname2']
benchmarks = options.benchmark.split(',')

for bench_name in benchmarks:
    process.append(bench_map[bench_name])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文