コード例 #1
0
 public String toUsageString() {
   StringBuilder sb = new StringBuilder();
   for (Argument a : parameters.values()) {
     sb.append(a.toUsageString("\t"));
   }
   return sb.toString();
 }
コード例 #2
0
  /*
  verifies that each of our registered parameters has a value, either given or default

  throws an exeption otherwise
   */
  private void verify() throws Exception {
    for (Argument parameter : parameters.values()) {
      if (parameter.getValue() == null && parameter.getDefaultValue() == null) {
        throw new Exception(
            String.format("Argument '%s' is required.  Please see usage.", parameter.name));
      }
    }
  }
コード例 #3
0
 public Object getValue(String argumentName) {
   Object value = null;
   argumentName = argumentName.toLowerCase();
   if (parameters.containsKey(argumentName)) {
     Argument argument = parameters.get(argumentName);
     value = argument.getValue();
     if (value == null) {
       String defaultValue = argument.getDefaultValue();
       try {
         value = argument.getHandler().tryArgument(defaultValue);
       } catch (Exception e) {
       }
     }
   }
   return value;
 }
コード例 #4
0
 /*
 Tries each handler for the given parameter and returns a success boolean
  */
 private boolean parseKV(String key, String value) throws Exception {
   boolean success = false;
   // must have a registered handler; see registerArgumentHandler()
   if (!parameters.containsKey(key)) {
     throw new Exception(key + " is not a valid argument");
   }
   Argument arg = parameters.get(key);
   // don't allow dupes
   if (arg.getValue() != null) {
     throw new Exception("Duplicate arguments: " + key);
   }
   try {
     // handler returns null if it can't handle the argument
     // it can throw an exception if the argument is an unacceptable value
     Object param = arg.getHandler().tryArgument(value);
     if (param != null) {
       arg.setValue(param);
       success = true;
     }
   } catch (Exception e) {
     throw new Exception("Bad argument value for '" + key + "': " + e.getMessage());
   }
   return success;
 }
コード例 #5
0
 /*
 Register a new Argument handler for a desired parameter
  */
 public void registerArgument(Argument argument) {
   parameters.put(argument.getName().toLowerCase(), argument);
 }