Categories:
Audio (13)
Biotech (29)
Bytecode (35)
Database (77)
Framework (7)
Game (7)
General (512)
Graphics (53)
I/O (32)
IDE (2)
JAR Tools (86)
JavaBeans (16)
JDBC (89)
JDK (337)
JSP (20)
Logging (103)
Mail (54)
Messaging (8)
Network (71)
PDF (94)
Report (7)
Scripting (83)
Security (32)
Server (119)
Servlet (17)
SOAP (24)
Testing (50)
Web (19)
XML (301)
Other Resources:
Commons CLI API - Short Options Example
Where to get a Java example of managing short options with Commons CLI API?
✍: FYIcenter.com
Here is good Java example of managing short 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 ShortOptionTest { public static void main(String[] args) throws Exception { // Define a short option: -h Options options = new Options(); options.addOption("h", "Print this help message"); try { // Parse options CommandLineParser parser = new DefaultParser(); CommandLine cmd = parser.parse(options, args); // Process options if (cmd.hasOption("h")) { 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("ShortOptionTest", 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 ShortOptionTest.java C:\fyicenter>java -cp .;C:\fyicenter\commons-cli-1.4\commons-cli-1.4.jar ShortOptionTest -h usage: ShortOptionTest -h Print this help message C:\fyicenter>java -cp .;C:\fyicenter\commons-cli-1.4\commons-cli-1.4.jar ShortOptionTest -help Invalid options: -help usage: ShortOptionTest -h Print this help message C:\fyicenter>java -cp .;C:\fyicenter\commons-cli-1.4\commons-cli-1.4.jar ShortOptionTest -? Invalid options: -? usage: ShortOptionTest -h Print this help message
Â
⇒ Commons CLI API - Long Options
⇠Commons CLI API - Short Options
2020-12-22, 443👍, 0💬
Popular Posts:
JDK 11 jdk.internal.vm.ci.jmod is the JMOD file for JDK 11 Internal VM CI module. JDK 11 Internal VM...
Jetty provides an HTTP server, HTTP client, and javax.servlet container. These components are open s...
JDK 1.1 source code directory contains Java source code for JDK 1.1 core classes: "C:\fyicenter\jdk-...
JDK 11 jdk.aot.jmod is the JMOD file for JDK 11 Ahead-of-Time (AOT) Compiler module. JDK 11 AOT Comp...
commons-collections4-4.2 -sources.jaris the source JAR file for Apache Commons Collections 4.2, whic...