{
trailingArguments.add(arg);
if ((maxTrailingArguments > 0) &&
(trailingArguments.size() > maxTrailingArguments))
{
Message message =
ERR_ARGPARSER_TOO_MANY_TRAILING_ARGS.get(maxTrailingArguments);
throw new ArgumentException(message);
}
continue;
}
if (arg.equals("--"))
{
// This is a special indicator that we have reached the end of the named
// arguments and that everything that follows after this should be
// considered trailing arguments.
inTrailingArgs = true;
}
else if (arg.startsWith("--"))
{
// This indicates that we are using the long name to reference the
// argument. It may be in any of the following forms:
// --name
// --name value
// --name=value
String argName = arg.substring(2);
String argValue = null;
int equalPos = argName.indexOf('=');
if (equalPos < 0)
{
// This is fine. The value is not part of the argument name token.
}
else if (equalPos == 0)
{
// The argument starts with "--=", which is not acceptable.
Message message = ERR_ARGPARSER_LONG_ARG_WITHOUT_NAME.get(arg);
throw new ArgumentException(message);
}
else
{
// The argument is in the form --name=value, so parse them both out.
argValue = argName.substring(equalPos+1);
argName = argName.substring(0, equalPos);
}
// If we're not case-sensitive, then convert the name to lowercase.
String origArgName = argName;
if (! longArgumentsCaseSensitive)
{
argName = toLowerCase(argName);
}
// Get the argument with the specified name.
Argument a = longIDMap.get(argName);
if (a == null)
{
if (argName.equals(OPTION_LONG_HELP))
{
// "--help" will always be interpreted as requesting usage
// information.
try
{
getUsage(usageOutputStream);
} catch (Exception e) {}
return;
}
else
if (argName.equals(OPTION_LONG_PRODUCT_VERSION))
{
// "--version" will always be interpreted as requesting version
// information.
usageOrVersionDisplayed = true;
versionPresent = true;
try
{
DirectoryServer.printVersion(usageOutputStream);
} catch (Exception e) {}
return;
}
else
{
// There is no such argument registered.
Message message =
ERR_ARGPARSER_NO_ARGUMENT_WITH_LONG_ID.get(origArgName);
throw new ArgumentException(message);
}
}
else
{
a.setPresent(true);
// If this is the usage argument, then immediately stop and print
// usage information.
if ((usageArgument != null) &&
usageArgument.getName().equals(a.getName()))
{
try
{
getUsage(usageOutputStream);
} catch (Exception e) {}
return;
}
}
// See if the argument takes a value. If so, then make sure one was
// provided. If not, then make sure none was provided.
if (a.needsValue())
{
if (argValue == null)
{
if ((i+1) == numArguments)
{
Message message =
ERR_ARGPARSER_NO_VALUE_FOR_ARGUMENT_WITH_LONG_ID.get(
origArgName);
throw new ArgumentException(message);
}
argValue = rawArguments[++i];
}
MessageBuilder invalidReason = new MessageBuilder();
if (! a.valueIsAcceptable(argValue, invalidReason))
{
Message message = ERR_ARGPARSER_VALUE_UNACCEPTABLE_FOR_LONG_ID.get(
argValue, origArgName, invalidReason.toString());
throw new ArgumentException(message);
}
// If the argument already has a value, then make sure it is
// acceptable to have more than one.
if (a.hasValue() && (! a.isMultiValued()))
{
Message message =
ERR_ARGPARSER_NOT_MULTIVALUED_FOR_LONG_ID.get(origArgName);
throw new ArgumentException(message);
}
a.addValue(argValue);
}
else
{
if (argValue != null)
{
Message message =
ERR_ARGPARSER_ARG_FOR_LONG_ID_DOESNT_TAKE_VALUE.get(
origArgName);
throw new ArgumentException(message);
}
}
}
else if (arg.startsWith("-"))
{
// This indicates that we are using the 1-character name to reference
// the argument. It may be in any of the following forms:
// -n
// -nvalue
// -n value
if (arg.equals("-"))
{
Message message = ERR_ARGPARSER_INVALID_DASH_AS_ARGUMENT.get();
throw new ArgumentException(message);
}
char argCharacter = arg.charAt(1);
String argValue;
if (arg.length() > 2)
{
argValue = arg.substring(2);
}
else
{
argValue = null;
}
// Get the argument with the specified short ID.
Argument a = shortIDMap.get(argCharacter);
if (a == null)
{
if (argCharacter == '?')
{
// "-?" will always be interpreted as requesting usage information.
try
{
getUsage(usageOutputStream);
} catch (Exception e) {}
return;
}
else
if ( (argCharacter == OPTION_SHORT_PRODUCT_VERSION)
&&
( ! shortIDMap.containsKey(OPTION_SHORT_PRODUCT_VERSION)))
{
// "-V" will always be interpreted as requesting
// version information except if it's already defined (e.g in
// ldap tools).
usageOrVersionDisplayed = true ;
versionPresent = true;
try
{
DirectoryServer.printVersion(usageOutputStream);
} catch (Exception e) {}
return;
}
else
{
// There is no such argument registered.
Message message = ERR_ARGPARSER_NO_ARGUMENT_WITH_SHORT_ID.get(
String.valueOf(argCharacter));
throw new ArgumentException(message);
}
}
else
{
a.setPresent(true);
// If this is the usage argument, then immediately stop and print
// usage information.
if ((usageArgument != null) &&
usageArgument.getName().equals(a.getName()))
{
try
{
getUsage(usageOutputStream);
} catch (Exception e) {}
return;
}
}
// See if the argument takes a value. If so, then make sure one was
// provided. If not, then make sure none was provided.
if (a.needsValue())
{
if (argValue == null)
{
if ((i+1) == numArguments)
{
Message message =
ERR_ARGPARSER_NO_VALUE_FOR_ARGUMENT_WITH_SHORT_ID.
get(String.valueOf(argCharacter));
throw new ArgumentException(message);
}
argValue = rawArguments[++i];
}
MessageBuilder invalidReason = new MessageBuilder();
if (! a.valueIsAcceptable(argValue, invalidReason))
{
Message message = ERR_ARGPARSER_VALUE_UNACCEPTABLE_FOR_SHORT_ID.
get(argValue, String.valueOf(argCharacter),
invalidReason.toString());
throw new ArgumentException(message);
}
// If the argument already has a value, then make sure it is
// acceptable to have more than one.
if (a.hasValue() && (! a.isMultiValued()))
{
Message message = ERR_ARGPARSER_NOT_MULTIVALUED_FOR_SHORT_ID.get(
String.valueOf(argCharacter));
throw new ArgumentException(message);
}
a.addValue(argValue);
}
else
{
if (argValue != null)
{
// If we've gotten here, then it means that we're in a scenario like
// "-abc" where "a" is a valid argument that doesn't take a value.
// However, this could still be valid if all remaining characters in
// the value are also valid argument characters that don't take
// values.
int valueLength = argValue.length();
for (int j=0; j < valueLength; j++)
{
char c = argValue.charAt(j);
Argument b = shortIDMap.get(c);
if (b == null)
{
// There is no such argument registered.
Message message = ERR_ARGPARSER_NO_ARGUMENT_WITH_SHORT_ID.get(
String.valueOf(argCharacter));
throw new ArgumentException(message);
}
else if (b.needsValue())
{
// This means we're in a scenario like "-abc" where b is a
// valid argument that takes a value. We don't support that.
Message message = ERR_ARGPARSER_CANT_MIX_ARGS_WITH_VALUES.get(
String.valueOf(argCharacter), argValue, String.valueOf(c));
throw new ArgumentException(message);
}
else
{
b.setPresent(true);
// If this is the usage argument, then immediately stop and
// print usage information.
if ((usageArgument != null) &&
usageArgument.getName().equals(b.getName()))
{
try
{
getUsage(usageOutputStream);
} catch (Exception e) {}
return;
}
}
}
}
}
}
else if (allowsTrailingArguments)
{
// It doesn't start with a dash, so it must be a trailing argument if
// that is acceptable.
inTrailingArgs = true;
trailingArguments.add(arg);
}
else
{
// It doesn't start with a dash and we don't allow trailing arguments,
// so this is illegal.
Message message = ERR_ARGPARSER_DISALLOWED_TRAILING_ARGUMENT.get(arg);
throw new ArgumentException(message);
}
}
// If we allow trailing arguments and there is a minimum number, then make
// sure at least that many were provided.
if (allowsTrailingArguments && (minTrailingArguments > 0))
{
if (trailingArguments.size() < minTrailingArguments)
{
Message message =
ERR_ARGPARSER_TOO_FEW_TRAILING_ARGUMENTS.get(minTrailingArguments);
throw new ArgumentException(message);
}
}
// If we don't have the argumentProperties, try to load a properties file.
if (argumentProperties == null)
{
argumentProperties = checkExternalProperties();
}
// Iterate through all of the arguments. For any that were not provided on
// the command line, see if there is an alternate default that can be used.
// For cases where there is not, see that argument is required.
for (Argument a : argumentList)
{
if (! a.isPresent())
{
// See if there is a value in the properties that can be used
if ((argumentProperties != null) && (a.getPropertyName() != null))
{
String value = argumentProperties.getProperty(a.getPropertyName()
.toLowerCase());
MessageBuilder invalidReason = new MessageBuilder();
if (value != null)
{
Boolean addValue = true;
if (!( a instanceof BooleanArgument))
{
addValue = a.valueIsAcceptable(value, invalidReason);
}
if (addValue)
{
a.addValue(value);
if (a.needsValue())
{
a.setPresent(true);
}
a.setValueSetByProperty(true);
}
}
}
}
if ((! a.isPresent()) && a.needsValue())
{
// See if the argument defines a default.
if (a.getDefaultValue() != null)
{
a.addValue(a.getDefaultValue());
}
// If there is still no value and the argument is required, then that's
// a problem.
if ((! a.hasValue()) && a.isRequired())
{
Message message =
ERR_ARGPARSER_NO_VALUE_FOR_REQUIRED_ARG.get(a.getName());
throw new ArgumentException(message);
}
}
}