Added command flag support, removed / prefix from command handling process.

This commit is contained in:
sk89q
2011-01-29 11:36:28 -08:00
parent d1ff0250aa
commit 059f30808d
16 changed files with 140 additions and 86 deletions

View File

@ -28,4 +28,5 @@ public @interface Command {
String desc();
int min();
int max();
String flags() default "";
}

View File

@ -18,14 +18,35 @@
package com.sk89q.util.commands;
import java.util.HashSet;
import java.util.Set;
public class CommandContext {
protected String[] args;
protected Set<Character> flags = new HashSet<Character>();
public CommandContext(String args) {
this.args = args.split(" ");
this(args.split(" "));
}
public CommandContext(String[] args) {
int i = 1;
for (; i < args.length; i++) {
if (args[i].charAt(0) == '-') {
for (int k = 1; k < args[i].length(); k++) {
flags.add(args[i].charAt(k));
}
} else {
break;
}
}
String[] newArgs = new String[args.length - i + 1];
System.arraycopy(args, i, newArgs, 1, args.length - i);
newArgs[0] = args[0];
this.args = args;
}
@ -70,6 +91,14 @@ public class CommandContext {
return slice;
}
public boolean hasFlag(char ch) {
return flags.contains(ch);
}
public Set<Character> getFlags() {
return flags;
}
public int length() {
return args.length;
}