commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Max Rydahl Andersen" <>
Subject Re: [cli] library change suggestion
Date Tue, 10 Dec 2002 09:49:50 GMT
Suggestion: Make it perform the validation on to an interface instead of
just a raw list or regular expression.


CLIValidArgumentsValidator valueWeekdays = new WeirdWeekdaysValidator() {
 String valid[] = {"Monday","Tuesday","Wednesday","Thursday"};

   boolean validateArgument(String argument) {
    if argument in valid then return true else return false;

Option weekday = OptionBuilder.withArgName( "Weekday" )
.withLongOpt( "weekday" )
.withDescription( "Weekday to generate.
Monday to Thursday or Auto" )

Or something like that thus CLI won't be limited on to string arrays and
regexp.......CLI could ofcourse come with prebuilt StringArrayValidators and

idea: Maybe the validateArgument should thrown an exception with info on why
it failed which CLI could understand and provide it as an message to the

Just a thought...

----- Original Message -----
From: "James Strachan" <>
To: "Mike McLean" <>; "Jakarta Commons Developers"
Sent: Tuesday, December 10, 2002 9:47 AM
Subject: [cli] library change suggestion

> Forwarding to the list....
> ----- Original Message -----
> From: "Mike McLean" <>
> To: <>
> 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.


> --
> To unsubscribe, e-mail:
> For additional commands, e-mail:

To unsubscribe, e-mail:   <>
For additional commands, e-mail: <>

View raw message