I've been wondering for a while why the CLI has the ability to specify a type for an option but no feature to type-check a parsed command line. If there is such a feature could someone point it out to me? If not then maybe a method like the one below could be used to at least allow primitive type checking:

public static void validate(Options options) throws ParseException{
Iterator it = options.getOptions().iterator();
while (it.hasNext()) {
Option nextOpt = (Option) it.next();
Object type = nextOpt.getType();
if (type != null) {
Class typeClass = (Class)type;
if (typeClass.isPrimitive()) {
String name = typeClass.getName();
String val = nextOpt.getValue();
if (val != null) {
try {
if (name.equals("int"))
Integer.parseInt(val);
if (name.equals("double"))
Double.parseDouble(val);
if (name.equals("long"))
Long.parseLong(val);
if (name.equals("char") && val.length() > 1)
throw new ParseException("expecting " + name +
" for " + nextOpt.getLongOpt() + " option: " + val);
if (name.equals("short"))
Short.parseShort(val);
if (name.equals("byte"))
Byte.parseByte(val);
if (name.equals("float"))
Float.parseFloat(val);
if (name.equals("boolean") &&
(!val.equals("true") && val.equals("false")))
throw new ParseException("expecting " + name +
" for " + nextOpt.getLongOpt() + " option: " + val);
}
catch(NumberFormatException e) {
throw new ParseException("expecting " + name + " for " +
nextOpt.getLongOpt() + " option: " + val);
}
}
}
}
}
}



--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to