001: /*
002: * Distributed as part of debuggen v.0.1.0
003: *
004: * Copyright (C) 2005 Machinery For Change, Inc.
005: *
006: * Author: Steve Waldman <swaldman@mchange.com>
007: *
008: * This library is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU Lesser General Public License version 2.1, as
010: * published by the Free Software Foundation.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public License
018: * along with this software; see the file LICENSE. If not, write to the
019: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: */
022:
023: package com.mchange.v2.cmdline;
024:
025: import java.util.*;
026:
027: class ParsedCommandLineImpl implements ParsedCommandLine {
028: String[] argv;
029: String switchPrefix;
030:
031: String[] unswitchedArgs;
032:
033: //we are relying upon the fact that
034: //HashMaps are null-accepting collections
035: HashMap foundSwitches = new HashMap();
036:
037: ParsedCommandLineImpl(String[] argv, String switchPrefix,
038: String[] validSwitches, String[] requiredSwitches,
039: String[] argSwitches) throws BadCommandLineException {
040: this .argv = argv;
041: this .switchPrefix = switchPrefix;
042:
043: List unswitchedArgsList = new LinkedList();
044: int sp_len = switchPrefix.length();
045:
046: for (int i = 0; i < argv.length; ++i) {
047: if (argv[i].startsWith(switchPrefix)) //okay, this is a switch
048: {
049: String sw = argv[i].substring(sp_len);
050: String arg = null;
051:
052: int eq = sw.indexOf('=');
053: if (eq >= 0) //equals convention
054: {
055: arg = sw.substring(eq + 1);
056: sw = sw.substring(0, eq);
057: } else if (contains(sw, argSwitches)) //we expect an argument, next arg convention
058: {
059: if (i < argv.length - 1
060: && !argv[i + 1].startsWith(switchPrefix))
061: arg = argv[++i];
062: }
063:
064: if (validSwitches != null
065: && !contains(sw, validSwitches))
066: throw new UnexpectedSwitchException(
067: "Unexpected Switch: " + sw, sw);
068: if (argSwitches != null && arg != null
069: && !contains(sw, argSwitches))
070: throw new UnexpectedSwitchArgumentException(
071: "Switch \"" + sw + "\" should not have an "
072: + "argument. Argument \"" + arg
073: + "\" found.", sw, arg);
074: foundSwitches.put(sw, arg);
075: } else
076: unswitchedArgsList.add(argv[i]);
077: }
078:
079: if (requiredSwitches != null) {
080: for (int i = 0; i < requiredSwitches.length; ++i)
081: if (!foundSwitches.containsKey(requiredSwitches[i]))
082: throw new MissingSwitchException(
083: "Required switch \"" + requiredSwitches[i]
084: + "\" not found.",
085: requiredSwitches[i]);
086: }
087:
088: unswitchedArgs = new String[unswitchedArgsList.size()];
089: unswitchedArgsList.toArray(unswitchedArgs);
090: }
091:
092: public String getSwitchPrefix() {
093: return switchPrefix;
094: }
095:
096: public String[] getRawArgs() {
097: return (String[]) argv.clone();
098: }
099:
100: public boolean includesSwitch(String sw) {
101: return foundSwitches.containsKey(sw);
102: }
103:
104: public String getSwitchArg(String sw) {
105: return (String) foundSwitches.get(sw);
106: }
107:
108: public String[] getUnswitchedArgs() {
109: return (String[]) unswitchedArgs.clone();
110: }
111:
112: private static boolean contains(String string, String[] list) {
113: for (int i = list.length; --i >= 0;)
114: if (list[i].equals(string))
115: return true;
116: return false;
117: }
118:
119: }
|