Skip to content

Commit

Permalink
[remkop#1904] Reproducer for issue 1904
Browse files Browse the repository at this point in the history
  • Loading branch information
sewe committed Jan 3, 2023
1 parent 070d2e9 commit c11300d
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions src/test/java/picocli/Issue1904.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package picocli;

import org.junit.Test;

import java.io.File;
import java.util.*;

import static org.junit.Assert.*;

public class Issue1904 {

enum DebugFacility { FOO, BAR, BAZ, ALL }

@CommandLine.Command
static class TestCommand implements Runnable {

@CommandLine.Option(
names = {"--debug"},
paramLabel = "FACILITY",
split = ",",
arity = "0..1",
fallbackValue = "ALL"
)
public Set<DebugFacility> debugFacilities = Collections.emptySet();

@CommandLine.Parameters(arity = "1..*")
public List<File> paths;

public void run() {}
}

@Test
public void singleOption() {
final CommandLine commandLine = new CommandLine(new TestCommand());
final CommandLine.ParseResult parseResult = commandLine.parseArgs("--debug", "ALL", "example.txt");
final TestCommand command = (TestCommand) parseResult.commandSpec().userObject();
assertEquals(command.debugFacilities, EnumSet.of(DebugFacility.ALL));
assertFalse(command.paths.isEmpty());
}

@Test
public void singleOptionWithCommandSeparatedValues() {
final CommandLine commandLine = new CommandLine(new TestCommand());
final CommandLine.ParseResult parseResult = commandLine.parseArgs("--debug", "FOO,BAR", "example.txt");
final TestCommand command = (TestCommand) parseResult.commandSpec().userObject();
assertEquals(command.debugFacilities, EnumSet.of(DebugFacility.FOO, DebugFacility.BAR));
assertFalse(command.paths.isEmpty());
}

@Test
public void singleOptionWithFallback() {
final CommandLine commandLine = new CommandLine(new TestCommand());
final CommandLine.ParseResult parseResult = commandLine.parseArgs("--debug", "example.txt");
final TestCommand command = (TestCommand) parseResult.commandSpec().userObject();
assertEquals(command.debugFacilities, EnumSet.of(DebugFacility.ALL));
assertFalse(command.paths.isEmpty());
}

@Test
public void multipleOptions() {
final CommandLine commandLine = new CommandLine(new TestCommand());
final CommandLine.ParseResult parseResult = commandLine.parseArgs("--debug", "FOO", "--debug", "BAR", "example.txt");
final TestCommand command = (TestCommand) parseResult.commandSpec().userObject();
assertEquals(command.debugFacilities, EnumSet.of(DebugFacility.FOO, DebugFacility.BAR));
assertFalse(command.paths.isEmpty());
}

@Test
public void multipleOptionsWithFallback() {
final CommandLine commandLine = new CommandLine(new TestCommand());
final CommandLine.ParseResult parseResult = commandLine.parseArgs("--debug", "--debug", "BAR", "example.txt");
final TestCommand command = (TestCommand) parseResult.commandSpec().userObject();
assertEquals(command.debugFacilities, EnumSet.of(DebugFacility.ALL, DebugFacility.BAR));
assertFalse(command.paths.isEmpty());
}
}

0 comments on commit c11300d

Please sign in to comment.