1

I would like to group options in the help message but have one option in that group required. Actually, I want to have one subgroup of the help-message-group required, but I have trouble with both:

import java.util.concurrent.Callable;
import picocli.CommandLine;

@CommandLine.Command(mixinStandardHelpOptions = true, sortOptions = false)
public class PicocliTest implements Callable<Integer> {

    @CommandLine.ArgGroup(heading = "Group heading%n")
    GroupArguments args;

    static class GroupArguments {

        @CommandLine.ArgGroup(exclusive = false, multiplicity = "1")
        AorB aorb;

        static class AorB {

            @CommandLine.Option(names = {"--a"})
            protected String a;

            @CommandLine.Option(names = {"--b"})
            protected String b;
        }

        @CommandLine.Option(required = true, names = {"--important"})
        protected boolean important;

        @CommandLine.Option(names = {"--notImportant"})
        protected boolean notImportant;
    }

    @CommandLine.Option(names = {"--alsoNotImportant"})
    protected boolean alsoNotImportant;

    public static void main(String[] args) {
        new CommandLine(new PicocliTest()).execute(args);
    }

    @Override
    public Integer call() throws Exception {
        System.out.println(args.aorb.a);
        return 0;
    }

}

picocli happily enters the program without any arguments. Setting multiplicity="1" for the args-group does not solve the problem because specifying --notImportant will again run the program just fine.

1 Answers1

0

I have not tested this, but I suspect you also need to put multiplicity = "1" on the outer arg group, as well as the inner arg group:

    @CommandLine.ArgGroup(heading = "Group heading%n", multiplicity = "1")
    GroupArguments args;
Remko Popma
  • 35,130
  • 11
  • 92
  • 114
  • yes, this will force the group to be required, but specifying `--notImportant` will satisfy picocli and it will again enter the program (i.e. without `--important` or `AorB` specified). I was hoping for a solution that would force the user to specify `--important` (and `AorB`). – user8557834 Apr 06 '22 at 07:33