mirror of
https://github.com/plexusorg/Plex-FAWE.git
synced 2024-12-23 09:47:38 +00:00
Some small improvements to value flags, and a unit test.
This commit is contained in:
parent
6cdfd86f0b
commit
75e843b965
@ -85,9 +85,7 @@ public class CommandContext {
|
||||
// Then flags
|
||||
for (int i = 1; i < args.length; ++i) {
|
||||
final String arg = args[i];
|
||||
if (arg.charAt(0) != '-') {
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
if (arg.equals("--")) {
|
||||
args = removePortionOfArray(args, i, i, null);
|
||||
@ -95,28 +93,26 @@ public class CommandContext {
|
||||
}
|
||||
|
||||
if (!arg.matches("^-[a-zA-Z]+$")) {
|
||||
throw new CommandException("Invalid flag character given.");
|
||||
continue;
|
||||
}
|
||||
|
||||
int index = i;
|
||||
for (int k = 1; k < arg.length(); ++k) {
|
||||
final char flagName = arg.charAt(k);
|
||||
if (valueFlags.contains(flagName)) {
|
||||
if (this.valueFlags.containsKey(flagName)) {
|
||||
throw new CommandException("Value flag '" + flagName + "' already given");
|
||||
}
|
||||
|
||||
final int index = i + 1;
|
||||
index++;
|
||||
if (index >= args.length) {
|
||||
throw new CommandException("Value flag '" + flagName + "' specified without value");
|
||||
}
|
||||
|
||||
this.valueFlags.put(flagName, args[index]);
|
||||
args = removePortionOfArray(args, index, index, null);
|
||||
} else {
|
||||
booleanFlags.add(flagName);
|
||||
}
|
||||
}
|
||||
args = removePortionOfArray(args, i, i, null);
|
||||
args = removePortionOfArray(args, i, index, null);
|
||||
}
|
||||
this.args = args;
|
||||
}
|
||||
|
@ -0,0 +1,32 @@
|
||||
package com.sk89q.minecraft.util.commands;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
|
||||
public class CommandContextTest {
|
||||
final String firstCmdString = "herpderp -opw testers \"mani world\" 'another thing' because something";
|
||||
CommandContext firstCommand;
|
||||
CommandContext secondCommand;
|
||||
|
||||
@Before
|
||||
public void setUpTest(){
|
||||
try {
|
||||
firstCommand = new CommandContext(firstCmdString, new HashSet<Character>(Arrays.asList('o', 'w')));
|
||||
} catch (CommandException e) {
|
||||
e.printStackTrace();
|
||||
fail();
|
||||
}
|
||||
}
|
||||
|
||||
@Test(expected = CommandException.class)
|
||||
public void testInvalidFlags() throws CommandException {
|
||||
String failingCommand = "herpderp -opw testers";
|
||||
new CommandContext(failingCommand, new HashSet<Character>(Arrays.asList('o', 'w')));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user