001: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
002: // Released under the terms of the GNU General Public License version 2 or later.
003: package fitnesse.components;
004:
005: import java.util.*;
006: import java.util.regex.*;
007:
008: public class CommandLine extends Option {
009: private static Pattern optionPattern = Pattern
010: .compile("\\[-(\\w+)((?: \\w+)*)\\]");
011:
012: private Map possibleOptions = new HashMap();
013:
014: public CommandLine(String optionDescriptor) {
015: int optionEndIndex = 0;
016: Matcher matcher = optionPattern.matcher(optionDescriptor);
017: while (matcher.find()) {
018: Option option = new Option();
019: option.parseArgumentDescriptor(matcher.group(2));
020: possibleOptions.put(matcher.group(1), option);
021: optionEndIndex = matcher.end();
022: }
023:
024: String remainder = optionDescriptor.substring(optionEndIndex);
025: parseArgumentDescriptor(remainder);
026: }
027:
028: public boolean parse(String[] args) {
029: boolean successfulParse = true;
030: Option currentOption = this ;
031: for (int i = 0; successfulParse && i < args.length; i++) {
032: String arg = args[i];
033:
034: if (currentOption != this
035: && !currentOption.needsMoreArguments())
036: currentOption = this ;
037:
038: if (arg.startsWith("-")) {
039: if (currentOption.needsMoreArguments()
040: && currentOption != this )
041: successfulParse = false;
042: else {
043: String argName = arg.substring(1);
044: currentOption = (Option) possibleOptions
045: .get(argName);
046: if (currentOption != null)
047: currentOption.active = true;
048: else
049: successfulParse = false;
050: }
051: } else if (currentOption.needsMoreArguments())
052: currentOption.addArgument(arg);
053: else
054: // too many args
055: successfulParse = false;
056: }
057: if (successfulParse && currentOption.needsMoreArguments())
058: successfulParse = false;
059: return successfulParse;
060: }
061:
062: public boolean hasOption(String optionName) {
063: Option option = (Option) possibleOptions.get(optionName);
064: if (option == null)
065: return false;
066:
067: return option.active;
068: }
069:
070: public String getOptionArgument(String optionName, String argName) {
071: Option option = (Option) possibleOptions.get(optionName);
072: if (option == null)
073: return null;
074: else
075: return option.getArgument(argName);
076: }
077: }
078:
079: class Option {
080: public boolean active = false;
081:
082: protected String[] argumentNames;
083:
084: protected String[] argumentValues;
085:
086: protected int argumentIndex = 0;
087:
088: protected void parseArgumentDescriptor(String arguments) {
089: String[] tokens = split(arguments);
090: argumentNames = tokens;
091: argumentValues = new String[argumentNames.length];
092: }
093:
094: public String getArgument(String argName) {
095: for (int i = 0; i < argumentNames.length; i++) {
096: String requiredArgumentName = argumentNames[i];
097: if (requiredArgumentName.equals(argName))
098: return argumentValues[i];
099: }
100: return null;
101: }
102:
103: public boolean needsMoreArguments() {
104: return argumentIndex < argumentNames.length;
105: }
106:
107: public void addArgument(String value) {
108: argumentValues[argumentIndex++] = value;
109: }
110:
111: protected String[] split(String value) {
112: String[] tokens = value.split(" ");
113: List usableTokens = new LinkedList();
114: for (int i = 0; i < tokens.length; i++) {
115: String token = tokens[i];
116: if (token.length() > 0)
117: usableTokens.add(token);
118: }
119: return (String[]) usableTokens.toArray(new String[] {});
120: }
121: }
|