| | |
| | | private String[] rawArguments; |
| | | |
| | | /** Set of argument groups. */ |
| | | protected Set<ArgumentGroup> argumentGroups; |
| | | protected final Set<ArgumentGroup> argumentGroups = new TreeSet<ArgumentGroup>(); |
| | | |
| | | /** |
| | | * Group for arguments that have not been explicitly grouped. These will |
| | | * appear at the top of the usage statement without a header. |
| | | */ |
| | | private final ArgumentGroup defaultArgGroup = new ArgumentGroup(LocalizableMessage.EMPTY, |
| | | Integer.MAX_VALUE); |
| | | private final ArgumentGroup defaultArgGroup = new ArgumentGroup( |
| | | LocalizableMessage.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 final ArgumentGroup ldapArgGroup = new ArgumentGroup( |
| | | final ArgumentGroup ldapArgGroup = new ArgumentGroup( |
| | | INFO_DESCRIPTION_LDAP_CONNECTION_ARGS.get(), Integer.MIN_VALUE + 2); |
| | | |
| | | /** |
| | |
| | | * properties file, no-prompt etc. These will appear toward the bottom of |
| | | * the usage statement. |
| | | */ |
| | | protected final ArgumentGroup ioArgGroup = new ArgumentGroup(INFO_DESCRIPTION_IO_ARGS.get(), |
| | | Integer.MIN_VALUE + 1); |
| | | private final 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 final ArgumentGroup generalArgGroup = new ArgumentGroup(INFO_DESCRIPTION_GENERAL_ARGS |
| | | .get(), Integer.MIN_VALUE); |
| | | private final ArgumentGroup generalArgGroup = new ArgumentGroup( |
| | | INFO_DESCRIPTION_GENERAL_ARGS.get(), Integer.MIN_VALUE); |
| | | |
| | | private static final String INDENT = " "; |
| | | |
| | |
| | | * has already been defined. |
| | | */ |
| | | public void addArgument(final Argument argument) throws ArgumentException { |
| | | addArgument(argument, null); |
| | | } |
| | | |
| | | /** |
| | | * Adds the provided argument to the set of arguments handled by this |
| | | * parser. |
| | | * |
| | | * @param argument |
| | | * The argument to be added. |
| | | * @param group |
| | | * The argument group to which the argument belongs. |
| | | * @throws ArgumentException |
| | | * If the provided argument conflicts with another argument that |
| | | * has already been defined. |
| | | */ |
| | | public void addArgument(final Argument argument, ArgumentGroup group) throws ArgumentException { |
| | | final Character shortID = argument.getShortIdentifier(); |
| | | if (shortID != null && shortIDMap.containsKey(shortID)) { |
| | | final String conflictingName = shortIDMap.get(shortID).getName(); |
| | |
| | | |
| | | argumentList.add(argument); |
| | | |
| | | if (group == null) { |
| | | group = getStandardGroup(argument); |
| | | } |
| | | final ArgumentGroup group = getStandardGroup(argument); |
| | | group.addArgument(argument); |
| | | argumentGroups.add(group); |
| | | } |
| | |
| | | } |
| | | |
| | | /** |
| | | * Adds the provided argument to the set of arguments handled by this parser |
| | | * and puts the argument 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. |
| | | */ |
| | | protected void addDefaultArgument(final 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 general group. |
| | | * |
| | | * @param argument |
| | | * The argument to be added. |
| | | * @throws ArgumentException |
| | | * If the provided argument conflicts with another argument that |
| | | * has already been defined. |
| | | */ |
| | | void addGeneralArgument(final Argument argument) throws ArgumentException { |
| | | addArgument(argument, generalArgGroup); |
| | | } |
| | | |
| | | /** |
| | | * 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(final Argument argument) throws ArgumentException { |
| | | addArgument(argument, ioArgGroup); |
| | | } |
| | | |
| | | /** |
| | | * 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(final Argument argument) throws ArgumentException { |
| | | addArgument(argument, ldapArgGroup); |
| | | } |
| | | |
| | | /** |
| | | * Indicates whether this parser will allow unnamed trailing arguments. |
| | | * These will be arguments at the end of the list that are not preceded by |
| | | * either a long or short identifier and will need to be manually parsed by |
| | |
| | | } |
| | | |
| | | // Help argument should be printed at the end |
| | | if (usageArgument != null && usageArgument.getName().equals(a.getName())) { |
| | | if (isUsageArgument(a)) { |
| | | helpArgument = a; |
| | | continue; |
| | | } |
| | |
| | | } |
| | | |
| | | /** |
| | | * Returns whether the provided argument is the usage argument. |
| | | * |
| | | * @param a the argument to test |
| | | * @return true if the provided argument is the usage argument, false otherwise |
| | | */ |
| | | boolean isUsageArgument(final Argument a) { |
| | | return usageArgument != null && usageArgument.getName().equals(a.getName()); |
| | | } |
| | | |
| | | /** |
| | | * Retrieves a message containing usage information based on the defined |
| | | * arguments. |
| | | * |
| | |
| | | // token. |
| | | } else if (equalPos == 0) { |
| | | // The argument starts with "--=", which is not acceptable. |
| | | final LocalizableMessage message = ERR_ARGPARSER_LONG_ARG_WITHOUT_NAME.get(arg); |
| | | throw new ArgumentException(message); |
| | | throw new ArgumentException(ERR_ARGPARSER_LONG_ARG_WITHOUT_NAME.get(arg)); |
| | | } else { |
| | | // The argument is in the form --name=value, so parse them |
| | | // both out. |
| | |
| | | writeToUsageOutputStream(getUsage()); |
| | | return; |
| | | } else if (versionHandler != null && OPTION_LONG_PRODUCT_VERSION.equals(argName)) { |
| | | // "--version" will always be interpreted as requesting |
| | | // version information. |
| | | // "--version" will always be interpreted as requesting version information. |
| | | printVersion(); |
| | | return; |
| | | } else { |
| | |
| | | |
| | | // If this is the usage argument, then immediately stop and |
| | | // print usage information. |
| | | if (usageArgument != null && usageArgument.getName().equals(a.getName())) { |
| | | if (isUsageArgument(a)) { |
| | | writeToUsageOutputStream(getUsage()); |
| | | return; |
| | | } |
| | |
| | | |
| | | // If this is the usage argument, then immediately stop and |
| | | // print usage information. |
| | | if (usageArgument != null && usageArgument.getName().equals(a.getName())) { |
| | | if (isUsageArgument(a)) { |
| | | writeToUsageOutputStream(getUsage()); |
| | | return; |
| | | } |
| | |
| | | |
| | | // If this is the usage argument, |
| | | // then immediately stop and print usage information. |
| | | if (usageArgument != null && usageArgument.getName().equals(b.getName())) { |
| | | if (isUsageArgument(b)) { |
| | | writeToUsageOutputStream(getUsage()); |
| | | return; |
| | | } |
| | |
| | | } |
| | | |
| | | private void initGroups() { |
| | | this.argumentGroups = new TreeSet<ArgumentGroup>(); |
| | | this.argumentGroups.add(defaultArgGroup); |
| | | this.argumentGroups.add(ldapArgGroup); |
| | | this.argumentGroups.add(generalArgGroup); |
| | |
| | | * The buffer to which the usage information should be appended. |
| | | */ |
| | | private void printArgumentUsage(final Argument a, final StringBuilder buffer) { |
| | | // Write a line with the short and/or long identifiers that may be |
| | | // used for the argument. |
| | | printLineForShortLongArgument(a, buffer); |
| | | |
| | | // Write one or more lines with the description of the argument. |
| | | // We will 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. |
| | | final int indentLength = INDENT.length(); |
| | | buffer.append(wrapText(a.getDescription(), MAX_LINE_WIDTH, indentLength)); |
| | | buffer.append(EOL); |
| | | |
| | | if (a.needsValue() && a.getDefaultValue() != null && a.getDefaultValue().length() > 0) { |
| | | buffer.append(INDENT); |
| | | buffer.append(INFO_ARGPARSER_USAGE_DEFAULT_VALUE.get(a.getDefaultValue())); |
| | | buffer.append(EOL); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * Appends a line with the short and/or long identifiers that may be used for the argument to the provided string |
| | | * builder. |
| | | * |
| | | * @param a the argument for which to print a line |
| | | * @param buffer the string builder where to append the line |
| | | */ |
| | | void printLineForShortLongArgument(final Argument a, final StringBuilder buffer) { |
| | | final Character shortID = a.getShortIdentifier(); |
| | | final String longID = a.getLongIdentifier(); |
| | | if (shortID != null) { |
| | | final int currentLength = buffer.length(); |
| | | |
| | | if (usageArgument.getName().equals(a.getName())) { |
| | | if (isUsageArgument(a)) { |
| | | buffer.append("-?, "); |
| | | } |
| | | |
| | |
| | | |
| | | buffer.append(EOL); |
| | | } else if (longID != null) { |
| | | if (usageArgument.getName().equals(a.getName())) { |
| | | if (isUsageArgument(a)) { |
| | | buffer.append("-?, "); |
| | | } |
| | | buffer.append("--"); |
| | |
| | | |
| | | buffer.append(EOL); |
| | | } |
| | | |
| | | // Write one or more lines with the description of the argument. |
| | | // We will 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. |
| | | buffer.append(wrapText(a.getDescription(), MAX_LINE_WIDTH, indentLength)); |
| | | buffer.append(EOL); |
| | | |
| | | if (a.needsValue() && a.getDefaultValue() != null && a.getDefaultValue().length() > 0) { |
| | | buffer.append(INDENT); |
| | | buffer.append(INFO_ARGPARSER_USAGE_DEFAULT_VALUE.get(a.getDefaultValue())); |
| | | buffer.append(EOL); |
| | | } |
| | | } |
| | | |
| | | void normalizeArguments(final Properties argumentProperties, final List<Argument> arguments) |
| | | throws ArgumentException { |
| | | for (final Argument a : arguments) { |
| | | if (!a.isPresent() |
| | | // See if there is a value in the properties that can be used |
| | | // See if there is a value in the properties that can be used |
| | | && argumentProperties != null && a.getPropertyName() != null) { |
| | | final String value = argumentProperties.getProperty(a.getPropertyName().toLowerCase()); |
| | | final LocalizableMessageBuilder invalidReason = new LocalizableMessageBuilder(); |
| | | if (value != null) { |
| | | Boolean addValue = true; |
| | | if (!(a instanceof BooleanArgument)) { |
| | | addValue = a.valueIsAcceptable(value, invalidReason); |
| | | } |
| | | boolean addValue = !(a instanceof BooleanArgument) |
| | | && a.valueIsAcceptable(value, invalidReason); |
| | | if (addValue) { |
| | | a.addValue(value); |
| | | if (a.needsValue()) { |