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 - Options with Arguments Example
Where to get a Java example of managing Options with Arguments with Commons CLI API?
✍: FYIcenter.com
Here is good Java example of managing Options with Arguments 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 ArgumentOptionTest { 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"); // Define an option with argument: --input ..., -input ..., -i ... options.addOption("i", "input", true, "Input string"); try { // Parse options CommandLineParser parser = new DefaultParser(); CommandLine cmd = parser.parse(options, args); // Process options if (cmd.hasOption("help")) { help(options); } else if (cmd.hasOption("input")) { 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("ArgumentOptionTest", 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 ArgumentOptionTest.java C:\fyicenter>java -cp .;C:\fyicenter\commons-cli-1.4\commons-cli-1.4.jar ArgumentOptionTest -i world Hello world C:\fyicenter>java -cp .;C:\fyicenter\commons-cli-1.4\commons-cli-1.4.jar ArgumentOptionTest -input world Hello world C:\fyicenter>java -cp .;C:\fyicenter\commons-cli-1.4\commons-cli-1.4.jar ArgumentOptionTest --input world Hello world C:\fyicenter>java -cp .;C:\fyicenter\commons-cli-1.4\commons-cli-1.4.jar ArgumentOptionTest --insert world Invalid options: --insert usage: ArgumentOptionTest -h,--help Print this help message -i,--input <arg> Input string
Â
⇒ Commons CLI API - Option Class
⇠Commons CLI API - Options with Arguments
2020-05-15, 701👍, 0💬
Popular Posts:
commons-io-1.4.jar is the JAR file for Commons IO 1.4, which is a library of utilities to assist wit...
The Web Services Description Language for Java Toolkit (WSDL4J), Release 1.6.2, allows the creation,...
Apache Axis2 is the core engine for Web services. It is a complete re-design and re-write of the wid...
JDK 11 jdk.localedata.jmod is the JMOD file for JDK 11 Localedata module. JDK 11 Locale Data module ...
What Is mail.jar of JavaMail 1.4? I got the JAR file from javamail-1_4.zip. mail.jar in javamail-1_4...