1. Apache Commons CLI
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.4</version>
</dependency>
Options options = new Options();
CommandLineParser parser = new DefaultParser();
try {
CommandLine cmd = parser.parse(options, args);
if (cmd.hasOption("help")) {
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("myapp", options);
return;
}
if (cmd.hasOption("verbose")) {
}
if (cmd.hasOption("file")) {
String filePath = cmd.getOptionValue("file");
}
} catch (ParseException e) {
}
2. Picocli
<dependency>
<groupId>info.picocli</groupId>
<artifactId>picocli</artifactId>
<version>4.6.1</version>
</dependency>
public class MyApp implements Runnable {
private boolean help;
private boolean verbose;
private String filePath;
public void run() {
if (help) {
CommandLine.usage(this, System.out);
return;
}
if (verbose) {
}
if (filePath != null) {
}
}
public static void main(String[] args) {
CommandLine.run(new MyApp(), args);
}
}