public void parseArguments(final String[] rawArguments,Properties argumentProperties) throws ArgumentException {
  boolean inTrailingArgs=false;
  final int numArguments=rawArguments.length;
  for (int i=0; i < numArguments; i++) {
    final String arg=rawArguments[i];
    if (inTrailingArgs) {
      trailingArguments.add(arg);
      if (maxTrailingArguments > 0 && trailingArguments.size() > maxTrailingArguments) {
        final LocalizableMessage message=ERR_ARGPARSER_TOO_MANY_TRAILING_ARGS.get(maxTrailingArguments);
        throw new ArgumentException(message);
      }
      continue;
    }
    if (arg.equals("--")) {
      inTrailingArgs=true;
    }
 else     if (arg.startsWith("--")) {
      String argName=arg.substring(2);
      String argValue=null;
      final int equalPos=argName.indexOf('=');
      if (equalPos == 0) {
        throw new ArgumentException(ERR_ARGPARSER_LONG_ARG_WITHOUT_NAME.get(arg));
      }
 else       if (equalPos > 0) {
        argValue=argName.substring(equalPos + 1);
        argName=argName.substring(0,equalPos);
      }
      final String origArgName=argName;
      argName=formatLongIdentifier(argName);
      final Argument a=longIDMap.get(argName);
      if (a == null) {
        if (OPTION_LONG_HELP.equals(argName)) {
          writeToUsageOutputStream(getUsage());
          return;
        }
 else         if (OPTION_LONG_PRODUCT_VERSION.equals(argName)) {
          printVersion();
          return;
        }
 else {
          throw new ArgumentException(ERR_ARGPARSER_NO_ARGUMENT_WITH_LONG_ID.get(origArgName));
        }
      }
 else {
        a.setPresent(true);
        if (isUsageArgument(a)) {
          writeToUsageOutputStream(getUsage());
          return;
        }
      }
      if (a.needsValue()) {
        if (argValue == null) {
          if ((i + 1) == numArguments) {
            throw new ArgumentException(ERR_ARGPARSER_NO_VALUE_FOR_ARGUMENT_WITH_LONG_ID.get(origArgName));
          }
          argValue=rawArguments[++i];
        }
        final LocalizableMessageBuilder invalidReason=new LocalizableMessageBuilder();
        if (!a.valueIsAcceptable(argValue,invalidReason)) {
          throw new ArgumentException(ERR_ARGPARSER_VALUE_UNACCEPTABLE_FOR_LONG_ID.get(argValue,origArgName,invalidReason));
        }
        if (a.hasValue() && !a.isMultiValued()) {
          throw new ArgumentException(ERR_ARGPARSER_NOT_MULTIVALUED_FOR_LONG_ID.get(origArgName));
        }
        a.addValue(argValue);
      }
 else       if (argValue != null) {
        throw new ArgumentException(ERR_ARGPARSER_ARG_FOR_LONG_ID_DOESNT_TAKE_VALUE.get(origArgName));
      }
    }
 else     if (arg.startsWith("-")) {
      if (arg.equals("-")) {
        throw new ArgumentException(ERR_ARGPARSER_INVALID_DASH_AS_ARGUMENT.get());
      }
      final char argCharacter=arg.charAt(1);
      String argValue;
      if (arg.length() > 2) {
        argValue=arg.substring(2);
      }
 else {
        argValue=null;
      }
      final Argument a=shortIDMap.get(argCharacter);
      if (a == null) {
        if (argCharacter == '?') {
          writeToUsageOutputStream(getUsage());
          return;
        }
 else         if (versionHandler != null && argCharacter == OPTION_SHORT_PRODUCT_VERSION && !shortIDMap.containsKey(OPTION_SHORT_PRODUCT_VERSION)) {
          printVersion();
          return;
        }
 else {
          throw new ArgumentException(ERR_ARGPARSER_NO_ARGUMENT_WITH_SHORT_ID.get(argCharacter));
        }
      }
 else {
        a.setPresent(true);
        if (isUsageArgument(a)) {
          writeToUsageOutputStream(getUsage());
          return;
        }
      }
      if (a.needsValue()) {
        if (argValue == null) {
          if ((i + 1) == numArguments) {
            throw new ArgumentException(ERR_ARGPARSER_NO_VALUE_FOR_ARGUMENT_WITH_SHORT_ID.get(argCharacter));
          }
          argValue=rawArguments[++i];
        }
        final LocalizableMessageBuilder invalidReason=new LocalizableMessageBuilder();
        if (!a.valueIsAcceptable(argValue,invalidReason)) {
          throw new ArgumentException(ERR_ARGPARSER_VALUE_UNACCEPTABLE_FOR_SHORT_ID.get(argValue,argCharacter,invalidReason));
        }
        if (a.hasValue() && !a.isMultiValued()) {
          throw new ArgumentException(ERR_ARGPARSER_NOT_MULTIVALUED_FOR_SHORT_ID.get(argCharacter));
        }
        a.addValue(argValue);
      }
 else       if (argValue != null) {
        final int valueLength=argValue.length();
        for (int j=0; j < valueLength; j++) {
          final char c=argValue.charAt(j);
          final Argument b=shortIDMap.get(c);
          if (b == null) {
            throw new ArgumentException(ERR_ARGPARSER_NO_ARGUMENT_WITH_SHORT_ID.get(argCharacter));
          }
 else           if (b.needsValue()) {
            throw new ArgumentException(ERR_ARGPARSER_CANT_MIX_ARGS_WITH_VALUES.get(argCharacter,argValue,c));
          }
 else {
            b.setPresent(true);
            if (isUsageArgument(b)) {
              writeToUsageOutputStream(getUsage());
              return;
            }
          }
        }
      }
    }
 else     if (allowsTrailingArguments) {
      inTrailingArgs=true;
      trailingArguments.add(arg);
    }
 else {
      throw new ArgumentException(ERR_ARGPARSER_DISALLOWED_TRAILING_ARGUMENT.get(arg));
    }
  }
  if (allowsTrailingArguments && minTrailingArguments > 0 && trailingArguments.size() < minTrailingArguments) {
    throw new ArgumentException(ERR_ARGPARSER_TOO_FEW_TRAILING_ARGUMENTS.get(minTrailingArguments));
  }
  if (argumentProperties == null) {
    argumentProperties=checkExternalProperties();
  }
  normalizeArguments(argumentProperties,argumentList);
}

public void parseArguments ( final String [ ] rawArguments , Properties argumentProperties ) throws ArgumentException { boolean inTrailingArgs = false ; final int numArguments = rawArguments . length ; for ( int i = 0 ; i < numArguments ; i ++ ) { final String arg = rawArguments [ i ] ; if ( inTrailingArgs ) { trailingArguments . add ( arg ) ; if ( maxTrailingArguments > 0 && trailingArguments . size ( ) > maxTrailingArguments ) { final LocalizableMessage message = ERR_ARGPARSER_TOO_MANY_TRAILING_ARGS . get ( maxTrailingArguments ) ; throw new ArgumentException ( message ) ; } continue ; } if ( arg . equals ( "--" ) ) { inTrailingArgs = true ; } else if ( arg . startsWith ( "--" ) ) { String argName = arg . substring ( 2 ) ; String argValue = null ; final int equalPos = argName . indexOf ( '=' ) ; if ( equalPos == 0 ) { throw new ArgumentException ( ERR_ARGPARSER_LONG_ARG_WITHOUT_NAME . get ( arg ) ) ; } else if ( equalPos > 0 ) { argValue = argName . substring ( equalPos + 1 ) ; argName = argName . substring ( 0 , equalPos ) ; } final String origArgName = argName ; argName = formatLongIdentifier ( argName ) ; final Argument a = longIDMap . get ( argName ) ; if ( a == null ) { if ( OPTION_LONG_HELP . equals ( argName ) ) { writeToUsageOutputStream ( getUsage ( ) ) ; return ; } else if ( OPTION_LONG_PRODUCT_VERSION . equals ( argName ) ) { printVersion ( ) ; return ; } else { throw new ArgumentException ( ERR_ARGPARSER_NO_ARGUMENT_WITH_LONG_ID . get ( origArgName ) ) ; } } else { a . setPresent ( true ) ; if ( isUsageArgument ( a ) ) { writeToUsageOutputStream ( getUsage ( ) ) ; return ; } } if ( a . needsValue ( ) ) { if ( argValue == null ) { if ( ( i + 1 ) == numArguments ) { throw new ArgumentException ( ERR_ARGPARSER_NO_VALUE_FOR_ARGUMENT_WITH_LONG_ID . get ( origArgName ) ) ; } argValue = rawArguments [ ++ i ] ; } final LocalizableMessageBuilder invalidReason = new LocalizableMessageBuilder ( ) ; if ( ! a . valueIsAcceptable ( argValue , invalidReason ) ) { throw new ArgumentException ( ERR_ARGPARSER_VALUE_UNACCEPTABLE_FOR_LONG_ID . get ( argValue , origArgName , invalidReason ) ) ; } if ( a . hasValue ( ) && ! a . isMultiValued ( ) ) { throw new ArgumentException ( ERR_ARGPARSER_NOT_MULTIVALUED_FOR_LONG_ID . get ( origArgName ) ) ; } a . addValue ( argValue ) ; } else if ( argValue != null ) { throw new ArgumentException ( ERR_ARGPARSER_ARG_FOR_LONG_ID_DOESNT_TAKE_VALUE . get ( origArgName ) ) ; } } else if ( arg . startsWith ( "-" ) ) { if ( arg . equals ( "-" ) ) { throw new ArgumentException ( ERR_ARGPARSER_INVALID_DASH_AS_ARGUMENT . get ( ) ) ; } final char argCharacter = arg . charAt ( 1 ) ; String argValue ; if ( arg . length ( ) > 2 ) { argValue = arg . substring ( 2 ) ; } else { argValue = null ; } final Argument a = shortIDMap . get ( argCharacter ) ; if ( a == null ) { if ( argCharacter == '?' ) { writeToUsageOutputStream ( getUsage ( ) ) ; return ; } else if ( versionHandler != null && argCharacter == OPTION_SHORT_PRODUCT_VERSION && ! shortIDMap . containsKey ( OPTION_SHORT_PRODUCT_VERSION ) ) { printVersion ( ) ; return ; } else { throw new ArgumentException ( ERR_ARGPARSER_NO_ARGUMENT_WITH_SHORT_ID . get ( argCharacter ) ) ; } } else { a . setPresent ( true ) ; if ( isUsageArgument ( a ) ) { writeToUsageOutputStream ( getUsage ( ) ) ; return ; } } if ( a . needsValue ( ) ) { if ( argValue == null ) { if ( ( i + 1 ) == numArguments ) { throw new ArgumentException ( ERR_ARGPARSER_NO_VALUE_FOR_ARGUMENT_WITH_SHORT_ID . get ( argCharacter ) ) ; } argValue = rawArguments [ ++ i ] ; } final LocalizableMessageBuilder invalidReason = new LocalizableMessageBuilder ( ) ; if ( ! a . valueIsAcceptable ( argValue , invalidReason ) ) { throw new ArgumentException ( ERR_ARGPARSER_VALUE_UNACCEPTABLE_FOR_SHORT_ID . get ( argValue , argCharacter , invalidReason ) ) ; } if ( a . hasValue ( ) && ! a . isMultiValued ( ) ) { throw new ArgumentException ( ERR_ARGPARSER_NOT_MULTIVALUED_FOR_SHORT_ID . get ( argCharacter ) ) ; } a . addValue ( argValue ) ; } else if ( argValue != null ) { final int valueLength = argValue . length ( ) ; for ( int j = 0 ; j < valueLength ; j ++ ) { final char c = argValue . charAt ( j ) ; final Argument b = shortIDMap . get ( c ) ; if ( b == null ) { throw new ArgumentException ( ERR_ARGPARSER_NO_ARGUMENT_WITH_SHORT_ID . get ( argCharacter ) ) ; } else if ( b . needsValue ( ) ) { throw new ArgumentException ( ERR_ARGPARSER_CANT_MIX_ARGS_WITH_VALUES . get ( argCharacter , argValue , c ) ) ; } else { b . setPresent ( true ) ; if ( isUsageArgument ( b ) ) { writeToUsageOutputStream ( getUsage ( ) ) ; return ; } } } } } else if ( allowsTrailingArguments ) { inTrailingArgs = true ; trailingArguments . add ( arg ) ; } else { throw new ArgumentException ( ERR_ARGPARSER_DISALLOWED_TRAILING_ARGUMENT . get ( arg ) ) ; } } if ( allowsTrailingArguments && minTrailingArguments > 0 && trailingArguments . size ( ) < minTrailingArguments ) { throw new ArgumentException ( ERR_ARGPARSER_TOO_FEW_TRAILING_ARGUMENTS . get ( minTrailingArguments ) ) ; } if ( argumentProperties == null ) { argumentProperties = checkExternalProperties ( ) ; } normalizeArguments ( argumentProperties , argumentList ) ; }
parses the provided set of arguments and updates the information associated with this parser accordingly . default values for unspecified arguments may be read from the specified properties if any are provided .
