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 theOption must be
added to it.
The addOption method has three parameters. The first
parameter is a java.lang.String that represents the option.
Thesecond paramter 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
it 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.
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 Options are
used to retrieve the argument values of options.
As one of the most ubquituous Java applications Ant it 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 on Option are used here.
The argument options are created using the OptionBuilder.
The last option to create is the Java property and it too is 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.
To see if an option has been passed thehasOption
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
a different parser is required, 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.