Added parameters parsing exception rather than boolean
Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
This commit is contained in:
parent
a12484a72a
commit
c27872de94
@ -148,7 +148,7 @@ public class ConsoleApplication implements ICommandProvider {
|
|||||||
args = GCLCConstants.splitCommand(cmd);
|
args = GCLCConstants.splitCommand(cmd);
|
||||||
} catch (CommandParsingException e1) {
|
} catch (CommandParsingException e1) {
|
||||||
manager.println("Command line cannot be parsed"); //$NON-NLS-1$
|
manager.println("Command line cannot be parsed"); //$NON-NLS-1$
|
||||||
LOGGER.log(Level.INFO, "Invalid user command " + cmd, e1); //$NON-NLS-1$
|
LOGGER.log(Level.FINE, "Invalid user command " + cmd, e1); //$NON-NLS-1$
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!args.isEmpty()) {
|
if (!args.isEmpty()) {
|
||||||
@ -156,7 +156,7 @@ public class ConsoleApplication implements ICommandProvider {
|
|||||||
executeSub(args.get(0), Arrays.copyOfRange(
|
executeSub(args.get(0), Arrays.copyOfRange(
|
||||||
args.toArray(new String[0]), 1, args.size()));
|
args.toArray(new String[0]), 1, args.size()));
|
||||||
} catch (final CommandRunException e) {
|
} catch (final CommandRunException e) {
|
||||||
LOGGER.log(Level.WARNING, "Command failed: " + cmd, e); //$NON-NLS-1$
|
LOGGER.log(Level.FINE, "Command failed: " + cmd, e); //$NON-NLS-1$
|
||||||
manager.println(Messages
|
manager.println(Messages
|
||||||
.getString("ConsoleApplication.cmd.failed", cmd)); //$NON-NLS-1$
|
.getString("ConsoleApplication.cmd.failed", cmd)); //$NON-NLS-1$
|
||||||
manager.println(e.getLocalizedMessage());
|
manager.println(e.getLocalizedMessage());
|
||||||
|
@ -45,6 +45,8 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import fr.bigeon.gclc.exception.CommandParsingException;
|
||||||
|
|
||||||
/** <p>
|
/** <p>
|
||||||
* An object representing a collection of parameters. It is used for defaulting
|
* An object representing a collection of parameters. It is used for defaulting
|
||||||
* values.
|
* values.
|
||||||
@ -98,8 +100,8 @@ public class CommandParameters {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** @param args the arguments to parse
|
/** @param args the arguments to parse
|
||||||
* @return if the arguments were parsed */
|
* @throws CommandParsingException if the arguments parsing failed */
|
||||||
public boolean parseArgs(String... args) {
|
public void parseArgs(String... args) throws CommandParsingException {
|
||||||
int i = 0;
|
int i = 0;
|
||||||
while (i < args.length) {
|
while (i < args.length) {
|
||||||
String next = null;
|
String next = null;
|
||||||
@ -108,11 +110,11 @@ public class CommandParameters {
|
|||||||
}
|
}
|
||||||
int p = parseArg(args[i], next);
|
int p = parseArg(args[i], next);
|
||||||
if (p == 0) {
|
if (p == 0) {
|
||||||
return false;
|
throw new CommandParsingException(
|
||||||
|
"Invalid parameter " + args[i]);
|
||||||
}
|
}
|
||||||
i += p;
|
i += p;
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -126,7 +128,7 @@ public class CommandParameters {
|
|||||||
* @return the number of element read */
|
* @return the number of element read */
|
||||||
private int parseArg(String arg, String next) {
|
private int parseArg(String arg, String next) {
|
||||||
if (!arg.startsWith("-")) { //$NON-NLS-1$
|
if (!arg.startsWith("-")) { //$NON-NLS-1$
|
||||||
return 1;
|
return strict ? 0 : 1;
|
||||||
}
|
}
|
||||||
String name = arg.substring(1);
|
String name = arg.substring(1);
|
||||||
if (booleanArguments.containsKey(name)) {
|
if (booleanArguments.containsKey(name)) {
|
||||||
|
@ -48,6 +48,7 @@ import java.util.Map;
|
|||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import fr.bigeon.gclc.exception.CommandParsingException;
|
||||||
import fr.bigeon.gclc.exception.CommandRunException;
|
import fr.bigeon.gclc.exception.CommandRunException;
|
||||||
import fr.bigeon.gclc.exception.CommandRunExceptionType;
|
import fr.bigeon.gclc.exception.CommandRunExceptionType;
|
||||||
import fr.bigeon.gclc.exception.InvalidParameterException;
|
import fr.bigeon.gclc.exception.InvalidParameterException;
|
||||||
@ -165,10 +166,11 @@ public abstract class ParametrizedCommand extends Command {
|
|||||||
public final void execute(String... args) throws CommandRunException {
|
public final void execute(String... args) throws CommandRunException {
|
||||||
final CommandParameters parameters = new CommandParameters(
|
final CommandParameters parameters = new CommandParameters(
|
||||||
boolParams, stringParams.keySet(), strict);
|
boolParams, stringParams.keySet(), strict);
|
||||||
if (!parameters.parseArgs(args)) {
|
try {
|
||||||
// the parameters could not be correctly parsed
|
parameters.parseArgs(args);
|
||||||
|
} catch (CommandParsingException e) {
|
||||||
throw new CommandRunException(CommandRunExceptionType.USAGE,
|
throw new CommandRunException(CommandRunExceptionType.USAGE,
|
||||||
"Unable to read arguments", this); //$NON-NLS-1$
|
"Unable to read arguments", e, this); //$NON-NLS-1$
|
||||||
}
|
}
|
||||||
final List<String> toProvide = new ArrayList<>();
|
final List<String> toProvide = new ArrayList<>();
|
||||||
for (final Entry<String, Boolean> string : params.entrySet()) {
|
for (final Entry<String, Boolean> string : params.entrySet()) {
|
||||||
|
@ -40,45 +40,54 @@ package fr.bigeon.gclc.command;
|
|||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
import static org.junit.Assert.assertNull;
|
import static org.junit.Assert.assertNull;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
/**
|
import fr.bigeon.gclc.exception.CommandParsingException;
|
||||||
* <p>
|
|
||||||
|
/** <p>
|
||||||
* TODO
|
* TODO
|
||||||
*
|
*
|
||||||
* @author Emmanuel Bigeon
|
* @author Emmanuel Bigeon */
|
||||||
*
|
|
||||||
*/
|
|
||||||
@SuppressWarnings({"static-method", "nls"})
|
@SuppressWarnings({"static-method", "nls"})
|
||||||
public class CommandParametersTest {
|
public class CommandParametersTest {
|
||||||
|
|
||||||
/**
|
/** Test method for
|
||||||
* Test method for {@link fr.bigeon.gclc.command.CommandParameters#CommandParameters(java.util.Set, java.util.Set, boolean)}.
|
* {@link fr.bigeon.gclc.command.CommandParameters#CommandParameters(java.util.Set, java.util.Set, boolean)}. */
|
||||||
*/
|
|
||||||
@Test
|
@Test
|
||||||
public final void testCommandParameters(){
|
public final void testCommandParameters() {
|
||||||
Set<String> strings = new HashSet<>();
|
Set<String> strings = new HashSet<>();
|
||||||
Set<String> bools = new HashSet<>();
|
Set<String> bools = new HashSet<>();
|
||||||
CommandParameters parameters = new CommandParameters(bools, strings,
|
CommandParameters parameters = new CommandParameters(bools, strings,
|
||||||
true);
|
true);
|
||||||
|
|
||||||
assertFalse(parameters.parseArgs("-ungivenFlag"));
|
try {
|
||||||
|
parameters.parseArgs("-ungivenFlag");
|
||||||
|
fail("parse of unknown in strict should fail");
|
||||||
|
} catch (CommandParsingException e) {
|
||||||
|
assertNotNull(e);
|
||||||
|
}
|
||||||
parameters = new CommandParameters(bools, strings, false);
|
parameters = new CommandParameters(bools, strings, false);
|
||||||
|
|
||||||
assertTrue(parameters.parseArgs("-ungivenFlag"));
|
try {
|
||||||
|
parameters.parseArgs("-ungivenFlag");
|
||||||
|
} catch (CommandParsingException e) {
|
||||||
|
fail("parse of unknown in non strict should suceed");
|
||||||
|
assertNull(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** Test method for
|
||||||
* Test method for {@link fr.bigeon.gclc.command.CommandParameters#get(java.lang.String)}.
|
* {@link fr.bigeon.gclc.command.CommandParameters#get(java.lang.String)}. */
|
||||||
*/
|
|
||||||
@Test
|
@Test
|
||||||
public final void testGet(){
|
public final void testGet() {
|
||||||
Set<String> strings = new HashSet<>();
|
Set<String> strings = new HashSet<>();
|
||||||
Set<String> bools = new HashSet<>();
|
Set<String> bools = new HashSet<>();
|
||||||
|
|
||||||
@ -91,24 +100,35 @@ public class CommandParametersTest {
|
|||||||
assertNull(parameters.get("ungiven"));
|
assertNull(parameters.get("ungiven"));
|
||||||
assertNull(parameters.get("str"));
|
assertNull(parameters.get("str"));
|
||||||
|
|
||||||
|
try {
|
||||||
parameters.parseArgs("-ungiven", "val");
|
parameters.parseArgs("-ungiven", "val");
|
||||||
|
} catch (CommandParsingException e) {
|
||||||
|
assertNotNull(e);
|
||||||
|
}
|
||||||
assertNull(parameters.get("ungiven"));
|
assertNull(parameters.get("ungiven"));
|
||||||
assertNull(parameters.get("str"));
|
assertNull(parameters.get("str"));
|
||||||
|
|
||||||
|
try {
|
||||||
parameters.parseArgs("-str", "val");
|
parameters.parseArgs("-str", "val");
|
||||||
|
} catch (CommandParsingException e) {
|
||||||
|
assertNull(e);
|
||||||
|
}
|
||||||
assertNull(parameters.get("ungiven"));
|
assertNull(parameters.get("ungiven"));
|
||||||
assertEquals("val", parameters.get("str"));
|
assertEquals("val", parameters.get("str"));
|
||||||
|
|
||||||
|
try {
|
||||||
parameters.parseArgs("-ungiven");
|
parameters.parseArgs("-ungiven");
|
||||||
|
} catch (CommandParsingException e) {
|
||||||
|
assertNotNull(e);
|
||||||
|
}
|
||||||
assertNull(parameters.get("ungiven"));
|
assertNull(parameters.get("ungiven"));
|
||||||
assertEquals("val", parameters.get("str"));
|
assertEquals("val", parameters.get("str"));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** Test method for
|
||||||
* Test method for {@link fr.bigeon.gclc.command.CommandParameters#getAdditionals()}.
|
* {@link fr.bigeon.gclc.command.CommandParameters#getAdditionals()}. */
|
||||||
*/
|
|
||||||
@Test
|
@Test
|
||||||
public final void testGetAdditionals(){
|
public final void testGetAdditionals() {
|
||||||
Set<String> strings = new HashSet<>();
|
Set<String> strings = new HashSet<>();
|
||||||
Set<String> bools = new HashSet<>();
|
Set<String> bools = new HashSet<>();
|
||||||
|
|
||||||
@ -118,27 +138,42 @@ public class CommandParametersTest {
|
|||||||
CommandParameters parameters = new CommandParameters(bools, strings,
|
CommandParameters parameters = new CommandParameters(bools, strings,
|
||||||
true);
|
true);
|
||||||
|
|
||||||
|
try {
|
||||||
parameters.parseArgs("-boolFlag");
|
parameters.parseArgs("-boolFlag");
|
||||||
|
} catch (CommandParsingException e) {
|
||||||
|
assertNull(e);
|
||||||
|
}
|
||||||
assertTrue(parameters.getAdditionals().isEmpty());
|
assertTrue(parameters.getAdditionals().isEmpty());
|
||||||
|
|
||||||
|
try {
|
||||||
parameters.parseArgs("-ungiven");
|
parameters.parseArgs("-ungiven");
|
||||||
|
} catch (CommandParsingException e) {
|
||||||
|
assertNotNull(e);
|
||||||
|
}
|
||||||
assertTrue(parameters.getAdditionals().isEmpty());
|
assertTrue(parameters.getAdditionals().isEmpty());
|
||||||
|
|
||||||
parameters = new CommandParameters(bools, strings, false);
|
parameters = new CommandParameters(bools, strings, false);
|
||||||
|
|
||||||
|
try {
|
||||||
parameters.parseArgs("-boolFlag");
|
parameters.parseArgs("-boolFlag");
|
||||||
|
} catch (CommandParsingException e) {
|
||||||
|
assertNull(e);
|
||||||
|
}
|
||||||
assertTrue(parameters.getAdditionals().isEmpty());
|
assertTrue(parameters.getAdditionals().isEmpty());
|
||||||
|
|
||||||
|
try {
|
||||||
parameters.parseArgs("-ungiven");
|
parameters.parseArgs("-ungiven");
|
||||||
|
} catch (CommandParsingException e) {
|
||||||
|
assertNotNull(e);
|
||||||
|
}
|
||||||
assertTrue(parameters.getAdditionals().contains("ungiven"));
|
assertTrue(parameters.getAdditionals().contains("ungiven"));
|
||||||
assertEquals(1, parameters.getAdditionals().size());
|
assertEquals(1, parameters.getAdditionals().size());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** Test method for
|
||||||
* Test method for {@link fr.bigeon.gclc.command.CommandParameters#getBool(java.lang.String)}.
|
* {@link fr.bigeon.gclc.command.CommandParameters#getBool(java.lang.String)}. */
|
||||||
*/
|
|
||||||
@Test
|
@Test
|
||||||
public final void testGetBool(){
|
public final void testGetBool() {
|
||||||
Set<String> strings = new HashSet<>();
|
Set<String> strings = new HashSet<>();
|
||||||
Set<String> bools = new HashSet<>();
|
Set<String> bools = new HashSet<>();
|
||||||
|
|
||||||
@ -151,11 +186,20 @@ public class CommandParametersTest {
|
|||||||
assertFalse(parameters.getBool("ungiven"));
|
assertFalse(parameters.getBool("ungiven"));
|
||||||
assertFalse(parameters.getBool("boolFlag"));
|
assertFalse(parameters.getBool("boolFlag"));
|
||||||
|
|
||||||
|
try {
|
||||||
parameters.parseArgs("-boolFlag");
|
parameters.parseArgs("-boolFlag");
|
||||||
|
} catch (CommandParsingException e) {
|
||||||
|
assertNull(e);
|
||||||
|
}
|
||||||
assertTrue(parameters.getBool("boolFlag"));
|
assertTrue(parameters.getBool("boolFlag"));
|
||||||
assertFalse(parameters.getBool("ungiven"));
|
assertFalse(parameters.getBool("ungiven"));
|
||||||
|
|
||||||
|
try {
|
||||||
parameters.parseArgs("-ungiven");
|
parameters.parseArgs("-ungiven");
|
||||||
|
fail("unknown parameter should fail");
|
||||||
|
} catch (CommandParsingException e) {
|
||||||
|
assertNotNull(e);
|
||||||
|
}
|
||||||
assertFalse(parameters.getBool("ungiven"));
|
assertFalse(parameters.getBool("ungiven"));
|
||||||
assertTrue(parameters.getBool("boolFlag"));
|
assertTrue(parameters.getBool("boolFlag"));
|
||||||
|
|
||||||
@ -164,20 +208,27 @@ public class CommandParametersTest {
|
|||||||
assertFalse(parameters.getBool("ungiven"));
|
assertFalse(parameters.getBool("ungiven"));
|
||||||
assertFalse(parameters.getBool("boolFlag"));
|
assertFalse(parameters.getBool("boolFlag"));
|
||||||
|
|
||||||
|
try {
|
||||||
parameters.parseArgs("-boolFlag");
|
parameters.parseArgs("-boolFlag");
|
||||||
|
} catch (CommandParsingException e) {
|
||||||
|
assertNull(e);
|
||||||
|
}
|
||||||
assertTrue(parameters.getBool("boolFlag"));
|
assertTrue(parameters.getBool("boolFlag"));
|
||||||
assertFalse(parameters.getBool("ungiven"));
|
assertFalse(parameters.getBool("ungiven"));
|
||||||
|
|
||||||
|
try {
|
||||||
parameters.parseArgs("-ungiven");
|
parameters.parseArgs("-ungiven");
|
||||||
|
} catch (CommandParsingException e) {
|
||||||
|
assertNull(e);
|
||||||
|
}
|
||||||
assertFalse(parameters.getBool("ungiven"));
|
assertFalse(parameters.getBool("ungiven"));
|
||||||
assertTrue(parameters.getBool("boolFlag"));
|
assertTrue(parameters.getBool("boolFlag"));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** Test method for
|
||||||
* Test method for {@link fr.bigeon.gclc.command.CommandParameters#parseArgs(java.lang.String[])}.
|
* {@link fr.bigeon.gclc.command.CommandParameters#parseArgs(java.lang.String[])}. */
|
||||||
*/
|
|
||||||
@Test
|
@Test
|
||||||
public final void testParseArgs(){
|
public final void testParseArgs() {
|
||||||
Set<String> strings = new HashSet<>();
|
Set<String> strings = new HashSet<>();
|
||||||
Set<String> bools = new HashSet<>();
|
Set<String> bools = new HashSet<>();
|
||||||
|
|
||||||
@ -187,19 +238,40 @@ public class CommandParametersTest {
|
|||||||
CommandParameters parameters = new CommandParameters(bools, strings,
|
CommandParameters parameters = new CommandParameters(bools, strings,
|
||||||
true);
|
true);
|
||||||
|
|
||||||
assertFalse(parameters.parseArgs("-ungivenFlag"));
|
try {
|
||||||
assertFalse(parameters.parseArgs("-str"));
|
parameters.parseArgs("-ungivenFlag");
|
||||||
assertTrue(parameters.parseArgs("-boolFlag"));
|
fail("unknown argument should fail in strict");
|
||||||
assertTrue(parameters.parseArgs("-str", "-boolFlag"));
|
} catch (CommandParsingException e) {
|
||||||
assertTrue(parameters.parseArgs("-boolFlag", "-str", "val"));
|
assertNotNull(e);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
parameters.parseArgs("-str");
|
||||||
|
fail("missing string argument value should fail");
|
||||||
|
} catch (CommandParsingException e) {
|
||||||
|
assertNotNull(e);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
parameters.parseArgs("-boolFlag");
|
||||||
|
} catch (CommandParsingException e) {
|
||||||
|
assertNull(e);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
parameters.parseArgs("-str", "-boolFlag");
|
||||||
|
} catch (CommandParsingException e) {
|
||||||
|
assertNull(e);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
parameters.parseArgs("-boolFlag", "-str", "val");
|
||||||
|
} catch (CommandParsingException e) {
|
||||||
|
assertNull(e);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** Test method for
|
||||||
* Test method for {@link fr.bigeon.gclc.command.CommandParameters#set(java.lang.String, boolean)}.
|
* {@link fr.bigeon.gclc.command.CommandParameters#set(java.lang.String, boolean)}. */
|
||||||
*/
|
|
||||||
@Test
|
@Test
|
||||||
public final void testSetStringBoolean(){
|
public final void testSetStringBoolean() {
|
||||||
Set<String> strings = new HashSet<>();
|
Set<String> strings = new HashSet<>();
|
||||||
Set<String> bools = new HashSet<>();
|
Set<String> bools = new HashSet<>();
|
||||||
|
|
||||||
@ -234,11 +306,10 @@ public class CommandParametersTest {
|
|||||||
assertTrue(parameters.getBool("boolFlag"));
|
assertTrue(parameters.getBool("boolFlag"));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/** Test method for
|
||||||
* Test method for {@link fr.bigeon.gclc.command.CommandParameters#set(java.lang.String, java.lang.String)}.
|
* {@link fr.bigeon.gclc.command.CommandParameters#set(java.lang.String, java.lang.String)}. */
|
||||||
*/
|
|
||||||
@Test
|
@Test
|
||||||
public final void testSetStringString(){
|
public final void testSetStringString() {
|
||||||
Set<String> strings = new HashSet<>();
|
Set<String> strings = new HashSet<>();
|
||||||
Set<String> bools = new HashSet<>();
|
Set<String> bools = new HashSet<>();
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user