|
15 | 15 | */ |
16 | 16 | package picocli; |
17 | 17 |
|
| 18 | +import org.junit.After; |
| 19 | +import org.junit.Before; |
| 20 | +import org.junit.Test; |
| 21 | +import picocli.CommandLine.*; |
| 22 | + |
18 | 23 | import java.io.File; |
19 | 24 | import java.net.InetAddress; |
20 | 25 | import java.util.Arrays; |
21 | 26 | import java.util.List; |
22 | 27 | import java.util.concurrent.TimeUnit; |
23 | 28 |
|
24 | | -import org.junit.After; |
25 | | -import org.junit.Before; |
26 | | -import org.junit.Ignore; |
27 | | -import org.junit.Test; |
28 | | - |
29 | | -import picocli.CommandLine.MissingParameterException; |
30 | | -import picocli.CommandLine.Option; |
31 | | -import picocli.CommandLine.Parameters; |
32 | | -import picocli.CommandLine.Range; |
33 | | -import picocli.CommandLine.UnmatchedArgumentException; |
34 | | - |
35 | 29 | import static org.junit.Assert.*; |
36 | 30 |
|
37 | 31 | public class CommandLineArityTest { |
@@ -842,18 +836,18 @@ class NonVarArgArrayParamsArity2 { |
842 | 836 | } |
843 | 837 | } |
844 | 838 | @Test |
845 | | - public void testMixPositionalParamsWithOptions_ParamsUnboundedArity_isGreedy() { |
| 839 | + public void testMixPositionalParamsWithOptions_ParamsUnboundedArity() { |
846 | 840 | class Arg { |
847 | 841 | @Parameters(arity = "1..*") List<String> parameters; |
848 | 842 | @Option(names = "-o") List<String> options; |
849 | 843 | } |
850 | 844 | Arg result = CommandLine.populateCommand(new Arg(), "-o", "v1", "p1", "p2", "-o", "v2", "p3", "p4"); |
851 | | - assertEquals(Arrays.asList("p1", "p2", "-o", "v2", "p3", "p4"), result.parameters); |
852 | | - assertEquals(Arrays.asList("v1"), result.options); |
| 845 | + assertEquals(Arrays.asList("p1", "p2", "p3", "p4"), result.parameters); |
| 846 | + assertEquals(Arrays.asList("v1", "v2"), result.options); |
853 | 847 |
|
854 | 848 | Arg result2 = CommandLine.populateCommand(new Arg(), "-o", "v1", "p1", "-o", "v2", "p3"); |
855 | | - assertEquals(Arrays.asList("p1", "-o", "v2", "p3"), result2.parameters); |
856 | | - assertEquals(Arrays.asList("v1"), result2.options); |
| 849 | + assertEquals(Arrays.asList("p1", "p3"), result2.parameters); |
| 850 | + assertEquals(Arrays.asList("v1", "v2"), result2.options); |
857 | 851 |
|
858 | 852 | try { |
859 | 853 | CommandLine.populateCommand(new Arg(), "-o", "v1", "-o", "v2"); |
@@ -911,4 +905,48 @@ class Arg { |
911 | 905 | assertEquals("positional parameter at index 0..* (<parameters>) requires at least 2 values, but only 1 were specified: [p3]", ex.getMessage()); |
912 | 906 | } |
913 | 907 | } |
| 908 | + |
| 909 | + @Test |
| 910 | + public void test284VarargPositionalShouldNotConsumeOptions() { |
| 911 | + class Cmd { |
| 912 | + @Option(names = "--alpha") String alpha; |
| 913 | + @Parameters(index = "0", arity = "1") String foo; |
| 914 | + @Parameters(index = "1..*", arity = "*") List<String> params; |
| 915 | + } |
| 916 | + Cmd cmd = CommandLine.populateCommand(new Cmd(), "foo", "xx", "--alpha", "--beta"); |
| 917 | + assertEquals("foo", cmd.foo); |
| 918 | + assertEquals("--beta", cmd.alpha); |
| 919 | + assertEquals(Arrays.asList("xx"), cmd.params); |
| 920 | + } |
| 921 | + |
| 922 | + @Test |
| 923 | + public void test284VarargPositionalShouldConsumeOptionsAfterDoubleDash() { |
| 924 | + class Cmd { |
| 925 | + @Option(names = "--alpha") String alpha; |
| 926 | + @Parameters(index = "0", arity = "1") String foo; |
| 927 | + @Parameters(index = "1..*", arity = "*") List<String> params; |
| 928 | + } |
| 929 | + Cmd cmd = CommandLine.populateCommand(new Cmd(), "foo", "--", "xx", "--alpha", "--beta"); |
| 930 | + assertEquals("foo", cmd.foo); |
| 931 | + assertEquals(null, cmd.alpha); |
| 932 | + assertEquals(Arrays.asList("xx", "--alpha", "--beta"), cmd.params); |
| 933 | + } |
| 934 | + |
| 935 | + @Test |
| 936 | + public void testPositionalShouldCaptureDoubleDashAfterDoubleDash() { |
| 937 | + class Cmd { |
| 938 | + @Parameters List<String> params; |
| 939 | + } |
| 940 | + Cmd cmd = CommandLine.populateCommand(new Cmd(), "foo", "--", "--", "--"); |
| 941 | + assertEquals(Arrays.asList("foo", "--", "--"), cmd.params); |
| 942 | + } |
| 943 | + |
| 944 | + @Test |
| 945 | + public void testVarargPositionalShouldCaptureDoubleDashAfterDoubleDash() { |
| 946 | + class Cmd { |
| 947 | + @Parameters(index = "0..*", arity = "*") List<String> params; |
| 948 | + } |
| 949 | + Cmd cmd = CommandLine.populateCommand(new Cmd(), "foo", "--", "--", "--"); |
| 950 | + assertEquals(Arrays.asList("foo", "--", "--"), cmd.params); |
| 951 | + } |
914 | 952 | } |
0 commit comments