Ejemplo n.º 1
0
  private void parseDataType(ArrayList<String> argList) throws NumberFormatException {

    // checking positional args
    for (int index = 0; index < argList.size(); index++) {
      incorrectDataTypeIndex = index;
      incorrectArgumentType = "positional";
      PositionalArgument currentArg = positionalArgumentList.get(index);
      if (currentArg.getType().equals("integer")) {
        int argValue = Integer.parseInt(argList.get(index));
        currentArg.setValue(argList.get(index));
        // currentArg.setValue(argValue);
      } else if (currentArg.getType().equals("float")) {
        float argValue = Float.parseFloat(argList.get(index));
        currentArg.setValue(argList.get(index));
      } else if (currentArg.getType().equals("string")) {
        String argValue = argList.get(index);
        currentArg.setValue(argValue);
      } else {
        Boolean argValue = parseBool(argList.get(index));
        currentArg.setValue(argList.get(index));
      }
    }
  }
Ejemplo n.º 2
0
  /**
   * Parses input data from the command line and throws the correct exceptions if tinput data is an
   * incorrect format
   *
   * @param args the input data from the command line
   * @throws HelpException if the help argument is given in the input data
   * @throws IncorrectNumberOfArgsException if an incorrect number of positional arguments is given
   *     in the input data
   * @throws IncorrectArgTypeException if an input argument is given as the wrong dataType specified
   *     by the user
   * @throws ArgumentDoesNotExistException if a named argument is given in the input data that has
   *     not been created by the user
   * @throws IncorrectArgumentValueException if the input data is not a possible value for a
   *     specified argument
   */
  public void parse(String[] args)
      throws HelpException, IncorrectNumberOfArgsException, IncorrectArgTypeException,
          ArgumentDoesNotExistException, IncorrectArgumentValueException {
    ArrayList<String> tempPositionalArgList = getPositionalArgs(args);

    try {
      setLongFormNamedArgValues(args);
      setShortFormNamedArgValues(args);
    } catch (Exception e) {
      throw new IncorrectArgTypeException(incorrectDataTypeMessage(tempPositionalArgList));
    }

    invalidNamedArgument(args); // throws ArgumentDoesNotExistException

    NamedArgument helpArgument = getNamedArgument("help");
    if (helpArgument != null) {
      Boolean helpArgValue = (Boolean) helpArgument.getValue();
      if (helpArgValue) {
        throw new HelpException(helpMessage());
      }
    }

    if (positionalArgumentList.size() != tempPositionalArgList.size()) {
      throw new IncorrectNumberOfArgsException(incorrectNumberOfArgsMessage(tempPositionalArgList));
    } else if (positionalArgumentList.size() == tempPositionalArgList.size()) {
      for (int i = 0; i < tempPositionalArgList.size(); i++) {
        PositionalArgument currentArg = positionalArgumentList.get(i);
        currentArg.setValue(tempPositionalArgList.get(i));
      }
      try {
        parseDataType(tempPositionalArgList);
      } catch (Exception e) {
        throw new IncorrectArgTypeException(incorrectDataTypeMessage(tempPositionalArgList));
      }
      parseNamedArgumentValues(namedArgumentList); // throws IncorrectArgumentValueException
      parsePositionalArgumentValues(
          positionalArgumentList); // throws IncorrectArgumentValueException
    }
  }