summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCedric Beust <cedric@beust.com>2014-02-17 20:38:31 -0800
committerCedric Beust <cedric@beust.com>2014-02-17 20:38:31 -0800
commit4650aa0afc774d5b8744e69fc5dfd6a5aadf521c (patch)
tree3678620bc35a4cdc0398ec75c3dcda1ad6a525f1
parent39cd44f110102be84275481fb6f57f35bc332bbc (diff)
parent2a4eef24e7c309e8fc69ccb7d7e3fd08681635d8 (diff)
downloadplatform_external_jcommander-4650aa0afc774d5b8744e69fc5dfd6a5aadf521c.tar.gz
platform_external_jcommander-4650aa0afc774d5b8744e69fc5dfd6a5aadf521c.tar.bz2
platform_external_jcommander-4650aa0afc774d5b8744e69fc5dfd6a5aadf521c.zip
Merge pull request #182 from andylaw/master
Fix for List<?> parameters with alternate names getting stomped by changes in name use within a single invocation
-rw-r--r--src/main/java/com/beust/jcommander/JCommander.java4
-rw-r--r--src/test/java/com/beust/jcommander/JCommanderTest.java30
-rw-r--r--src/test/java/com/beust/jcommander/args/AlternateNamesForListArgs.java32
3 files changed, 64 insertions, 2 deletions
diff --git a/src/main/java/com/beust/jcommander/JCommander.java b/src/main/java/com/beust/jcommander/JCommander.java
index b78d890..a763bc1 100644
--- a/src/main/java/com/beust/jcommander/JCommander.java
+++ b/src/main/java/com/beust/jcommander/JCommander.java
@@ -544,13 +544,13 @@ public class JCommander {
m_mainParameterDescription =
new ParameterDescription(object, p, parameterized, m_bundle, this);
} else {
+ ParameterDescription pd =
+ new ParameterDescription(object, p, parameterized, m_bundle, this);
for (String name : p.names()) {
if (m_descriptions.containsKey(new StringKey(name))) {
throw new ParameterException("Found the option " + name + " multiple times");
}
p("Adding description for " + name);
- ParameterDescription pd =
- new ParameterDescription(object, p, parameterized, m_bundle, this);
m_fields.put(parameterized, pd);
m_descriptions.put(new StringKey(name), pd);
diff --git a/src/test/java/com/beust/jcommander/JCommanderTest.java b/src/test/java/com/beust/jcommander/JCommanderTest.java
index 356d442..be9a7ea 100644
--- a/src/test/java/com/beust/jcommander/JCommanderTest.java
+++ b/src/test/java/com/beust/jcommander/JCommanderTest.java
@@ -18,6 +18,7 @@
package com.beust.jcommander;
+import com.beust.jcommander.args.AlternateNamesForListArgs;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileWriter;
@@ -96,6 +97,35 @@ public class JCommanderTest {
Assert.assertEquals(args.date, new SimpleDateFormat("yyyy-MM-dd").parse("2011-10-26"));
}
+ @DataProvider
+ public Object[][] alternateNamesListArgs() {
+ return new Object[][] {
+ new String[][] {new String[] {"--servers", "1", "-s", "2", "--servers", "3"}},
+ new String[][] {new String[] {"-s", "1", "-s", "2", "--servers", "3"}},
+ new String[][] {new String[] {"--servers", "1", "--servers", "2", "-s", "3"}},
+ new String[][] {new String[] {"-s", "1", "--servers", "2", "-s", "3"}},
+ new String[][] {new String[] {"-s", "1", "-s", "2", "--servers", "3"}},
+ };
+ }
+
+ /**
+ * Confirm that List<?> parameters with alternate names return the correct
+ * List regardless of how the arguments are specified
+ */
+
+ @Test(dataProvider = "alternateNamesListArgs")
+ public void testAlternateNamesForListArguments(String[] argv) {
+ AlternateNamesForListArgs args = new AlternateNamesForListArgs();
+
+ new JCommander(args, argv);
+
+ Assert.assertEquals(args.serverNames.size(), 3);
+ Assert.assertEquals(args.serverNames.get(0), argv[1]);
+ Assert.assertEquals(args.serverNames.get(1), argv[3]);
+ Assert.assertEquals(args.serverNames.get(2), argv[5]);
+ }
+
+
/**
* Make sure that if there are args with multiple names (e.g. "-log" and "-verbose"),
* the usage will only display it once.
diff --git a/src/test/java/com/beust/jcommander/args/AlternateNamesForListArgs.java b/src/test/java/com/beust/jcommander/args/AlternateNamesForListArgs.java
new file mode 100644
index 0000000..18a1655
--- /dev/null
+++ b/src/test/java/com/beust/jcommander/args/AlternateNamesForListArgs.java
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2014 the original author or authors.
+ * See the notice.md file distributed with this work for additional
+ * information regarding copyright ownership.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.beust.jcommander.args;
+
+import com.beust.jcommander.Parameter;
+import com.beust.jcommander.internal.Lists;
+import java.util.List;
+
+/**
+ *
+ * @author Andy Law <andy.law@roslin.ed.ac.uk>
+ */
+public class AlternateNamesForListArgs {
+
+ @Parameter(names = {"-s", "--servers"}, description = "blah")
+ public List<String> serverNames = Lists.newLinkedList();
+}