- CommandContext's valueFlag constructor will now ignore the 0th element of args (no idea why that is even there)

- Limited flags to a-zA-Z
- Passing null to the valueFlag constructor will disable flag parsing altogether
- Adjusted the error message
- Added javadoc to CommandContext(String[], Set<Character>)
- Added CommandContext(String, Set<Character>)
This commit is contained in:
TomyLobo 2011-08-27 12:07:07 +02:00
parent 799b84622f
commit 447de4b206

View File

@ -55,11 +55,22 @@ public class CommandContext {
this.args = newArgs; this.args = newArgs;
} }
public CommandContext(String[] args, Set<Character> isValueFlag) throws CommandException { public CommandContext(String args, Set<Character> isValueFlag) throws CommandException {
int nextArg = 0; this(args.split(" "), isValueFlag);
}
booleanFlags.clear(); /**
valueFlags.clear(); * @param args An array with arguments empty strings will be ignored by most things
* @param isValueFlag A set containing all value flags. Pass null to disable flag parsing altogether.
* @throws CommandException This is thrown if a value flag was passed without a value.
*/
public CommandContext(String[] args, Set<Character> isValueFlag) throws CommandException {
if (isValueFlag == null) {
this.args = args;
return;
}
int nextArg = 1;
while (nextArg < args.length) { while (nextArg < args.length) {
// Fetch argument // Fetch argument
@ -70,7 +81,7 @@ public class CommandContext {
continue; continue;
// No more flags? // No more flags?
if (arg.charAt(0) != '-' || arg.length() == 1) { if (arg.charAt(0) != '-' || arg.length() == 1 || !arg.matches("^-[a-zA-Z]+$")) {
--nextArg; --nextArg;
break; break;
} }
@ -89,7 +100,7 @@ public class CommandContext {
++nextArg; ++nextArg;
if (nextArg >= args.length) if (nextArg >= args.length)
throw new CommandException("No value specified for "+flagName+" flag."); throw new CommandException("No value specified for the '-"+flagName+"' flag.");
// If it is a value flag, read another argument and add it // If it is a value flag, read another argument and add it
valueFlags.put(flagName, args[nextArg++]); valueFlags.put(flagName, args[nextArg++]);
@ -100,7 +111,8 @@ public class CommandContext {
} }
} }
this.args = Arrays.copyOfRange(args, nextArg, args.length); this.args = Arrays.copyOfRange(args, nextArg-1, args.length);
this.args[0] = args[0];
} }
public String getCommand() { public String getCommand() {