1 /* 2 * Copyright (C) 2012 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.caliper.options; 18 19 import com.google.caliper.util.InvalidCommandException; 20 21 import dagger.Module; 22 import dagger.Provides; 23 24 import java.io.File; 25 26 import javax.inject.Singleton; 27 28 /** 29 * Bindings for Caliper command line options. 30 */ 31 @Module 32 public final class OptionsModule { 33 34 private static final String[] EMPTY_ARGS = new String[] {}; 35 36 private final String[] args; 37 38 private boolean requireBenchmarkClassName; 39 40 /** 41 * Return a module that will provide access to configuration options and the name of the 42 * benchmark class. 43 * 44 * @param args the arguments from which the configuration options and the benchmark class name 45 * are parsed; must have one non-option value that is the benchmark class name. 46 */ withBenchmarkClass(String [] args)47 public static OptionsModule withBenchmarkClass(String [] args) { 48 return new OptionsModule(args, true); 49 } 50 51 /** 52 * Return a module that will provide access to configuration options without the name of the 53 * benchmark class. 54 * 55 * @param args the arguments from which the configuration options are parsed; it must have no 56 * non-option values. 57 */ withoutBenchmarkClass(String [] args)58 public static OptionsModule withoutBenchmarkClass(String [] args) { 59 return new OptionsModule(args, false); 60 } 61 62 /** 63 * Return a module that will provide access to the default configuration options. 64 */ defaultOptionsModule()65 public static OptionsModule defaultOptionsModule() { 66 return new OptionsModule(EMPTY_ARGS, false); 67 } 68 OptionsModule(String[] args, boolean requireBenchmarkClassName)69 public OptionsModule(String[] args, boolean requireBenchmarkClassName) { 70 this.args = args.clone(); // defensive copy, just in case 71 this.requireBenchmarkClassName = requireBenchmarkClassName; 72 } 73 74 @Provides 75 @Singleton provideOptions()76 CaliperOptions provideOptions() throws InvalidCommandException { 77 return ParsedOptions.from(args, requireBenchmarkClassName); 78 } 79 provideCaliperDirectory(CaliperOptions options)80 @Provides @CaliperDirectory static File provideCaliperDirectory(CaliperOptions options) { 81 return options.caliperDirectory(); 82 } 83 } 84