| | |
| | | |
| | | |
| | | |
| | | import static org.opends.messages.ToolMessages.INFO_DESCRIPTION_GENERAL_ARGS; |
| | | import static org.opends.messages.ToolMessages.INFO_DESCRIPTION_IO_ARGS; |
| | | import static org.opends.messages.ToolMessages.INFO_DESCRIPTION_LDAP_CONNECTION_ARGS; |
| | | import static org.opends.messages.ToolMessages.INFO_DESCRIPTION_PRODUCT_VERSION; |
| | | import static org.opends.messages.UtilityMessages.*; |
| | | import static org.opends.server.tools.ToolConstants.*; |
| | | import static org.opends.server.util.ServerConstants.EOL; |
| | | import static org.opends.server.util.ServerConstants.PROPERTY_SCRIPT_NAME; |
| | | import static org.opends.server.util.StaticUtils.getBytes; |
| | | import static org.opends.server.util.StaticUtils.getExceptionMessage; |
| | | import static org.opends.server.util.StaticUtils.toLowerCase; |
| | | import static org.opends.server.util.StaticUtils.wrapText; |
| | | import static com.sun.opends.sdk.util.Messages.*; |
| | | import static org.opends.sdk.tools.ToolConstants.*; |
| | | import static org.opends.sdk.tools.Utils.*; |
| | | import static org.opends.sdk.util.StaticUtils.*; |
| | | |
| | | import java.io.File; |
| | | import java.io.FileInputStream; |
| | |
| | | import java.io.OutputStream; |
| | | import java.util.*; |
| | | |
| | | import org.opends.messages.Message; |
| | | import org.opends.messages.MessageBuilder; |
| | | import org.opends.server.util.SetupUtils; |
| | | import com.sun.opends.sdk.util.Message; |
| | | import com.sun.opends.sdk.util.MessageBuilder; |
| | | |
| | | |
| | | |
| | |
| | | * Group for arguments that have not been explicitly grouped. These |
| | | * will appear at the top of the usage statement without a header. |
| | | */ |
| | | private ArgumentGroup defaultArgGroup = |
| | | new ArgumentGroup(Message.EMPTY, Integer.MAX_VALUE); |
| | | private ArgumentGroup defaultArgGroup = new ArgumentGroup( |
| | | Message.EMPTY, Integer.MAX_VALUE); |
| | | |
| | | /** |
| | | * Group for arguments that are related to connection through LDAP. |
| | | * This includes options like the bind DN, the port, etc. |
| | | */ |
| | | private ArgumentGroup ldapArgGroup = |
| | | new ArgumentGroup(INFO_DESCRIPTION_LDAP_CONNECTION_ARGS.get(), |
| | | Integer.MIN_VALUE + 2); |
| | | private ArgumentGroup ldapArgGroup = new ArgumentGroup( |
| | | INFO_DESCRIPTION_LDAP_CONNECTION_ARGS.get(), |
| | | Integer.MIN_VALUE + 2); |
| | | |
| | | /** |
| | | * Group for arguments that are related to utility input/output like |
| | | * properties file, no-prompt etc. These will appear toward the bottom |
| | | * of the usage statement. |
| | | */ |
| | | private ArgumentGroup ioArgGroup = |
| | | new ArgumentGroup(INFO_DESCRIPTION_IO_ARGS.get(), |
| | | Integer.MIN_VALUE + 1); |
| | | private ArgumentGroup ioArgGroup = new ArgumentGroup( |
| | | INFO_DESCRIPTION_IO_ARGS.get(), Integer.MIN_VALUE + 1); |
| | | |
| | | /** |
| | | * Group for arguments that are general like help, version etc. These |
| | | * will appear at the end of the usage statement. |
| | | */ |
| | | private ArgumentGroup generalArgGroup = |
| | | new ArgumentGroup(INFO_DESCRIPTION_GENERAL_ARGS.get(), |
| | | Integer.MIN_VALUE); |
| | | private ArgumentGroup generalArgGroup = new ArgumentGroup( |
| | | INFO_DESCRIPTION_GENERAL_ARGS.get(), Integer.MIN_VALUE); |
| | | |
| | | private final static String INDENT = " "; |
| | | private final static int MAX_LENGTH = |
| | | SetupUtils.isWindows() ? 79 : 80; |
| | | |
| | | private final static int MAX_LENGTH = 80; |
| | | |
| | | |
| | | |
| | | /** |
| | | * Creates a new instance of this argument parser with no arguments. |
| | | * Unnamed trailing arguments will not be allowed. |
| | | * |
| | | * |
| | | * @param mainClassName |
| | | * The fully-qualified name of the Java class that should be |
| | | * invoked to launch the program with which this argument |
| | |
| | | * Indicates whether long arguments should be treated in a |
| | | * case-sensitive manner. |
| | | */ |
| | | public ArgumentParser(String mainClassName, Message toolDescription, |
| | | ArgumentParser(String mainClassName, Message toolDescription, |
| | | boolean longArgumentsCaseSensitive) |
| | | { |
| | | this.mainClassName = mainClassName; |
| | |
| | | /** |
| | | * Creates a new instance of this argument parser with no arguments |
| | | * that may or may not be allowed to have unnamed trailing arguments. |
| | | * |
| | | * |
| | | * @param mainClassName |
| | | * The fully-qualified name of the Java class that should be |
| | | * invoked to launch the program with which this argument |
| | |
| | | * unnamed trailing arguments in the generated usage |
| | | * information. |
| | | */ |
| | | public ArgumentParser(String mainClassName, Message toolDescription, |
| | | ArgumentParser(String mainClassName, Message toolDescription, |
| | | boolean longArgumentsCaseSensitive, |
| | | boolean allowsTrailingArguments, int minTrailingArguments, |
| | | int maxTrailingArguments, String trailingArgsDisplayName) |
| | |
| | | * Retrieves the fully-qualified name of the Java class that should be |
| | | * invoked to launch the program with which this argument parser is |
| | | * associated. |
| | | * |
| | | * |
| | | * @return The fully-qualified name of the Java class that should be |
| | | * invoked to launch the program with which this argument |
| | | * parser is associated. |
| | | */ |
| | | public String getMainClassName() |
| | | String getMainClassName() |
| | | { |
| | | return mainClassName; |
| | | } |
| | |
| | | /** |
| | | * Retrieves a human-readable description for this tool, which should |
| | | * be included at the top of the command-line usage information. |
| | | * |
| | | * |
| | | * @return A human-readable description for this tool, or {@code null} |
| | | * if none is available. |
| | | */ |
| | | public Message getToolDescription() |
| | | Message getToolDescription() |
| | | { |
| | | return toolDescription; |
| | | } |
| | |
| | | * be manually parsed by the application using this parser. Note that |
| | | * once an unnamed trailing argument has been identified, all |
| | | * remaining arguments will be classified as such. |
| | | * |
| | | * |
| | | * @return <CODE>true</CODE> if this parser allows unnamed trailing |
| | | * arguments, or <CODE>false</CODE> if it does not. |
| | | */ |
| | | public boolean allowsTrailingArguments() |
| | | boolean allowsTrailingArguments() |
| | | { |
| | | return allowsTrailingArguments; |
| | | } |
| | |
| | | /** |
| | | * Retrieves the minimum number of unnamed trailing arguments that |
| | | * must be provided. |
| | | * |
| | | * |
| | | * @return The minimum number of unnamed trailing arguments that must |
| | | * be provided, or a value less than or equal to zero if no |
| | | * minimum will be enforced. |
| | | */ |
| | | public int getMinTrailingArguments() |
| | | int getMinTrailingArguments() |
| | | { |
| | | return minTrailingArguments; |
| | | } |
| | |
| | | /** |
| | | * Retrieves the maximum number of unnamed trailing arguments that may |
| | | * be provided. |
| | | * |
| | | * |
| | | * @return The maximum number of unnamed trailing arguments that may |
| | | * be provided, or a value less than or equal to zero if no |
| | | * maximum will be enforced. |
| | | */ |
| | | public int getMaxTrailingArguments() |
| | | int getMaxTrailingArguments() |
| | | { |
| | | return maxTrailingArguments; |
| | | } |
| | |
| | | /** |
| | | * Retrieves the list of all arguments that have been defined for this |
| | | * argument parser. |
| | | * |
| | | * |
| | | * @return The list of all arguments that have been defined for this |
| | | * argument parser. |
| | | */ |
| | | public LinkedList<Argument> getArgumentList() |
| | | LinkedList<Argument> getArgumentList() |
| | | { |
| | | return argumentList; |
| | | } |
| | |
| | | |
| | | /** |
| | | * Retrieves the argument with the specified name. |
| | | * |
| | | * |
| | | * @param name |
| | | * The name of the argument to retrieve. |
| | | * @return The argument with the specified name, or <CODE>null</CODE> |
| | | * if there is no such argument. |
| | | */ |
| | | public Argument getArgument(String name) |
| | | Argument getArgument(String name) |
| | | { |
| | | return argumentMap.get(name); |
| | | } |
| | |
| | | * Retrieves the set of arguments mapped by the short identifier that |
| | | * may be used to reference them. Note that arguments that do not have |
| | | * a short identifier will not be present in this list. |
| | | * |
| | | * |
| | | * @return The set of arguments mapped by the short identifier that |
| | | * may be used to reference them. |
| | | */ |
| | | public HashMap<Character, Argument> getArgumentsByShortID() |
| | | HashMap<Character, Argument> getArgumentsByShortID() |
| | | { |
| | | return shortIDMap; |
| | | } |
| | |
| | | |
| | | /** |
| | | * Retrieves the argument with the specified short identifier. |
| | | * |
| | | * |
| | | * @param shortID |
| | | * The short ID for the argument to retrieve. |
| | | * @return The argument with the specified short identifier, or |
| | | * <CODE>null</CODE> if there is no such argument. |
| | | */ |
| | | public Argument getArgumentForShortID(Character shortID) |
| | | Argument getArgumentForShortID(Character shortID) |
| | | { |
| | | return shortIDMap.get(shortID); |
| | | } |
| | |
| | | * Retrieves the set of arguments mapped by the long identifier that |
| | | * may be used to reference them. Note that arguments that do not have |
| | | * a long identifier will not be present in this list. |
| | | * |
| | | * |
| | | * @return The set of arguments mapped by the long identifier that may |
| | | * be used to reference them. |
| | | */ |
| | | public HashMap<String, Argument> getArgumentsByLongID() |
| | | HashMap<String, Argument> getArgumentsByLongID() |
| | | { |
| | | return longIDMap; |
| | | } |
| | |
| | | |
| | | /** |
| | | * Retrieves the argument with the specified long identifier. |
| | | * |
| | | * |
| | | * @param longID |
| | | * The long identifier of the argument to retrieve. |
| | | * @return The argument with the specified long identifier, or |
| | | * <CODE>null</CODE> if there is no such argument. |
| | | */ |
| | | public Argument getArgumentForLongID(String longID) |
| | | Argument getArgumentForLongID(String longID) |
| | | { |
| | | return longIDMap.get(longID); |
| | | } |
| | |
| | | /** |
| | | * Retrieves the set of unnamed trailing arguments that were provided |
| | | * on the command line. |
| | | * |
| | | * |
| | | * @return The set of unnamed trailing arguments that were provided on |
| | | * the command line. |
| | | */ |
| | | public ArrayList<String> getTrailingArguments() |
| | | ArrayList<String> getTrailingArguments() |
| | | { |
| | | return trailingArguments; |
| | | } |
| | |
| | | |
| | | /** |
| | | * Retrieves the raw set of arguments that were provided. |
| | | * |
| | | * |
| | | * @return The raw set of arguments that were provided, or |
| | | * <CODE>null</CODE> if the argument list has not yet been |
| | | * parsed. |
| | | */ |
| | | public String[] getRawArguments() |
| | | String[] getRawArguments() |
| | | { |
| | | return rawArguments; |
| | | } |
| | |
| | | |
| | | /** |
| | | * Sets the usage group description for the default argument group. |
| | | * |
| | | * |
| | | * @param description |
| | | * for the default group |
| | | */ |
| | | public void setDefaultArgumentGroupDescription(Message description) |
| | | void setDefaultArgumentGroupDescription(Message description) |
| | | { |
| | | this.defaultArgGroup.setDescription(description); |
| | | } |
| | |
| | | |
| | | /** |
| | | * Sets the usage group description for the LDAP argument group. |
| | | * |
| | | * |
| | | * @param description |
| | | * for the LDAP group |
| | | */ |
| | | public void setLdapArgumentGroupDescription(Message description) |
| | | void setLdapArgumentGroupDescription(Message description) |
| | | { |
| | | this.ldapArgGroup.setDescription(description); |
| | | } |
| | |
| | | /** |
| | | * Sets the usage group description for the input/output argument |
| | | * group. |
| | | * |
| | | * |
| | | * @param description |
| | | * for the input/output group |
| | | */ |
| | | public void setInputOutputArgumentGroupDescription(Message description) |
| | | void setInputOutputArgumentGroupDescription(Message description) |
| | | { |
| | | this.ioArgGroup.setDescription(description); |
| | | } |
| | |
| | | |
| | | /** |
| | | * Sets the usage group description for the general argument group. |
| | | * |
| | | * |
| | | * @param description |
| | | * for the general group |
| | | */ |
| | | public void setGeneralArgumentGroupDescription(Message description) |
| | | void setGeneralArgumentGroupDescription(Message description) |
| | | { |
| | | this.generalArgGroup.setDescription(description); |
| | | } |
| | |
| | | /** |
| | | * Adds the provided argument to the set of arguments handled by this |
| | | * parser. |
| | | * |
| | | * |
| | | * @param argument |
| | | * The argument to be added. |
| | | * @throws ArgumentException |
| | | * If the provided argument conflicts with another argument |
| | | * that has already been defined. |
| | | */ |
| | | public void addArgument(Argument argument) throws ArgumentException |
| | | void addArgument(Argument argument) throws ArgumentException |
| | | { |
| | | addArgument(argument, null); |
| | | } |
| | |
| | | /** |
| | | * Adds the provided argument to the set of arguments handled by this |
| | | * parser and puts the arguement in the default group. |
| | | * |
| | | * |
| | | * @param argument |
| | | * The argument to be added. |
| | | * @throws ArgumentException |
| | | * If the provided argument conflicts with another argument |
| | | * that has already been defined. |
| | | */ |
| | | public void addDefaultArgument(Argument argument) |
| | | throws ArgumentException |
| | | void addDefaultArgument(Argument argument) throws ArgumentException |
| | | { |
| | | addArgument(argument, defaultArgGroup); |
| | | } |
| | |
| | | /** |
| | | * Adds the provided argument to the set of arguments handled by this |
| | | * parser and puts the argument in the LDAP connection group. |
| | | * |
| | | * |
| | | * @param argument |
| | | * The argument to be added. |
| | | * @throws ArgumentException |
| | | * If the provided argument conflicts with another argument |
| | | * that has already been defined. |
| | | */ |
| | | public void addLdapConnectionArgument(Argument argument) |
| | | void addLdapConnectionArgument(Argument argument) |
| | | throws ArgumentException |
| | | { |
| | | addArgument(argument, ldapArgGroup); |
| | |
| | | /** |
| | | * Adds the provided argument to the set of arguments handled by this |
| | | * parser and puts the argument in the input/output group. |
| | | * |
| | | * |
| | | * @param argument |
| | | * The argument to be added. |
| | | * @throws ArgumentException |
| | | * If the provided argument conflicts with another argument |
| | | * that has already been defined. |
| | | */ |
| | | public void addInputOutputArgument(Argument argument) |
| | | void addInputOutputArgument(Argument argument) |
| | | throws ArgumentException |
| | | { |
| | | addArgument(argument, ioArgGroup); |
| | |
| | | /** |
| | | * Adds the provided argument to the set of arguments handled by this |
| | | * parser and puts the arguement in the general group. |
| | | * |
| | | * |
| | | * @param argument |
| | | * The argument to be added. |
| | | * @throws ArgumentException |
| | | * If the provided argument conflicts with another argument |
| | | * that has already been defined. |
| | | */ |
| | | public void addGeneralArgument(Argument argument) |
| | | throws ArgumentException |
| | | void addGeneralArgument(Argument argument) throws ArgumentException |
| | | { |
| | | addArgument(argument, generalArgGroup); |
| | | } |
| | |
| | | /** |
| | | * Adds the provided argument to the set of arguments handled by this |
| | | * parser. |
| | | * |
| | | * |
| | | * @param argument |
| | | * The argument to be added. |
| | | * @param group |
| | |
| | | * If the provided argument conflicts with another argument |
| | | * that has already been defined. |
| | | */ |
| | | public void addArgument(Argument argument, ArgumentGroup group) |
| | | void addArgument(Argument argument, ArgumentGroup group) |
| | | throws ArgumentException |
| | | { |
| | | |
| | |
| | | { |
| | | String conflictingName = shortIDMap.get(shortID).getName(); |
| | | |
| | | Message message = |
| | | ERR_ARGPARSER_DUPLICATE_SHORT_ID.get(argument.getName(), |
| | | String.valueOf(shortID), conflictingName); |
| | | Message message = ERR_ARGPARSER_DUPLICATE_SHORT_ID.get(argument |
| | | .getName(), String.valueOf(shortID), conflictingName); |
| | | throw new ArgumentException(message); |
| | | } |
| | | |
| | |
| | | // identifier. |
| | | try |
| | | { |
| | | versionArgument = |
| | | new BooleanArgument(OPTION_LONG_PRODUCT_VERSION, null, |
| | | OPTION_LONG_PRODUCT_VERSION, |
| | | INFO_DESCRIPTION_PRODUCT_VERSION.get()); |
| | | versionArgument = new BooleanArgument( |
| | | OPTION_LONG_PRODUCT_VERSION, null, |
| | | OPTION_LONG_PRODUCT_VERSION, |
| | | INFO_DESCRIPTION_PRODUCT_VERSION.get()); |
| | | this.generalArgGroup.addArgument(versionArgument); |
| | | } |
| | | catch (ArgumentException e) |
| | |
| | | { |
| | | String conflictingName = longIDMap.get(longID).getName(); |
| | | |
| | | Message message = |
| | | ERR_ARGPARSER_DUPLICATE_LONG_ID.get(argument.getName(), |
| | | argument.getLongIdentifier(), conflictingName); |
| | | Message message = ERR_ARGPARSER_DUPLICATE_LONG_ID.get(argument |
| | | .getName(), argument.getLongIdentifier(), conflictingName); |
| | | throw new ArgumentException(message); |
| | | } |
| | | } |
| | |
| | | * still need to check for the presence of the usage argument after |
| | | * calling <CODE>parseArguments</CODE> to know that no further |
| | | * processing will be required. |
| | | * |
| | | * |
| | | * @param argument |
| | | * The argument whose presence should automatically trigger |
| | | * the display of usage information. |
| | | */ |
| | | public void setUsageArgument(Argument argument) |
| | | void setUsageArgument(Argument argument) |
| | | { |
| | | usageArgument = argument; |
| | | usageOutputStream = System.out; |
| | |
| | | * still need to check for the presence of the usage argument after |
| | | * calling <CODE>parseArguments</CODE> to know that no further |
| | | * processing will be required. |
| | | * |
| | | * |
| | | * @param argument |
| | | * The argument whose presence should automatically trigger |
| | | * the display of usage information. |
| | |
| | | * The output stream to which the usage information should be |
| | | * written. |
| | | */ |
| | | public void setUsageArgument(Argument argument, |
| | | OutputStream outputStream) |
| | | void setUsageArgument(Argument argument, OutputStream outputStream) |
| | | { |
| | | usageArgument = argument; |
| | | usageOutputStream = outputStream; |
| | |
| | | /** |
| | | * Sets the provided argument which will be used to identify the file |
| | | * properties. |
| | | * |
| | | * |
| | | * @param argument |
| | | * The argument which will be used to identify the file |
| | | * properties. |
| | | */ |
| | | public void setFilePropertiesArgument(StringArgument argument) |
| | | void setFilePropertiesArgument(StringArgument argument) |
| | | { |
| | | filePropertiesPathArgument = argument; |
| | | } |
| | |
| | | /** |
| | | * Sets the provided argument which will be used to identify the file |
| | | * properties. |
| | | * |
| | | * |
| | | * @param argument |
| | | * The argument which will be used to indicate if we have to |
| | | * look for properties file. |
| | | */ |
| | | public void setNoPropertiesFileArgument(BooleanArgument argument) |
| | | void setNoPropertiesFileArgument(BooleanArgument argument) |
| | | { |
| | | noPropertiesFileArgument = argument; |
| | | } |
| | |
| | | /** |
| | | * Parses the provided set of arguments and updates the information |
| | | * associated with this parser accordingly. |
| | | * |
| | | * |
| | | * @param rawArguments |
| | | * The raw set of arguments to parse. |
| | | * @throws ArgumentException |
| | | * If a problem was encountered while parsing the provided |
| | | * arguments. |
| | | */ |
| | | public void parseArguments(String[] rawArguments) |
| | | throws ArgumentException |
| | | void parseArguments(String[] rawArguments) throws ArgumentException |
| | | { |
| | | parseArguments(rawArguments, null); |
| | | } |
| | |
| | | * associated with this parser accordingly. Default values for |
| | | * unspecified arguments may be read from the specified properties |
| | | * file. |
| | | * |
| | | * |
| | | * @param rawArguments |
| | | * The set of raw arguments to parse. |
| | | * @param propertiesFile |
| | |
| | | * If a problem was encountered while parsing the provided |
| | | * arguments or interacting with the properties file. |
| | | */ |
| | | public void parseArguments(String[] rawArguments, |
| | | String propertiesFile, boolean requirePropertiesFile) |
| | | throws ArgumentException |
| | | void parseArguments(String[] rawArguments, String propertiesFile, |
| | | boolean requirePropertiesFile) throws ArgumentException |
| | | { |
| | | this.rawArguments = rawArguments; |
| | | |
| | |
| | | { |
| | | if (requirePropertiesFile) |
| | | { |
| | | Message message = |
| | | ERR_ARGPARSER_CANNOT_READ_PROPERTIES_FILE.get(String |
| | | .valueOf(propertiesFile), getExceptionMessage(e)); |
| | | Message message = ERR_ARGPARSER_CANNOT_READ_PROPERTIES_FILE |
| | | .get(String.valueOf(propertiesFile), getExceptionMessage(e)); |
| | | throw new ArgumentException(message, e); |
| | | } |
| | | } |
| | |
| | | * associated with this parser accordingly. Default values for |
| | | * unspecified arguments may be read from the specified properties if |
| | | * any are provided. |
| | | * |
| | | * |
| | | * @param rawArguments |
| | | * The set of raw arguments to parse. |
| | | * @param argumentProperties |
| | |
| | | * If a problem was encountered while parsing the provided |
| | | * arguments. |
| | | */ |
| | | public void parseArguments(String[] rawArguments, |
| | | void parseArguments(String[] rawArguments, |
| | | Properties argumentProperties) throws ArgumentException |
| | | { |
| | | this.rawArguments = rawArguments; |
| | |
| | | if ((maxTrailingArguments > 0) |
| | | && (trailingArguments.size() > maxTrailingArguments)) |
| | | { |
| | | Message message = |
| | | ERR_ARGPARSER_TOO_MANY_TRAILING_ARGS |
| | | .get(maxTrailingArguments); |
| | | Message message = ERR_ARGPARSER_TOO_MANY_TRAILING_ARGS |
| | | .get(maxTrailingArguments); |
| | | throw new ArgumentException(message); |
| | | } |
| | | |
| | |
| | | else if (equalPos == 0) |
| | | { |
| | | // The argument starts with "--=", which is not acceptable. |
| | | Message message = |
| | | ERR_ARGPARSER_LONG_ARG_WITHOUT_NAME.get(arg); |
| | | Message message = ERR_ARGPARSER_LONG_ARG_WITHOUT_NAME |
| | | .get(arg); |
| | | throw new ArgumentException(message); |
| | | } |
| | | else |
| | |
| | | else |
| | | { |
| | | // There is no such argument registered. |
| | | Message message = |
| | | ERR_ARGPARSER_NO_ARGUMENT_WITH_LONG_ID.get(origArgName); |
| | | Message message = ERR_ARGPARSER_NO_ARGUMENT_WITH_LONG_ID |
| | | .get(origArgName); |
| | | throw new ArgumentException(message); |
| | | } |
| | | } |
| | |
| | | { |
| | | if ((i + 1) == numArguments) |
| | | { |
| | | Message message = |
| | | ERR_ARGPARSER_NO_VALUE_FOR_ARGUMENT_WITH_LONG_ID |
| | | .get(origArgName); |
| | | Message message = ERR_ARGPARSER_NO_VALUE_FOR_ARGUMENT_WITH_LONG_ID |
| | | .get(origArgName); |
| | | throw new ArgumentException(message); |
| | | } |
| | | |
| | |
| | | MessageBuilder invalidReason = new MessageBuilder(); |
| | | if (!a.valueIsAcceptable(argValue, invalidReason)) |
| | | { |
| | | Message message = |
| | | ERR_ARGPARSER_VALUE_UNACCEPTABLE_FOR_LONG_ID.get( |
| | | argValue, origArgName, invalidReason.toString()); |
| | | Message message = ERR_ARGPARSER_VALUE_UNACCEPTABLE_FOR_LONG_ID |
| | | .get(argValue, origArgName, invalidReason.toString()); |
| | | throw new ArgumentException(message); |
| | | } |
| | | |
| | |
| | | // acceptable to have more than one. |
| | | if (a.hasValue() && (!a.isMultiValued())) |
| | | { |
| | | Message message = |
| | | ERR_ARGPARSER_NOT_MULTIVALUED_FOR_LONG_ID |
| | | .get(origArgName); |
| | | Message message = ERR_ARGPARSER_NOT_MULTIVALUED_FOR_LONG_ID |
| | | .get(origArgName); |
| | | throw new ArgumentException(message); |
| | | } |
| | | |
| | |
| | | { |
| | | if (argValue != null) |
| | | { |
| | | Message message = |
| | | ERR_ARGPARSER_ARG_FOR_LONG_ID_DOESNT_TAKE_VALUE |
| | | .get(origArgName); |
| | | Message message = ERR_ARGPARSER_ARG_FOR_LONG_ID_DOESNT_TAKE_VALUE |
| | | .get(origArgName); |
| | | throw new ArgumentException(message); |
| | | } |
| | | } |
| | |
| | | // -n value |
| | | if (arg.equals("-")) |
| | | { |
| | | Message message = |
| | | ERR_ARGPARSER_INVALID_DASH_AS_ARGUMENT.get(); |
| | | Message message = ERR_ARGPARSER_INVALID_DASH_AS_ARGUMENT |
| | | .get(); |
| | | throw new ArgumentException(message); |
| | | } |
| | | |
| | |
| | | else |
| | | { |
| | | // There is no such argument registered. |
| | | Message message = |
| | | ERR_ARGPARSER_NO_ARGUMENT_WITH_SHORT_ID.get(String |
| | | .valueOf(argCharacter)); |
| | | Message message = ERR_ARGPARSER_NO_ARGUMENT_WITH_SHORT_ID |
| | | .get(String.valueOf(argCharacter)); |
| | | throw new ArgumentException(message); |
| | | } |
| | | } |
| | |
| | | { |
| | | if ((i + 1) == numArguments) |
| | | { |
| | | Message message = |
| | | ERR_ARGPARSER_NO_VALUE_FOR_ARGUMENT_WITH_SHORT_ID |
| | | .get(String.valueOf(argCharacter)); |
| | | Message message = ERR_ARGPARSER_NO_VALUE_FOR_ARGUMENT_WITH_SHORT_ID |
| | | .get(String.valueOf(argCharacter)); |
| | | throw new ArgumentException(message); |
| | | } |
| | | |
| | |
| | | MessageBuilder invalidReason = new MessageBuilder(); |
| | | if (!a.valueIsAcceptable(argValue, invalidReason)) |
| | | { |
| | | Message message = |
| | | ERR_ARGPARSER_VALUE_UNACCEPTABLE_FOR_SHORT_ID.get( |
| | | argValue, String.valueOf(argCharacter), |
| | | Message message = ERR_ARGPARSER_VALUE_UNACCEPTABLE_FOR_SHORT_ID |
| | | .get(argValue, String.valueOf(argCharacter), |
| | | invalidReason.toString()); |
| | | throw new ArgumentException(message); |
| | | } |
| | |
| | | // acceptable to have more than one. |
| | | if (a.hasValue() && (!a.isMultiValued())) |
| | | { |
| | | Message message = |
| | | ERR_ARGPARSER_NOT_MULTIVALUED_FOR_SHORT_ID.get(String |
| | | .valueOf(argCharacter)); |
| | | Message message = ERR_ARGPARSER_NOT_MULTIVALUED_FOR_SHORT_ID |
| | | .get(String.valueOf(argCharacter)); |
| | | throw new ArgumentException(message); |
| | | } |
| | | |
| | |
| | | if (b == null) |
| | | { |
| | | // There is no such argument registered. |
| | | Message message = |
| | | ERR_ARGPARSER_NO_ARGUMENT_WITH_SHORT_ID.get(String |
| | | .valueOf(argCharacter)); |
| | | Message message = ERR_ARGPARSER_NO_ARGUMENT_WITH_SHORT_ID |
| | | .get(String.valueOf(argCharacter)); |
| | | throw new ArgumentException(message); |
| | | } |
| | | else if (b.needsValue()) |
| | |
| | | // 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 |
| | | Message message = ERR_ARGPARSER_CANT_MIX_ARGS_WITH_VALUES |
| | | .get(String.valueOf(argCharacter), argValue, String |
| | | .valueOf(c)); |
| | | throw new ArgumentException(message); |
| | | } |
| | |
| | | // 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); |
| | | Message message = ERR_ARGPARSER_DISALLOWED_TRAILING_ARGUMENT |
| | | .get(arg); |
| | | throw new ArgumentException(message); |
| | | } |
| | | } |
| | |
| | | { |
| | | if (trailingArguments.size() < minTrailingArguments) |
| | | { |
| | | Message message = |
| | | ERR_ARGPARSER_TOO_FEW_TRAILING_ARGUMENTS |
| | | .get(minTrailingArguments); |
| | | Message message = ERR_ARGPARSER_TOO_FEW_TRAILING_ARGUMENTS |
| | | .get(minTrailingArguments); |
| | | throw new ArgumentException(message); |
| | | } |
| | | } |
| | |
| | | if ((argumentProperties != null) |
| | | && (a.getPropertyName() != null)) |
| | | { |
| | | String value = |
| | | argumentProperties.getProperty(a.getPropertyName() |
| | | .toLowerCase()); |
| | | String value = argumentProperties.getProperty(a |
| | | .getPropertyName().toLowerCase()); |
| | | MessageBuilder invalidReason = new MessageBuilder(); |
| | | if (value != null) |
| | | { |
| | |
| | | // a problem. |
| | | if ((!a.hasValue()) && a.isRequired()) |
| | | { |
| | | Message message = |
| | | ERR_ARGPARSER_NO_VALUE_FOR_REQUIRED_ARG.get(a.getName()); |
| | | Message message = ERR_ARGPARSER_NO_VALUE_FOR_REQUIRED_ARG |
| | | .get(a.getName()); |
| | | throw new ArgumentException(message); |
| | | } |
| | | } |
| | |
| | | |
| | | /** |
| | | * Check if we have a properties file. |
| | | * |
| | | * |
| | | * @return The properties found in the properties file or null. |
| | | * @throws ArgumentException |
| | | * If a problem was encountered while parsing the provided |
| | |
| | | { |
| | | // Check in "user home"/.opends directory |
| | | String userDir = System.getProperty("user.home"); |
| | | propertiesFilePath = |
| | | findPropertiesFile(userDir + File.separator |
| | | + DEFAULT_OPENDS_CONFIG_DIR); |
| | | propertiesFilePath = findPropertiesFile(userDir + File.separator |
| | | + DEFAULT_OPENDS_CONFIG_DIR); |
| | | |
| | | // TODO |
| | | /* |
| | |
| | | |
| | | // We have a location for the properties file. |
| | | Properties argumentProperties = new Properties(); |
| | | String scriptName = System.getProperty(PROPERTY_SCRIPT_NAME); |
| | | String scriptName = System.getProperty(Utils.PROPERTY_SCRIPT_NAME); |
| | | try |
| | | { |
| | | Properties p = new Properties(); |
| | |
| | | { |
| | | if (currentPropertyName.startsWith(scriptName)) |
| | | { |
| | | propertyName = |
| | | currentPropertyName.substring(scriptName.length() + 1); |
| | | propertyName = currentPropertyName.substring(scriptName |
| | | .length() + 1); |
| | | } |
| | | else |
| | | { |
| | |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | Message message = |
| | | ERR_ARGPARSER_CANNOT_READ_PROPERTIES_FILE.get(String |
| | | .valueOf(propertiesFilePath), getExceptionMessage(e)); |
| | | Message message = ERR_ARGPARSER_CANNOT_READ_PROPERTIES_FILE.get( |
| | | String.valueOf(propertiesFilePath), getExceptionMessage(e)); |
| | | throw new ArgumentException(message, e); |
| | | } |
| | | return argumentProperties; |
| | |
| | | |
| | | /** |
| | | * Get the absolute path of the properties file. |
| | | * |
| | | * |
| | | * @param directory |
| | | * The location in which we should look for properties file |
| | | * @return The absolute path of the properties file or null |
| | |
| | | private String findPropertiesFile(String directory) |
| | | { |
| | | // Look for the tools properties file |
| | | File f = |
| | | new File(directory, DEFAULT_OPENDS_PROPERTIES_FILE_NAME |
| | | + DEFAULT_OPENDS_PROPERTIES_FILE_EXTENSION); |
| | | File f = new File(directory, DEFAULT_OPENDS_PROPERTIES_FILE_NAME |
| | | + DEFAULT_OPENDS_PROPERTIES_FILE_EXTENSION); |
| | | if (f.exists() && f.canRead()) |
| | | { |
| | | return f.getAbsolutePath(); |
| | |
| | | /** |
| | | * Appends usage information based on the defined arguments to the |
| | | * provided buffer. |
| | | * |
| | | * |
| | | * @param buffer |
| | | * The buffer to which the usage information should be |
| | | * appended. |
| | | */ |
| | | public void getUsage(StringBuilder buffer) |
| | | void getUsage(StringBuilder buffer) |
| | | { |
| | | usageOrVersionDisplayed = true; |
| | | if ((toolDescription != null) && (toolDescription.length() > 0)) |
| | |
| | | /** |
| | | * Retrieves a message containing usage information based on the |
| | | * defined arguments. |
| | | * |
| | | * |
| | | * @return A string containing usage information based on the defined |
| | | * arguments. |
| | | */ |
| | | public Message getUsageMessage() |
| | | Message getUsageMessage() |
| | | { |
| | | StringBuilder buffer = new StringBuilder(); |
| | | getUsage(buffer); |
| | |
| | | /** |
| | | * Retrieves a string containing usage information based on the |
| | | * defined arguments. |
| | | * |
| | | * |
| | | * @return A string containing usage information based on the defined |
| | | * arguments. |
| | | */ |
| | | public String getUsage() |
| | | String getUsage() |
| | | { |
| | | StringBuilder buffer = new StringBuilder(); |
| | | getUsage(buffer); |
| | |
| | | /** |
| | | * Writes usage information based on the defined arguments to the |
| | | * provided output stream. |
| | | * |
| | | * |
| | | * @param outputStream |
| | | * The output stream to which the usage information should be |
| | | * written. |
| | |
| | | * If a problem occurs while attempting to write the usage |
| | | * information to the provided output stream. |
| | | */ |
| | | public void getUsage(OutputStream outputStream) throws IOException |
| | | void getUsage(OutputStream outputStream) throws IOException |
| | | { |
| | | StringBuilder buffer = new StringBuilder(); |
| | | getUsage(buffer); |
| | |
| | | * Indicates whether the version or the usage information has been |
| | | * displayed to the end user either by an explicit argument like "-H" |
| | | * or "--help", or by a built-in argument like "-?". |
| | | * |
| | | * |
| | | * @return {@code true} if the usage information has been displayed, |
| | | * or {@code false} if not. |
| | | */ |
| | | public boolean usageOrVersionDisplayed() |
| | | boolean usageOrVersionDisplayed() |
| | | { |
| | | return usageOrVersionDisplayed; |
| | | } |
| | |
| | | |
| | | /** |
| | | * Appends argument usage information to the provided buffer. |
| | | * |
| | | * |
| | | * @param a |
| | | * The argument to handle. |
| | | * @param buffer |
| | |
| | | newBuffer.append(a.getValuePlaceholder()); |
| | | } |
| | | |
| | | int lineLength = |
| | | (buffer.length() - currentLength) + newBuffer.length(); |
| | | int lineLength = (buffer.length() - currentLength) |
| | | + newBuffer.length(); |
| | | if (lineLength > MAX_LENGTH) |
| | | { |
| | | buffer.append(EOL); |
| | |
| | | /** |
| | | * Given an argument, returns an appropriate group. Arguments may be |
| | | * part of one of the special groups or the default group. |
| | | * |
| | | * |
| | | * @param argument |
| | | * for which a group is requested |
| | | * @return argument group appropriate for <code>argument</code> |
| | |
| | | /** |
| | | * Indicates whether or not argument group description headers should |
| | | * be printed. |
| | | * |
| | | * |
| | | * @return boolean where true means print the descriptions |
| | | */ |
| | | boolean printUsageGroupHeaders() |
| | |
| | | |
| | | try |
| | | { |
| | | versionArgument = |
| | | new BooleanArgument(OPTION_LONG_PRODUCT_VERSION, |
| | | OPTION_SHORT_PRODUCT_VERSION, |
| | | OPTION_LONG_PRODUCT_VERSION, |
| | | INFO_DESCRIPTION_PRODUCT_VERSION.get()); |
| | | versionArgument = new BooleanArgument( |
| | | OPTION_LONG_PRODUCT_VERSION, OPTION_SHORT_PRODUCT_VERSION, |
| | | OPTION_LONG_PRODUCT_VERSION, INFO_DESCRIPTION_PRODUCT_VERSION |
| | | .get()); |
| | | this.generalArgGroup.addArgument(versionArgument); |
| | | } |
| | | catch (ArgumentException e) |
| | |
| | | if (arg != null) |
| | | { |
| | | String longId = arg.getLongIdentifier(); |
| | | io = |
| | | OPTION_LONG_VERBOSE.equals(longId) |
| | | || OPTION_LONG_QUIET.equals(longId) |
| | | || OPTION_LONG_NO_PROMPT.equals(longId) |
| | | || OPTION_LONG_PROP_FILE_PATH.equals(longId) |
| | | || OPTION_LONG_NO_PROP_FILE.equals(longId) |
| | | || OPTION_LONG_SCRIPT_FRIENDLY.equals(longId) |
| | | || OPTION_LONG_DONT_WRAP.equals(longId) |
| | | || OPTION_LONG_ENCODING.equals(longId) |
| | | || OPTION_DSCFG_LONG_DISPLAY_EQUIVALENT.equals(longId) |
| | | || OPTION_LONG_EQUIVALENT_COMMAND_FILE_PATH |
| | | .equals(longId) |
| | | || OPTION_LONG_BATCH_FILE_PATH.equals(longId); |
| | | io = OPTION_LONG_VERBOSE.equals(longId) |
| | | || OPTION_LONG_QUIET.equals(longId) |
| | | || OPTION_LONG_NO_PROMPT.equals(longId) |
| | | || OPTION_LONG_PROP_FILE_PATH.equals(longId) |
| | | || OPTION_LONG_NO_PROP_FILE.equals(longId) |
| | | || OPTION_LONG_SCRIPT_FRIENDLY.equals(longId) |
| | | || OPTION_LONG_DONT_WRAP.equals(longId) |
| | | || OPTION_LONG_ENCODING.equals(longId) |
| | | || OPTION_LONG_BATCH_FILE_PATH.equals(longId); |
| | | } |
| | | return io; |
| | | } |
| | |
| | | if (arg != null) |
| | | { |
| | | String longId = arg.getLongIdentifier(); |
| | | ldap = |
| | | OPTION_LONG_USE_SSL.equals(longId) |
| | | || OPTION_LONG_START_TLS.equals(longId) |
| | | || OPTION_LONG_HOST.equals(longId) |
| | | || OPTION_LONG_PORT.equals(longId) |
| | | || OPTION_LONG_BINDDN.equals(longId) |
| | | || OPTION_LONG_BINDPWD.equals(longId) |
| | | || OPTION_LONG_BINDPWD_FILE.equals(longId) |
| | | || OPTION_LONG_SASLOPTION.equals(longId) |
| | | || OPTION_LONG_TRUSTALL.equals(longId) |
| | | || OPTION_LONG_TRUSTSTOREPATH.equals(longId) |
| | | || OPTION_LONG_TRUSTSTORE_PWD.equals(longId) |
| | | || OPTION_LONG_TRUSTSTORE_PWD_FILE.equals(longId) |
| | | || OPTION_LONG_KEYSTOREPATH.equals(longId) |
| | | || OPTION_LONG_KEYSTORE_PWD.equals(longId) |
| | | || OPTION_LONG_KEYSTORE_PWD_FILE.equals(longId) |
| | | || OPTION_LONG_CERT_NICKNAME.equals(longId) |
| | | || OPTION_LONG_REFERENCED_HOST_NAME.equals(longId) |
| | | || OPTION_LONG_ADMIN_UID.equals(longId) |
| | | || OPTION_LONG_REPORT_AUTHZ_ID.equals(longId) |
| | | || OPTION_LONG_USE_PW_POLICY_CTL.equals(longId) |
| | | || OPTION_LONG_USE_SASL_EXTERNAL.equals(longId) |
| | | || OPTION_LONG_PROTOCOL_VERSION.equals(longId); |
| | | ldap = OPTION_LONG_USE_SSL.equals(longId) |
| | | || OPTION_LONG_START_TLS.equals(longId) |
| | | || OPTION_LONG_HOST.equals(longId) |
| | | || OPTION_LONG_PORT.equals(longId) |
| | | || OPTION_LONG_BINDDN.equals(longId) |
| | | || OPTION_LONG_BINDPWD.equals(longId) |
| | | || OPTION_LONG_BINDPWD_FILE.equals(longId) |
| | | || OPTION_LONG_SASLOPTION.equals(longId) |
| | | || OPTION_LONG_TRUSTALL.equals(longId) |
| | | || OPTION_LONG_TRUSTSTOREPATH.equals(longId) |
| | | || OPTION_LONG_TRUSTSTORE_PWD.equals(longId) |
| | | || OPTION_LONG_TRUSTSTORE_PWD_FILE.equals(longId) |
| | | || OPTION_LONG_KEYSTOREPATH.equals(longId) |
| | | || OPTION_LONG_KEYSTORE_PWD.equals(longId) |
| | | || OPTION_LONG_KEYSTORE_PWD_FILE.equals(longId) |
| | | || OPTION_LONG_CERT_NICKNAME.equals(longId) |
| | | || OPTION_LONG_REFERENCED_HOST_NAME.equals(longId) |
| | | || OPTION_LONG_ADMIN_UID.equals(longId) |
| | | || OPTION_LONG_REPORT_AUTHZ_ID.equals(longId) |
| | | || OPTION_LONG_USE_PW_POLICY_CTL.equals(longId) |
| | | || OPTION_LONG_USE_SASL_EXTERNAL.equals(longId) |
| | | || OPTION_LONG_PROTOCOL_VERSION.equals(longId); |
| | | } |
| | | return ldap; |
| | | } |
| | |
| | | if (arg != null) |
| | | { |
| | | String longId = arg.getLongIdentifier(); |
| | | general = |
| | | OPTION_LONG_HELP.equals(longId) |
| | | || OPTION_LONG_PRODUCT_VERSION.equals(longId); |
| | | general = OPTION_LONG_HELP.equals(longId) |
| | | || OPTION_LONG_PRODUCT_VERSION.equals(longId); |
| | | } |
| | | return general; |
| | | } |
| | |
| | | /** |
| | | * Returns whether the usage argument was provided or not. This method |
| | | * should be called after a call to parseArguments. |
| | | * |
| | | * |
| | | * @return <CODE>true</CODE> if the usage argument was provided and |
| | | * <CODE>false</CODE> otherwise. |
| | | */ |
| | | public boolean isUsageArgumentPresent() |
| | | boolean isUsageArgumentPresent() |
| | | { |
| | | boolean isUsageArgumentPresent = false; |
| | | if (usageArgument != null) |
| | |
| | | /** |
| | | * Returns whether the version argument was provided or not. This |
| | | * method should be called after a call to parseArguments. |
| | | * |
| | | * |
| | | * @return <CODE>true</CODE> if the version argument was provided and |
| | | * <CODE>false</CODE> otherwise. |
| | | */ |
| | | public boolean isVersionArgumentPresent() |
| | | boolean isVersionArgumentPresent() |
| | | { |
| | | return versionPresent; |
| | | } |