Categories:
Audio (13)
Biotech (29)
Bytecode (36)
Database (77)
Framework (7)
Game (7)
General (507)
Graphics (53)
I/O (35)
IDE (2)
JAR Tools (101)
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 (309)
Collections:
Other Resources:
Commons CLI API - Option Class
How to use org.apache.commons.cli.Option class to define options?
✍: FYIcenter.com
Commons CLI API allows you to define and add options directly to
the org.apache.commons.cli.Options object.
It also allows you to define options using the org.apache.commons.cli.Option class and add them to the org.apache.commons.cli.Options object later.
Here is good Java example of using org.apache.commons.cli.Option class to define options with Commons CLI API, ShortOptionTest.java:
// Copyright (c) 2018 FYIcenter.com import org.apache.commons.cli.Option; 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 OptionClassTest { public static void main(String[] args) throws Exception { Options options = new Options(); // Define a short option: -h Option help = new Option("h", "Print this help message"); options.addOption(help); // Define a long option: --version -version -v Option version = new Option("v", "version", false, "Print version info"); options.addOption(version); // Define a short option with argument: -n 3 Option n = new Option("n", true, "Number of output lines"); options.addOption(n); // Define a long option with argument: --input ..., -input ..., -i ... Option input = new Option("i", "input", true, "Input string"); options.addOption(input); try { // Parse options CommandLineParser parser = new DefaultParser(); CommandLine cmd = parser.parse(options, args); int lines = 1; if (cmd.hasOption("n")) { lines = Integer.parseInt(cmd.getOptionValue("n")); } if (cmd.hasOption("h")) { help(options); } else if (cmd.hasOption("version")) { System.out.println("OptionClassTest version 1.0.0"); } else if (cmd.hasOption("input")) { for (int i=0; i<lines; i++) { System.out.println("Hello "+cmd.getOptionValue("input")); } } } 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("OptionClassTest", 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 OptionClassTest.java C:\fyicenter>java -cp .;C:\fyicenter\commons-cli-1.4\commons-cli-1.4.jar OptionClassTest -h usage: OptionClassTest -h Print this help message -i,--input <arg> Input string -n <arg> Number of output lines -v,--version Print version info C:\fyicenter>java -cp .;C:\fyicenter\commons-cli-1.4\commons-cli-1.4.jar OptionClassTest --version OptionClassTest version 1.0.0 C:\fyicenter>java -cp .;C:\fyicenter\commons-cli-1.4\commons-cli-1.4.jar OptionClassTest -i world Hello world C:\fyicenter>java -cp .;C:\fyicenter\commons-cli-1.4\commons-cli-1.4.jar OptionClassTest -i world -n 3 Hello world Hello world Hello world
⇒ Commons CLI API - Option.Builder Class
⇐ Commons CLI API - Options with Arguments Example
2020-05-15, 828👍, 0💬
Popular Posts:
What Is commons-fileupload-1.3.3 .jar?commons-fileupload-1.3.3 .jaris the JAR file for Apache Common...
The Java Naming and Directory Interface (JNDI) is part of the Java platform, providing applications ...
MXP1 is a stable XmlPull parsing engine that is based on ideas from XPP and in particular XPP2 but c...
XStream is a simple library to serialize objects to XML and back again. JAR File Size and Download L...
What Is jsse.jar (JDK 6) Java Secure Socket Extension? jsse.jar, Java Secure Socket Extension, is Ja...