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 - Options with Arguments
How to manage options with arguments with Apache Commons CLI API?
✍: FYIcenter.com
An option with argument is an option that requires an input arguement. The argument value must be specified immediately after the option name. For example, "-i world", "-input world", "--input world" are alias of a single option with an argument.
An option with an argument can be managed with Apache Commons CLI API in 4 steps:
1. Define Options - An option with an argument can be defined and added to the options collection by long the addOption() method with 4 arguments. The 3rd argument "true" specifies that the option has an argument. For example:
import org.apache.commons.cli.Options; options.addOption("i", "input", true, "Input string");
2. Parse Options - An option with an argument can be parsed together with all other options in a single call of CommandLineParser.parse(). For example:
import org.apache.commons.cli.CommandLineParser; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.DefaultParser; CommandLineParser parser = new DefaultParser(); CommandLine cmd = parser.parse( options, args);
3. Process Options - A long option can be processed by calling the CommandLine.hasOption() method and the CommandLine.getOptionValue() method. For example:
} else if (cmd.hasOption("input")) { System.out.println("Hello "+cmd.getOptionValue("input")); }
4. Print Options - An option with an argument can be printed out as help message together with all other options. For example:
import org.apache.commons.cli.HelpFormatter; HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("ArgumentOptionTest", options);
⇒ Commons CLI API - Options with Arguments Example
⇐ Commons CLI API - Long Options Example
2020-12-15, 1018🔥, 0💬
Popular Posts:
JDK 11 jdk.jconsole.jmod is the JMOD file for JDK 11 JConsole tool, which can be invoked by the "jco...
JDK 11 java.xml.jmod is the JMOD file for JDK 11 XML (eXtensible Markup Language) module. JDK 11 XML...
Jettison is a collection of Java APIs (like STaX and DOM) which read and write JSON. This allows nea...
JRE 5 sunjce_provider.jar is the JAR file for JRE 5 Sun JCE Provider, which provides implementations...
Jackson is "the Java JSON library" or "the best JSON parser for Java". Or simply as "JSON for Java"....