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: private Map possibleOptions = new HashMap();
012:
013: public CommandLine(String optionDescriptor) {
014: int optionEndIndex = 0;
015: Matcher matcher = optionPattern.matcher(optionDescriptor);
016: while (matcher.find()) {
017: Option option = new Option();
018: option.parseArgumentDescriptor(matcher.group(2));
019: possibleOptions.put(matcher.group(1), option);
020: optionEndIndex = matcher.end();
021: }
022:
023: String remainder = optionDescriptor.substring(optionEndIndex);
024: parseArgumentDescriptor(remainder);
025: }
026:
027: public boolean parse(String[] args) {
028: boolean successfulParse = true;
029: Option currentOption = this ;
030: for (int i = 0; successfulParse && i < args.length; i++) {
031: String arg = args[i];
032:
033: if (currentOption != this
034: && !currentOption.needsMoreArguments())
035: currentOption = this ;
036:
037: if (arg.startsWith("-")) {
038: if (currentOption.needsMoreArguments()
039: && currentOption != this )
040: successfulParse = false;
041: else {
042: String argName = arg.substring(1);
043: currentOption = (Option) possibleOptions
044: .get(argName);
045: if (currentOption != null)
046: currentOption.active = true;
047: else
048: successfulParse = false;
049: }
050: } else if (currentOption.needsMoreArguments())
051: currentOption.addArgument(arg);
052: else
053: // too many args
054: successfulParse = false;
055: }
056: if (successfulParse && currentOption.needsMoreArguments())
057: successfulParse = false;
058: return successfulParse;
059: }
060:
061: public boolean hasOption(String optionName) {
062: Option option = (Option) possibleOptions.get(optionName);
063: if (option == null)
064: return false;
065:
066: return option.active;
067: }
068:
069: public String getOptionArgument(String optionName, String argName) {
070: Option option = (Option) possibleOptions.get(optionName);
071: if (option == null)
072: return null;
073: else
074: return option.getArgument(argName);
075: }
076: }
077:
078: class Option {
079: public boolean active = false;
080: protected String[] argumentNames;
081: protected String[] argumentValues;
082: protected int argumentIndex = 0;
083:
084: protected void parseArgumentDescriptor(String arguments) {
085: String[] tokens = split(arguments);
086: argumentNames = tokens;
087: argumentValues = new String[argumentNames.length];
088: }
089:
090: public String getArgument(String argName) {
091: for (int i = 0; i < argumentNames.length; i++) {
092: String requiredArgumentName = argumentNames[i];
093: if (requiredArgumentName.equals(argName))
094: return argumentValues[i];
095: }
096: return null;
097: }
098:
099: public boolean needsMoreArguments() {
100: return argumentIndex < argumentNames.length;
101: }
102:
103: public void addArgument(String value) {
104: argumentValues[argumentIndex++] = value;
105: }
106:
107: protected String[] split(String value) {
108: String[] tokens = value.split(" ");
109: List usableTokens = new LinkedList();
110: for (int i = 0; i < tokens.length; i++) {
111: String token = tokens[i];
112: if (token.length() > 0)
113: usableTokens.add(token);
114: }
115: return (String[]) usableTokens.toArray(new String[] {});
116: }
117: }
|