commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Omair-Inam Abdul-Matin <oiina...@cs.uwaterloo.ca>
Subject commons-cli feature request/suggestion: type-checking of parsed cmd line
Date Wed, 22 Sep 2004 19:51:30 GMT
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: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message