commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Berin Loritsch" <>
Subject RE: [CLI] Making Option Extensible
Date Wed, 24 Jul 2002 17:20:10 GMT
> From: Berin Loritsch [] 
> As I don't have any other concerns, I don't think so.  Are 
> there docs for the Commons CLI package?  I want to see how 
> different it is to set up the options for a program.

Thanks John for pointing me to the docs.  Here are some observations:

* Commons CLI is more featureful, but could use a little refinement
* Avalon CLI is more specific, has some good points, but needs a lot
  of refinement.

1) Creating A Parser

The process of setting System properties is not really something
CLI should require.  Reason: DMZ boxen.  If I have a server or
project running on a hardened box, then a Security Admin who knows
about Java permissions will likely remove the ability to assign
values to the System properties.

Instead, Commons CLI should provide an interface like the JSSE
and JCE APIs.  Creating an instance of the CommandLineParser would
be best if we have an optional argument:

CommandLineParser parser = CommandLineParserFactory.newParser("GNU");

Something along those lines would be best.

2) Processing the Options

Sometimes there are a lot of potential options that need to be parsed.
The current Commons API requires you to provide a long if/then/else
to process the options.  This is time consuming, and a lot more verbose
than the way Avalon CLI does it.  Contrast it with the following

CLArgsParser parser = new CLArgsParser( args, OPTIONS );
Iterator i = parser.getArguments().iterator();

while ( i.hasNext() ) {
    CLOption option = (CLOption);

    switch (option.getId()) {
        case HELP_OPT:

        case VERSION_OPT:

        case CONTEXT_DIR_OPT:
            m_contextDir = option.getArgument();


It is short and to the point.

3) Printing the usage/options

Avalon CLI has support to automatically print out your options and what
mean directly.  I have to dig deeper to see if Commons supports this or
It is really easy:

    private static final void printUsage() {
        System.out.println( "Usage: java -jar myproject.jar [options]"
        System.out.println( "Options:" );
        System.out.println( CLUtil.describeOptions( OPTIONS ) );
        System.out.println( "Note: the default context directory is
'../'." );

The CLUtil.describeOptions( Option[] ) method creates a string that
the short and long options and the associated descriptions so that we
have to document the options in one place.

4) Describing Options

I think the Commons CLI is prettier, but this is what it looks like in

    private static final int HELP_OPT =        'h';
    private static final int VERSION_OPT =     'v';
    private static final int CONTEXT_DIR_OPT = 'd';

    private static final CLOptionDescriptor[] OPTIONS = new
        new CLOptionDescriptor("help",
                               "Print this help message and exit."),
        new CLOptionDescriptor("version",
                               "Print the version information and
        new CLOptionDescriptor("contextDir",
                               "Override the context directory"),

That means the options are already set up when the class is loaded.
There is
no additional work that needs to be done.  In order to do the same in
Commons CLI,
we would have to put the work in a static initializer.

BTW, the code is snipped from an actual class I have using Avalon CLI.
I removed
most of the options for brevity's sake.

Let's see if we can merge them together and make a better whole.

To unsubscribe, e-mail:   <>
For additional commands, e-mail: <>

View raw message