No Exception on unknown option

48 views
Skip to first unread message

Ralf Schandl

unread,
Aug 17, 2020, 12:16:30 PM8/17/20
to jcommander
JCommander does not throw an exception when an unknown option is given.

In the following example, the unknown option '-x' is just added to the "main arguments".
I would expect that JCommander throws a exception in this case. Am I doing something wrong?
Ps: Tested with JCommander 1.78

public static void main(final String[] args) {

    final String[] argv = { "-d", "-x", "file.txt" };

    Arguments argDef = new Arguments();

    JCommander jc = JCommander.newBuilder().addObject(argDef).build();
    jc.parse(argv);

    System.out.println("debug: " + argDef.debug);
    System.out.println("filename(s): " + argDef.mainArgs);
}

public static class Arguments {

    @Parameter(names = "-d")
    private boolean debug;

    @Parameter(description = "main arguments")
    private List<String> mainArgs;
}

Reply all
Reply to author
Forward
0 new messages