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; }
@Test public void testUnambiguousPartialLongOption2() throws Exception { String[] args = new String[] { "-ver" }; Options options = new Options(); options.addOption(OptionBuilder.withLongOpt("version").create()); options.addOption(OptionBuilder.withLongOpt("help").create()); CommandLine cl = parser.parse(options, args); assertTrue("Confirm --version is set", cl.hasOption("version")); }
@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 test14786() throws Exception { Option o = OptionBuilder.isRequired().withDescription("test").create("test"); Options opts = new Options(); opts.addOption(o); opts.addOption(o); CommandLineParser parser = new GnuParser(); String[] args = new String[] { "-test" }; CommandLine line = parser.parse( opts, args ); assertTrue( line.hasOption( "test" ) ); }
@Test public void testPropertyOptionRequired() throws Exception { Options opts = new Options(); opts.addOption(OptionBuilder.isRequired().create("f")); Properties properties = new Properties(); properties.setProperty("f", "true"); CommandLine cmd = parse(parser, opts, null, properties); assertTrue(cmd.hasOption("f")); }
@Test public void testSpecialOptChars() throws Exception { // '?' Option opt1 = OptionBuilder.withDescription("help options").create('?'); assertEquals("?", opt1.getOpt()); // '@' Option opt2 = OptionBuilder.withDescription("read from stdin").create('@'); assertEquals("@", opt2.getOpt()); // ' ' try { OptionBuilder.create(' '); fail( "IllegalArgumentException not caught" ); } catch (IllegalArgumentException e) { // success } }
@Test public void testOptionArgNumbers() { Option opt = OptionBuilder.withDescription( "option description" ) .hasArgs( 2 ) .create( 'o' ); assertEquals( 2, opt.getArgs() ); }
private static Options buildGeneralOptions(Options opts) { Options r = new Options(); for (Object o : opts.getOptions()) r.addOption((Option) o); Option jar = OptionBuilder.withArgName("path").hasArg() .withDescription("topology jar of the submitted topology").create("jar"); r.addOption(jar); Option conf = OptionBuilder.withArgName("configuration file").hasArg() .withDescription("an application configuration file").create("conf"); r.addOption(conf); return r; }
@Test public void testUnambiguousPartialLongOption1() throws Exception { String[] args = new String[] { "--ver" }; Options options = new Options(); options.addOption(OptionBuilder.withLongOpt("version").create()); options.addOption(OptionBuilder.withLongOpt("help").create()); CommandLine cl = parser.parse(options, args); assertTrue("Confirm --version is set", cl.hasOption("version")); }
@Test public void test11456() throws Exception { // Posix Options options = new Options(); options.addOption( OptionBuilder.hasOptionalArg().create( 'a' ) ); options.addOption( OptionBuilder.hasArg().create( 'b' ) ); String[] args = new String[] { "-a", "-bvalue" }; CommandLineParser parser = new PosixParser(); CommandLine cmd = parser.parse( options, args ); assertEquals( cmd.getOptionValue( 'b' ), "value" ); // GNU options = new Options(); options.addOption( OptionBuilder.hasOptionalArg().create( 'a' ) ); options.addOption( OptionBuilder.hasArg().create( 'b' ) ); args = new String[] { "-a", "-b", "value" }; parser = new GnuParser(); cmd = parser.parse( options, args ); assertEquals( cmd.getOptionValue( 'b' ), "value" ); }
@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 testMissingOptionException() throws ParseException { Options options = new Options(); options.addOption(OptionBuilder.isRequired().create("f")); try { new PosixParser().parse(options, new String[0]); fail("Expected MissingOptionException to be thrown"); } catch (MissingOptionException e) { assertEquals("Missing required option: f", e.getMessage()); } }
@Test public void testBaseOptionStringOpt() { Option base = OptionBuilder.withDescription( "option description") .create( "o" ); assertEquals( "o", base.getOpt() ); assertEquals( "option description", base.getDescription() ); assertTrue( !base.hasArg() ); }
public BeelineOptionsProcessor() { options.addOption(OptionBuilder.hasArg().withArgName("url").create('u')); options.addOption(OptionBuilder.hasArg().withArgName("username").create('n')); options.addOption(OptionBuilder.hasArg().withArgName("password").create('p')); }
@Test public void test11457() throws Exception { Options options = new Options(); options.addOption(OptionBuilder.withLongOpt("verbose").create()); String[] args = new String[]{"--verbose"}; CommandLineParser parser = new PosixParser(); CommandLine cmd = parser.parse(options, args); assertTrue(cmd.hasOption("verbose")); }
@Test public void testGetParsedOptionValue() throws Exception { Options options = new Options(); options.addOption(OptionBuilder.hasArg().withType(Number.class).create("i")); options.addOption(OptionBuilder.hasArg().create("f")); CommandLineParser parser = new DefaultParser(); CommandLine cmd = parser.parse(options, new String[] { "-i", "123", "-f", "foo" }); assertEquals(123, ((Number) cmd.getParsedOptionValue("i")).intValue()); assertEquals("foo", cmd.getParsedOptionValue("f")); }
@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') ) ); }
@Test public void testBaseOptionCharOpt() { Option base = OptionBuilder.withDescription( "option description") .create( 'o' ); assertEquals( "o", base.getOpt() ); assertEquals( "option description", base.getDescription() ); assertTrue( !base.hasArg() ); }