Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclusive Option ArgGroup is enforced even when help.option is supplied #653

Closed
deining opened this issue Apr 2, 2019 · 2 comments
Closed

Comments

@deining
Copy link
Contributor

deining commented Apr 2, 2019

I just started to try out the new 4.0.0 alpha build, the new argument group feature comes in very handy for me. I came across the following issue, though:

Minimal code sample:

public class ArgGroupsTest {

    public static void main(String[] args) {
        CommandLine cmd = new CommandLine(new TestCommand());
        cmd.parseWithHandler(new CommandLine.RunAll(), args);
    }

    @Command(subcommands = { TestSubCommand.class, CommandLine.HelpCommand.class }, name = "ArgGroupsTest", mixinStandardHelpOptions = true, version = "0.xx")
    static class TestCommand implements Runnable {

        @ArgGroup(exclusive = true, multiplicity = "1")
        DataSource datasource;

        static class DataSource {
            @Option(names = "-a", required = true)
            static boolean isA;
            @Option(names = "-b", required = true)
            static File dataFile;
        }

        @Override
        public void run() {
            // nothing to do here
        }
    }
    
    @Command(name = "test", showDefaultValues = true)
    static class TestSubCommand implements Runnable {

        @Override
        public void run() {
            System.out.println("Subcommand called!");
            
        }
    }
}

Now, when requesting help:

$ ArgGroupsTest --help

the following message is presented on stderr:

Error: Missing required argument (specify one of these): -a, -b=<dataFile>
Usage: ArgGroupsTest (-a | -b=<dataFile>) [-hV] [COMMAND]
  -a
  -b=<dataFile>
  -h, --help       Show this help message and exit.
  -V, --version    Print version information and exit.
Commands:
  test
  help  Displays help information about the specified command

Once help is requested, usage instructions should be given on stdout, I don't expect any error due to a missing required mutually exclusive option here.

@remkop
Copy link
Owner

remkop commented Apr 2, 2019

Ouch! Good catch! I agree this is not expected behavior. I’ll look at this as soon as I can.

@remkop remkop added this to the 4.0-alpha-2 milestone Apr 2, 2019
@remkop
Copy link
Owner

remkop commented Apr 3, 2019

I found the cause. Basically, all validation should be skipped if any help is requested. Currently only some validation is skipped but not all. The validation for the arg groups is among the ones that are not skipped.

This should fix it:

Index: src/main/java/picocli/CommandLine.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/main/java/picocli/CommandLine.java	(date 1554265596285)
+++ src/main/java/picocli/CommandLine.java	(date 1554265596285)
@@ -8852,11 +8852,13 @@
                 }
             } while (!argumentStack.isEmpty() && continueOnError);
 
-            validateConstraints(argumentStack, required, initialized);
+            if (!isAnyHelpRequested()) {
+                validateConstraints(argumentStack, required, initialized);
+            }
         }
 
         private void validateConstraints(Stack<String> argumentStack, List<ArgSpec> required, Set<ArgSpec> matched) {
-            if (!isAnyHelpRequested() && !required.isEmpty()) {
+            if (!required.isEmpty()) {
                 for (ArgSpec missing : required) {
                     Assert.assertTrue(missing.group() == null, "Arguments in a group are not necessarily required for the command");
                     if (missing.isOption()) {

I'll push this when I get a chance.

@remkop remkop closed this as completed in d36d643 Apr 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants