commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From John Keyes <j...@mac.com>
Subject [CLI] value separator support
Date Mon, 05 Aug 2002 00:21:19 GMT
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.

Cheers,
-John K


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