commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "James Strachan" <james_strac...@yahoo.co.uk>
Subject [cli] library change suggestion
Date Tue, 10 Dec 2002 08:47:24 GMT
Forwarding to the list....

----- Original Message ----- 
From: "Mike McLean" <MikeM@pslgroup.com>
To: <jstrachan@apache.org>

I started using the CLI library and found it really nice. One thing I
though might be missing for my needs was a way to validate the arguments
given to a parameter. In other words, if my args[0] looks like -w
Monday, I want to be sure that Monday is a valid value for the w Option.

So I figured I'd give it a shot. I downloaded the source code and have
made the modifications to suit my needs. I've done limited testing, but
it works for my needs. I figured I'd send you guys the small changes I
made in case you find the idea ok. 

Here are the changes that were made:
- OptionBuilder:
Added the validArguments String array memeber along with the
withValidArguments method. Changed the reset method to reset
validArguments.

- Option:
Added the validArguments member along with the accessor methods. Also
changed the clone method to clone the new field.

- Parser:
I added a checkArgValidity method. The method throws a
InvalidArgumentException. The reason I didn't want to throw just a
ParseException is that I didn't find the message usefull. If the
argument isn't valid, then I throw that exception. The call to
checkArgValidity is made from the processArgs method.

- InvalidArgumentException:
As I mentionned, this is a new class that extends ParseException so no
change in code to throw new exception types.

Here's how I use it:
String[] valueWeekdays = {"Monday","Tuesday","Wednesday","Thursday"};
Option weekday = OptionBuilder.withArgName( "Weekday" )
.withLongOpt( "weekday" )
.withDescription( "Weekday to generate.
Monday to Thursday or Auto" )
.hasArg()
.hasArgs(1)
.isRequired()
.withValidArguments(valueWeekdays)
.create("w");

If my w param is Monday (or monday), then no problem. If it's Friday,
then it throws an exception.

As I've mentionned, the testing I've done is limited. I figured that you
guys are probably setup with Junit and have all your tests setup.

If you like the idea and intend to use it, let me know and I'll try to
whip up a regexp version of it where (well you guessed it), the argument
could be validated against a regular expression.

Mike.


Mime
View raw message