diff --git a/src/main/java/com/beust/jcommander/JCommander.java b/src/main/java/com/beust/jcommander/JCommander.java index 3f40806c2..efe8fd9c6 100644 --- a/src/main/java/com/beust/jcommander/JCommander.java +++ b/src/main/java/com/beust/jcommander/JCommander.java @@ -495,7 +495,7 @@ private List expandDynamicArg(String arg) { for (ParameterDescription pd : descriptions.values()) { if (pd.isDynamicParameter()) { for (String name : pd.getParameter().names()) { - if (arg.startsWith(name) && !arg.equals(name)) { + if (arg.startsWith(name) && !arg.equals(name) && arg.contains(pd.getParameter().getAssignment())) { return Arrays.asList(name, arg.substring(name.length())); } } diff --git a/src/test/java/com/beust/jcommander/dynamic/DynamicParameterTest.java b/src/test/java/com/beust/jcommander/dynamic/DynamicParameterTest.java index 7d8e8e6c4..452d71df5 100644 --- a/src/test/java/com/beust/jcommander/dynamic/DynamicParameterTest.java +++ b/src/test/java/com/beust/jcommander/dynamic/DynamicParameterTest.java @@ -3,7 +3,6 @@ import com.beust.jcommander.JCommander; import com.beust.jcommander.ParameterException; import com.beust.jcommander.internal.Maps; - import org.testng.Assert; import org.testng.annotations.Test; @@ -55,4 +54,12 @@ public static void main(String[] args) { DynamicParameterTest dpt = new DynamicParameterTest(); dpt.simpleWithSpaces(); } + + @Test + public void shouldNotBreakSimilarParameter() { + ShadowingParameter shadowingParameter = new ShadowingParameter(); + new JCommander(shadowingParameter).parse("-abc", "abc", "-ab", "a=b"); + Assert.assertEquals(shadowingParameter.abc, "abc"); + Assert.assertEquals(shadowingParameter.ab.get("a"), "b"); + } } diff --git a/src/test/java/com/beust/jcommander/dynamic/ShadowingParameter.java b/src/test/java/com/beust/jcommander/dynamic/ShadowingParameter.java new file mode 100644 index 000000000..008579451 --- /dev/null +++ b/src/test/java/com/beust/jcommander/dynamic/ShadowingParameter.java @@ -0,0 +1,20 @@ +package com.beust.jcommander.dynamic; + +import com.beust.jcommander.DynamicParameter; +import com.beust.jcommander.Parameter; + +import java.util.HashMap; +import java.util.Map; + +public class ShadowingParameter { + + @Parameter( + names = "-abc" + ) + String abc; + @DynamicParameter( + names = "-ab" + ) + Map ab = new HashMap<>(); + +}