예제 #1
0
 private void setLongFormNamedArgValues(String[] args) throws NumberFormatException {
   for (int i = 0; i < args.length; i++) {
     String[] tempNamedArg = new String[2];
     if (args[i].startsWith("--")) {
       tempNamedArg = args[i].split("--");
       for (int j = 0; j < namedArgumentList.size(); j++) {
         incorrectDataTypeIndex = j;
         incorrectArgumentType = "named";
         NamedArgument currentArg = namedArgumentList.get(j);
         if (currentArg.getName().equals(tempNamedArg[1])) {
           if (!currentArg.getType().equals("boolean")) {
             incorrectArgValueForSpecifiedDataType = args[i + 1];
             if (currentArg.getType().equals("integer")) {
               int argValue = Integer.parseInt(args[i + 1]);
               currentArg.setValue(args[i + 1]);
             } else if (currentArg.getType().equals("float")) {
               float argValue = Float.parseFloat(args[i + 1]);
               currentArg.setValue(args[i + 1]);
             } else if (currentArg.getType().equals("string")) {
               currentArg.setValue(args[i + 1]);
             }
           } else {
             currentArg.setValue("true");
           }
         }
       }
     }
   }
 }
예제 #2
0
  private String incorrectDataTypeMessage(ArrayList<String> argList) {
    String errorMessage = this.usageMessage();

    if (incorrectArgumentType.equals("positional")) {
      PositionalArgument currentArg = positionalArgumentList.get(incorrectDataTypeIndex);
      errorMessage +=
          "\n"
              + programName
              + ".java: error: argument "
              + currentArg.getName()
              + ": invalid "
              + currentArg.getType()
              + " value: "
              + argList.get(incorrectDataTypeIndex);
      return errorMessage;
    } else {
      NamedArgument currentArg = namedArgumentList.get(incorrectDataTypeIndex);
      errorMessage +=
          "\n"
              + programName
              + ".java: error: argument "
              + currentArg.getName()
              + ": invalid "
              + currentArg.getType()
              + " value: "
              + incorrectArgValueForSpecifiedDataType;
      return errorMessage;
    }
  }
예제 #3
0
 /**
  * Returns the named argument with the specified character short form name
  *
  * @param argShortFormName the specified character short form name
  * @return the named argument with the specified char short form name
  */
 public NamedArgument getNamedArgument(char argShortFormName) {
   NamedArgument returnArg = null;
   for (int i = 0; i < namedArgumentList.size(); i++) {
     NamedArgument currentArg = namedArgumentList.get(i);
     if (currentArg.getShortFormName() == argShortFormName) {
       returnArg = currentArg;
     }
   }
   return returnArg;
 }
예제 #4
0
 /**
  * Returns the named argument with the specified string name
  *
  * @param argName the specified string name
  * @return the named argument with the specified string name
  */
 public NamedArgument getNamedArgument(String argName) {
   NamedArgument returnArg = null;
   for (int i = 0; i < namedArgumentList.size(); i++) {
     NamedArgument currentArg = namedArgumentList.get(i);
     if (currentArg.getName().equals(argName)) {
       returnArg = currentArg;
     }
   }
   return returnArg;
 }
예제 #5
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
    }
  }
예제 #6
0
 private void parseNamedArgumentValues(List<NamedArgument> namedArgList)
     throws IncorrectArgumentValueException {
   for (int i = 0; i < namedArgList.size(); i++) {
     NamedArgument currentArg = namedArgList.get(i);
     String errorArg = currentArg.getName();
     String[] valueSet = currentArg.getValueSet();
     if (!valueSet[0].equals("")) {
       Boolean valueSetContainsArgValue = false;
       String errorValue = "";
       for (int j = 0; j < valueSet.length; j++) {
         if (currentArg.getType().equals("integer")) {
           int intValue = (Integer) currentArg.getValue();
           if (valueSet[j].equals(Integer.toString(intValue))) {
             valueSetContainsArgValue = true;
           }
           errorValue = Integer.toString(intValue);
         } else if (currentArg.getType().equals("float")) {
           float floatValue = (Float) currentArg.getValue();
           if (valueSet[j].equals(Float.toString(floatValue))) {
             valueSetContainsArgValue = true;
           }
           errorValue = Float.toString(floatValue);
         } else if (currentArg.getType().equals("string")) {
           String stringValue = (String) currentArg.getValue();
           if (valueSet[j].equals(stringValue)) {
             valueSetContainsArgValue = true;
           }
           errorValue = stringValue;
         }
         /*else{
         	Boolean boolValue = (Boolean)currentArg.getValue();
         	if(valueSet[j].equals(boolValue)){
         		valueSetContainsArgValue = true;
         	}
         	errorValue = Boolean.toString(boolValue);
         }*/
       }
       if (!valueSetContainsArgValue) {
         throw new IncorrectArgumentValueException(
             incorrectArgumentValueMessage(errorArg, errorValue));
       }
     }
   }
 }
예제 #7
0
  private String helpMessage() {
    String helpMessage = this.usageMessage();

    /*for(int i = 0; i < namedArgumentList.size(); i++){
    	NamedArgument namedArg = namedArgumentList.get(i);
    	helpMessage += " [" + namedArg.getName() + "]";
    }*/
    helpMessage += "\n" + programDescription + "\npositional arguments:";

    for (int i = 0; i < positionalArgumentList.size(); i++) {
      PositionalArgument currentArg = positionalArgumentList.get(i);
      helpMessage +=
          "\n["
              + currentArg.getName()
              + "] ("
              + currentArg.getType()
              + ") "
              + currentArg.getDescription();
    }

    helpMessage += "\nnamed arguments:";

    for (int i = 0; i < namedArgumentList.size(); i++) {
      NamedArgument currentArg = namedArgumentList.get(i);
      helpMessage +=
          "\n[--"
              + currentArg.getName()
              + "] [-"
              + currentArg.getShortFormName()
              + "] ("
              + currentArg.getType()
              + ") "
              + currentArg.getDescription()
              + " (optional)";
    }

    return helpMessage;
  }
예제 #8
0
 private ArrayList<String> getPositionalArgs(String[] args) {
   ArrayList<String> posArgList = new ArrayList<String>();
   for (int i = 0; i < args.length; i++) { // going through args from CLI
     if (!args[i].startsWith("-") && i == 0) { // test if the first arg is a positional arg
       posArgList.add(args[i]);
     } else if (!args[i].startsWith("-")) { // arg from CLI doesn't have a dash
       if (!args[i - 1].startsWith(
           "-")) { // if the one before it doesn't have a dash, then it's a pos arg
         posArgList.add(args[i]);
       } else {
         String[] tempNamedArg = new String[2];
         if (args[i - 1].startsWith("--")) {
           tempNamedArg = args[i - 1].split("--");
           for (int j = 0; j < namedArgumentList.size(); j++) { // g
             NamedArgument currentNamedArg = namedArgumentList.get(j);
             if (currentNamedArg.getName().equals(tempNamedArg[1])) {
               if (currentNamedArg.getType().equals("boolean")) { // to pos args
                 posArgList.add(args[i]);
               }
             }
           }
         } else {
           tempNamedArg = args[i - 1].split("-");
           if (tempNamedArg[1].length() == 1) {
             for (int j = 0; j < namedArgumentList.size(); j++) {
               NamedArgument currentNamedArg = namedArgumentList.get(j);
               if (Character.toString(currentNamedArg.getShortFormName())
                   .equals(tempNamedArg[1])) {
                 if (currentNamedArg.getType().equals("boolean")) {
                   posArgList.add(args[i]);
                 }
               }
             }
           } else {
             posArgList.add(args[i]);
           }
         }
       }
     }
   }
   return posArgList;
 }
예제 #9
0
  /**
   * Writes argument parser information to the specified XML file. The program name, program
   * description, positional arguments, and named arguments are saved in the file in standard XML
   * format. It also saves each aspect of the arguments such as argument name, description,
   * dataType, defaultValue, and possible value set.
   *
   * @param fileName the XML file to which all information will be written
   */
  public void writeToXMLFile(String fileName) {
    File outputFile = new File(fileName);
    try {
      PrintWriter outputFileWriter = new PrintWriter(outputFile);
      outputFileWriter.println("<?xml version=" + "\"1.0\"?>");
      outputFileWriter.println("<program>");
      outputFileWriter.println("<name>" + getProgramName() + "</name>");
      outputFileWriter.println("<description>" + getProgramDescription() + "</description>");
      outputFileWriter.println("<arguments>");

      for (int i = 0; i < positionalArgumentList.size(); i++) {
        PositionalArgument posArg = positionalArgumentList.get(i);
        outputFileWriter.println("<positional>");
        outputFileWriter.println("<name>" + posArg.getName() + "</name>");
        outputFileWriter.println("<type>" + posArg.getType() + "</type>");
        outputFileWriter.println("<description>" + posArg.getDescription() + "</description>");
        outputFileWriter.println(
            "<position>"
                + (posArg.getPosition() + 1)
                + "</position>"); // prints position starting at 1
        String[] valueSet = posArg.getValueSet();
        if (!valueSet[0].equals("")) {
          String line = "<valueset>";

          line += valueSet[0];
          for (int j = 1; j < valueSet.length; j++) {
            line += "," + valueSet[j];
          }
          line += "</valueset>";
          outputFileWriter.println(line);
        }
        // outputFileWriter.println(line);
        outputFileWriter.println("</positional>");
      }

      for (int j = 0; j < namedArgumentList.size(); j++) {
        NamedArgument namedArg = namedArgumentList.get(j);
        outputFileWriter.println("<named>");
        outputFileWriter.println("<name>" + namedArg.getName() + "</name>");
        outputFileWriter.println("<shortname>" + namedArg.getShortFormName() + "</shortname>");
        outputFileWriter.println("<type>" + namedArg.getType() + "</type>");
        outputFileWriter.println("<description>" + namedArg.getDescription() + "</description>");
        outputFileWriter.println("<default>" + namedArg.getDefaultValue() + "</default>");
        String[] valueSet = namedArg.getValueSet();
        if (!valueSet[0].equals("")) {
          String line = "<valueset>";
          line += valueSet[0];
          for (int k = 1; k < valueSet.length; k++) {
            line += "," + valueSet[k];
          }
          line += "</valueset>";
          outputFileWriter.println(line);
        }
        outputFileWriter.println("</named>");
      }

      outputFileWriter.println("</arguments>");
      outputFileWriter.println("</program>");

      outputFileWriter.close();
    } catch (Exception e) {
      System.out.println(e.getMessage());
    }
  }
예제 #10
0
  private void setShortFormNamedArgValues(String[] args) throws NumberFormatException {
    for (int i = 0; i < args.length; i++) {
      String[] tempNamedArg = new String[2];
      if (args[i].startsWith("-")) {
        tempNamedArg = args[i].split("-");
        for (int k = 0; k < namedArgumentList.size(); k++) {
          incorrectDataTypeIndex = k;
          incorrectArgumentType = "named";
          NamedArgument currentNamedArg = namedArgumentList.get(k);
          if (tempNamedArg[1].length() == 1) { // single char
            if (Character.toString(currentNamedArg.getShortFormName()).equals(tempNamedArg[1])) {
              if (!currentNamedArg.getType().equals("boolean")) {
                incorrectArgValueForSpecifiedDataType = args[i + 1];
                if (currentNamedArg.getType().equals("integer")) {
                  int argValue = Integer.parseInt(args[i + 1]);
                  currentNamedArg.setValue(args[i + 1]);
                } else if (currentNamedArg.getType().equals("float")) {
                  float argValue = Float.parseFloat(args[i + 1]);
                  currentNamedArg.setValue(args[i + 1]);
                } else if (currentNamedArg.getType().equals("string")) {
                  currentNamedArg.setValue(args[i + 1]);
                }

              } else {
                currentNamedArg.setValue("true");
              }
            }
          } else { // multiple flags in one specification
            for (int j = 0; j < tempNamedArg[1].length(); j++) {
              if (currentNamedArg.getShortFormName() == tempNamedArg[1].charAt(j)) {
                currentNamedArg.setValue("true");
              }
            }
          }
        }
      }
    }
  }