Skip to content

Commit

Permalink
[#1125][#1538] add test
Browse files Browse the repository at this point in the history
  • Loading branch information
remkop committed Jan 3, 2022
1 parent 1a65fd7 commit ce28ef9
Showing 1 changed file with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import java.util.Stack;
import java.util.concurrent.Callable;

import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.SystemErrRule;
import picocli.CommandLine.Command;
import picocli.CommandLine.IParameterPreprocessor;
import picocli.CommandLine.Model.ArgSpec;
Expand All @@ -16,6 +18,10 @@
import static org.junit.Assert.*;

public class Issue1125_1538_OptionNameOrSubcommandAsOptionValue {

@Rule
public SystemErrRule systemErrRule = new SystemErrRule().enableLog().muteForSuccessfulTests();

@Command(name = "mycommand")
static class MyCommand implements Callable<Integer> {

Expand All @@ -34,6 +40,9 @@ public boolean preprocess(Stack<String> args, CommandSpec commandSpec, ArgSpec a
}
}

@Option(names = "-x") String x;
@Option(names = "-y") String y;

public Integer call() {
return 11;
}
Expand All @@ -60,4 +69,21 @@ public void testSubcommandAsOptionName() {
assertEquals(13, exitCode);
assertEquals("mySubcommand", obj.output.getName());
}

@Test
public void testAmbiguousOptions() {
//-x -y=123
MyCommand obj = new MyCommand();
CommandLine cmdLine = new CommandLine(obj);
int exitCode = cmdLine.execute("-x", "-y=123");
assertEquals(2, exitCode);
String expected = String.format("Expected parameter for option '-x' but found '-y=123'%n" +
"Usage: mycommand [-output=<output>] [-x=<x>] [-y=<y>] [COMMAND]%n" +
" -output=<output>%n" +
" -x=<x>%n" +
" -y=<y>%n" +
"Commands:%n" +
" mySubcommand%n");
assertEquals(expected, systemErrRule.getLog());
}
}

0 comments on commit ce28ef9

Please sign in to comment.