Create the Options

The addOption method has three parameters. The first
parameter is a java.lang.String that represents the option.
The second parameter is a boolean that specifies whether the
option requires an argument or not. In the case of a boolean option
(sometimes referred to as a flag) an argument value is not present so
false is passed. The third parameter is the description
of the option. This description will be used in the usage text of the
application.

Parsing the command line arguments

The parse methods of CommandLineParser are used
to parse the command line arguments. There may be several implementations
of the CommandLineParser interface, the recommended one is the
DefaultParser.

Now we need to check if the t option is present. To do
this we will interrogate the
CommandLine
object. The hasOption method takes a
java.lang.String parameter and returns true if the option
represented by the java.lang.String is present, otherwise
it returns false.

International Time

The InternationalDateApp utility extends the
DateApp utility by providing the ability to print the
date and time in any country in the world. To facilitate this a new
command line option, c, has been introduced.

// add c option
options.addOption("c", true, "country code");

The second parameter is true this time. This specifies that the
c option requires an argument value. If the required option
argument value is specified on the command line it is returned,
otherwise null is returned.

Retrieving the argument value

The getOptionValue methods of CommandLine are
used to retrieve the argument values of options.

Ant Example

One of the most ubiquitous Java applications
Ant will be used
here to illustrate how to create the Options required. The following
is the help output for Ant.

ant [options] [target [target2 [target3] ...]]
Options:
-help print this message
-projecthelp print project help information
-version print the version information and exit
-quiet be extra quiet
-verbose be extra verbose
-debug print debugging information
-emacs produce logging information without adornments
-logfile <file> use given file for log
-logger <classname> the class which is to perform logging
-listener <classname> add an instance of class as a project listener
-buildfile <file> use given buildfile
-D<property>=<value> use value for given property
-find <file> search for buildfile towards the root of the
filesystem and use it

Boolean Options

Lets create the boolean options for the application as they
are the easiest to create. For clarity the constructors for
Option are used here.

usage: ant
-D <property=value> use value for given property
-buildfile <file> use given buildfile
-debug print debugging information
-emacs produce logging information without adornments
-file <file> search for buildfile towards the root of the
filesystem and use it
-help print this message
-listener <classname> add an instance of class as a project listener
-logger <classname> the class which it to perform logging
-projecthelp print project help information
-quiet be extra quiet
-verbose be extra verbose
-version print the version information and exit

If you also require to have a usage statement printed
then calling formatter.printHelp( "ant", options, true )
will generate a usage statment as well as the help information.

ls Example

One of the most widely used command line applications in the *nix world
is ls. Due to the large number of options required for ls
this example will only cover a small proportion of the options. The following
is a section of the help output.

Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuSUX nor --sort.
-a, --all do not hide entries starting with .
-A, --almost-all do not list implied . and ..
-b, --escape print octal escapes for nongraphic characters
--block-size=SIZE use SIZE-byte blocks
-B, --ignore-backups do not list implied entries ending with ~
-c with -lt: sort by, and show, ctime (time of last
modification of file status information)
with -l: show ctime and sort by name
otherwise: sort by ctime
-C list entries by columns

The following is the code that is used to create the
Options for this example.

Apache Commons, Apache Commons CLI, Apache, the Apache feather logo, and the Apache Commons project logos are trademarks of The Apache Software Foundation.
All other marks mentioned may be trademarks or registered trademarks of their respective owners.