public void test11458() { Options options = new Options(); 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"}; CommandLineParser parser = new PosixParser(); try { CommandLine cmd = parser.parse(options, args); String[] values = cmd.getOptionValues('D'); assertEquals(values[0], "JAVA_HOME"); assertEquals(values[1], "/opt/java"); values = cmd.getOptionValues('p'); assertEquals(values[0], "file1"); assertEquals(values[1], "file2"); assertEquals(values[2], "file3"); java.util.Iterator iter = cmd.iterator(); while (iter.hasNext()) { Option opt = (Option) iter.next(); switch (opt.getId()) { case 'D': assertEquals(opt.getValue(0), "JAVA_HOME"); assertEquals(opt.getValue(1), "/opt/java"); break; case 'p': assertEquals(opt.getValue(0), "file1"); assertEquals(opt.getValue(1), "file2"); assertEquals(opt.getValue(2), "file3"); break; default: fail("-D option not found"); } } } catch (ParseException exp) { fail( "Unexpected Exception:\nMessage:" + exp.getMessage() + "Type: " + exp.getClass().getName()); } }
@SuppressWarnings("static-access") public OptionsProcessor() { // -database database options.addOption( OptionBuilder.hasArg() .withArgName("databasename") .withLongOpt("database") .withDescription("Specify the database to use") .create()); // -e 'quoted-query-string' options.addOption( OptionBuilder.hasArg() .withArgName("quoted-query-string") .withDescription("SQL from command line") .create('e')); // -f <query-file> options.addOption( OptionBuilder.hasArg() .withArgName("filename") .withDescription("SQL from files") .create('f')); // -i <init-query-file> options.addOption( OptionBuilder.hasArg() .withArgName("filename") .withDescription("Initialization SQL file") .create('i')); // -hiveconf x=y options.addOption( OptionBuilder.withValueSeparator() .hasArgs(2) .withArgName("property=value") .withLongOpt("hiveconf") .withDescription("Use value for given property") .create()); // Substitution option -d, --define options.addOption( OptionBuilder.withValueSeparator() .hasArgs(2) .withArgName("key=value") .withLongOpt("define") .withDescription( "Variable subsitution to apply to hive commands. e.g. -d A=B or --define A=B") .create('d')); // Substitution option --hivevar options.addOption( OptionBuilder.withValueSeparator() .hasArgs(2) .withArgName("key=value") .withLongOpt("hivevar") .withDescription("Variable subsitution to apply to hive commands. e.g. --hivevar A=B") .create()); // [-S|--silent] options.addOption(new Option("S", "silent", false, "Silent mode in interactive shell")); // [-v|--verbose] options.addOption( new Option("v", "verbose", false, "Verbose mode (echo executed SQL to the console)")); // [-H|--help] options.addOption(new Option("H", "help", false, "Print help information")); }