commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From John Keyes <j...@mac.com>
Subject Re: [CLI] Making Option Extensible
Date Thu, 25 Jul 2002 22:53:09 GMT
On Wednesday, July 24, 2002, at 05:20 , Berin Loritsch wrote:

>> From: Berin Loritsch [mailto:bloritsch@apache.org]
>>
>> 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
> approach:
>
> CLArgsParser parser = new CLArgsParser( args, OPTIONS );
> Iterator i = parser.getArguments().iterator();
>
> while ( i.hasNext() ) {
>     CLOption option = (CLOption) i.next();
>
>     switch (option.getId()) {
>         case HELP_OPT:
>             printUsage();
>             break;
>
>         case VERSION_OPT:
>             printVersion();
>             break;
>
>         case CONTEXT_DIR_OPT:
>             m_contextDir = option.getArgument();
>             break;
>
>         default:
>             printUsage();
>             break;
>    }
> }
>
> It is short and to the point.

This is short and to the point, this breaks down if I am
using Java style option names e.g. "-classpath".

To do this we would need a means of assigning a specified
id to an Option e.g.

int CLASSPATH = 0;

Option classpath = new Option( "classpath",
                                true,
                                "application classpath");
classpath.setId( CLASSPATH );

This again shows that the Builder pattern bob suggested
would be useful.

-John K

>
> 3) Printing the usage/options
>
> Avalon CLI has support to automatically print out your options and what
> they
> mean directly.  I have to dig deeper to see if Commons supports this or
> not.
> It is really easy:
>
>     private static final void printUsage() {
>         System.out.println( "Usage: java -jar myproject.jar [options]"
> );
>         System.out.println();
>         System.out.println( "Options:" );
>         System.out.println( CLUtil.describeOptions( OPTIONS ) );
>         System.out.println();
>         System.out.println( "Note: the default context directory is
> '../'." );
>         System.exit(0);
>     }
>
> The CLUtil.describeOptions( Option[] ) method creates a string that
> formats
> the short and long options and the associated descriptions so that we
> only
> 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
> Avalon:
>
>     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
> CLOptionDescriptor[]
>     {
>         new CLOptionDescriptor("help",
> CLOptionDescriptor.ARGUMENT_DISALLOWED,
>                                HELP_OPT,
>                                "Print this help message and exit."),
>         new CLOptionDescriptor("version",
> CLOptionDescriptor.ARGUMENT_DISALLOWED,
>                                VERSION_OPT,
>                                "Print the version information and
> exit."),
>         new CLOptionDescriptor("contextDir",
> CLOptionDescriptor.ARGUMENT_REQUIRED,
>                                CONTEXT_DIR_OPT,
>                                "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:   <mailto:commons-dev-
> unsubscribe@jakarta.apache.org>
> For additional commands, e-mail: <mailto:commons-dev-
> help@jakarta.apache.org>
>


--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message