Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added: JCommander#allowAbbreviatedOptions (default: false)

  • Loading branch information...
commit 2c092c0c1966a21f1b585eac22b4e36045a43549 1 parent 14bcad3
@cbeust authored
View
1  CHANGELOG
@@ -1,5 +1,6 @@
Current
+Added: JCommander#allowAbbreviatedOptions (default: false)
Added: JCommander#setCaseSensitiveOptions (default: true)
Fixed: The description of commands is now displayed on the next line and indented.
Fixed: Empty string defaults now displayed as "<empty string>" in the usage
View
43 src/main/java/com/beust/jcommander/JCommander.java
@@ -795,6 +795,7 @@ public int processVariableArity(String optionName, String[] options) {
private boolean m_caseSensitiveOptions = true;
private boolean m_caseSensitiveCommands = true;
+ private boolean m_allowAbbreviatedOptions = false;
/**
* @return the number of options that were processed.
@@ -1415,13 +1416,43 @@ private String s(int count) {
return m_objects;
}
+ private <V> V findAbbreviatedOption(Map<String, V> map, String name, boolean caseSensitive) {
+ Map<String, V> results = Maps.newHashMap();
+ for (String c : map.keySet()) {
+ boolean match = (caseSensitive && c.startsWith(name))
+ || ((! caseSensitive) && c.toLowerCase().startsWith(name.toLowerCase()));
+ if (match) {
+ results.put(c, map.get(c));
+ }
+ }
+ V result;
+ if (results.size() > 1) {
+ throw new ParameterException("Ambiguous option: " + name
+ + " matches " + results.keySet());
+ } else if (results.size() == 1) {
+ result = results.values().iterator().next();
+ } else {
+ result = null;
+ }
+
+ return result;
+ }
+
private <V> V findCaseSensitiveMap(Map<String, V> map, String name) {
if (m_caseSensitiveOptions) {
- return map.get(name);
+ if (m_allowAbbreviatedOptions) {
+ return findAbbreviatedOption(map, name, m_caseSensitiveOptions);
+ } else {
+ return map.get(name);
+ }
} else {
- for (String c : map.keySet()) {
- if (c.equalsIgnoreCase(name)) {
- return map.get(c);
+ if (m_allowAbbreviatedOptions) {
+ return findAbbreviatedOption(map, name, m_caseSensitiveOptions);
+ } else {
+ for (String c : map.keySet()) {
+ if (c.equalsIgnoreCase(name)) {
+ return map.get(c);
+ }
}
}
}
@@ -1543,6 +1574,10 @@ public void setCaseSensitiveOptions(boolean b) {
m_caseSensitiveOptions = b;
}
+ public void setAllowAbbreviatedOptions(boolean b) {
+ m_allowAbbreviatedOptions = b;
+ }
+
// public void setCaseSensitiveCommands(boolean b) {
// m_caseSensitiveCommands = b;
// }
View
58 src/test/java/com/beust/jcommander/JCommanderTest.java
@@ -884,9 +884,65 @@ public void caseInsensitiveCommand() {
Assert.assertEquals(command, "--configure");
}
+ public void abbreviatedOptions() {
+ class Arg {
+ @Parameter(names = { "-p", "--param" })
+ private String param;
+ }
+ Arg a = new Arg();
+ JCommander jc = new JCommander(a);
+ jc.setAllowAbbreviatedOptions(true);
+ jc.parse(new String[] { "--par", "foo" });
+ Assert.assertEquals(a.param, "foo");
+ }
+
+ public void abbreviatedOptionsCaseInsensitive() {
+ class Arg {
+ @Parameter(names = { "-p", "--param" })
+ private String param;
+ }
+ Arg a = new Arg();
+ JCommander jc = new JCommander(a);
+ jc.setCaseSensitiveOptions(false);
+ jc.setAllowAbbreviatedOptions(true);
+ jc.parse(new String[] { "--PAR", "foo" });
+ Assert.assertEquals(a.param, "foo");
+ }
+
+ @Test(expectedExceptions = ParameterException.class)
+ public void ambiguousAbbreviatedOptions() {
+ class Arg {
+ @Parameter(names = { "--param" })
+ private String param;
+ @Parameter(names = { "--parb" })
+ private String parb;
+ }
+ Arg a = new Arg();
+ JCommander jc = new JCommander(a);
+ jc.setAllowAbbreviatedOptions(true);
+ jc.parse(new String[] { "--par", "foo" });
+ Assert.assertEquals(a.param, "foo");
+ }
+
+ @Test(expectedExceptions = ParameterException.class)
+ public void ambiguousAbbreviatedOptionsCaseInsensitive() {
+ class Arg {
+ @Parameter(names = { "--param" })
+ private String param;
+ @Parameter(names = { "--parb" })
+ private String parb;
+ }
+ Arg a = new Arg();
+ JCommander jc = new JCommander(a);
+ jc.setCaseSensitiveOptions(false);
+ jc.setAllowAbbreviatedOptions(true);
+ jc.parse(new String[] { "--PAR", "foo" });
+ Assert.assertEquals(a.param, "foo");
+ }
+
@Test(enabled = false)
public static void main(String[] args) throws Exception {
- new JCommanderTest().caseInsensitiveCommand();
+ new JCommanderTest().ambiguousAbbreviatedOptionsCaseInsensitive();
// class A {
// @Parameter(names = "-short", required = true)
// List<String> parameters;
Please sign in to comment.
Something went wrong with that request. Please try again.