Sample command line tool development using Apache Commons CLI

Source: Internet
Author: User

Sample command line tool development using Apache Commons CLI

Directory

Concepts
Introduction to Apache Commons CLI
CLI definition phase
CLI parsing phase
CLI inquiry stage
Sample Code
 
Concepts
 
Introduction to Apache Commons CLI

Apache Commons CLI is a toolkit for parsing command line Input in Apache. It also provides the function of automatically generating output help documents.

Apache Commons CLI supports multiple input parameter formats, including the following formats:

1. POSIX (Portable Operating System Interface of Unix) parameter format, such as tar-zxvf foo.tar.gz

2. long parameter form in GNU, such as du -- human-readable -- max-depth = 1

3. The parameter format in the Java command, such as java -Djava.net. useSystemProxies = true Foo

4. The form of the short bar parameter with the parameter value, such as gcc-O2 foo. c

5. The long bar parameter does not contain the parameter value, for example, ant-projecthelp.

CLI command code implementation

The command line program processing process is relatively simple. The main process is to set command line parameters-> parse input parameters-> use input data for logical processing.

CLI definition phase

Each command line must define a set of parameters that are used to define the interface of the application. Apache Commons CLI uses the Options class to define and set parameters. It is the container of all Option instances. Currently, there are two methods to create Options in CLI. One is through constructor, which is the most common and well-known method; another method is to implement it through the factory method defined in Options.

The target result of the CLI definition stage is to create an Options instance.

// Create an Options object
Options options = new Options ();
 
// Add the-h Parameter
Options. addOption ("h", false, "Lists short help ");
 
// Add the-t parameter
Options. addOption ("t", true, "Sets the HTTP communication protocol for CIM connection ");

The addOption () method has three parameters. The first parameter sets the single-character name of this option, and the second parameter specifies whether a value is required for this option, the third parameter is a brief description of this option. In this code snippet, the first parameter only lists the Help file, and you do not need to enter any value. The second parameter requires the user to enter the HTTP communication protocol, therefore, the second parameters of the two options are false and true, respectively.

CLI parsing phase

In the parsing phase, pass in the application text through the command line for processing. The processing process is based on the rules defined during the implementation of the parser. The parse method defined in the CommandLineParser class uses the Options instance and a set of strings generated in the CLI definition phase as input, and returns the CommandLine generated after parsing.

The target result of the CLI parsing stage is to create a CommandLine instance.

CommandLineParser parser = new PosixParser ();
CommandLine cmd = parser. parse (options, args );
 
If (cmd. hasOption ("h ")){
// Brief help information is displayed here
}

CLI inquiry stage

In the query phase, the application queries CommandLine and determines the program branches to be executed based on the Boolean parameters and the parameter values provided to the application. This stage is implemented in the user's code. The access method in CommandLine provides the CLI inquiry capability for the user's code.

The purpose of the CLI query stage is to pass all the text information obtained through the command line and processing parameters to the user's code.

CommandLine = parser. parse (options, args );
If (commandLine. hasOption ('H ')){
// Print help
Hf. printHelp ("testApp", options, true );
}

Sample Code

Import org. apache. commons. cli. CommandLine;
Import org. apache. commons. cli. CommandLineParser;
Import org. apache. commons. cli. HelpFormatter;
Import org. apache. commons. cli. Option;
Import org. apache. commons. cli. Options;
Import org. apache. commons. cli. ParseException;
Import org. apache. commons. cli. PosixParser;
 
 
/**
* DateTime: 5:07:31, January 1, January 1, 2015
*
*/
Public class Test {
Public static void main (String [] args ){
String [] arg = {"-h", "-c", "config. xml "};
TestOptions (arg );
}
 
 
Public static void testOptions (String [] args ){
Options options = new Options ();
Option opt = new Option ("h", "help", false, "Print help ");
Opt. setRequired (false );
Options. addOption (opt );
 
Opt = new Option ("c", "configFile", true, "Name server config properties file ");
Opt. setRequired (false );
Options. addOption (opt );
 
Opt = new Option ("p", "printConfigItem", false, "Print all config item ");
Opt. setRequired (false );
Options. addOption (opt );
 
HelpFormatter hf = new HelpFormatter ();
Hf. setWidth (110 );
CommandLine commandLine = null;
CommandLineParser parser = new PosixParser ();
Try {
CommandLine = parser. parse (options, args );
If (commandLine. hasOption ('H ')){
// Print help
Hf. printHelp ("testApp", options, true );
}
 
// Print the opts name and Value
System. out. println ("--------------------------------------");
Option [] opts = commandLine. getOptions ();
If (opts! = Null ){
For (Option opt1: opts ){
String name = opt1.getLongOpt ();
String value = commandLine. getOptionValue (name );
System. out. println (name + "=>" + value );
}
}
}
Catch (ParseException e ){
Hf. printHelp ("testApp", options, true );
}
}
}

This article permanently updates the link address:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.