Example #1
0
 /**
  * Handles the following tokens:
  *
  * <p>--L -L --l -l
  *
  * @param token the command line token to handle
  */
 private void handleLongOptionWithoutEqual(String token) throws ParseException {
   List<String> matchingOpts = options.getMatchingOptions(token);
   if (matchingOpts.isEmpty()) {
     handleUnknownToken(currentToken);
   } else if (matchingOpts.size() > 1) {
     throw new AmbiguousOptionException(token, matchingOpts);
   } else {
     handleOption(options.getOption(matchingOpts.get(0)));
   }
 }
Example #2
0
  /**
   * Tells if the token looks like a long option.
   *
   * @param token
   */
  private boolean isLongOption(String token) {
    if (!token.startsWith("-") || token.length() == 1) {
      return false;
    }

    int pos = token.indexOf("=");
    String t = pos == -1 ? token : token.substring(0, pos);

    if (!options.getMatchingOptions(t).isEmpty()) {
      // long or partial long options (--L, -L, --L=V, -L=V, --l, --l=V)
      return true;
    } else if (getLongPrefix(token) != null && !token.startsWith("--")) {
      // -LV
      return true;
    }

    return false;
  }
Example #3
0
  /**
   * Handles the following tokens:
   *
   * <p>--L=V -L=V --l=V -l=V
   *
   * @param token the command line token to handle
   */
  private void handleLongOptionWithEqual(String token) throws ParseException {
    int pos = token.indexOf('=');

    String value = token.substring(pos + 1);

    String opt = token.substring(0, pos);

    List<String> matchingOpts = options.getMatchingOptions(opt);
    if (matchingOpts.isEmpty()) {
      handleUnknownToken(currentToken);
    } else if (matchingOpts.size() > 1) {
      throw new AmbiguousOptionException(opt, matchingOpts);
    } else {
      Option option = options.getOption(matchingOpts.get(0));

      if (option.acceptsArg()) {
        handleOption(option);
        currentOption.addValueForProcessing(value);
        currentOption = null;
      } else {
        handleUnknownToken(currentToken);
      }
    }
  }
Example #4
0
  /**
   * Handles the following tokens:
   *
   * <p>-S -SV -S V -S=V -S1S2 -S1S2 V -SV1=V2
   *
   * <p>-L -LV -L V -L=V -l
   *
   * @param token the command line token to handle
   */
  private void handleShortAndLongOption(String token) throws ParseException {
    String t = Util.stripLeadingHyphens(token);

    int pos = t.indexOf('=');

    if (t.length() == 1) {
      // -S
      if (options.hasShortOption(t)) {
        handleOption(options.getOption(t));
      } else {
        handleUnknownToken(token);
      }
    } else if (pos == -1) {
      // no equal sign found (-xxx)
      if (options.hasShortOption(t)) {
        handleOption(options.getOption(t));
      } else if (!options.getMatchingOptions(t).isEmpty()) {
        // -L or -l
        handleLongOptionWithoutEqual(token);
      } else {
        // look for a long prefix (-Xmx512m)
        String opt = getLongPrefix(t);

        if (opt != null && options.getOption(opt).acceptsArg()) {
          handleOption(options.getOption(opt));
          currentOption.addValueForProcessing(t.substring(opt.length()));
          currentOption = null;
        } else if (isJavaProperty(t)) {
          // -SV1 (-Dflag)
          handleOption(options.getOption(t.substring(0, 1)));
          currentOption.addValueForProcessing(t.substring(1));
          currentOption = null;
        } else {
          // -S1S2S3 or -S1S2V
          handleConcatenatedOptions(token);
        }
      }
    } else {
      // equal sign found (-xxx=yyy)
      String opt = t.substring(0, pos);
      String value = t.substring(pos + 1);

      if (opt.length() == 1) {
        // -S=V
        Option option = options.getOption(opt);
        if (option != null && option.acceptsArg()) {
          handleOption(option);
          currentOption.addValueForProcessing(value);
          currentOption = null;
        } else {
          handleUnknownToken(token);
        }
      } else if (isJavaProperty(opt)) {
        // -SV1=V2 (-Dkey=value)
        handleOption(options.getOption(opt.substring(0, 1)));
        currentOption.addValueForProcessing(opt.substring(1));
        currentOption.addValueForProcessing(value);
        currentOption = null;
      } else {
        // -L=V or -l=V
        handleLongOptionWithEqual(token);
      }
    }
  }