Package org.snmp4j.util.ArgumentParser

Examples of org.snmp4j.util.ArgumentParser.ArgumentFormat


    return parameterFormat;
  }

  protected static Map parseFormat(String format, boolean parameterFormat) {
    Map options = new LinkedHashMap();
    ArgumentFormat last = null;
    StringTokenizer st = new StringTokenizer(format, " ");
    while (st.hasMoreTokens()) {
      String token = st.nextToken();
      if ("..".equals(token)) {
        if (last != null) {
          last.vararg = true;
          break;
        }
        else {
          throw new IllegalArgumentException("'..' without parameter definition");
        }
      }
      ArgumentFormat af = new ArgumentFormat();
      last = af;
      af.parameter = parameterFormat;
      af.mandatory = (token.charAt(0) != '+');
      token = token.substring(1);
      if (token.endsWith("]")) {
View Full Code Here


   *    parameters.
   */
  public Map parse(String[] args) throws ParseException {
    Map options = new LinkedHashMap();
    Iterator params = parameterFormat.values().iterator();
    ArgumentFormat lastFormat = null;
    for (int i=0; i<args.length; i++) {
      if (args[i].length() == 0) {
        continue;
      }
      ArgumentFormat format;
      if (args[i].charAt(0) == '-') {
        String option = args[i].substring(1);
        format = (ArgumentFormat) optionFormat.get(option);
        if (format == null) {
          throw new ParseException("Unknown option '"+option+"' at position "+i, i);
        }
      }
      else {
        format = params.hasNext() ? (ArgumentFormat) params.next() :
            ((lastFormat != null) && (lastFormat.isVariableLength())) ? lastFormat : null;
        if (format == null) {
          throw new ParseException("Unrecognized parameter at position "+i, i);
        }
      }
      if ((format.getParameters() != null) &&
          (format.getParameters().length > 0)) {
        int diff = (format.isParameter()) ? 1 : 0;
        List values = parseValues(args, i+(1-diff), format);
        i += Math.max(values.size() - diff, 0);
        if (format.isVariableLength() &&
            options.containsKey(format.getOption())) {
          List extValues = (List)options.get(format.getOption());
          extValues.addAll(values);
        }
        else {
          addValues2Option(format.getOption(), values, options);
        }
      }
      else {
        addValues2Option(format.getOption(), null, options);
      }
      lastFormat = format;
    }
    while (params.hasNext()) {
      ArgumentFormat af = (ArgumentFormat) params.next();
      if (af.isMandatory()) {
        throw new ArgumentParseException(-1, null, af, af.getParameters()[0]);
      }
    }
    for (Iterator it = optionFormat.values().iterator(); it.hasNext();) {
      ArgumentFormat of = (ArgumentFormat) it.next();
      if (of.isMandatory() && !options.containsKey(of.getOption())) {
        List defaults = new ArrayList();
        for (int i=0; i<of.getParameters().length; i++) {
          if (of.getParameters()[i].getDefaultValue() != null) {
            defaults.add(parseParameterValue(of.getParameters()[i],
                                             of.getParameters()[i].getDefaultValue(),
                                             of, i));
          }
        }
        if (defaults.size() == 0) {
          throw new ArgumentParseException( -1, null, of, of.getParameters()[0]);
        }
        else {
          addValues2Option(of.getOption(), defaults, options);
        }
      }
    }
    return options;
  }
View Full Code Here

TOP

Related Classes of org.snmp4j.util.ArgumentParser.ArgumentFormat

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.