You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by James Strachan <ja...@yahoo.co.uk> on 2002/12/10 09:47:24 UTC
[cli] library change suggestion
Forwarding to the list....
----- Original Message -----
From: "Mike McLean" <Mi...@pslgroup.com>
To: <js...@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.
Re: [cli] library change suggestion
Posted by Max Rydahl Andersen <ma...@eos.dk>.
Suggestion: Make it perform the validation on to an interface instead of
just a raw list or regular expression.
e.g.
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" )
.hasArg()
.hasArgs(1)
.isRequired()
.withValidArguments(valueWeekdays)
.create("w");
Or something like that thus CLI won't be limited on to string arrays and
regexp.......CLI could ofcourse come with prebuilt StringArrayValidators and
RegExpValidators....
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
user...
Just a thought...
/max
----- Original Message -----
From: "James Strachan" <ja...@yahoo.co.uk>
To: "Mike McLean" <Mi...@pslgroup.com>; "Jakarta Commons Developers"
<co...@jakarta.apache.org>
Sent: Tuesday, December 10, 2002 9:47 AM
Subject: [cli] library change suggestion
> Forwarding to the list....
>
> ----- Original Message -----
> From: "Mike McLean" <Mi...@pslgroup.com>
> To: <js...@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.
>
>
----------------------------------------------------------------------------
----
> --
> To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
> For additional commands, e-mail:
<ma...@jakarta.apache.org>
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>