|
| 1 | +package picocli; |
| 2 | + |
| 3 | +import org.junit.Rule; |
| 4 | +import org.junit.Test; |
| 5 | +import org.junit.contrib.java.lang.system.ProvideSystemProperty; |
| 6 | +import org.junit.contrib.java.lang.system.RestoreSystemProperties; |
| 7 | +import org.junit.rules.TestRule; |
| 8 | +import picocli.CommandLine.Command; |
| 9 | +import picocli.CommandLine.Model.CommandSpec; |
| 10 | +import picocli.CommandLine.ParameterException; |
| 11 | +import picocli.CommandLine.Parameters; |
| 12 | +import picocli.CommandLine.Spec; |
| 13 | +import picocli.CommandLine; |
| 14 | + |
| 15 | +import java.util.concurrent.Callable; |
| 16 | + |
| 17 | +import static org.junit.Assert.*; |
| 18 | + |
| 19 | +public class Issue1309 { |
| 20 | + |
| 21 | + // allows tests to set any kind of properties they like, without having to individually roll them back |
| 22 | + @Rule |
| 23 | + public final TestRule restoreSystemProperties = new RestoreSystemProperties(); |
| 24 | + |
| 25 | + @Rule |
| 26 | + public final ProvideSystemProperty ansiOFF = new ProvideSystemProperty("picocli.ansi", "false"); |
| 27 | + |
| 28 | + @Command(name = "test", mixinStandardHelpOptions = true) |
| 29 | + static class TestCommand implements Callable<Integer> { |
| 30 | + |
| 31 | + @Spec |
| 32 | + CommandSpec spec; |
| 33 | + |
| 34 | + public Integer call() throws Exception { |
| 35 | + throw new ParameterException(spec.commandLine(), "Missing required subcommand"); |
| 36 | + } |
| 37 | + |
| 38 | + @Command(name = "start", description = "start", mixinStandardHelpOptions = true) |
| 39 | + public int start( |
| 40 | + @Parameters(index = "0", paramLabel = "id") String id) { |
| 41 | + System.out.printf("start was called with %s%n", id); |
| 42 | + return 111; |
| 43 | + } |
| 44 | + |
| 45 | + @Command(name = "restart", description = "restart", mixinStandardHelpOptions = true) |
| 46 | + public int restart( |
| 47 | + @Parameters(index = "0", paramLabel = "id") String id) { |
| 48 | + System.out.printf("restart was called with %s%n", id); |
| 49 | + return 222; |
| 50 | + } |
| 51 | + |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testIssue1309() { |
| 56 | + assertEquals(222, new CommandLine(new TestCommand()).execute("restart", "rest")); |
| 57 | + assertEquals(111, new CommandLine(new TestCommand()).execute("start", "sta")); |
| 58 | + } |
| 59 | +} |
0 commit comments