⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 argumentparser.java

📁 一个已经完善的极其方便的实现snmp的开发包
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        if (defaults.size() == 0) {          throw new ArgumentParseException( -1, null, of, of.getParameters()[0]);        }        else {          addValues2Option(of.getOption(), defaults, options);        }      }    }    return options;  }  protected void addValues2Option(String option, List values, Map options) {    List existingValues = (List) options.get(option);    if ((existingValues != null) && (values != null)) {      existingValues.addAll(values);    }    else {      options.put(option, values);    }  }  protected List parseValues(String[] args, int offset,                             ArgumentFormat format) throws ParseException {    int numParams = format.getParameters().length;    List values = new ArrayList(numParams);    for (int i=0; (i+offset < args.length) && (i < numParams); i++) {      try {        values.add(parseParameterValue(format.getParameters()[i],                                       args[i + offset], format,                                       i+offset));      }      catch (ArgumentParseException apex) {        throw apex;      }      catch (Exception ex) {        ex.printStackTrace();        int pos = i + offset;        throw new ArgumentParseException(pos,                                         args[pos],                                         format,                                         format.getParameters()[i]);      }    }    return values;  }  protected Object parseParameterValue(ArgumentParameter type,                                       String value,                                       ArgumentFormat format, int pos) throws      org.snmp4j.util.ArgumentParser.ArgumentParseException  {    if (value.startsWith("'") && value.endsWith("'")) {      value = value.substring(1, value.length()-2);    }    if (type.pattern != null) {      Matcher m = type.pattern.matcher(value);      if (!m.matches()) {        throw new ArgumentParseException("Value '"+value+"' for "+                                         (format.isParameter() ?                                          "parameter " : "option ")+                                         format.getOption()+                                         ((format.getParameters().length > 1) ?                                          " part "+type.getName() : "")+                                         " does not match pattern '"+                                         type.pattern.pattern()+"'",                                         pos,                                         value,                                         format,                                         type);      }    }    switch (type.getType()) {      case TYPE_INTEGER:        return new Integer(value);      case TYPE_LONG:        return new Long(value);      default:        return value;    }  }  public static class ArgumentFormat {    private String option;    private boolean mandatory;    private boolean parameter;    private ArgumentParameter[] params;    private boolean vararg;    public boolean isMandatory() {      return mandatory;    }    public boolean isParameter() {      return parameter;    }    public String getOption() {      return option;    }    public ArgumentParameter[] getParameters() {      return params;    }    public boolean isVariableLength() {      return vararg;    }    public String toString() {      return "ArgumentFormat[option="+option+",parameter="+parameter+          ",vararg="+vararg+          ",mandatatory="+mandatory+",parameters="+Arrays.asList(params)+"]";    }  }  public static class ArgumentParameter {    private String name;    private int type;    private Pattern pattern;    private String defaultValue;    public String getName() {      return name;    }    public String getDefaultValue() {      return defaultValue;    }    public int getType() {      return type;    }    public String toString() {      return "ArgumentParameter[name="+name+",type="+type+          ",patttern="+((pattern == null) ? null : pattern.pattern())+          ",defaultValue="+defaultValue+"]";    }  }  public static class ArgumentParseException extends ParseException {    private ArgumentParameter parameterFormatDetail;    private ArgumentFormat parameterFormat;    private String value;    public ArgumentParseException(int position,                                  String value,                                  ArgumentFormat parameterFormat,                                  ArgumentParameter parameterFormatDetail) {      super((value != null)            ? "Invalid value '"+value+"' at position "+position            : "Mandatory parameter "+"-"+parameterFormat.getOption()+            parameterFormatDetail.getName()+" not specified", position);      this.parameterFormat = parameterFormat;      this.parameterFormatDetail = parameterFormatDetail;      this.value = value;    }    public ArgumentParseException(String message,                                  int position,                                  String value,                                  ArgumentFormat parameterFormat,                                  ArgumentParameter parameterFormatDetail) {      super(message, position);      this.parameterFormat = parameterFormat;      this.parameterFormatDetail = parameterFormatDetail;      this.value = value;    }    public ArgumentParameter getParameterFormatDetail() {      return parameterFormatDetail;    }    public ArgumentFormat getParameterFormat() {      return parameterFormat;    }    public String getValue() {      return value;    }  }  /**   * Gets the first option value of a list of values - if available.   * @param optionValues   *    a probably empty list of values - could be <code>null</code>.   * @return   *    the first option value in <code>optionValues</code> if it exists,   *    <code>null</code> otherwise.   * @since 1.9.2   */  public static Object getFirstValue(List optionValues) {    if ((optionValues != null) && (optionValues.size()>0)) {      return optionValues.get(0);    }    return null;  }  /**   * Test application to try out patterns and command line parameters.   * The default option and parameter patterns can be overridden by setting   * the system properties <code>org.snmp4j.OptionFormat</code> and   * <code>org.snmp4j.ParameterFormat</code> respectively.   * <p>   * The given command line is parsed using the specified patterns and the   * parsed values are returned on the console output.   * </p>   * <p>   * The default option pattern is <code>-o1[i{parameter1}] -o2[s,l]</code>   * and the default parameter pattern is   * <code>-param1[i] -param2[s<(udp|tcp):.*[/[0-9]+]?>] +optParam1[l{=-100}] ..   * </code>   * </p>   * @param args   *    the command line arguments to match with the specified format patterns.   */  public static void main(String[] args) {    ArgumentParser argumentparser =        new ArgumentParser(System.getProperty("org.snmp4j.OptionFormat",                                              "-o1[i{parameter1}] -o2[s,l]"),                           System.getProperty("org.snmp4j.ParameterFormat",                                              "-param1[i] -param2[s<(udp|tcp):.*[/[0-9]+]?>{=udp:127.0.0.1/161}] "+                                              "+optParam1[l{=-100}] .."));    System.out.println("Option format is: "+argumentparser.getOptionFormat());    System.out.println("Parameter format is: "+argumentparser.getParameterFormat());    Map options = null;    try {      options = argumentparser.parse(args);      System.out.println(options);    }    catch (ParseException ex) {      System.err.println("Failed to parse args: "+ex.getMessage());      ex.printStackTrace();    }  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -