Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modify convertValue() to fix issue #509 and add a test #519

Merged
merged 1 commit into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1302,11 +1302,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();
}
}