From 447de4b206b2ab18f90731767739681378c6a6d3 Mon Sep 17 00:00:00 2001 From: TomyLobo Date: Sat, 27 Aug 2011 12:07:07 +0200 Subject: [PATCH] - 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) - Added CommandContext(String, Set) --- .../util/commands/CommandContext.java | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/sk89q/minecraft/util/commands/CommandContext.java b/src/main/java/com/sk89q/minecraft/util/commands/CommandContext.java index f8d446373..6bad1f8b4 100644 --- a/src/main/java/com/sk89q/minecraft/util/commands/CommandContext.java +++ b/src/main/java/com/sk89q/minecraft/util/commands/CommandContext.java @@ -55,11 +55,22 @@ public class CommandContext { this.args = newArgs; } - public CommandContext(String[] args, Set isValueFlag) throws CommandException { - int nextArg = 0; + public CommandContext(String args, Set isValueFlag) throws CommandException { + 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 isValueFlag) throws CommandException { + if (isValueFlag == null) { + this.args = args; + return; + } + + int nextArg = 1; while (nextArg < args.length) { // Fetch argument @@ -70,7 +81,7 @@ public class CommandContext { continue; // No more flags? - if (arg.charAt(0) != '-' || arg.length() == 1) { + if (arg.charAt(0) != '-' || arg.length() == 1 || !arg.matches("^-[a-zA-Z]+$")) { --nextArg; break; } @@ -89,7 +100,7 @@ public class CommandContext { ++nextArg; 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 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() {