commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Berin Loritsch" <blorit...@apache.org>
Subject RE: [CLI] value separator support
Date Mon, 05 Aug 2002 15:10:59 GMT
> From: John Keyes [mailto:jbjk@mac.com] 
> 
> I have added two withValueSeparator method to OptionBuilder. 
> The no parameter one uses the default value of '='.  Here's a usage
> example:
> 
> Option option = OptionBuilder.withValueSeparator()
>                                        .hasArgs()
>                                        .create( 'D' );
> 
> Options options = new Options().addOption( option );
> 
> CommandLineParser parser = CommandLineParserFactory.newParser();
> 
> String[] args = new String[] {
>      "-Dlog4j.configuration=file:log.properties",
>      "-DJAVA_HOME=/opt/java"
> };
> 
> CommandLine cmd = parser.parse( args );
> 
> // Access the values through the Option itself
> for( int i = 0; i < option.getValues().length; i+2 ) {
>      String property = option.getValue( i );
>      String value = option.getValue( i + 1 );
> }
> 
> //Access the values through the CommandLine
> Iterator iter = cmd.iterator();
> while( iter.hasNext() ) {
>      Option opt = (Option)iter.next();
>      switch ( opt.getId() ) {
>          case 'D':
>              String[] values = line.getValues( 'D' );
>              for( int i = 0; i < values.length; i+2 ) {
>                  String property = values[ i ];
>                  String value = values[ i + 1 ];
>              }
>              break;
>          default:
>              // ...
>      }
> }
> 
> There is one issue that I'd like to get people's opinions on. 
>  If the number of arguments for an Option is set and a value 
> is being split e.g. in the above example if I create the 
> Option withArgs( 3 ), then the value '/opt/java' cannot be 
> added to the Option.  I have implemented is so any additional 
> values found are added to the args list but I'm not sure if 
> this is the best thing to do.


I see.  So what you are saying is that if you turn on hasArgs(),
then every odd entry is the first half, and every even entry is
the second half.

This is different from having multiple instances of the Option
with the same ID.

If we had the CommandLineParser have a new instance of the Option
for every pair, we could limit it with hasArgs(2) and still process
everything correctly.

However, by doing it that way, you lose the ability of doing
CommandLine.getOption("D") and retrieving all the pairs.

Avalon CLI has the ability to do both approaches, and I believe
that for the getOption('D') style it only returns the first instance.

The CommandLine object can be adjusted to have a List of Options,
and keep the order of processing there.  It's a thought, however
its something to weigh out.  I personally like the simplicity of
processing each instance of an Option separately--but I am also
not the only user either.


--
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