The following sections describe some example scenarios on how to use CLI in applications.
A boolean option is represented on a command line by the presence
of the option, i.e. if the option is found then the option value
is true, otherwise the value is false.
The DateApp utility prints the current date to standard
output. If the -t option is present the current time is
also printed.
An
Options object must be created and the Option must be
added to it.
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.
The parse methods of CommandLineParser are used
to parse the command line arguments. The PosixPaser is
great when you need to handle options that are one character long,
like the t option in this example.
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.
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.
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.
The getOptionValue methods of CommandLine are
used to retrieve the argument values of options.
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.
Lets create the boolean options for the application as they
are the easiest to create. For clarity the constructors for
Option are used here.
The argument options are created using the OptionBuilder.
The last option to create is the Java property and it is also created using the OptionBuilder.
Now that we have created each
Option we need
to create the
Options
instance. This is achieved using the
addOption
method of Options.
All the preperation is now complete and we are now ready to parse the command line arguments.
We now need to create a Parser. This will parse the command
line arguments, using the rules specified by the Options and
return an instance of CommandLine.
This time we will use a GnuParser
which is able to handle options that are more than one character long.
To see if an option has been passed the hasOption
method is used. The argument value can be retrieved using
the getValue method.
CLI also provides the means to automatically generate usage and help information. This is achieved with the HelpFormatter class.
When executed the following output is produced:
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.
One of the most widely used command line applications in the *nix world
is ls. To parse a command line for an application like this
we will use the PosixParser.
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.
The following is the code that is used to create the Options for this example.