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] value separator support
Date Mon, 05 Aug 2002 23:18:47 GMT
On Monday, August 5, 2002, at 03:10 , Berin Loritsch wrote:

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

If you have hasArgs and withValueSeparator this is the case.

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

Yeap.

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

This is true.  I have been thinking about the multiple instances approach
and how this could be implemented.

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

I think I will be able to achieve this with the implementation I have
in mind.

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

Agreed.  The approach I take to implementing new features is that
as long as it doesn't affect the APIs that were previously exposed
then just go for it.  If it does affect the APIs then mail the list and
trash out the issue.

I'm not satisfied with the implementation of the value separator
stuff.  I've embedded it into the parsers but I'd much prefer to
sink this into the Option itself.  I like the idea of keeping the parser
code as simple as possible.

So I will be hoping to get this done soon.  I will keep you posted on
how it is going.

Thanks,
-John K

p.s. I think I'll have to add you to the contributor list after all of
the help you've been giving me.


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