picocli.CommandLine.ArgGroup Java Examples

The following examples show how to use picocli.CommandLine.ArgGroup. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ArgGroupTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test
public void testValidationDependentMultiplicity0_1_Partial() {
    class All {
        @Option(names = "-a", required = true) boolean a;
        @Option(names = "-b", required = true) boolean b;
        @Option(names = "-c", required = true) boolean c;
    }
    class App {
        @ArgGroup(exclusive = false)
        All all;
    }
    try {
        new CommandLine(new App(), new InnerClassFactory(this)).parseArgs("-a", "-b");
        fail("Expected exception");
    } catch (MissingParameterException ex) {
        assertEquals("Error: Missing required argument(s): -c", ex.getMessage());
    }
}
 
Example #2
Source File: ArgGroupTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test
public void testRequiredArgsInAGroupAreNotValidated() {
    class App {
        @ArgGroup(exclusive = true, multiplicity = "0..1")
        Object exclusive = new Object() {
            @Option(names = "-x", required = true) boolean x;

            @ArgGroup(exclusive = false, multiplicity = "1")
            Object all = new Object() {
                @Option(names = "-a", required = true) int a;
                @Parameters(index = "0") File f0;
            };
        };
    }
    String expected = String.format("" +
            "Usage: <main class> [-x | (-a=<a> <f0>)]%n" +
            "      <f0>%n" +
            "  -a=<a>%n" +
            "  -x%n");

    CommandLine cmd = new CommandLine(new App());
    String actual = cmd.getUsageMessage(Help.Ansi.OFF);
    assertEquals(expected, actual);

    cmd.parseArgs(); // no error
}
 
Example #3
Source File: ArgGroupTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Ignore //https://github.com/remkop/picocli/issues/871
@Test
public void testNonExclusiveGroupMustHaveOneRequiredOption() {
    class MyApp {
        @ArgGroup(exclusive = false)
        NonExclusiveGroup871 group;
    }
    CommandLine cmd = new CommandLine(new MyApp());
    List<ArgGroupSpec> argGroupSpecs = cmd.getCommandSpec().argGroups();
    assertEquals(1, argGroupSpecs.size());

    int requiredCount = 0;
    for (ArgSpec arg : argGroupSpecs.get(0).args()) {
        if (arg.required()) {
            requiredCount++;
        }
    }
    assertTrue(requiredCount > 0);
}
 
Example #4
Source File: ArgGroupTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test
public void testUsageHelpNonValidatingGroupDoesNotImpactSynopsis() {
    class All {
        @Option(names = "-x") boolean x;
        @Option(names = "-y") boolean y;
    }
    class App {
        @ArgGroup(validate = false)
        All all;
    }
    String expected = String.format("" +
            "Usage: <main class> [-xy]%n" +
            "  -x%n" +
            "  -y%n");
    String actual = new CommandLine(new App(), new InnerClassFactory(this)).getUsageMessage(Help.Ansi.OFF);
    assertEquals(expected, actual);
}
 
Example #5
Source File: ArgGroupTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test
public void testUsageHelpNonRequiredNonExclusiveGroup() {
    class All {
        @Option(names = "-x", required = true) boolean x;
        @Option(names = "-y", required = true) boolean y;
    }
    class App {
        @ArgGroup(exclusive = false, multiplicity = "0..1")
        All all;
    }
    String expected = String.format("" +
            "Usage: <main class> [-x -y]%n" +
            "  -x%n" +
            "  -y%n");
    String actual = new CommandLine(new App(), new InnerClassFactory(this)).getUsageMessage(Help.Ansi.OFF);
    assertEquals(expected, actual);
}
 
Example #6
Source File: ArgGroupTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test
public void testUsageHelpRequiredNonExclusiveGroup() {
    class All {
        @Option(names = "-x", required = true) boolean x;
        @Option(names = "-y", required = true) boolean y;
    }
    class App {
        @ArgGroup(exclusive = false, multiplicity = "1")
        All all;
    }
    String expected = String.format("" +
            "Usage: <main class> (-x -y)%n" +
            "  -x%n" +
            "  -y%n");
    String actual = new CommandLine(new App(), new InnerClassFactory(this)).getUsageMessage(Help.Ansi.OFF);
    assertEquals(expected, actual);
}
 
Example #7
Source File: ArgGroupTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test
public void testUsageHelpNonRequiredExclusiveGroup() {
    class All {
        @Option(names = "-x", required = true) boolean x;
        @Option(names = "-y", required = true) boolean y;
    }
    class App {
        @ArgGroup(exclusive = true, multiplicity = "0..1")
        All all;
    }
    String expected = String.format("" +
            "Usage: <main class> [-x | -y]%n" +
            "  -x%n" +
            "  -y%n");
    String actual = new CommandLine(new App(), new InnerClassFactory(this)).getUsageMessage(Help.Ansi.OFF);
    assertEquals(expected, actual);
}
 
Example #8
Source File: ArgGroupTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test
public void testUsageHelpRequiredExclusiveGroup() {
    class Excl {
        @Option(names = "-x", required = true) boolean x;
        @Option(names = "-y", required = true) boolean y;
    }
    class App {
        @ArgGroup(exclusive = true, multiplicity = "1") Excl excl;
    }
    String expected = String.format("" +
            "Usage: <main class> (-x | -y)%n" +
            "  -x%n" +
            "  -y%n");
    String actual = new CommandLine(new App(), new InnerClassFactory(this)).getUsageMessage(Help.Ansi.OFF);
    assertEquals(expected, actual);
}
 
Example #9
Source File: ArgGroupTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test
public void testValidationDependentMultiplicity1_Zero() {
    class All {
        @Option(names = "-a", required = true) boolean a;
        @Option(names = "-b", required = true) boolean b;
        @Option(names = "-c", required = true) boolean c;
    }
    class App {
        @ArgGroup(exclusive = false, multiplicity = "1")
        All all;
    }
    try {
        new CommandLine(new App(), new InnerClassFactory(this)).parseArgs();
        fail("Expected exception");
    } catch (MissingParameterException ex) {
        assertEquals("Error: Missing required argument(s): (-a -b -c)", ex.getMessage());
    }
}
 
Example #10
Source File: ArgGroupTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test
public void testValidationDependentMultiplicity1_Partial() {
    class All {
        @Option(names = "-a", required = true) boolean a;
        @Option(names = "-b", required = true) boolean b;
        @Option(names = "-c", required = true) boolean c;
    }
    class App {
        @ArgGroup(exclusive = false, multiplicity = "1")
        All all;
    }
    try {
        new CommandLine(new App(), new InnerClassFactory(this)).parseArgs("-b");
        fail("Expected exception");
    } catch (MissingParameterException ex) {
        assertEquals("Error: Missing required argument(s): -a, -c", ex.getMessage());
    }
}
 
Example #11
Source File: ArgGroupTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test // https://github.com/remkop/picocli/issues/1027
public void testIssue1027RepeatingPositionalParams() {
    class Issue1027 {
        @ArgGroup(exclusive = false, multiplicity = "1..*")
        List<StudentGrade> gradeList;
    }

    Issue1027 bean = new Issue1027();
    new CommandLine(bean).parseArgs("Abby 4.0 Billy 3.5 Caily 3.5 Danny 4.0".split(" "));

    assertEquals(4, bean.gradeList.size());
    assertEquals(new StudentGrade("Abby", "4.0"), bean.gradeList.get(0));
    assertEquals(new StudentGrade("Billy", "3.5"), bean.gradeList.get(1));
    assertEquals(new StudentGrade("Caily", "3.5"), bean.gradeList.get(2));
    assertEquals(new StudentGrade("Danny", "4.0"), bean.gradeList.get(3));
}
 
Example #12
Source File: ArgGroupTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test // https://github.com/remkop/picocli/issues/1027
public void testIssue1027RepeatingPositionalParamsEdgeCase1() {
    class Issue1027 {
        @ArgGroup(exclusive = false, multiplicity = "4..*")
        List<StudentGrade> gradeList;
    }

    Issue1027 bean = new Issue1027();
    new CommandLine(bean).parseArgs("Abby 4.0 Billy 3.5 Caily 3.5 Danny 4.0".split(" "));

    assertEquals(4, bean.gradeList.size());
    assertEquals(new StudentGrade("Abby", "4.0"), bean.gradeList.get(0));
    assertEquals(new StudentGrade("Billy", "3.5"), bean.gradeList.get(1));
    assertEquals(new StudentGrade("Caily", "3.5"), bean.gradeList.get(2));
    assertEquals(new StudentGrade("Danny", "4.0"), bean.gradeList.get(3));
}
 
Example #13
Source File: ArgGroupTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test // https://github.com/remkop/picocli/issues/1027
public void testIssue1027RepeatingPositionalParamsEdgeCase2() {
    class Issue1027 {
        @ArgGroup(exclusive = false, multiplicity = "1..4")
        List<StudentGrade> gradeList;
    }

    Issue1027 bean = new Issue1027();
    new CommandLine(bean).parseArgs("Abby 4.0 Billy 3.5 Caily 3.5 Danny 4.0".split(" "));

    assertEquals(4, bean.gradeList.size());
    assertEquals(new StudentGrade("Abby", "4.0"), bean.gradeList.get(0));
    assertEquals(new StudentGrade("Billy", "3.5"), bean.gradeList.get(1));
    assertEquals(new StudentGrade("Caily", "3.5"), bean.gradeList.get(2));
    assertEquals(new StudentGrade("Danny", "4.0"), bean.gradeList.get(3));
}
 
Example #14
Source File: ArgGroupTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test
public void testValidationExclusiveMultiplicity1_ActualZero() {
    class All {
        @Option(names = "-a", required = true) boolean a;
        @Option(names = "-b", required = true) boolean b;
    }
    class App {
        @ArgGroup(exclusive = true, multiplicity = "1")
        All all;
    }
    try {
        new CommandLine(new App(), new InnerClassFactory(this)).parseArgs();
        fail("Expected exception");
    } catch (MissingParameterException ex) {
        assertEquals("Error: Missing required argument (specify one of these): (-a | -b)", ex.getMessage());
    }
}
 
Example #15
Source File: ArgGroupTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test
public void testValidationGroups2Violations0() {
    class Group1 {
        @Option(names = "-a", required = true) boolean a;
        @Option(names = "-b", required = true) boolean b;
    }
    class Group2 {
        @Option(names = "-x", required = true) boolean x;
        @Option(names = "-y", required = true) boolean y;
    }
    class App {
        @ArgGroup(exclusive = true, multiplicity = "0..1")
        Group1 g1;

        @ArgGroup(exclusive = true, multiplicity = "0..1")
        Group2 g2;
    }
    // no error
    new CommandLine(new App(), new InnerClassFactory(this)).parseArgs("-x", "-a");
}
 
Example #16
Source File: AbstractCommandSpecProcessor.java    From picocli with Apache License 2.0 6 votes vote down vote up
private boolean isSubcommand(ExecutableElement method, RoundEnvironment roundEnv) {
    Element typeElement = method.getEnclosingElement();
    if (typeElement.getAnnotation(Command.class) != null && typeElement.getAnnotation(Command.class).addMethodSubcommands()) {
        return true;
    }
    if (typeElement.getAnnotation(Command.class) == null) {
        Set<Element> elements = new HashSet<Element>(typeElement.getEnclosedElements());

        // The class is a Command if it has any fields or methods annotated with the below:
        return roundEnv.getElementsAnnotatedWith(Option.class).removeAll(elements)
                || roundEnv.getElementsAnnotatedWith(Parameters.class).removeAll(elements)
                || roundEnv.getElementsAnnotatedWith(Mixin.class).removeAll(elements)
                || roundEnv.getElementsAnnotatedWith(ArgGroup.class).removeAll(elements)
                || roundEnv.getElementsAnnotatedWith(Unmatched.class).removeAll(elements)
                || roundEnv.getElementsAnnotatedWith(Spec.class).removeAll(elements);
    }
    return false;
}
 
Example #17
Source File: ArgGroupTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test // #1061
public void testHelpForGroupWithPositionalsAndOptionsAndEndOfOptions() {
    @Command(mixinStandardHelpOptions = true, showEndOfOptionsDelimiterInUsageHelp = true)
    class Nested {
        @ArgGroup(exclusive = false, multiplicity = "0..*")
        List<Outer1027> outers;
    }
    String expected = String.format("" +
            "Usage: <main class> [-hV] [-x <param0> <param1> [-y (<param00>%n" +
            "                    <param01>)]...]... [--]%n" +
            "      <param0>%n" +
            "      <param00>%n" +
            "      <param1>%n" +
            "      <param01>%n" +
            "  -h, --help      Show this help message and exit.%n" +
            "  -V, --version   Print version information and exit.%n" +
            "  -x%n" +
            "  -y%n" +
            "  --              This option can be used to separate command-line options from%n" +
            "                    the list of positional parameters.%n");
    assertEquals(expected, new CommandLine(new Nested()).getUsageMessage());
}
 
Example #18
Source File: ArgGroupTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test
public void testAllPositionalParametersNested() {
    class Nested {
        @ArgGroup(exclusive = false, multiplicity = "0..*")
        List<Outer1027> outers;
    }

    List<ArgGroupSpec> argGroupSpecs = new CommandLine(new Nested()).getCommandSpec().argGroups();
    assertEquals(1, argGroupSpecs.size());
    ArgGroupSpec group = argGroupSpecs.get(0);
    List<PositionalParamSpec> positionals = group.positionalParameters();
    assertEquals(2, positionals.size());
    assertEquals("<param0>", positionals.get(0).paramLabel());

    List<PositionalParamSpec> allPositionals = group.allPositionalParametersNested();
    assertEquals(4, allPositionals.size());
    assertEquals("<param0>", allPositionals.get(0).paramLabel());
    assertEquals("<param1>", allPositionals.get(1).paramLabel());
    assertEquals("<param00>", allPositionals.get(2).paramLabel());
    assertEquals("<param01>", allPositionals.get(3).paramLabel());
}
 
Example #19
Source File: ArgGroupTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test
public void testAllOptionsNested() {
    class Nested {
        @ArgGroup(exclusive = false, multiplicity = "0..*")
        List<Outer1027> outers;
    }

    List<ArgGroupSpec> argGroupSpecs = new CommandLine(new Nested()).getCommandSpec().argGroups();
    assertEquals(1, argGroupSpecs.size());
    ArgGroupSpec group = argGroupSpecs.get(0);
    List<OptionSpec> options = group.options();
    assertEquals(1, options.size());
    assertEquals("-x", options.get(0).shortestName());

    List<OptionSpec> allOptions = group.allOptionsNested();
    assertEquals(2, allOptions.size());
    assertEquals("-x", allOptions.get(0).shortestName());
    assertEquals("-y", allOptions.get(1).shortestName());
}
 
Example #20
Source File: ModelCommandSpecTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test
public void testCommandSpecRemoveFailIfInGroup() {
    class Args {
        @Option(names = "-x") int x;
    }
    class App {
        @ArgGroup(exclusive = false, validate = false, multiplicity = "1",
                headingKey = "headingKeyXXX", heading = "headingXXX", order = 123)
        Args args;
    }

    CommandLine cmd = new CommandLine(new App(), new InnerClassFactory(this));
    CommandSpec spec = cmd.getCommandSpec();

    try {
        spec.remove(spec.findOption("-x"));
        fail("Expected exception");
    } catch (UnsupportedOperationException ex) {
        assertEquals("Cannot remove ArgSpec that is part of an ArgGroup", ex.getMessage());
    }
}
 
Example #21
Source File: ArgGroupTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test
public void testReflectionRequiresNonEmpty() {
    class Invalid {}
    class App {
        @ArgGroup Invalid invalid;
    }
    App app = new App();
    try {
        new CommandLine(app, new InnerClassFactory(this));
        fail("Expected exception");
    } catch (InitializationException ex) {
        assertEquals("ArgGroup has no options or positional parameters, and no subgroups: " +
                "FieldBinding(" + Invalid.class.getName() + " " + app.getClass().getName() + ".invalid) in " +
                        app.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(app))
                , ex.getMessage());
    }
}
 
Example #22
Source File: ArgGroupTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test
public void testValidationExclusiveMultiplicity0_1_ActualTwo() {
    class All {
        @Option(names = "-a", required = true) boolean a;
        @Option(names = "-b", required = true) boolean b;
    }
    class App {
        @ArgGroup(exclusive = true, multiplicity = "0..1")
        All all;
    }
    try {
        new CommandLine(new App(), new InnerClassFactory(this)).parseArgs("-a", "-b");
        fail("Expected exception");
    } catch (MutuallyExclusiveArgsException ex) {
        assertEquals("Error: -a, -b are mutually exclusive (specify only one)", ex.getMessage());
    }
}
 
Example #23
Source File: ArgGroupTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test
public void testValidationGroups2Violation1ExclusiveMultiplicity0_1_ActualTwo() {
    class Group1 {
        @Option(names = "-a", required = true) boolean a;
        @Option(names = "-b", required = true) boolean b;
    }
    class Group2 {
        @Option(names = "-x", required = true) boolean x;
        @Option(names = "-y", required = true) boolean y;
    }
    class App {
        @ArgGroup(exclusive = true, multiplicity = "0..1")
        Group1 g1;

        @ArgGroup(exclusive = true, multiplicity = "0..1")
        Group2 g2;
    }
    try {
        new CommandLine(new App(), new InnerClassFactory(this)).parseArgs("-x", "-a", "-b");
        fail("Expected exception");
    } catch (MutuallyExclusiveArgsException ex) {
        assertEquals("Error: -a, -b are mutually exclusive (specify only one)", ex.getMessage());
    }
}
 
Example #24
Source File: ArgGroupTest.java    From picocli with Apache License 2.0 6 votes vote down vote up
@Test
public void testValidationGroups2Violations2BothExclusiveMultiplicity0_1_ActualTwo() {
    class Group1 {
        @Option(names = "-a", required = true) boolean a;
        @Option(names = "-b", required = true) boolean b;
    }
    class Group2 {
        @Option(names = "-x", required = true) boolean x;
        @Option(names = "-y", required = true) boolean y;
    }
    class App {
        @ArgGroup(exclusive = true, multiplicity = "0..1")
        Group1 g1;

        @ArgGroup(exclusive = true, multiplicity = "0..1")
        Group2 g2;
    }
    try {
        new CommandLine(new App(), new InnerClassFactory(this)).parseArgs("-x", "-y", "-a", "-b");
        fail("Expected exception");
    } catch (MutuallyExclusiveArgsException ex) {
        assertEquals("Error: -x, -y are mutually exclusive (specify only one)", ex.getMessage());
    }
}
 
Example #25
Source File: ArgGroupTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Command(mixinStandardHelpOptions = true)
void posAndOptAndMixin(int[] posInt, @Option(names = "-s") String[] strings, @Mixin SomeMixin mixin, @ArgGroup(multiplicity = "0..1") Composite composite) {
    this.myMixin = mixin;
    this.myComposite = composite;
    this.myPositionalInt = posInt;
    this.myStrings = strings;
    invoked.add(InvokedSub.posAndOptAndMixin);
}
 
Example #26
Source File: ArgGroupTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Command(mixinStandardHelpOptions = true)
void posAndMixin(int[] posInt, @ArgGroup(multiplicity = "0..1") Composite composite, @Mixin SomeMixin mixin) {
    this.myMixin = mixin;
    this.myComposite = composite;
    this.myPositionalInt = posInt;
    invoked.add(InvokedSub.posAndMixin);
}
 
Example #27
Source File: ArgGroupTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Command(name = "generate", description = "Generate")
void generate(@Option(names = "-x", description = "x", required = true) int x,
              @Option(names = "-y", description = "y", required = true) int y,
              @ArgGroup Exclusive e) {

    this.generateX = x;
    this.generateY = y;
    this.generateExclusive = e;
}
 
Example #28
Source File: ArgGroupTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultipleGroupsWithPositional() {
    class Issue1027 {
        @ArgGroup(exclusive = false, multiplicity = "1..4")
        List<StudentGrade> gradeList;

        @ArgGroup(exclusive = false, multiplicity = "1")
        List<StudentGrade> anotherList;
    }

    Issue1027 bean4 = new Issue1027();
    new CommandLine(bean4).parseArgs("Abby 4.0 Billy 3.5 Caily 3.5 Danny 4.0".split(" "));
    assertEquals(4, bean4.gradeList.size());
    assertEquals(new StudentGrade("Abby", "4.0"),  bean4.gradeList.get(0));
    assertEquals(new StudentGrade("Billy", "3.5"), bean4.gradeList.get(1));
    assertEquals(new StudentGrade("Caily", "3.5"), bean4.gradeList.get(2));
    assertEquals(new StudentGrade("Danny", "4.0"), bean4.gradeList.get(3));

    assertEquals(1, bean4.anotherList.size());
    assertEquals(new StudentGrade("Abby", "4.0"),  bean4.anotherList.get(0));

    Issue1027 bean5 = new Issue1027();
    try {
        new CommandLine(bean5).parseArgs("Abby 4.0 Billy 3.5 Caily 3.5 Danny 4.0 Egon 3.5".split(" "));
        fail("Expected exception");
    } catch (UnmatchedArgumentException ex) {
        assertEquals("Unmatched arguments from index 8: 'Egon', '3.5'", ex.getMessage());
    }
}
 
Example #29
Source File: ArgGroupTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Ignore
@Test
public void testNestedPositionals() {
    class Nested {
        @ArgGroup(exclusive = false, multiplicity = "0..*")
        List<Outer1027> outers;
    }
    Nested bean = new Nested();
    new CommandLine(bean).parseArgs("-x 0 1 -x 00 11 -y 000 111 -y 0000 1111 -x 00000 11111".split(" "));
    assertEquals(3, bean.outers.size());
    assertEquals(new Outer1027("0", "1", null),  bean.outers.get(0));
    List<Inner1027> inners = Arrays.asList(new Inner1027("000", "111"), new Inner1027("0000", "1111"));
    assertEquals(new Outer1027("00", "11", inners), bean.outers.get(1));
    assertEquals(new Outer1027("00000", "11111", null), bean.outers.get(2));
}
 
Example #30
Source File: ArgGroupTest.java    From picocli with Apache License 2.0 5 votes vote down vote up
@Command(mixinStandardHelpOptions = true)
void groupFirst(@ArgGroup(multiplicity = "0..1") Composite composite, @Mixin SomeMixin mixin, int[] posInt, @Option(names = "-s") String[] strings) {
    this.myMixin = mixin;
    this.myComposite = composite;
    this.myPositionalInt = posInt;
    this.myStrings = strings;
    invoked.add(InvokedSub.groupFirst);
}