diff --git a/gclc/src/main/java/fr/bigeon/gclc/ConsoleApplication.java b/gclc/src/main/java/fr/bigeon/gclc/ConsoleApplication.java index 69a8fa4..35ec554 100644 --- a/gclc/src/main/java/fr/bigeon/gclc/ConsoleApplication.java +++ b/gclc/src/main/java/fr/bigeon/gclc/ConsoleApplication.java @@ -48,9 +48,9 @@ import fr.bigeon.gclc.command.HelpExecutor; import fr.bigeon.gclc.command.ICommand; import fr.bigeon.gclc.command.ICommandProvider; import fr.bigeon.gclc.command.SubedCommand; -import fr.bigeon.gclc.command.UnrecognizedCommand; import fr.bigeon.gclc.exception.CommandParsingException; import fr.bigeon.gclc.exception.CommandRunException; +import fr.bigeon.gclc.exception.CommandRunExceptionType; import fr.bigeon.gclc.exception.InvalidCommandName; import fr.bigeon.gclc.i18n.Messages; import fr.bigeon.gclc.manager.ConsoleManager; @@ -100,7 +100,7 @@ public class ConsoleApplication implements ICommandProvider { this.header = welcome; this.footer = goodbye; this.manager = manager; - root = new SubedCommand(new String(), new UnrecognizedCommand(manager)); + root = new SubedCommand(new String()); } /** @param manager the manager @@ -208,6 +208,9 @@ public class ConsoleApplication implements ICommandProvider { manager.println(Messages .getString("ConsoleApplication.cmd.failed", cmd)); //$NON-NLS-1$ manager.println(e.getLocalizedMessage()); + if (e.getType() == CommandRunExceptionType.USAGE) { + e.getSource().help(getManager()); + } } } } diff --git a/gclc/src/main/java/fr/bigeon/gclc/command/CommandProvider.java b/gclc/src/main/java/fr/bigeon/gclc/command/CommandProvider.java index 503bce9..df77293 100644 --- a/gclc/src/main/java/fr/bigeon/gclc/command/CommandProvider.java +++ b/gclc/src/main/java/fr/bigeon/gclc/command/CommandProvider.java @@ -83,7 +83,7 @@ public class CommandProvider implements ICommandProvider { } } throw new CommandRunException( - Messages.getString("CommandProvider.unrecognized", cmd)); //$NON-NLS-1$ + Messages.getString("CommandProvider.unrecognized", cmd), null); //$NON-NLS-1$ } /* (non-Javadoc) diff --git a/gclc/src/main/java/fr/bigeon/gclc/command/HelpExecutor.java b/gclc/src/main/java/fr/bigeon/gclc/command/HelpExecutor.java index 6eee60b..885a756 100644 --- a/gclc/src/main/java/fr/bigeon/gclc/command/HelpExecutor.java +++ b/gclc/src/main/java/fr/bigeon/gclc/command/HelpExecutor.java @@ -41,6 +41,7 @@ package fr.bigeon.gclc.command; import java.io.IOException; import fr.bigeon.gclc.exception.CommandRunException; +import fr.bigeon.gclc.exception.CommandRunExceptionType; import fr.bigeon.gclc.manager.ConsoleManager; import fr.bigeon.gclc.prompt.CLIPrompterMessages; @@ -77,7 +78,8 @@ public class HelpExecutor extends Command { try { cmd.help(consoleManager, args); } catch (IOException e) { - throw new CommandRunException("Console manager closed", e); //$NON-NLS-1$ + throw new CommandRunException(CommandRunExceptionType.INTERACTION, + "Console manager closed", e, this); //$NON-NLS-1$ } } diff --git a/gclc/src/main/java/fr/bigeon/gclc/command/ParametrizedCommand.java b/gclc/src/main/java/fr/bigeon/gclc/command/ParametrizedCommand.java index aad57a7..77f995a 100644 --- a/gclc/src/main/java/fr/bigeon/gclc/command/ParametrizedCommand.java +++ b/gclc/src/main/java/fr/bigeon/gclc/command/ParametrizedCommand.java @@ -45,6 +45,7 @@ import java.util.List; import java.util.Map; import fr.bigeon.gclc.exception.CommandRunException; +import fr.bigeon.gclc.exception.CommandRunExceptionType; import fr.bigeon.gclc.manager.ConsoleManager; /**
@@ -122,7 +123,8 @@ public abstract class ParametrizedCommand extends Command {
boolParams.keySet(), stringParams.keySet(), strict);
if (!parameters.parseArgs(args)) {
// ERROR the parameters could not be correctly parsed
- throw new CommandRunException("Unable to read arguments"); //$NON-NLS-1$
+ throw new CommandRunException(CommandRunExceptionType.USAGE,
+ "Unable to read arguments", this); //$NON-NLS-1$
}
final List
+ * TODO
+ *
+ * @author Emmanuel Bigeon */
+public class ScriptExecution extends Command {
+
+ private final ConsoleApplication application;
+ private final String commentPrefix;
+
+ /** @param name
+ * @param application
+ * @param commentPrefix */
+ public ScriptExecution(String name, ConsoleApplication application,
+ String commentPrefix) {
+ super(name);
+ this.application = application;
+ this.commentPrefix = commentPrefix;
+ }
+
+ /* (non-Javadoc)
+ * @see fr.bigeon.gclc.command.ICommand#execute(java.lang.String[]) */
+ @Override
+ public void execute(String... args) throws CommandRunException {
+ if (args.length == 0) {
+ throw new CommandRunException(CommandRunExceptionType.USAGE,
+ "Expecting a file", this);
+ }
+ List
- * The error message for unrecognized commands
- *
- * @author Emmanuel BIGEON */
-public final class UnrecognizedCommand implements ICommand {
- /** The unrecognized command key */
- private static final String UNRECOGNIZED_CMD = "unrecognized.cmd"; //$NON-NLS-1$
- /** The unrecognized command key */
- private static final String EXPECTED_CMD = "expected.cmd"; //$NON-NLS-1$
- /** The manager */
- private final ConsoleManager manager;
-
- /** @param manager the console manager to use */
- public UnrecognizedCommand(ConsoleManager manager) {
- if (manager == null) {
- throw new NullPointerException("The argument cannot be null"); //$NON-NLS-1$
- }
- this.manager = manager;
- }
-
- @Override
- public void execute(String... args) throws CommandRunException {
-
- try {
- if (args.length > 0) {
- manager.println(CLIPrompterMessages.getString(UNRECOGNIZED_CMD,
- (Object[]) args));
- } else {
- manager.println(CLIPrompterMessages.getString(EXPECTED_CMD));
- }
- } catch (IOException e) {
- throw new CommandRunException("Manager closed", e); //$NON-NLS-1$
- }
- }
-
- /* (non-Javadoc)
- * @see fr.bigeon.gclc.command.ICommand#getCommandName() */
- @Override
- public String getCommandName() {
- return ""; //$NON-NLS-1$
- }
-
- @Override
- public void help(@SuppressWarnings("hiding") ConsoleManager manager,
- String... args) {
- // Nothing to do (no help provided as this is not a user command
- // (usually)
- }
-
- @Override
- public String tip() {
- return null;
- }
-}
\ No newline at end of file
diff --git a/gclc/src/main/java/fr/bigeon/gclc/command/package-info.java b/gclc/src/main/java/fr/bigeon/gclc/command/package-info.java
index d24d6b3..76163a6 100644
--- a/gclc/src/main/java/fr/bigeon/gclc/command/package-info.java
+++ b/gclc/src/main/java/fr/bigeon/gclc/command/package-info.java
@@ -44,8 +44,6 @@
* {@link fr.bigeon.gclc.command.SubedCommand} for a command that is declined in
* a set of sub commands, the {@link fr.bigeon.gclc.command.HelpExecutor} for
* help display of other commands and the
- * {@link fr.bigeon.gclc.command.UnrecognizedCommand} that should not be
- * directly accessible to the user, but will be used in case of error in typing.
*
* @author Emmanuel BIGEON */
package fr.bigeon.gclc.command;
\ No newline at end of file
diff --git a/gclc/src/main/java/fr/bigeon/gclc/exception/CommandRunException.java b/gclc/src/main/java/fr/bigeon/gclc/exception/CommandRunException.java
index d4713ee..c28a8d1 100644
--- a/gclc/src/main/java/fr/bigeon/gclc/exception/CommandRunException.java
+++ b/gclc/src/main/java/fr/bigeon/gclc/exception/CommandRunException.java
@@ -38,6 +38,8 @@
*/
package fr.bigeon.gclc.exception;
+import fr.bigeon.gclc.command.ICommand;
+
/**
* An exception thrown when a command failed to run correctly.
*
@@ -49,15 +51,43 @@ public class CommandRunException extends Exception {
*/
private static final long serialVersionUID = 1L;
+ private final CommandRunExceptionType type;
+ /** The command that caused the error */
+ private final ICommand source;
+
/** @param message a message */
- public CommandRunException(String message) {
+ public CommandRunException(String message, ICommand source) {
super(message);
+ type = CommandRunExceptionType.EXECUTION;
+ this.source = source;
}
/** @param message a message
* @param cause the cause */
- public CommandRunException(String message, Throwable cause) {
+ public CommandRunException(String message, Throwable cause,
+ ICommand source) {
super(message, cause);
+ type = CommandRunExceptionType.EXECUTION;
+ this.source = source;
+ }
+
+ /** @param type the type of exception
+ * @param message the message */
+ public CommandRunException(CommandRunExceptionType type, String message,
+ ICommand source) {
+ super(message);
+ this.type = type;
+ this.source = source;
+ }
+
+ /** @param type the type of exception
+ * @param message a message
+ * @param cause the cause */
+ public CommandRunException(CommandRunExceptionType type, String message,
+ Throwable cause, ICommand source) {
+ super(message, cause);
+ this.type = type;
+ this.source = source;
}
/* (non-Javadoc)
@@ -71,4 +101,13 @@ public class CommandRunException extends Exception {
return super.getLocalizedMessage();
}
+ /** @return the source */
+ public ICommand getSource() {
+ return source;
+ }
+
+ /** @return the type */
+ public CommandRunExceptionType getType() {
+ return type;
+ }
}
diff --git a/gclc/src/main/java/fr/bigeon/gclc/exception/CommandRunExceptionType.java b/gclc/src/main/java/fr/bigeon/gclc/exception/CommandRunExceptionType.java
new file mode 100644
index 0000000..bbe04d8
--- /dev/null
+++ b/gclc/src/main/java/fr/bigeon/gclc/exception/CommandRunExceptionType.java
@@ -0,0 +1,21 @@
+/**
+ * gclc:fr.bigeon.gclc.exception.CommandRunExceptionType.java
+ * Created on: Jun 12, 2016
+ */
+package fr.bigeon.gclc.exception;
+
+/**
+ *
+ * TODO
+ *
+ * @author Emmanuel Bigeon
+ *
+ */
+public enum CommandRunExceptionType {
+ /** Type of exception due to a wrong usage */
+ USAGE,
+ /** Type of exception due to a problem in execution */
+ EXECUTION,
+ /** Type of exception due to the impossibility to interact with user */
+ INTERACTION;
+}
diff --git a/gclc/src/test/java/fr/bigeon/gclc/ConsoleTestApplication.java b/gclc/src/test/java/fr/bigeon/gclc/ConsoleTestApplication.java
index babe0ef..ad75001 100644
--- a/gclc/src/test/java/fr/bigeon/gclc/ConsoleTestApplication.java
+++ b/gclc/src/test/java/fr/bigeon/gclc/ConsoleTestApplication.java
@@ -36,7 +36,6 @@ package fr.bigeon.gclc;
import java.io.IOException;
-import fr.bigeon.gclc.ConsoleApplication;
import fr.bigeon.gclc.command.Command;
import fr.bigeon.gclc.command.ExitCommand;
import fr.bigeon.gclc.command.HelpExecutor;
@@ -74,7 +73,8 @@ public class ConsoleTestApplication extends ConsoleApplication {
try {
manager.println("Test command ran fine");
} catch (IOException e) {
- throw new CommandRunException("manager closed", e);
+ throw new CommandRunException("manager closed", e,
+ this);
}
}
});
@@ -92,9 +92,11 @@ public class ConsoleTestApplication extends ConsoleApplication {
Thread.sleep(TWO_SECONDS);
manager.println("done!");
} catch (IOException e) {
- throw new CommandRunException("manager closed", e);
+ throw new CommandRunException("manager closed", e,
+ this);
} catch (InterruptedException e) {
- throw new CommandRunException("wait interrupted", e);
+ throw new CommandRunException("wait interrupted", e,
+ this);
}
}
});