Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for not saving options, get by name. #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion src/main/java/com/sanityinc/jargs/CmdLineParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -451,13 +451,20 @@ public final <T> T getOptionValue( Option<T> o ) {
return getOptionValue(o, null);
}

/**
* Use instead of getOptionValue(Option) for when not saving the options.
*/
public final <T> 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> T getOptionValue( Option<T> o, T def ) {
List<?> v = values.get(o.longForm());
List<?> v = values.get(o.longForm());

if (v == null) {
return def;
Expand All @@ -474,6 +481,24 @@ public final <T> T getOptionValue( Option<T> o, T def ) {
}
}

/**
* Use instead of getOptionValue(Option) for when not saving the options.
*/
public final <T> 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
Expand All @@ -493,6 +518,20 @@ public final <T> Collection<T> getOptionValues(Option<T> 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 <T> Collection<T> getValues(String long_form) {
Collection<T> vals = new ArrayList<T>();

T o = getValue(long_form, null);
while (o != null) {
vals.add(o);
o = getValue(long_form, null);
}
return vals;
}

/**
* @return the non-option arguments
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/com/sanityinc/jargs/CmdLineParserTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Boolean> verbose = parser.addBooleanOption('v', "verbose");
Option<Integer> size = parser.addIntegerOption('s', "size");
Option<String> name = parser.addStringOption('n', "name");
Option<Double> fraction = parser.addDoubleOption('f', "fraction");
Option<Boolean> missing = parser.addBooleanOption('m', "missing");
Option<Boolean> careful = parser.addBooleanOption("careful");
Option<Long> 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 {
Expand Down