Skip to content

Duplicate name exception: better / more concise error message #758

@deining

Description

@deining

I'm using the following code to ilustrate the issue:

import picocli.CommandLine;
import picocli.CommandLine.ArgGroup;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;

public class ExecuteTest {
    
    static CommandLine cmd;

    public static void main(String[] args) {
        cmd = new CommandLine(new TestCommand());
        cmd.execute("");
    }

    @Command(name = "ExecuteTest")
    static class TestCommand implements Runnable {

        @ArgGroup(exclusive = false)
        static D d;
        
        static class D {
        
        @Option(names = "p")
        static int p1;

        @Option(names = "p")
        static int p2;
        }
        
        @Override
        public void run() {
            // nothing to do here
        }
    }
}

Issue 1: If I'm running the code above, an exception is thrown:

Exception in thread "main" picocli.CommandLine$DuplicateNameException: An option cannot be in multiple groups but p is in [[p=<p1>] [p=<p2>]] and [[p=<p1>] [p=<p2>]]. Refactor to avoid this. For example, (-a | (-a -b)) can be rewritten as (-a [-b]), and (-a -b | -a -c) can be rewritten as (-a (-b | -c)).
	at picocli.CommandLine$Model$CommandSpec.addGroupArgsToCommand(CommandLine.java:5425)

Concerning the error message, there is still room for improvement IMHO: currently, the message tells that an option cannot be in multiple groups. This is misleading since in my example, I do have one argument group only. Is it really the argument group you are referring to?
I would propose to come up with an error message similar to:

Duplicate option name 'p' in argument group 'd'.

Just my 2 cents, thanks for providing and continuously improving picocli.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions