| | |
| | | // the program with which this argument parser is associated. |
| | | private String mainClassName; |
| | | |
| | | // A human-readable description for the tool, which will be included when |
| | | // displaying usage information. |
| | | private String toolDescription; |
| | | |
| | | // The display name that will be used for the trailing arguments in the usage |
| | | // information. |
| | | private String trailingArgsDisplayName; |
| | |
| | | * class that should be invoked to launch |
| | | * the program with which this argument |
| | | * parser is associated. |
| | | * @param toolDescription A human-readable description for the |
| | | * tool, which will be included when |
| | | * displaying usage information. |
| | | * @param longArgumentsCaseSensitive Indicates whether long arguments should |
| | | * be treated in a case-sensitive manner. |
| | | */ |
| | | public ArgumentParser(String mainClassName, |
| | | public ArgumentParser(String mainClassName, String toolDescription, |
| | | boolean longArgumentsCaseSensitive) |
| | | { |
| | | this.mainClassName = mainClassName; |
| | | this.toolDescription = toolDescription; |
| | | this.longArgumentsCaseSensitive = longArgumentsCaseSensitive; |
| | | |
| | | argumentList = new LinkedList<Argument>(); |
| | |
| | | * class that should be invoked to launch |
| | | * the program with which this argument |
| | | * parser is associated. |
| | | * @param toolDescription A human-readable description for the |
| | | * tool, which will be included when |
| | | * displaying usage information. |
| | | * @param longArgumentsCaseSensitive Indicates whether long arguments should |
| | | * be treated in a case-sensitive manner. |
| | | * @param allowsTrailingArguments Indicates whether this parser allows |
| | |
| | | * arguments in the generated usage |
| | | * information. |
| | | */ |
| | | public ArgumentParser(String mainClassName, |
| | | public ArgumentParser(String mainClassName, String toolDescription, |
| | | boolean longArgumentsCaseSensitive, |
| | | boolean allowsTrailingArguments, |
| | | int minTrailingArguments, int maxTrailingArguments, |
| | | String trailingArgsDisplayName) |
| | | { |
| | | this.mainClassName = mainClassName; |
| | | this.toolDescription = toolDescription; |
| | | this.longArgumentsCaseSensitive = longArgumentsCaseSensitive; |
| | | this.allowsTrailingArguments = allowsTrailingArguments; |
| | | this.minTrailingArguments = minTrailingArguments; |
| | |
| | | |
| | | |
| | | /** |
| | | * 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 String getToolDescription() |
| | | { |
| | | return toolDescription; |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 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 the |
| | |
| | | */ |
| | | public void getUsage(StringBuilder buffer) |
| | | { |
| | | buffer.append("Usage: java "); |
| | | buffer.append(mainClassName); |
| | | if ((toolDescription != null) && (toolDescription.length() > 0)) |
| | | { |
| | | buffer.append(wrapText(toolDescription, 79)); |
| | | buffer.append(EOL); |
| | | } |
| | | |
| | | String scriptName = System.getProperty(PROPERTY_SCRIPT_NAME); |
| | | if ((scriptName == null) || (scriptName.length() == 0)) |
| | | { |
| | | buffer.append("Usage: java "); |
| | | buffer.append(mainClassName); |
| | | } |
| | | else |
| | | { |
| | | buffer.append("Usage: "); |
| | | buffer.append(scriptName); |
| | | } |
| | | |
| | | buffer.append(" {options}"); |
| | | |
| | | if (allowsTrailingArguments) |
| | |
| | | // 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(); |
| | | if (description.length() <= 74) |
| | | if (description.length() <= 75) |
| | | { |
| | | buffer.append(" "); |
| | | buffer.append(" "); |
| | | buffer.append(description); |
| | | buffer.append(EOL); |
| | | } |
| | | else |
| | | { |
| | | String s = description; |
| | | while (s.length() > 74) |
| | | while (s.length() > 75) |
| | | { |
| | | int spacePos = s.lastIndexOf(' ', 74); |
| | | int spacePos = s.lastIndexOf(' ', 75); |
| | | if (spacePos > 0) |
| | | { |
| | | buffer.append(" "); |
| | | buffer.append(" "); |
| | | buffer.append(s.substring(0, spacePos).trim()); |
| | | s = s.substring(spacePos+1).trim(); |
| | | buffer.append(EOL); |
| | |
| | | spacePos = s.indexOf(' '); |
| | | if (spacePos > 0) |
| | | { |
| | | buffer.append(" "); |
| | | buffer.append(" "); |
| | | buffer.append(s.substring(0, spacePos).trim()); |
| | | s = s.substring(spacePos+1).trim(); |
| | | buffer.append(EOL); |
| | | } |
| | | else |
| | | { |
| | | buffer.append(" "); |
| | | buffer.append(" "); |
| | | buffer.append(s); |
| | | s = ""; |
| | | buffer.append(EOL); |
| | |
| | | |
| | | if (s.length() > 0) |
| | | { |
| | | buffer.append(" "); |
| | | buffer.append(" "); |
| | | buffer.append(s); |
| | | buffer.append(EOL); |
| | | } |
| | | } |
| | | |
| | | buffer.append(EOL); |
| | | } |
| | | } |
| | | |