}
argValue = rawArguments[++i];
}
MessageBuilder invalidReason = new MessageBuilder();
if (! a.valueIsAcceptable(argValue, invalidReason))
{
Message message = ERR_SUBCMDPARSER_VALUE_UNACCEPTABLE_FOR_LONG_ID.
get(argValue, argName, 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_SUBCMDPARSER_NOT_MULTIVALUED_FOR_LONG_ID.get(origArgName);
throw new ArgumentException(message);
}
a.addValue(argValue);
}
else
{
if (argValue != null)
{
Message message =
ERR_SUBCMDPARSER_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_SUBCMDPARSER_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. It may be either a
// global argument or a subcommand-specific argument.
Argument a = globalShortIDMap.get(argCharacter);
if (a == null)
{
if (subCommand == null)
{
if (argCharacter == '?')
{
// "-?" will always be interpreted as requesting usage.
try
{
getUsage(usageOutputStream);
if (usageArgument != null)
{
usageArgument.setPresent(true);
}
} catch (Exception e) {}
return;
}
else
if (argCharacter == OPTION_SHORT_PRODUCT_VERSION)
{
// "-V" will always be interpreted as requesting
// version information except if it's already defined.
boolean dashVAccepted = true;
if (globalShortIDMap.containsKey(OPTION_SHORT_PRODUCT_VERSION))
{
dashVAccepted = false;
}
else
{
for (SubCommand subCmd : subCommands.values())
{
if (subCmd.getArgument(OPTION_SHORT_PRODUCT_VERSION) != null)
{
dashVAccepted = false;
break;
}
}
}
if (dashVAccepted)
{
usageOrVersionDisplayed = true;
versionPresent = true;
try
{
DirectoryServer.printVersion(usageOutputStream);
}
catch (Exception e)
{
}
return;
}
else
{
// -V is defined in another suncommand, so we can
// accepted it as the version information argument
Message message =
ERR_SUBCMDPARSER_NO_GLOBAL_ARGUMENT_FOR_SHORT_ID.
get(String.valueOf(argCharacter));
throw new ArgumentException(message);
}
}
else
{
// There is no such argument registered.
Message message =
ERR_SUBCMDPARSER_NO_GLOBAL_ARGUMENT_FOR_SHORT_ID.
get(String.valueOf(argCharacter));
throw new ArgumentException(message);
}
}
else
{
a = subCommand.getArgument(argCharacter);
if (a == null)
{
if (argCharacter == '?')
{
// "-?" will always be interpreted as requesting usage.
try
{
getUsage(usageOutputStream);
} catch (Exception e) {}
return;
}
else
if (argCharacter == OPTION_SHORT_PRODUCT_VERSION)
{
// "-V" will always be interpreted as requesting
// version information except if it's already defined.
boolean dashVAccepted = true;
if (globalShortIDMap.containsKey(OPTION_SHORT_PRODUCT_VERSION))
{
dashVAccepted = false;
}
else
{
for (SubCommand subCmd : subCommands.values())
{
if (subCmd.getArgument(OPTION_SHORT_PRODUCT_VERSION)!=null)
{
dashVAccepted = false;
break;
}
}
}
if (dashVAccepted)
{
usageOrVersionDisplayed = true;
versionPresent = true;
try
{
DirectoryServer.printVersion(usageOutputStream);
}
catch (Exception e)
{
}
return;
}
}
else
{
// There is no such argument registered.
Message message = ERR_SUBCMDPARSER_NO_ARGUMENT_FOR_SHORT_ID.get(
String.valueOf(argCharacter));
throw new ArgumentException(message);
}
}
}
}
a.setPresent(true);
// If this is the usage argument, then immediately stop and print
// usage information.
if (usageGroupArguments.containsKey(a))
{
try
{
getUsage(a, 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_SUBCMDPARSER_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_SUBCMDPARSER_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_SUBCMDPARSER_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 = globalShortIDMap.get(c);
if (b == null)
{
if (subCommand == null)
{
Message message =
ERR_SUBCMDPARSER_NO_GLOBAL_ARGUMENT_FOR_SHORT_ID.
get(String.valueOf(argCharacter));
throw new ArgumentException(message);
}
else
{
b = subCommand.getArgument(c);
if (b == null)
{
Message message = ERR_SUBCMDPARSER_NO_ARGUMENT_FOR_SHORT_ID.
get(String.valueOf(argCharacter));
throw new ArgumentException(message);
}
}
}
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_SUBCMDPARSER_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 (usageGroupArguments.containsKey(b))
{
try
{
getUsage(b, usageOutputStream);
} catch (Exception e) {}
return;
}
}
}
}
}
}
else if (subCommand != null)
{
// It's not a short or long identifier and the sub-command has
// already been specified, so it must be the first trailing argument.
if (subCommand.allowsTrailingArguments())
{
trailingArguments.add(arg);
inTrailingArgs = true;
}
else
{
// Trailing arguments are not allowed for this sub-command.
Message message = ERR_ARGPARSER_DISALLOWED_TRAILING_ARGUMENT.get(arg);
throw new ArgumentException(message);
}
}
else
{
// It must be the sub-command.
String nameToCheck = arg;
if (! longArgumentsCaseSensitive)
{
nameToCheck = toLowerCase(arg);
}
SubCommand sc = subCommands.get(nameToCheck);
if (sc == null)
{
Message message = ERR_SUBCMDPARSER_INVALID_ARGUMENT.get(arg);
throw new ArgumentException(message);
}
else
{
subCommand = sc;
}
}
}
// If we have a sub-command and it allows trailing arguments and
// there is a minimum number, then make sure at least that many
// were provided.
if (subCommand != null)
{
int minTrailingArguments = subCommand.getMinTrailingArguments();
if (subCommand.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 the global arguments and make sure that they have
// values or a suitable default is available.
for (Argument a : globalArgumentList)
{
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())
{
// ISee 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_SUBCMDPARSER_NO_VALUE_FOR_REQUIRED_ARG.get(a.getName());
throw new ArgumentException(message);
}
}
}
// Iterate through all the subcommand-specific arguments and make sure that
// they have values or a suitable default is available.
if (subCommand != null)
{
for (Argument a : subCommand.getArguments())
{
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))
{