Better validations - without subclassing
java command line option processing library
Brought to you by:
flamingpenguin
What about better validations? For example:
@Option(shortName="c", longName = "commands", validators={FileExistsValidator.class, FileIsADirectoryValidator.class})
public File getDirectory();
must be written in this way:
@Option(shortName="c", longName = "commands")
public ExistingDirectory getDirectory();
public static class ExistingDirectory extends File{
private static final long serialVersionUID = 4490759682935809445L;
public ExistingDirectory(String name) throws IOException {
super(name);
if(!exists()){
throw new IOException("File "+name+" does not exist!");
}
if(!isDirectory()){
throw new IOException("File "+name+" is not a directory!");
}
}
}
OK, what I am thinking for this is to allow an optional factory class to be specified
factory={ExistingDirectoryFactory.class}
I think this will satisfy a number of requirements in a straight-forward way. Including the problem of constructing a type that you cannot modify or extend.