Categories:
Audio (13)
Biotech (29)
Bytecode (36)
Database (77)
Framework (7)
Game (7)
General (507)
Graphics (53)
I/O (35)
IDE (2)
JAR Tools (102)
JavaBeans (21)
JDBC (121)
JDK (426)
JSP (20)
Logging (108)
Mail (58)
Messaging (8)
Network (84)
PDF (97)
Report (7)
Scripting (84)
Security (32)
Server (121)
Servlet (26)
SOAP (24)
Testing (54)
Web (15)
XML (322)
Collections:
Other Resources:
Commons CLI API - Long Options Example
Where to get a Java example of managing long options with Commons CLI API?
✍: FYIcenter.com
Here is good Java example of managing long options with Commons CLI API,
ShortOptionTest.java:
// Copyright (c) 2018 FYIcenter.com
import org.apache.commons.cli.Options;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.UnrecognizedOptionException;
public class LongOptionTest {
public static void main(String[] args) throws Exception {
// Define a long option: --help -help -h
Options options = new Options();
options.addOption("h", "help", false, "Print this help message");
try {
// Parse options
CommandLineParser parser = new DefaultParser();
CommandLine cmd = parser.parse(options, args);
// Process options
if (cmd.hasOption("help")) {
help(options);
}
} catch (UnrecognizedOptionException e) {
System.out.println("Invalid options: "+e.getOption());
help(options);
}
}
public static void help(Options options) {
// Print options
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("LongOptionTest", options);
}
}
You can compile and run it with commons-cli-1.4.jar:
C:\fyicenter>javac -cp C:\fyicenter\commons-cli-1.4\commons-cli-1.4.jar LongOptionTest.java C:\fyicenter>java -cp .;C:\fyicenter\commons-cli-1.4\commons-cli-1.4.jar LongOptionTest -h usage: LongOptionTest -h,--help Print this help message C:\fyicenter>java -cp .;C:\fyicenter\commons-cli-1.4\commons-cli-1.4.jar LongOptionTest --help usage: LongOptionTest -h,--help Print this help message C:\fyicenter>java -cp .;C:\fyicenter\commons-cli-1.4\commons-cli-1.4.jar LongOptionTest -help usage: LongOptionTest -h,--help Print this help message C:\fyicenter>java -cp .;C:\fyicenter\commons-cli-1.4\commons-cli-1.4.jar LongOptionTest -helps Invalid options: -helps usage: LongOptionTest -h,--help Print this help message
⇒ Commons CLI API - Options with Arguments
⇐ Commons CLI API - Long Options
2020-12-15, ∼1427🔥, 0💬
Popular Posts:
commons-collections4-4.2 -sources.jaris the source JAR file for Apache Commons Collections 4.2, whic...
JDK 17 java.xml.crypto.jmod is the JMOD file for JDK 17 XML (eXtensible Markup Language) Crypto modu...
commons-lang-2.6.jar is the JAR file for Apache Commons Lang 2.6, which provides a host of helper ut...
JDK 11 java.compiler.jmod is the JMOD file for JDK 11 Compiler module. JDK 11 Compiler module compil...
MP3SPI is a Java Service Provider Interface that adds MP3 (MPEG 1/2/2.5 Layer 1/2/3) audio format su...