Added parameters parsing exception rather than boolean
Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
This commit is contained in:
@@ -148,7 +148,7 @@ public class ConsoleApplication implements ICommandProvider {
|
||||
args = GCLCConstants.splitCommand(cmd);
|
||||
} catch (CommandParsingException e1) {
|
||||
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;
|
||||
}
|
||||
if (!args.isEmpty()) {
|
||||
@@ -156,7 +156,7 @@ public class ConsoleApplication implements ICommandProvider {
|
||||
executeSub(args.get(0), Arrays.copyOfRange(
|
||||
args.toArray(new String[0]), 1, args.size()));
|
||||
} 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
|
||||
.getString("ConsoleApplication.cmd.failed", cmd)); //$NON-NLS-1$
|
||||
manager.println(e.getLocalizedMessage());
|
||||
|
||||
@@ -45,6 +45,8 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import fr.bigeon.gclc.exception.CommandParsingException;
|
||||
|
||||
/** <p>
|
||||
* An object representing a collection of parameters. It is used for defaulting
|
||||
* values.
|
||||
@@ -98,8 +100,8 @@ public class CommandParameters {
|
||||
}
|
||||
|
||||
/** @param args the arguments to parse
|
||||
* @return if the arguments were parsed */
|
||||
public boolean parseArgs(String... args) {
|
||||
* @throws CommandParsingException if the arguments parsing failed */
|
||||
public void parseArgs(String... args) throws CommandParsingException {
|
||||
int i = 0;
|
||||
while (i < args.length) {
|
||||
String next = null;
|
||||
@@ -108,11 +110,11 @@ public class CommandParameters {
|
||||
}
|
||||
int p = parseArg(args[i], next);
|
||||
if (p == 0) {
|
||||
return false;
|
||||
throw new CommandParsingException(
|
||||
"Invalid parameter " + args[i]);
|
||||
}
|
||||
i += p;
|
||||
}
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
@@ -126,7 +128,7 @@ public class CommandParameters {
|
||||
* @return the number of element read */
|
||||
private int parseArg(String arg, String next) {
|
||||
if (!arg.startsWith("-")) { //$NON-NLS-1$
|
||||
return 1;
|
||||
return strict ? 0 : 1;
|
||||
}
|
||||
String name = arg.substring(1);
|
||||
if (booleanArguments.containsKey(name)) {
|
||||
|
||||
@@ -48,6 +48,7 @@ import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
|
||||
import fr.bigeon.gclc.exception.CommandParsingException;
|
||||
import fr.bigeon.gclc.exception.CommandRunException;
|
||||
import fr.bigeon.gclc.exception.CommandRunExceptionType;
|
||||
import fr.bigeon.gclc.exception.InvalidParameterException;
|
||||
@@ -165,10 +166,11 @@ public abstract class ParametrizedCommand extends Command {
|
||||
public final void execute(String... args) throws CommandRunException {
|
||||
final CommandParameters parameters = new CommandParameters(
|
||||
boolParams, stringParams.keySet(), strict);
|
||||
if (!parameters.parseArgs(args)) {
|
||||
// the parameters could not be correctly parsed
|
||||
try {
|
||||
parameters.parseArgs(args);
|
||||
} catch (CommandParsingException e) {
|
||||
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<>();
|
||||
for (final Entry<String, Boolean> string : params.entrySet()) {
|
||||
|
||||
Reference in New Issue
Block a user