2011-01-29 10:05:22 +00:00
|
|
|
// $Id$
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2011-02-18 06:53:44 +00:00
|
|
|
package com.sk89q.minecraft.util.commands;
|
2011-01-29 10:05:22 +00:00
|
|
|
|
2011-08-26 15:28:37 +00:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.HashMap;
|
2011-01-29 19:36:28 +00:00
|
|
|
import java.util.HashSet;
|
2011-08-26 15:28:37 +00:00
|
|
|
import java.util.Map;
|
2011-01-29 19:36:28 +00:00
|
|
|
import java.util.Set;
|
|
|
|
|
2011-01-29 10:05:22 +00:00
|
|
|
public class CommandContext {
|
2011-08-26 15:28:37 +00:00
|
|
|
protected final String[] args;
|
|
|
|
protected final Set<Character> booleanFlags = new HashSet<Character>();
|
|
|
|
protected final Map<Character, String> valueFlags = new HashMap<Character, String>();
|
|
|
|
|
2011-01-29 10:05:22 +00:00
|
|
|
public CommandContext(String args) {
|
2011-01-29 19:36:28 +00:00
|
|
|
this(args.split(" "));
|
2011-01-29 10:05:22 +00:00
|
|
|
}
|
2011-08-26 15:28:37 +00:00
|
|
|
|
2011-01-29 10:05:22 +00:00
|
|
|
public CommandContext(String[] args) {
|
2011-01-29 19:36:28 +00:00
|
|
|
int i = 1;
|
2011-07-15 07:00:48 +00:00
|
|
|
for (; i < args.length; ++i) {
|
2011-04-24 06:59:42 +00:00
|
|
|
if (args[i].length() == 0) {
|
|
|
|
// Ignore this
|
|
|
|
} else if (args[i].charAt(0) == '-' && args[i].matches("^-[a-zA-Z]+$")) {
|
2011-07-15 07:00:48 +00:00
|
|
|
for (int k = 1; k < args[i].length(); ++k) {
|
2011-08-26 15:28:37 +00:00
|
|
|
booleanFlags.add(args[i].charAt(k));
|
2011-01-29 19:36:28 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-08-26 15:28:37 +00:00
|
|
|
|
2011-01-29 19:36:28 +00:00
|
|
|
String[] newArgs = new String[args.length - i + 1];
|
2011-08-26 15:28:37 +00:00
|
|
|
|
2011-01-29 19:36:28 +00:00
|
|
|
System.arraycopy(args, i, newArgs, 1, args.length - i);
|
|
|
|
newArgs[0] = args[0];
|
2011-08-26 15:28:37 +00:00
|
|
|
|
2011-01-29 20:00:36 +00:00
|
|
|
this.args = newArgs;
|
2011-01-29 10:05:22 +00:00
|
|
|
}
|
2011-08-26 15:28:37 +00:00
|
|
|
|
|
|
|
public CommandContext(String[] args, Set<Character> isValueFlag) throws CommandException {
|
|
|
|
int nextArg = 0;
|
|
|
|
|
|
|
|
booleanFlags.clear();
|
|
|
|
valueFlags.clear();
|
|
|
|
|
|
|
|
while (nextArg < args.length) {
|
|
|
|
// Fetch argument
|
|
|
|
String arg = args[nextArg++];
|
|
|
|
|
|
|
|
// Empty argument? (multiple consecutive spaces)
|
|
|
|
if (arg.isEmpty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// No more flags?
|
|
|
|
if (arg.charAt(0) != '-' || arg.length() == 1) {
|
|
|
|
--nextArg;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle flag parsing terminator --
|
|
|
|
if (arg.equals("--"))
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Go through the flags
|
|
|
|
for (int i = 1; i < arg.length(); ++i) {
|
|
|
|
char flagName = arg.charAt(i);
|
|
|
|
|
|
|
|
if (isValueFlag.contains(flagName)) {
|
|
|
|
// Skip empty arguments...
|
|
|
|
while (nextArg < args.length && args[nextArg].isEmpty())
|
|
|
|
++nextArg;
|
|
|
|
|
|
|
|
if (nextArg >= args.length)
|
|
|
|
throw new CommandException("No value specified for "+flagName+" flag.");
|
|
|
|
|
|
|
|
// If it is a value flag, read another argument and add it
|
|
|
|
valueFlags.put(flagName, args[nextArg++]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
booleanFlags.add(flagName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.args = Arrays.copyOfRange(args, nextArg, args.length);
|
|
|
|
}
|
|
|
|
|
2011-01-29 10:05:22 +00:00
|
|
|
public String getCommand() {
|
|
|
|
return args[0];
|
|
|
|
}
|
2011-08-26 15:28:37 +00:00
|
|
|
|
2011-01-29 10:05:22 +00:00
|
|
|
public boolean matches(String command) {
|
|
|
|
return args[0].equalsIgnoreCase(command);
|
|
|
|
}
|
2011-08-26 15:28:37 +00:00
|
|
|
|
2011-01-29 10:05:22 +00:00
|
|
|
public String getString(int index) {
|
|
|
|
return args[index + 1];
|
|
|
|
}
|
2011-08-26 15:28:37 +00:00
|
|
|
|
2011-01-31 06:14:36 +00:00
|
|
|
public String getString(int index, String def) {
|
|
|
|
return index + 1 < args.length ? args[index + 1] : def;
|
|
|
|
}
|
2011-01-29 10:05:22 +00:00
|
|
|
|
|
|
|
public String getJoinedStrings(int initialIndex) {
|
|
|
|
initialIndex = initialIndex + 1;
|
|
|
|
StringBuilder buffer = new StringBuilder(args[initialIndex]);
|
2011-07-15 07:00:48 +00:00
|
|
|
for (int i = initialIndex + 1; i < args.length; ++i) {
|
2011-01-29 10:05:22 +00:00
|
|
|
buffer.append(" ").append(args[i]);
|
|
|
|
}
|
|
|
|
return buffer.toString();
|
|
|
|
}
|
2011-08-26 15:28:37 +00:00
|
|
|
|
2011-01-29 10:05:22 +00:00
|
|
|
public int getInteger(int index) throws NumberFormatException {
|
|
|
|
return Integer.parseInt(args[index + 1]);
|
|
|
|
}
|
2011-08-26 15:28:37 +00:00
|
|
|
|
2011-01-31 06:14:36 +00:00
|
|
|
public int getInteger(int index, int def) throws NumberFormatException {
|
|
|
|
return index + 1 < args.length ? Integer.parseInt(args[index + 1]) : def;
|
|
|
|
}
|
2011-08-26 15:28:37 +00:00
|
|
|
|
2011-01-29 10:05:22 +00:00
|
|
|
public double getDouble(int index) throws NumberFormatException {
|
|
|
|
return Double.parseDouble(args[index + 1]);
|
|
|
|
}
|
2011-08-26 15:28:37 +00:00
|
|
|
|
2011-01-31 06:14:36 +00:00
|
|
|
public double getDouble(int index, double def) throws NumberFormatException {
|
|
|
|
return index + 1 < args.length ? Double.parseDouble(args[index + 1]) : def;
|
|
|
|
}
|
2011-08-26 15:28:37 +00:00
|
|
|
|
2011-01-29 10:05:22 +00:00
|
|
|
public String[] getSlice(int index) {
|
|
|
|
String[] slice = new String[args.length - index];
|
|
|
|
System.arraycopy(args, index, slice, 0, args.length - index);
|
|
|
|
return slice;
|
|
|
|
}
|
2011-08-26 15:28:37 +00:00
|
|
|
|
2011-01-29 10:05:22 +00:00
|
|
|
public String[] getPaddedSlice(int index, int padding) {
|
|
|
|
String[] slice = new String[args.length - index + padding];
|
2011-01-29 17:31:48 +00:00
|
|
|
System.arraycopy(args, index, slice, padding, args.length - index);
|
2011-01-29 10:05:22 +00:00
|
|
|
return slice;
|
|
|
|
}
|
2011-08-26 15:28:37 +00:00
|
|
|
|
2011-01-29 19:36:28 +00:00
|
|
|
public boolean hasFlag(char ch) {
|
2011-08-26 15:28:37 +00:00
|
|
|
return booleanFlags.contains(ch) || valueFlags.containsKey(ch);
|
2011-01-29 19:36:28 +00:00
|
|
|
}
|
2011-08-26 15:28:37 +00:00
|
|
|
|
2011-01-29 19:36:28 +00:00
|
|
|
public Set<Character> getFlags() {
|
2011-08-26 15:28:37 +00:00
|
|
|
return booleanFlags;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Map<Character, String> getValueFlags() {
|
|
|
|
return valueFlags;
|
2011-01-29 19:36:28 +00:00
|
|
|
}
|
2011-08-26 15:28:37 +00:00
|
|
|
|
|
|
|
public String getFlag(char ch) {
|
|
|
|
return valueFlags.get(ch);
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getFlag(char ch, String def) {
|
|
|
|
final String value = valueFlags.get(ch);
|
|
|
|
if (value == null)
|
|
|
|
return def;
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getFlagInteger(char ch) throws NumberFormatException {
|
|
|
|
return Integer.parseInt(valueFlags.get(ch));
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getFlagInteger(char ch, int def) throws NumberFormatException {
|
|
|
|
final String value = valueFlags.get(ch);
|
|
|
|
if (value == null)
|
|
|
|
return def;
|
|
|
|
|
|
|
|
return Integer.parseInt(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public double getFlagDouble(char ch) throws NumberFormatException {
|
|
|
|
return Double.parseDouble(valueFlags.get(ch));
|
|
|
|
}
|
|
|
|
|
|
|
|
public double getFlagDouble(char ch, double def) throws NumberFormatException {
|
|
|
|
final String value = valueFlags.get(ch);
|
|
|
|
if (value == null)
|
|
|
|
return def;
|
|
|
|
|
|
|
|
return Double.parseDouble(value);
|
|
|
|
}
|
|
|
|
|
2011-01-29 10:05:22 +00:00
|
|
|
public int length() {
|
|
|
|
return args.length;
|
|
|
|
}
|
2011-08-26 15:28:37 +00:00
|
|
|
|
2011-01-29 10:05:22 +00:00
|
|
|
public int argsLength() {
|
|
|
|
return args.length - 1;
|
|
|
|
}
|
|
|
|
}
|