| | |
| | | // manner. |
| | | private boolean longArgumentsCaseSensitive; |
| | | |
| | | // Indicates whether the usage information has been displayed. |
| | | private boolean usageDisplayed; |
| | | |
| | | // The set of arguments defined for this parser, referenced by short ID. |
| | | private HashMap<Character,Argument> shortIDMap; |
| | | |
| | |
| | | shortIDMap = new HashMap<Character,Argument>(); |
| | | longIDMap = new HashMap<String,Argument>(); |
| | | allowsTrailingArguments = false; |
| | | usageDisplayed = false; |
| | | trailingArgsDisplayName = null; |
| | | maxTrailingArguments = 0; |
| | | minTrailingArguments = 0; |
| | |
| | | shortIDMap = new HashMap<Character,Argument>(); |
| | | longIDMap = new HashMap<String,Argument>(); |
| | | trailingArguments = new ArrayList<String>(); |
| | | usageDisplayed = false; |
| | | rawArguments = null; |
| | | usageArgument = null; |
| | | usageOutputStream = System.out; |
| | |
| | | Argument a = longIDMap.get(argName); |
| | | if (a == null) |
| | | { |
| | | // 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); |
| | | if (argName.equals("help")) |
| | | { |
| | | // "--help" will always be interpreted as requesting usage |
| | | // information. |
| | | try |
| | | { |
| | | getUsage(usageOutputStream); |
| | | } catch (Exception e) {} |
| | | |
| | | return; |
| | | } |
| | | 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); |
| | | } |
| | | } |
| | | else |
| | | { |
| | |
| | | } |
| | | } |
| | | } |
| | | else if (arg.startsWith("-")) |
| | | else if (arg.startsWith("-") || arg.startsWith("/")) |
| | | { |
| | | // This indicates that we are using the 1-character name to reference |
| | | // the argument. It may be in any of the following forms: |
| | | // -n |
| | | // -nvalue |
| | | // -n value |
| | | if (arg.equals("-")) |
| | | if (arg.equals("-") || arg.equals("/")) |
| | | { |
| | | int msgID = MSGID_ARGPARSER_INVALID_DASH_AS_ARGUMENT; |
| | | String message = getMessage(msgID); |
| | |
| | | Argument a = shortIDMap.get(argCharacter); |
| | | if (a == 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); |
| | | if (argCharacter == '?') |
| | | { |
| | | // "-?" will always be interpreted as requesting usage information. |
| | | try |
| | | { |
| | | getUsage(usageOutputStream); |
| | | } catch (Exception e) {} |
| | | |
| | | return; |
| | | } |
| | | 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); |
| | | } |
| | | } |
| | | else |
| | | { |
| | |
| | | */ |
| | | public void getUsage(StringBuilder buffer) |
| | | { |
| | | usageDisplayed = true; |
| | | if ((toolDescription != null) && (toolDescription.length() > 0)) |
| | | { |
| | | buffer.append(wrapText(toolDescription, 79)); |
| | |
| | | |
| | | outputStream.write(getBytes(buffer.toString())); |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * Indicates whether 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 usageDisplayed() |
| | | { |
| | | return usageDisplayed; |
| | | } |
| | | } |
| | | |