List of usage examples for org.apache.commons.cli Option getOpt
public String getOpt()
From source file:org.eu.eark.textclassification.TextClassifierJob.java
@SuppressWarnings("static-access") protected int parseArgs(String[] args) { Options cliOptions = new Options(); Option tableOption = OptionBuilder.isRequired(false).withArgName("table name").hasArg() .withDescription("Lily table name").withLongOpt("table").create("t"); cliOptions.addOption(tableOption);/*w ww .j av a2s . c o m*/ Option zkOption = OptionBuilder.isRequired().withArgName("connection-string").hasArg() .withDescription("ZooKeeper connection string: hostname1:port,hostname2:port,...") .withLongOpt("zookeeper").create("z"); cliOptions.addOption(zkOption); // command line parameters (all mandatory) for text classification: // -i: a file that contains on every line <path>,<application/type> for every Lily object that should be classified - must be on HDFS! Option inputOption = OptionBuilder.isRequired().withArgName("input").hasArg() .withDescription("File with paths and contentTypes (as saved in Lily)").withLongOpt("input") .create("i"); cliOptions.addOption(inputOption); // -c: the python file containing the classifier that will be used (requires a path on the local filesystem) Option classifierOption = OptionBuilder.isRequired().withArgName("classifier script").hasArg() .withDescription("path to Python classifier script").withLongOpt("pyclf").create("c"); cliOptions.addOption(classifierOption); // -m: the model used for classification (requires a path on the local filesystem, to <model>.pkl - all other .pkl files for this model must be in the same folder) Option modelOption = OptionBuilder.isRequired().withArgName("model").hasArg() .withDescription("path to model").withLongOpt("model").create("m"); cliOptions.addOption(modelOption); CommandLineParser parser = new PosixParser(); CommandLine cmd; try { cmd = parser.parse(cliOptions, args); } catch (ParseException e) { System.out.println(e.getMessage()); System.out.println(); HelpFormatter help = new HelpFormatter(); help.printHelp(getClass().getSimpleName(), cliOptions, true); return 1; } tableName = cmd.getOptionValue(tableOption.getOpt()); zkConnectString = cmd.getOptionValue(zkOption.getOpt()); inputFile = cmd.getOptionValue(inputOption.getOpt()); classifier = cmd.getOptionValue(classifierOption.getOpt()); model = cmd.getOptionValue(modelOption.getOpt()); return 0; }
From source file:org.fcrepo.importexport.ArgParser.java
/** * This method writes the configuration file to disk. The current * implementation omits the user/password information. * @param args to be persisted/*from ww w . j a v a 2 s . c o m*/ */ private void saveConfig(final CommandLine cmd) { final File configFile = new File(System.getProperty("java.io.tmpdir"), CONFIG_FILE_NAME); // Leave existing config file alone if (configFile.exists()) { logger.info("Configuration file exists, new file will NOT be created: {}", configFile.getPath()); return; } // Write config to file try (final BufferedWriter configWriter = new BufferedWriter(new FileWriter(configFile));) { for (Option option : cmd.getOptions()) { // write out all but the username/password if (!option.getOpt().equals("u")) { configWriter.write("-" + option.getOpt()); configWriter.newLine(); if (option.getValue() != null) { configWriter.write(option.getValue()); configWriter.newLine(); } } } logger.info("Saved configuration to: {}", configFile.getPath()); } catch (IOException e) { throw new RuntimeException("Unable to write configuration file due to: " + e.getMessage(), e); } }
From source file:org.finra.datagenerator.samples.CmdLine.java
/** * Prints the help on the command line/*from w w w. ja v a2s . c o m*/ * * @param options Options object from commons-cli */ public static void printHelp(final Options options) { Collection<Option> c = options.getOptions(); System.out.println("Command line options are:"); int longestLongOption = 0; for (Option op : c) { if (op.getLongOpt().length() > longestLongOption) { longestLongOption = op.getLongOpt().length(); } } longestLongOption += 2; String spaces = StringUtils.repeat(" ", longestLongOption); for (Option op : c) { System.out.print("\t-" + op.getOpt() + " --" + op.getLongOpt()); if (op.getLongOpt().length() < spaces.length()) { System.out.print(spaces.substring(op.getLongOpt().length())); } else { System.out.print(" "); } System.out.println(op.getDescription()); } }
From source file:org.finra.dm.core.ArgumentParser.java
/** * Query to see if an option has been set. * * @param option the option that we want to query for * * @return true if set, false if not/*w ww . ja v a 2 s.c o m*/ */ @SuppressFBWarnings(value = "UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR", justification = "This is a false positive. A null check is present.") public Boolean getBooleanValue(Option option) throws IllegalStateException { ensureCommandLineNotNull(); return commandLine.hasOption(option.getOpt()); }
From source file:org.finra.dm.core.ArgumentParser.java
/** * Retrieves the argument as a String object, if any, of an option. * * @param option the option that we want argument value to be returned for * * @return Value of the argument if option is set, and has an argument, otherwise defaultValue. *//*from w w w . j a v a 2s . co m*/ @SuppressFBWarnings(value = "UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR", justification = "This is a false positive. A null check is present.") public String getStringValue(Option option) throws IllegalStateException { ensureCommandLineNotNull(); return commandLine.getOptionValue(option.getOpt()); }
From source file:org.finra.dm.core.ArgumentParserTest.java
@Test public void testAddArgument() { Option opt1 = new Option("a", "some_flag", false, "Some flag parameter"); Option opt2 = new Option("b", "some_parameter", true, "Some parameter with an argument"); List<String> optionsIn = Arrays.asList(optionToString(opt1), optionToString(opt2)); ArgumentParser argParser = new ArgumentParser(""); argParser.addArgument(opt1, true);// w w w . j a v a 2s. c om argParser.addArgument(opt2.getOpt(), opt2.getLongOpt(), opt2.hasArg(), opt2.getDescription(), false); Collection resultOptions = argParser.getConfiguredOptions(); List<String> optionsOut = new ArrayList<>(); for (Object obj : resultOptions) { assertTrue(obj instanceof Option); optionsOut.add(optionToString((Option) obj)); } optionsOut.containsAll(optionsIn); optionsIn.containsAll(optionsOut); }
From source file:org.finra.dm.core.ArgumentParserTest.java
@Test public void testGetUsageInformation() { List<Option> optionsIn = new ArrayList<>(); optionsIn.add(new Option("a", "some_flag", false, "Some flag parameter")); optionsIn.add(new Option("b", "some_parameter", true, "Some parameter with an argument")); ArgumentParser argParser = new ArgumentParser("TestApp"); argParser.addArgument(optionsIn.get(0), false); argParser.addArgument(optionsIn.get(1), true); String usage = argParser.getUsageInformation(); assertNotNull(usage);//from ww w . j a va 2s.co m assertTrue(usage.contains(String.format("usage: %s", argParser.getApplicationName()))); for (Option option : optionsIn) { assertTrue(usage.contains(String.format("-%s,", option.getOpt()))); assertTrue(usage.contains(String.format("--%s", option.getLongOpt()))); assertTrue(usage.contains(option.getDescription())); assertTrue(!option.hasArg() || usage.contains(String.format("<%s>", option.getArgName()))); } }
From source file:org.finra.dm.core.ArgumentParserTest.java
@Test public void testGetBooleanValue() throws ParseException { ArgumentParser argParser = new ArgumentParser(""); Option boolOpt = argParser.addArgument("b", "bool", false, "Some optional configuration flag", false); Boolean resultValue;//from ww w.j a v a 2 s . com final String shortBoolOpt = String.format("-%s", boolOpt.getOpt()); final String longBoolOpt = String.format("--%s", boolOpt.getLongOpt()); argParser.parseArguments(new String[] {}); resultValue = argParser.getBooleanValue(boolOpt); assertNotNull(resultValue); assertFalse(resultValue); argParser.parseArguments(new String[] { shortBoolOpt }); resultValue = argParser.getBooleanValue(boolOpt); assertNotNull(resultValue); assertTrue(resultValue); argParser.parseArguments(new String[] { longBoolOpt }); resultValue = argParser.getBooleanValue(boolOpt); assertNotNull(resultValue); assertTrue(resultValue); }
From source file:org.finra.dm.core.ArgumentParserTest.java
@Test public void testGetStringValueAsBoolean() throws ParseException { ArgumentParser argParser = new ArgumentParser(""); Option strOpt = argParser.addArgument("s", "str", true, "Some string input parameter to have a boolean value", false); final String shortStrOpt = String.format("-%s", strOpt.getOpt()); final String longStrOpt = String.format("--%s", strOpt.getLongOpt()); // Validate the default value - no option is specified. argParser.parseArguments(new String[] {}); assertFalse(argParser.getStringValueAsBoolean(strOpt, false)); assertTrue(argParser.getStringValueAsBoolean(strOpt, true)); // Validate all "true" boolean values using both short an long options. for (String inputValue : Arrays.asList(CustomBooleanEditor.VALUE_TRUE, CustomBooleanEditor.VALUE_YES, CustomBooleanEditor.VALUE_ON, CustomBooleanEditor.VALUE_1)) { argParser.parseArguments(new String[] { shortStrOpt, inputValue }); assertTrue(argParser.getStringValueAsBoolean(strOpt, false)); argParser.parseArguments(new String[] { longStrOpt, inputValue }); assertTrue(argParser.getStringValueAsBoolean(strOpt, false)); }//from ww w . ja v a 2 s. c o m // Validate all "false" boolean values. for (String inputValue : Arrays.asList(CustomBooleanEditor.VALUE_FALSE, CustomBooleanEditor.VALUE_NO, CustomBooleanEditor.VALUE_OFF, CustomBooleanEditor.VALUE_0)) { argParser.parseArguments(new String[] { shortStrOpt, inputValue }); assertFalse(argParser.getStringValueAsBoolean(strOpt, true)); } // Try to parse an invalid boolean value. argParser.parseArguments(new String[] { shortStrOpt, INVALID_BOOLEAN_VALUE }); try { argParser.getStringValueAsBoolean(strOpt, false); fail("Suppose to throw a ParseException when option has an invalid boolean value."); } catch (ParseException e) { assertEquals(String.format("Invalid boolean value [%s]", INVALID_BOOLEAN_VALUE), e.getMessage()); } }
From source file:org.finra.dm.core.ArgumentParserTest.java
@Test public void testGetStringValue() throws ParseException { final String testDefaultValue = "default_str_value"; ArgumentParser argParser = new ArgumentParser(""); Option strOpt = argParser.addArgument("s", "str", true, "Some string input parameter", false); String inputValue;//from w w w . j a v a2s . c o m String resultValue; final String shortStrOpt = String.format("-%s", strOpt.getOpt()); final String longStrOpt = String.format("--%s", strOpt.getLongOpt()); argParser.parseArguments(new String[] {}); assertNull(argParser.getStringValue(strOpt)); assertEquals(testDefaultValue, argParser.getStringValue(strOpt, testDefaultValue)); inputValue = "my_string_value_1"; argParser.parseArguments(new String[] { shortStrOpt, inputValue }); resultValue = argParser.getStringValue(strOpt); assertNotNull(resultValue); assertEquals(inputValue, resultValue); inputValue = "my_string_value_2"; argParser.parseArguments(new String[] { shortStrOpt, inputValue }); resultValue = argParser.getStringValue(strOpt, testDefaultValue); assertNotNull(resultValue); assertEquals(inputValue, resultValue); inputValue = "my_string_value_3"; argParser.parseArguments(new String[] { longStrOpt, inputValue }); resultValue = argParser.getStringValue(strOpt); assertNotNull(resultValue); assertEquals(inputValue, resultValue); }