4

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

3 Answers 3

8

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']
Sign up to request clarification or add additional context in comments.

Comments

4
    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')

2 Comments

than use it as 'my.py --skip a --skip b'
I used "--skip benchname1 --skip benchname2" but I get this error: "AttributeError: Values instance has no attribute 'benchmark'". The line it points is "if options.benchmark == 'benchname1':" Did you mean I replace '-b' with '-s'?
1

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])

2 Comments

should I remove my "if, elif, else" then?
ok, I found it. There is no need for "bench_map". I pass the options through ',' seperated list then split them as what you said. Finally I append them to process with "process.append(bench_name)" thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.