kohsuke / args4j

args4j
http://args4j.kohsuke.org/
MIT License
789 stars 189 forks source link

Add ability to have different methods for different arguments #92

Open JosephWPlant opened 9 years ago

JosephWPlant commented 9 years ago

I have a set of arguments in a specific order. Each needs validation applying to it, so parsing to a method instead of a variable is beneficial. So my code should look something like this:

@Argument(index = 0)
public void method0(String str){
    //method0 validation
   this.method0Var = str;
}
@Argument(index = 1)
public void method1(String str){
    //method1 validation
   this.method1Var = str;
}

Unfortunately, args4J currently presumes that arguments with a method are multivalued arguments, and thusly associates all arguments with the same method. For example, if there were 2 arguments argument0 argument1, both would be parsed with method0, when it should be possible to have each parsed with it's own relevant method.

My fix has involved editing the method isMultiValued() in org.kohsuke.args4j.spi.MethodSetter to return false instead of true, stopping the parser from using this same method for all arguments, and instead moving onto the next argument/method index

It would be nice to have this use the multiValued field of Argument (ie @Argument(index = 0, multiValue = false)</code.

JosephWPlant commented 9 years ago
kohsuke commented 9 years ago

I can't fix this without breaking backward compatibility, as this commit changes the default handling of a method with @Argument.

The closest change I can think of is to only treat @Argument as multi-valued when its index is the largest, but in your example it'll not prevent people from specifying 3 options when you only expected two.