コード例 #1
0
ファイル: CliParser.java プロジェクト: jnan77/jcommon
  private static List<Map.Entry<String, String>> getOption(
      CliOption option, ArgumentList argumentList) {
    List<Map.Entry<String, String>> result = new ArrayList<>();
    boolean flag = option.isFlag();

    for (String switchName : option.getSwitchNames()) {
      List<Map.Entry<String, String>> values;

      if (flag) {
        values = argumentList.removeFlag(switchName);
      } else {
        values = argumentList.removeSwitchValues(switchName);
      }

      result.addAll(values);
    }

    if (result.isEmpty()) {
      String defaultValue = option.getDefaultValue();

      if (defaultValue != null) {
        Map.Entry<String, String> defaultEntry =
            new AbstractMap.SimpleImmutableEntry<>(last(option.getSwitchNames()), defaultValue);

        result.add(defaultEntry);
      }
    }

    return result;
  }
コード例 #2
0
ファイル: CliParser.java プロジェクト: jnan77/jcommon
  public void verify(PrintStream out) {
    List<String> errors = new ArrayList<>();

    if (!missing.isEmpty()) {
      StringBuilder missingMessage = new StringBuilder(80);
      List<String> missingSwitches = new ArrayList<>(missing.size());

      for (CliOption option : missing) {
        missingSwitches.add(last(option.getSwitchNames()));
      }

      missingMessage.append("Missing required option");

      if (missing.size() > 1) {
        missingMessage.append('s');
      }

      missingMessage.append(": ").append(join(", ", missingSwitches));
      errors.add(missingMessage.toString());
    }

    if (!unexpected.isEmpty()) {
      String unexpectedMessage = "Unexpected parameters: " + join(" ", unexpected);

      errors.add(unexpectedMessage);
    }

    if (!duplicates.isEmpty()) {
      StringBuilder duplicatesMessage = new StringBuilder(80);
      List<String> duplicateSwitches = new ArrayList<>(duplicates.size());

      for (Map.Entry<String, String> duplicate : duplicates) {
        duplicateSwitches.add(duplicate.getKey() + "=" + duplicate.getValue());
      }

      duplicatesMessage.append("Duplicate options: ").append(join(", ", duplicateSwitches));
      errors.add(duplicatesMessage.toString());
    }

    if (!errors.isEmpty()) {
      out.println(command.getDocumentation());
      out.println();
      out.flush();

      throw new ErrorMessage(join("\n", errors));
    }
  }
コード例 #3
0
ファイル: CliParser.java プロジェクト: jnan77/jcommon
  public CliParser(CliCommand command, List<String> arguments) {
    this.command = command;

    // getOption removes options as it parses them
    ArgumentList argumentList = new ArgumentList(arguments);

    for (CliOption option : command.getOptions()) {
      switches.addAll(option.getSwitchNames());

      List<Map.Entry<String, String>> parsedValues = getOption(option, argumentList);

      if (parsedValues.isEmpty()) {
        if (option.isRequired()) {
          missing.add(option);
        }
      } else if (parsedValues.size() > 1) {
        if (option.isUnique()) {
          duplicates.addAll(parsedValues);
        } else {
          for (String switchName : option.getSwitchNames()) {
            for (Map.Entry<String, String> parsedValue : parsedValues) {
              List<String> switchValues = multiValues.get(switchName);

              if (switchValues == null) {
                switchValues = new ArrayList<>();
                multiValues.put(switchName, switchValues);
              }

              switchValues.add(parsedValue.getValue());
            }
          }
        }
      } else {
        String value = parsedValues.get(0).getValue();

        for (String switchName : option.getSwitchNames()) {
          values.put(switchName, value);
        }
      }
    }

    Iterator<CliParameter> parameters = command.getParameters().iterator();
    Iterator<String> trailingArguments = argumentList.trailing();

    while (parameters.hasNext() && trailingArguments.hasNext()) {
      String name = parameters.next().getName();

      values.put(name, trailingArguments.next());
      trailingArguments.remove();
      switches.add(name);
    }

    while (parameters.hasNext()) {
      CliParameter parameter = parameters.next();

      switches.add(parameter.getName());

      if (parameter.isRequired()) {
        CliOption option = new CliOption.SwitchBuilder().withSwitch(parameter.getName()).build();

        missing.add(option);
      }
    }

    if (command.allowsTrailingParameter()) {
      while (trailingArguments.hasNext()) {
        trailing.add(trailingArguments.next());
        trailingArguments.remove();
      }
    }

    Iterator<String> unexpected = argumentList.remaining();

    while (unexpected.hasNext()) {
      this.unexpected.add(unexpected.next());
    }
  }