I realize that the question mentions a preference for Commons CLI, but I guess that when this question was asked, there was not much choice in terms of Java command line parsing libraries. But nine years later, in 2020, would you not rather write code like the below?
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;
import java.io.File;
import java.util.List;
import java.util.concurrent.Callable;
@Command(name = "myprogram", mixinStandardHelpOptions = true,
description = "Does something useful.", version = "1.0")
public class MyProgram implements Callable<Integer> {
@Option(names = "-r", description = "The r option") String rValue;
@Option(names = "-S", description = "The S option") String sValue;
@Option(names = "-A", description = "The A file") File aFile;
@Option(names = "--test", description = "The test option") boolean test;
@Parameters(description = "Positional params") List<String> positional;
@Override
public Integer call() {
System.out.printf("-r=%s%n", rValue);
System.out.printf("-S=%s%n", sValue);
System.out.printf("-A=%s%n", aFile);
System.out.printf("--test=%s%n", test);
System.out.printf("positionals=%s%n", positional);
return 0;
}
public static void main(String... args) {
System.exit(new CommandLine(new MyProgram()).execute(args));
}
}
Execute by running the command in the question:
java MyProgram -r opt1 -S opt2 arg1 arg2 arg3 arg4 --test -A opt3
What I like about this code is that it is:
call
method is free of parsing-related logic--help
and --version
optionsThe above functionality is only part of what you get when you use the picocli (https://picocli.info) library.
Now, bear in mind that I am totally, completely, and utterly biased, being the author of picocli. :-) But I do believe that in 2020 we have better alternatives for building a command line apps than Commons CLI.