diff --git a/src/main/java/com/sanityinc/jargs/CmdLineParser.java b/src/main/java/com/sanityinc/jargs/CmdLineParser.java index 4c48795..1003f51 100644 --- a/src/main/java/com/sanityinc/jargs/CmdLineParser.java +++ b/src/main/java/com/sanityinc/jargs/CmdLineParser.java @@ -451,13 +451,20 @@ public final T getOptionValue( Option o ) { return getOptionValue(o, null); } + /** + * Use instead of getOptionValue(Option) for when not saving the options. + */ + public final T getValue(String long_form) { + return getValue(long_form, null); + } + /** * @return the parsed value of the given Option, or the given default 'def' * if the option was not set */ public final T getOptionValue( Option o, T def ) { - List v = values.get(o.longForm()); + List v = values.get(o.longForm()); if (v == null) { return def; @@ -474,6 +481,24 @@ public final T getOptionValue( Option o, T def ) { } } + /** + * Use instead of getOptionValue(Option) for when not saving the options. + */ + public final T getValue(String long_form, T def) { + List v = values.get(long_form); + if (v == null) { + return def; + } else if (v.isEmpty()) { + return null; + } else { + /* + * See above + */ + @SuppressWarnings("unchecked") + T result = (T)v.remove(0); + return result; + } + } /** * @return A Collection giving the parsed values of all the occurrences of @@ -493,6 +518,20 @@ public final Collection getOptionValues(Option option) { } } + /** + * @return A Collection giving the parsed values of all the occurrences of + * the given Option, or an empty Collection if the option was not set. + */ + public final Collection getValues(String long_form) { + Collection vals = new ArrayList(); + + T o = getValue(long_form, null); + while (o != null) { + vals.add(o); + o = getValue(long_form, null); + } + return vals; + } /** * @return the non-option arguments diff --git a/src/test/java/com/sanityinc/jargs/CmdLineParserTestCase.java b/src/test/java/com/sanityinc/jargs/CmdLineParserTestCase.java index e3f42ba..f828c33 100644 --- a/src/test/java/com/sanityinc/jargs/CmdLineParserTestCase.java +++ b/src/test/java/com/sanityinc/jargs/CmdLineParserTestCase.java @@ -68,6 +68,30 @@ public void testStandardOptions() throws Exception { assertArrayEquals(new String[]{"rest"}, parser.getRemainingArgs()); } + @Test + public void testStandardOptions_string() throws Exception { + CmdLineParser parser = new CmdLineParser(); + Option verbose = parser.addBooleanOption('v', "verbose"); + Option size = parser.addIntegerOption('s', "size"); + Option name = parser.addStringOption('n', "name"); + Option fraction = parser.addDoubleOption('f', "fraction"); + Option missing = parser.addBooleanOption('m', "missing"); + Option careful = parser.addBooleanOption("careful"); + Option bignum = parser.addLongOption('b', "bignum"); + assertEquals(null, parser.getOptionValue(size)); + Long longValue = new Long(new Long(Integer.MAX_VALUE).longValue() + 1); + parser.parse(new String[] { "-v", "--size=100", "-b", + longValue.toString(), "-n", "foo", "-f", "0.1", "rest" }, + Locale.US); + Boolean missed = parser.getValue("missing"); + assertEquals(null, missed); + assertEquals(Boolean.TRUE, parser.getValue("verbose")); + assertEquals(100, ((Integer)parser.getValue("size")).intValue()); + assertEquals("foo", parser.getValue("name")); + assertEquals(longValue, parser.getValue("bignum")); + assertEquals(0.1, ((Double)parser.getValue("fraction")).doubleValue(), 0.1e-6); + assertArrayEquals(new String[]{"rest"}, parser.getRemainingArgs()); + } @Test public void testDefaults() throws Exception {