private static Option option(int argCount, String shortName, String longName, String argName, String description) { Option option = OptionBuilder.hasArgs(argCount) .withArgName(argName) .withLongOpt(longName) .withDescription(description) .create(shortName); return option; }
@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; } }
.hasArgs(2) .withArgName("property=value") .withLongOpt("hiveconf") .hasArgs(1) .withArgName("versionNumber") .withLongOpt("deregister") .hasArgs(0) .withLongOpt("listHAPeers") .withDescription("List all HS2 instances when running in Active Passive HA mode") .hasArgs(1) .withArgName("workerIdentity") .withLongOpt("failover")
/** * 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());
.hasArgs(2) .withArgName("property=value") .withLongOpt("hiveconf")
public static void main(String[] args) { MetastoreSchemaTool tool = new HiveSchemaTool(); OptionGroup additionalGroup = new OptionGroup(); Option metaDbTypeOpt = OptionBuilder.withArgName("metaDatabaseType") .hasArgs().withDescription("Used only if upgrading the system catalog for hive") .create("metaDbType"); additionalGroup.addOption(metaDbTypeOpt); System.setProperty(MetastoreConf.ConfVars.SCHEMA_VERIFICATION.getVarname(), "true"); System.exit(tool.run(System.getenv("HIVE_HOME"), args, additionalGroup, new HiveConf(HiveSchemaTool.class))); } }
.hasArgs(2) .withArgName("property=value") .withLongOpt("hiveconf")
@Test public void test15648() throws Exception { CommandLineParser parser = new PosixParser(); final String[] args = new String[] { "-m", "\"Two Words\"" }; Option m = OptionBuilder.hasArgs().create("m"); Options options = new Options(); options.addOption( m ); CommandLine line = parser.parse( options, args ); assertEquals( "Two Words", line.getOptionValue( "m" ) ); }
@Test public void testUnlimitedArgs() throws Exception { String[] args = new String[]{"-e", "one", "two", "-f", "alpha"}; Options options = new Options(); options.addOption(OptionBuilder.hasArgs().create("e")); options.addOption(OptionBuilder.hasArgs().create("f")); CommandLine cl = parser.parse(options, args); assertTrue("Confirm -e is set", cl.hasOption("e")); assertEquals("number of arg for -e", 2, cl.getOptionValues("e").length); assertTrue("Confirm -f is set", cl.hasOption("f")); assertEquals("number of arg for -f", 1, cl.getOptionValues("f").length); }
@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("d", "d", true, "set -d"); options.addOption(OptionBuilder.withLongOpt("e").hasArgs().withDescription("set -e ").create('e')); options.addOption("f", "f", false, "jk"); options.addOption(OptionBuilder.withLongOpt("g").hasArgs(2).withDescription("set -g").create('g')); 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'));
@Test public void testOptionArgNumbers() { Option opt = OptionBuilder.withDescription( "option description" ) .hasArgs( 2 ) .create( 'o' ); assertEquals( 2, opt.getArgs() ); }
@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)); }
options.addOption( "buildfile", true, "use given buildfile" ); options.addOption( OptionBuilder.withDescription( "use value for given property" ) .hasArgs() .withValueSeparator() .create( 'D' ) );
@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")); }
@Test public void testCompleteOption( ) { Option simple = OptionBuilder.withLongOpt( "simple option") .hasArg( ) .isRequired( ) .hasArgs( ) .withType( Float.class ) .withDescription( "this is a simple option" ) .create( 's' ); assertEquals( "s", simple.getOpt() ); assertEquals( "simple option", simple.getLongOpt() ); assertEquals( "this is a simple option", simple.getDescription() ); assertEquals( simple.getType(), Float.class ); assertTrue( simple.hasArg() ); assertTrue( simple.isRequired() ); assertTrue( simple.hasArgs() ); }
@Test public void testTwoCompleteOptions( ) { Option simple = OptionBuilder.withLongOpt( "simple option") .hasArg( ) .isRequired( ) .hasArgs( ) .withType( Float.class ) .withDescription( "this is a simple option" ) .create( 's' ); assertEquals( "s", simple.getOpt() ); assertEquals( "simple option", simple.getLongOpt() ); assertEquals( "this is a simple option", simple.getDescription() ); assertEquals( simple.getType(), Float.class ); assertTrue( simple.hasArg() ); assertTrue( simple.isRequired() ); assertTrue( simple.hasArgs() ); simple = OptionBuilder.withLongOpt( "dimple option") .hasArg( ) .withDescription( "this is a dimple option" ) .create( 'd' ); assertEquals( "d", simple.getOpt() ); assertEquals( "dimple option", simple.getLongOpt() ); assertEquals( "this is a dimple option", simple.getDescription() ); assertEquals( String.class, simple.getType() ); assertTrue( simple.hasArg() ); assertTrue( !simple.isRequired() ); assertTrue( !simple.hasArgs() ); }