| | |
| | | // 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 raw set of command-line arguments that were provided. |
| | | private String[] rawArguments; |
| | | |
| | |
| | | * 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 subcommand and long |
| | | * argument names should be treated in a |
| | | * case-sensitive manner. |
| | | */ |
| | | public SubCommandArgumentParser(String mainClassName, |
| | | public SubCommandArgumentParser(String mainClassName, String toolDescription, |
| | | boolean longArgumentsCaseSensitive) |
| | | { |
| | | this.mainClassName = mainClassName; |
| | | this.toolDescription = toolDescription; |
| | | this.longArgumentsCaseSensitive = longArgumentsCaseSensitive; |
| | | |
| | | globalArgumentList = new LinkedList<Argument>(); |
| | |
| | | |
| | | |
| | | /** |
| | | * 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 subcommand names and long argument strings should be |
| | | * treated in a case-sensitive manner. |
| | | * |
| | |
| | | */ |
| | | public void getFullUsage(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(" {subcommand} {options}"); |
| | | buffer.append(EOL); |
| | | buffer.append(EOL); |
| | |
| | | buffer.append(value); |
| | | } |
| | | } |
| | | |
| | | buffer.append(EOL); |
| | | } |
| | | |
| | | buffer.append(EOL); |
| | |
| | | |
| | | buffer.append(EOL); |
| | | } |
| | | |
| | | buffer.append(EOL); |
| | | } |
| | | } |
| | | |
| | |
| | | */ |
| | | public void getSubCommandUsage(StringBuilder buffer, SubCommand subCommand) |
| | | { |
| | | buffer.append("Usage: java "); |
| | | buffer.append(mainClassName); |
| | | 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(" "); |
| | | buffer.append(subCommand.getName()); |
| | | buffer.append(" {options}"); |
| | |
| | | // 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); |
| | | } |
| | | } |
| | | |
| | | buffer.append(EOL); |
| | | } |
| | | } |
| | | |