Skip to content

Commit

Permalink
modify convertValue() to fix issue 509 and add a test
Browse files Browse the repository at this point in the history
  • Loading branch information
= authored and mkarg committed Oct 2, 2023
1 parent 694dd56 commit 0d8e65e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/beust/jcommander/JCommander.java
Original file line number Diff line number Diff line change
Expand Up @@ -1375,11 +1375,11 @@ public Object convertValue(final Parameterized parameterized, Class type, String
}

IStringConverter<?> converter = null;
if (type.isAssignableFrom(List.class)) {
if (type.isAssignableFrom(List.class) || type.isAssignableFrom(Set.class)) {
// If a list converter was specified, pass the value to it for direct conversion
converter = tryInstantiateConverter(optionName, annotation.listConverter());
}
if (type.isAssignableFrom(List.class) && converter == null) {
if ((type.isAssignableFrom(List.class) || type.isAssignableFrom(Set.class)) && converter == null) {
// No list converter: use the single value converter and pass each parsed value to it individually
final IParameterSplitter splitter = tryInstantiateConverter(null, annotation.splitter());
converter = new DefaultListConverter(splitter, new IStringConverter() {
Expand Down
37 changes: 37 additions & 0 deletions src/test/java/com/beust/jcommander/SetOfEnumTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.beust.jcommander;

import org.testng.Assert;
import org.testng.annotations.Test;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class SetOfEnumTest {

public enum Season{
SPRING,
SUMMER,
AUTUMN,
WINTER;
}

@Test
public void testParse()
{
class Args {
@Parameter(names = { "--season"}, description = "List of seasons separated by comma")
private Set<Season> seasons = new HashSet<>();
}
Args args = new Args();
JCommander.newBuilder()
.addObject(args)
.build()
.parse("--season", "SPRING");
Assert.assertEquals(Season.class, args.seasons.toArray()[0].getClass());
}

public static void main(String[] args) {
new SetOfEnumTest().testParse();
}
}

0 comments on commit 0d8e65e

Please sign in to comment.