@Nonnull private Options getOptions() { Options options = new Options(); options.addOption("c", true, "Continue processing even if one of the JSON configuration file is invalid."); options.addOption("j", true, "Directory where json configuration is stored. Default is ."); options.addOption("f", true, "A single json file to execute."); options.addOption("e", false, "Run endlessly. Default false."); options.addOption("s", true, "Seconds between server job runs (not defined with cron). Default: 60"); options.addOption(OptionBuilder .withArgName("a") .withLongOpt("additionalJars") .hasArgs() .withValueSeparator(',') .withDescription("Comma delimited list of additional jars to add to the class path") .create("a")); options.addOption("h", false, "Help"); return options; } }
/** * Create an instance with common options (help, verbose, etc...). * * @param cliname the name of the command * @param includeHiveConf include "hiveconf" as an option if true */ @SuppressWarnings("static-access") public CommonCliOptions(String cliname, boolean includeHiveConf) { this.cliname = cliname; // [-v|--verbose] OPTIONS.addOption(new Option("v", "verbose", false, "Verbose mode")); // [-h|--help] OPTIONS.addOption(new Option("h", "help", false, "Print help information")); if (includeHiveConf) { OPTIONS.addOption(OptionBuilder .withValueSeparator() .hasArgs(2) .withArgName("property=value") .withLongOpt("hiveconf") .withDescription("Use value for given property") .create()); } }
/** * Create an instance with common options (help, verbose, etc...). * * @param cliname the name of the command * @param includeHiveConf include "hiveconf" as an option if true */ @SuppressWarnings("static-access") public CommonCliOptions(String cliname, boolean includeHiveConf) { this.cliname = cliname; // [-v|--verbose] OPTIONS.addOption(new Option("v", "verbose", false, "Verbose mode")); // [-h|--help] OPTIONS.addOption(new Option("h", "help", false, "Print help information")); if (includeHiveConf) { OPTIONS.addOption(OptionBuilder .withValueSeparator() .hasArgs(2) .withArgName("property=value") .withLongOpt("hiveconf") .withDescription("Use value for given property") .create()); } }
options.addOption(OptionBuilder.withValueSeparator().hasArgs(2).withArgName("property=value") .withLongOpt("hiveconf").withDescription("Use value for given property").create()); options.addOption(OptionBuilder.withValueSeparator().hasArgs(2).withArgName("key=value") .withLongOpt("define").withDescription("Variable substitution to apply to Hive commands. e" + ".g. -d A=B or --define A=B").create('d')); options.addOption(OptionBuilder.withValueSeparator().hasArgs(2).withArgName("key=value") .withLongOpt("hivevar").withDescription("Variable substitution to apply to Hive commands. " + "e.g. --hivevar A=B").create());
.withValueSeparator() .hasArgs(2) .withArgName("property=value")
.withValueSeparator() .hasArgs(2) .withArgName("property=value")
.withValueSeparator() .hasArgs(2) .withArgName("property=value") .withValueSeparator() .hasArgs(2) .withArgName("key=value") .withValueSeparator() .hasArgs(2) .withArgName("key=value")
.withValueSeparator() .hasArgs(2) .withArgName("property=value")
.withValueSeparator() .hasArgs(2) .withArgName("property=value") .withValueSeparator() .hasArgs(2) .withArgName("key=value") .withValueSeparator() .hasArgs(2) .withArgName("key=value")
options.addOption(OptionBuilder.withValueSeparator().hasArgs(2).withArgName("property=value") .withLongOpt(OPTION_HIVECONF) .withDescription("Use value for given property. Overridden by explicit parameters") .withDescription("The size of the service AppMaster container in MB").create('b')); options.addOption(OptionBuilder.withValueSeparator().hasArgs(2).withArgName("property=value") .withLongOpt(OPTION_SERVICE_APPCONFIG_GLOBAL) .withDescription("Property (key=value) to be set in the global section of the Service appConfig")
.withValueSeparator() .withDescription("custom properties") .create("D");
@Test public void testPropertyOptionMultipleValues() throws Exception { Options opts = new Options(); opts.addOption(OptionBuilder.hasArgs().withValueSeparator(',').create('k')); Properties properties = new Properties(); properties.setProperty( "k", "one,two" ); String[] values = new String[] { "one", "two" }; CommandLine cmd = parse(parser, opts, null, properties); assertTrue( cmd.hasOption("k") ); assertTrue( Arrays.equals( values, cmd.getOptionValues('k') ) ); }
options.addOption(OptionBuilder.withLongOpt("h").hasArg().withDescription("set -h").create('h')); options.addOption(OptionBuilder.withLongOpt("i").withDescription("set -i").create('i')); options.addOption(OptionBuilder.withLongOpt("j").hasArgs().withDescription("set -j").withValueSeparator('=').create('j')); options.addOption(OptionBuilder.withLongOpt("k").hasArgs().withDescription("set -k").withValueSeparator('=').create('k')); options.addOption(OptionBuilder.withLongOpt("m").hasArgs().withDescription("set -m").withValueSeparator().create('m'));
.hasArgs(2) .withArgName("property=value") .withValueSeparator() .withDescription("use hadoop value for given property") .create('D'));
.withValueSeparator() .withDescription("Set time limit for execution, in minutes") .create("l"); .withValueSeparator() .withDescription("Age (in days) of cache item before being recomputed") .create("a"); .withValueSeparator() .withDescription("The NLT server address") .create("s"); .withValueSeparator() .withDescription("Number of results per item") .create("r"); .withValueSeparator() .withDescription("Use the specified configuration file") .create();
@Test public void testPropertiesOption2() throws Exception { String[] args = new String[] { "-Dparam1", "-Dparam2=value2", "-D"}; Options options = new Options(); options.addOption(OptionBuilder.withValueSeparator().hasOptionalArgs(2).create('D')); CommandLine cl = parser.parse(options, args); Properties props = cl.getOptionProperties("D"); assertNotNull("null properties", props); assertEquals("number of properties in " + props, 2, props.size()); assertEquals("property 1", "true", props.getProperty("param1")); assertEquals("property 2", "value2", props.getProperty("param2")); List<?> argsleft = cl.getArgList(); assertEquals("Should be no arg left", 0, argsleft.size()); }
@Test public void testPropertiesOption1() throws Exception { String[] args = new String[] { "-Jsource=1.5", "-J", "target", "1.5", "foo" }; Options options = new Options(); options.addOption(OptionBuilder.withValueSeparator().hasArgs(2).create('J')); CommandLine cl = parser.parse(options, args); List<String> values = Arrays.asList(cl.getOptionValues("J")); assertNotNull("null values", values); assertEquals("number of values", 4, values.size()); assertEquals("value 1", "source", values.get(0)); assertEquals("value 2", "1.5", values.get(1)); assertEquals("value 3", "target", values.get(2)); assertEquals("value 4", "1.5", values.get(3)); List<?> argsleft = cl.getArgList(); assertEquals("Should be 1 arg left", 1, argsleft.size()); assertEquals("Expecting foo", "foo", argsleft.get(0)); }
@Test public void testGetOptionProperties() throws Exception { String[] args = new String[] { "-Dparam1=value1", "-Dparam2=value2", "-Dparam3", "-Dparam4=value4", "-D", "--property", "foo=bar" }; Options options = new Options(); options.addOption(OptionBuilder.withValueSeparator().hasOptionalArgs(2).create('D')); options.addOption(OptionBuilder.withValueSeparator().hasArgs(2).withLongOpt("property").create()); Parser parser = new GnuParser(); CommandLine cl = parser.parse(options, args); Properties props = cl.getOptionProperties("D"); assertNotNull("null properties", props); assertEquals("number of properties in " + props, 4, props.size()); assertEquals("property 1", "value1", props.getProperty("param1")); assertEquals("property 2", "value2", props.getProperty("param2")); assertEquals("property 3", "true", props.getProperty("param3")); assertEquals("property 4", "value4", props.getProperty("param4")); assertEquals("property with long format", "bar", cl.getOptionProperties("property").getProperty("foo")); }
options.addOption( OptionBuilder.withDescription( "use value for given property" ) .hasArgs() .withValueSeparator() .create( 'D' ) );
options.addOption( OptionBuilder.withValueSeparator( '=' ).hasArgs().create( 'D' ) ); options.addOption( OptionBuilder.withValueSeparator( ':' ).hasArgs().create( 'p' ) ); String[] args = new String[] { "-DJAVA_HOME=/opt/java" , "-pfile1:file2:file3" };