}
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))
{