/** * Returns the value/first value of this Option or the * <code>defaultValue</code> if there is no value. * * @param defaultValue The value to be returned if there * is no value. * * @return the value/first value of this Option or the * <code>defaultValue</code> if there are no values. */ public String getValue(String defaultValue) { String value = getValue(); return (value != null) ? value : defaultValue; }
configuration.setContinueOnJsonError(Boolean.parseBoolean(option.getValue())); } else if (option.getOpt().equals("j")) { File jsonDir = new File(option.getValue()); if (jsonDir.exists() && jsonDir.isDirectory()) { configuration.setProcessConfigDir(jsonDir); File jsonFile = new File(option.getValue()); if (jsonFile.exists() && jsonFile.isFile()) { configuration.setProcessConfigFile(jsonFile); } else if (option.getOpt().equals("s")) { try { configuration.setRunPeriod(Integer.parseInt(option.getValue())); } catch (NumberFormatException nfe) { throw new OptionsException("Seconds between server job runs must be an integer");
public String getOptionsAsString() { StringBuilder buf = new StringBuilder(); for (Option option : commandLine.getOptions()) { buf.append(" "); buf.append(option.getOpt()); if (option.hasArg()) { buf.append("="); buf.append(option.getValue()); } } return buf.toString(); }
/** * Builds an instance of T using the selected constructor getting the constructor * parameters from the {@link CommandLine}. * * Note: this method will also automatically call {@link #applyCommandLineOptions(CommandLine, T)} on * the constructed object. */ private T buildInstance(CommandLine cli) { String[] constructorArgs = new String[this.constructor.getParameterTypes().length]; for (Option option : cli.getOptions()) { if (this.constructoArgumentsMap.containsKey(option.getOpt())) { int idx = this.constructoArgumentsMap.get(option.getOpt()); constructorArgs[idx] = option.getValue(); } } T embeddedGobblin; try { embeddedGobblin = this.constructor.newInstance((Object[]) constructorArgs); return embeddedGobblin; } catch (IllegalAccessException | InvocationTargetException | InstantiationException exc) { throw new RuntimeException("Could not instantiate " + this.klazz.getName(), exc); } }
/** * For each method for which the helper created an {@link Option} and for which the input {@link CommandLine} contains * that option, this method will automatically call the method on the input object with the correct * arguments. */ public void applyCommandLineOptions(CommandLine cli, T embeddedGobblin) { try { for (Option option : cli.getOptions()) { if (!this.methodsMap.containsKey(option.getOpt())) { // Option added by cli driver itself. continue; } if (option.hasArg()) { this.methodsMap.get(option.getOpt()).invoke(embeddedGobblin, option.getValue()); } else { this.methodsMap.get(option.getOpt()).invoke(embeddedGobblin); } } } catch (IllegalAccessException | InvocationTargetException exc) { throw new RuntimeException("Could not apply options to " + embeddedGobblin.getClass().getName(), exc); } }
/** * Returns {@link ParameterTool} for the arguments parsed by {@link GenericOptionsParser}. * * @param args Input array arguments. It should be parsable by {@link GenericOptionsParser} * @return A {@link ParameterTool} * @throws IOException If arguments cannot be parsed by {@link GenericOptionsParser} * @see GenericOptionsParser */ public static ParameterTool paramsFromGenericOptionsParser(String[] args) throws IOException { Option[] options = new GenericOptionsParser(args).getCommandLine().getOptions(); Map<String, String> map = new HashMap<String, String>(); for (Option option : options) { String[] split = option.getValue().split("="); map.put(split[0], split[1]); } return ParameterTool.fromMap(map); } }
Map<String, String> optionsMap = new HashMap<>(); for (Option opt : cmd.getOptions()) { optionsMap.put(opt.getOpt(), opt.getValue());
@Test public void testGetValue() { Option option = new Option("f", null); option.setArgs(Option.UNLIMITED_VALUES); assertEquals("default", option.getValue("default")); assertEquals(null, option.getValue(0)); option.addValueForProcessing("foo"); assertEquals("foo", option.getValue()); assertEquals("foo", option.getValue(0)); assertEquals("foo", option.getValue("default")); }
final OptionDefinition optionDefinitionSpecified = this.optionDefinitionFromCliOption.get(cliOptionSpecified); optionDefinitionSpecified.behave(commandLineBuilder, cliOptionSpecified.getValue());
String val = opt.getValue(); switch (opt.getLongOpt()) { case "output":
@Test public void testSubclass() { Option option = new DefaultOption("f", "file", "myfile.txt"); Option clone = (Option) option.clone(); assertEquals("myfile.txt", clone.getValue()); assertEquals(DefaultOption.class, clone.getClass()); }
public String getOptionsAsString() { StringBuilder buf = new StringBuilder(); for (Option option : commandLine.getOptions()) { buf.append(" "); buf.append(option.getOpt()); if (option.hasArg()) { buf.append("="); buf.append(option.getValue()); } } return buf.toString(); }
String v = option.getValue(); if (v == null || v.equals("")) { v = "true";
protected CommandLineParameters(CommandLine cmd){ for(Option opt:cmd.getOptions()){ parameters.put(revertOption(opt.getOpt()), revertOption(opt.getValue())); } for(String arg:cmd.getArgs()){ arguments.add(revertOption(arg)); } } public List<String> getArguments() {
public String getOptionsAsString() { StringBuilder buf = new StringBuilder(); for (Option option : commandLine.getOptions()) { buf.append(" "); buf.append(option.getOpt()); if (option.hasArg()) { buf.append("="); buf.append(option.getValue()); } } return buf.toString(); }
public String toString() { StringBuilder s = new StringBuilder(); if (parsedData != null) { Option[] ops = parsedData.getOptions(); for (int i = 0; i < ops.length; ++i) { s.append(ops[i].getOpt() + " = " + s.append(ops[i].getValue()) + ","); } } return s.toString(); }