| | |
| | | * Portions Copyright 2006-2007 Sun Microsystems, Inc. |
| | | */ |
| | | package org.opends.server.util.args; |
| | | import org.opends.messages.Message; |
| | | |
| | | |
| | | |
| | |
| | | |
| | | import org.opends.server.core.DirectoryServer; |
| | | |
| | | import static org.opends.server.messages.MessageHandler.*; |
| | | import static org.opends.server.messages.UtilityMessages.*; |
| | | import static org.opends.messages.UtilityMessages.*; |
| | | import static org.opends.server.util.ServerConstants.*; |
| | | import static org.opends.server.util.StaticUtils.*; |
| | | import static org.opends.server.tools.ToolConstants.*; |
| | | import static org.opends.server.messages.ToolMessages.*; |
| | | import static org.opends.messages.ToolMessages.*; |
| | | |
| | | import org.opends.messages.MessageBuilder; |
| | | |
| | | |
| | | /** |
| | |
| | | |
| | | // A human-readable description for the tool, which will be included when |
| | | // displaying usage information. |
| | | private String toolDescription; |
| | | private Message toolDescription; |
| | | |
| | | // The display name that will be used for the trailing arguments in the usage |
| | | // information. |
| | |
| | | * @param longArgumentsCaseSensitive Indicates whether long arguments should |
| | | * be treated in a case-sensitive manner. |
| | | */ |
| | | public ArgumentParser(String mainClassName, String toolDescription, |
| | | public ArgumentParser(String mainClassName, Message toolDescription, |
| | | boolean longArgumentsCaseSensitive) |
| | | { |
| | | this.mainClassName = mainClassName; |
| | |
| | | * arguments in the generated usage |
| | | * information. |
| | | */ |
| | | public ArgumentParser(String mainClassName, String toolDescription, |
| | | public ArgumentParser(String mainClassName, Message toolDescription, |
| | | boolean longArgumentsCaseSensitive, |
| | | boolean allowsTrailingArguments, |
| | | int minTrailingArguments, int maxTrailingArguments, |
| | |
| | | * @return A human-readable description for this tool, or {@code null} if |
| | | * none is available. |
| | | */ |
| | | public String getToolDescription() |
| | | public Message getToolDescription() |
| | | { |
| | | return toolDescription; |
| | | } |
| | |
| | | { |
| | | String conflictingName = shortIDMap.get(shortID).getName(); |
| | | |
| | | int msgID = MSGID_ARGPARSER_DUPLICATE_SHORT_ID; |
| | | String message = getMessage(msgID, argument.getName(), |
| | | String.valueOf(shortID), conflictingName); |
| | | throw new ArgumentException(msgID, message); |
| | | Message message = ERR_ARGPARSER_DUPLICATE_SHORT_ID.get( |
| | | argument.getName(), String.valueOf(shortID), conflictingName); |
| | | throw new ArgumentException(message); |
| | | } |
| | | |
| | | String longID = argument.getLongIdentifier(); |
| | |
| | | { |
| | | String conflictingName = longIDMap.get(longID).getName(); |
| | | |
| | | int msgID = MSGID_ARGPARSER_DUPLICATE_LONG_ID; |
| | | String message = getMessage(msgID, argument.getName(), |
| | | String.valueOf(longID), conflictingName); |
| | | throw new ArgumentException(msgID, message); |
| | | Message message = ERR_ARGPARSER_DUPLICATE_LONG_ID.get( |
| | | argument.getName(), String.valueOf(longID), conflictingName); |
| | | throw new ArgumentException(message); |
| | | } |
| | | } |
| | | |
| | |
| | | { |
| | | if (requirePropertiesFile) |
| | | { |
| | | int msgID = MSGID_ARGPARSER_CANNOT_READ_PROPERTIES_FILE; |
| | | String message = getMessage(msgID, String.valueOf(propertiesFile), |
| | | getExceptionMessage(e)); |
| | | throw new ArgumentException(msgID, message, e); |
| | | Message message = ERR_ARGPARSER_CANNOT_READ_PROPERTIES_FILE.get( |
| | | String.valueOf(propertiesFile), getExceptionMessage(e)); |
| | | throw new ArgumentException(message, e); |
| | | } |
| | | } |
| | | |
| | |
| | | if ((maxTrailingArguments > 0) && |
| | | (trailingArguments.size() > maxTrailingArguments)) |
| | | { |
| | | int msgID = MSGID_ARGPARSER_TOO_MANY_TRAILING_ARGS; |
| | | String message = getMessage(msgID, maxTrailingArguments); |
| | | throw new ArgumentException(msgID, message); |
| | | Message message = |
| | | ERR_ARGPARSER_TOO_MANY_TRAILING_ARGS.get(maxTrailingArguments); |
| | | throw new ArgumentException(message); |
| | | } |
| | | |
| | | continue; |
| | |
| | | else if (equalPos == 0) |
| | | { |
| | | // The argument starts with "--=", which is not acceptable. |
| | | int msgID = MSGID_ARGPARSER_LONG_ARG_WITHOUT_NAME; |
| | | String message = getMessage(msgID, arg); |
| | | throw new ArgumentException(msgID, message); |
| | | Message message = ERR_ARGPARSER_LONG_ARG_WITHOUT_NAME.get(arg); |
| | | throw new ArgumentException(message); |
| | | } |
| | | else |
| | | { |
| | |
| | | else |
| | | { |
| | | // There is no such argument registered. |
| | | int msgID = MSGID_ARGPARSER_NO_ARGUMENT_WITH_LONG_ID; |
| | | String message = getMessage(msgID, argName); |
| | | throw new ArgumentException(msgID, message); |
| | | Message message = |
| | | ERR_ARGPARSER_NO_ARGUMENT_WITH_LONG_ID.get(argName); |
| | | throw new ArgumentException(message); |
| | | } |
| | | } |
| | | else |
| | |
| | | { |
| | | if ((i+1) == numArguments) |
| | | { |
| | | int msgID = MSGID_ARGPARSER_NO_VALUE_FOR_ARGUMENT_WITH_LONG_ID; |
| | | String message = getMessage(msgID, argName); |
| | | throw new ArgumentException(msgID, message); |
| | | Message message = |
| | | ERR_ARGPARSER_NO_VALUE_FOR_ARGUMENT_WITH_LONG_ID.get(argName); |
| | | throw new ArgumentException(message); |
| | | } |
| | | |
| | | argValue = rawArguments[++i]; |
| | | } |
| | | |
| | | StringBuilder invalidReason = new StringBuilder(); |
| | | MessageBuilder invalidReason = new MessageBuilder(); |
| | | if (! a.valueIsAcceptable(argValue, invalidReason)) |
| | | { |
| | | int msgID = MSGID_ARGPARSER_VALUE_UNACCEPTABLE_FOR_LONG_ID; |
| | | String message = getMessage(msgID, argValue, argName, |
| | | invalidReason.toString()); |
| | | throw new ArgumentException(msgID, message); |
| | | Message message = ERR_ARGPARSER_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())) |
| | | { |
| | | int msgID = MSGID_ARGPARSER_NOT_MULTIVALUED_FOR_LONG_ID; |
| | | String message = getMessage(msgID, argName); |
| | | throw new ArgumentException(msgID, message); |
| | | Message message = |
| | | ERR_ARGPARSER_NOT_MULTIVALUED_FOR_LONG_ID.get(argName); |
| | | throw new ArgumentException(message); |
| | | } |
| | | |
| | | a.addValue(argValue); |
| | |
| | | { |
| | | if (argValue != null) |
| | | { |
| | | int msgID = MSGID_ARGPARSER_ARG_FOR_LONG_ID_DOESNT_TAKE_VALUE; |
| | | String message = getMessage(msgID, argName); |
| | | throw new ArgumentException(msgID, message); |
| | | Message message = |
| | | ERR_ARGPARSER_ARG_FOR_LONG_ID_DOESNT_TAKE_VALUE.get(argName); |
| | | throw new ArgumentException(message); |
| | | } |
| | | } |
| | | } |
| | |
| | | // -n value |
| | | if (arg.equals("-")) |
| | | { |
| | | int msgID = MSGID_ARGPARSER_INVALID_DASH_AS_ARGUMENT; |
| | | String message = getMessage(msgID); |
| | | throw new ArgumentException(msgID, message); |
| | | Message message = ERR_ARGPARSER_INVALID_DASH_AS_ARGUMENT.get(); |
| | | throw new ArgumentException(message); |
| | | } |
| | | |
| | | char argCharacter = arg.charAt(1); |
| | |
| | | else |
| | | { |
| | | // There is no such argument registered. |
| | | int msgID = MSGID_ARGPARSER_NO_ARGUMENT_WITH_SHORT_ID; |
| | | String message = getMessage(msgID, String.valueOf(argCharacter)); |
| | | throw new ArgumentException(msgID, message); |
| | | Message message = ERR_ARGPARSER_NO_ARGUMENT_WITH_SHORT_ID.get( |
| | | String.valueOf(argCharacter)); |
| | | throw new ArgumentException(message); |
| | | } |
| | | } |
| | | else |
| | |
| | | { |
| | | if ((i+1) == numArguments) |
| | | { |
| | | int msgID = MSGID_ARGPARSER_NO_VALUE_FOR_ARGUMENT_WITH_SHORT_ID; |
| | | String message = getMessage(msgID, String.valueOf(argCharacter)); |
| | | throw new ArgumentException(msgID, message); |
| | | Message message = |
| | | ERR_ARGPARSER_NO_VALUE_FOR_ARGUMENT_WITH_SHORT_ID. |
| | | get(String.valueOf(argCharacter)); |
| | | throw new ArgumentException(message); |
| | | } |
| | | |
| | | argValue = rawArguments[++i]; |
| | | } |
| | | |
| | | StringBuilder invalidReason = new StringBuilder(); |
| | | MessageBuilder invalidReason = new MessageBuilder(); |
| | | if (! a.valueIsAcceptable(argValue, invalidReason)) |
| | | { |
| | | int msgID = MSGID_ARGPARSER_VALUE_UNACCEPTABLE_FOR_SHORT_ID; |
| | | String message = getMessage(msgID, argValue, |
| | | String.valueOf(argCharacter), |
| | | invalidReason.toString()); |
| | | throw new ArgumentException(msgID, message); |
| | | 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())) |
| | | { |
| | | int msgID = MSGID_ARGPARSER_NOT_MULTIVALUED_FOR_SHORT_ID; |
| | | String message = getMessage(msgID, String.valueOf(argCharacter)); |
| | | throw new ArgumentException(msgID, message); |
| | | Message message = ERR_ARGPARSER_NOT_MULTIVALUED_FOR_SHORT_ID.get( |
| | | String.valueOf(argCharacter)); |
| | | throw new ArgumentException(message); |
| | | } |
| | | |
| | | a.addValue(argValue); |
| | |
| | | if (b == null) |
| | | { |
| | | // There is no such argument registered. |
| | | int msgID = MSGID_ARGPARSER_NO_ARGUMENT_WITH_SHORT_ID; |
| | | String message = getMessage(msgID, |
| | | String.valueOf(argCharacter)); |
| | | throw new ArgumentException(msgID, message); |
| | | 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. |
| | | int msgID = MSGID_ARGPARSER_CANT_MIX_ARGS_WITH_VALUES; |
| | | String message = getMessage(msgID, String.valueOf(argCharacter), |
| | | argValue, String.valueOf(c)); |
| | | throw new ArgumentException(msgID, message); |
| | | Message message = ERR_ARGPARSER_CANT_MIX_ARGS_WITH_VALUES.get( |
| | | String.valueOf(argCharacter), argValue, String.valueOf(c)); |
| | | throw new ArgumentException(message); |
| | | } |
| | | else |
| | | { |
| | |
| | | { |
| | | // It doesn't start with a dash and we don't allow trailing arguments, |
| | | // so this is illegal. |
| | | int msgID = MSGID_ARGPARSER_DISALLOWED_TRAILING_ARGUMENT; |
| | | String message = getMessage(msgID, arg); |
| | | throw new ArgumentException(msgID, message); |
| | | Message message = ERR_ARGPARSER_DISALLOWED_TRAILING_ARGUMENT.get(arg); |
| | | throw new ArgumentException(message); |
| | | } |
| | | } |
| | | |
| | |
| | | { |
| | | if (trailingArguments.size() < minTrailingArguments) |
| | | { |
| | | int msgID = MSGID_ARGPARSER_TOO_FEW_TRAILING_ARGUMENTS; |
| | | String message = getMessage(msgID, minTrailingArguments); |
| | | throw new ArgumentException(msgID, message); |
| | | Message message = |
| | | ERR_ARGPARSER_TOO_FEW_TRAILING_ARGUMENTS.get(minTrailingArguments); |
| | | throw new ArgumentException(message); |
| | | } |
| | | } |
| | | |
| | |
| | | // a problem. |
| | | if ((! valueSet) && a.isRequired()) |
| | | { |
| | | int msgID = MSGID_ARGPARSER_NO_VALUE_FOR_REQUIRED_ARG; |
| | | String message = getMessage(msgID, a.getName()); |
| | | throw new ArgumentException(msgID, message); |
| | | Message message = |
| | | ERR_ARGPARSER_NO_VALUE_FOR_REQUIRED_ARG.get(a.getName()); |
| | | throw new ArgumentException(message); |
| | | } |
| | | } |
| | | } |
| | |
| | | usageOrVersionDisplayed = true; |
| | | if ((toolDescription != null) && (toolDescription.length() > 0)) |
| | | { |
| | | buffer.append(wrapText(toolDescription, 79)); |
| | | buffer.append(wrapText(toolDescription.toString(), 79)); |
| | | buffer.append(EOL); |
| | | } |
| | | |
| | |
| | | buffer.append("--" + OPTION_LONG_PRODUCT_VERSION); |
| | | buffer.append(EOL); |
| | | buffer.append(" "); |
| | | buffer.append( getMessage(MSGID_DESCRIPTION_PRODUCT_VERSION)); |
| | | buffer.append( INFO_DESCRIPTION_PRODUCT_VERSION.get()); |
| | | buffer.append(EOL); |
| | | |
| | | Argument helpArgument = null ; |
| | |
| | | // indent the description five characters and try our best to wrap |
| | | // at or |
| | | // before column 79 so it will be friendly to 80-column displays. |
| | | String description = a.getDescription(); |
| | | Message description = a.getDescription(); |
| | | if (description.length() <= 75) |
| | | { |
| | | buffer.append(" "); |
| | |
| | | } |
| | | else |
| | | { |
| | | String s = description; |
| | | String s = description.toString(); |
| | | while (s.length() > 75) |
| | | { |
| | | int spacePos = s.lastIndexOf(' ', 75); |