Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed: if using a different option prefix, unknown option are mistake…

…nly reported as "no main parameter defined" (kurmasz)
  • Loading branch information...
commit 9e22cffb2d22829cb5e2d1bf4d64d87df69d2983 1 parent c10f00e
@cbeust authored
View
1  CHANGELOG
@@ -1,5 +1,6 @@
Current
+Fixed: if using a different option prefix, unknown option are mistakenly reported as "no main parameter defined" (kurmasz)
Fixed: 113: getCommandDescription() returns the description of the main parameter instead of that of the command
Fixed: bug with several multiple arity parameters (VariableArityTest)
Fixed: variable arities not working when same parameter appears multiple times.
View
22 src/main/java/com/beust/jcommander/JCommander.java
@@ -439,8 +439,22 @@ private String getOptionPrefixes(String[] args, String arg) {
.getAnnotation(Parameters.class);
if (p != null) return p.optionPrefixes();
}
+ String result = Parameters.DEFAULT_OPTION_PREFIXES;
- return Parameters.DEFAULT_OPTION_PREFIXES;
+ // See if any of the objects contains a @Parameters(optionPrefixes)
+ StringBuilder sb = new StringBuilder();
+ for (Object o : m_objects) {
+ Parameters p = o.getClass().getAnnotation(Parameters.class);
+ if (p != null && !Parameters.DEFAULT_OPTION_PREFIXES.equals(p.optionPrefixes())) {
+ sb.append(p.optionPrefixes());
+ }
+ }
+
+ if (! Strings.isStringEmpty(sb.toString())) {
+ result = sb.toString();
+ }
+
+ return result;
}
/**
@@ -643,7 +657,7 @@ private void parseValues(String[] args) {
//
// Main parameter
//
- if (! isStringEmpty(arg)) {
+ if (! Strings.isStringEmpty(arg)) {
if (m_commands.isEmpty()) {
//
// Regular (non-command) parsing
@@ -800,10 +814,6 @@ private int processFixedArity(String[] args, int originalIndex, ParameterDescrip
return result;
}
- private static boolean isStringEmpty(String s) {
- return s == null || "".equals(s);
- }
-
/**
* @return the field that's meant to receive all the parameters that are not options.
*
View
7 src/main/java/com/beust/jcommander/Strings.java
@@ -0,0 +1,7 @@
+package com.beust.jcommander;
+
+public class Strings {
+ public static boolean isStringEmpty(String s) {
+ return s == null || "".equals(s);
+ }
+}
View
57 src/test/java/com/beust/jcommander/JCommanderTest.java
@@ -701,11 +701,21 @@ public void multiVariableArityList() {
private List<String> paramB = Lists.newArrayList();
}
- String args[] = { "-paramA", "a1", "a2", "-paramB", "b1", "b2", "b3" };
- Params p = new Params();
- new JCommander(p, args).parse();
- Assert.assertEquals(p.paramA, Arrays.asList(new String[] { "a1", "a2" }));
- Assert.assertEquals(p.paramB, Arrays.asList(new String[] { "b1", "b2", "b3" }));
+ {
+ String args[] = { "-paramA", "a1", "a2", "-paramB", "b1", "b2", "b3" };
+ Params p = new Params();
+ new JCommander(p, args).parse();
+ Assert.assertEquals(p.paramA, Arrays.asList(new String[] { "a1", "a2" }));
+ Assert.assertEquals(p.paramB, Arrays.asList(new String[] { "b1", "b2", "b3" }));
+ }
+
+ {
+ String args[] = { "-paramA", "a1", "a2", "-paramB", "b1", "-paramA", "a3" };
+ Params p = new Params();
+ new JCommander(p, args).parse();
+ Assert.assertEquals(p.paramA, Arrays.asList(new String[] { "a1", "a2", "a3" }));
+ Assert.assertEquals(p.paramB, Arrays.asList(new String[] { "b1" }));
+ }
}
@Test(enabled = false,
@@ -722,20 +732,45 @@ public void commandKey() {
j.usage();
}
+ public void tmp() {
+ class A {
+ @Parameter(names = "-b")
+ public String b;
+ }
+ new JCommander(new A()).parse("");
+ }
+
+ public void unknownOptionWithDifferentPrefix() {
+ @Parameters(optionPrefixes = "/")
+ class SlashSeparator {
+
+ @Parameter(names = "/verbose")
+ public boolean verbose = false;
+
+ @Parameter(names = "/file")
+ public String file;
+ }
+ SlashSeparator ss = new SlashSeparator();
+ try {
+ new JCommander(ss).parse("/notAParam");
+ } catch (ParameterException ex) {
+ boolean result = ex.getMessage().contains("Unknown option");
+ Assert.assertTrue(result);
+ }
+ }
+
@Test(enabled = false)
public static void main(String[] args) throws Exception {
- new JCommanderTest().commandKey();
-// System.out.println("Help:" + a.help);
-// System.out.println("A");
+ new JCommanderTest().unknownOptionWithDifferentPrefix();
// class A {
-// @Parameter
+// @Parameter(names = "-short", required = true)
// List<String> parameters;
//
-// @Parameter(names = "-long")
+// @Parameter(names = "-long", required = true)
// public long l;
// }
// A a = new A();
-// new JCommander(a).parse("-long", "32");
+// new JCommander(a).parse();
// System.out.println(a.l);
// System.out.println(a.parameters);
// ArgsList al = new ArgsList();

0 comments on commit 9e22cff

Please sign in to comment.
Something went wrong with that request. Please try again.