diff --git a/src/main/java/org/jboss/aesh/console/AeshConsole.java b/src/main/java/org/jboss/aesh/console/AeshConsole.java index eaf7bb4ca..d812b0ed7 100644 --- a/src/main/java/org/jboss/aesh/console/AeshConsole.java +++ b/src/main/java/org/jboss/aesh/console/AeshConsole.java @@ -23,6 +23,7 @@ import org.jboss.aesh.console.command.registry.CommandRegistry; import org.jboss.aesh.console.export.ExportManager; import org.jboss.aesh.console.helper.ManProvider; +import org.jboss.aesh.history.History; import org.jboss.aesh.terminal.Shell; /** @@ -115,4 +116,6 @@ void registerCommandInvocationProvider(String name, boolean isEchoing(); void setEcho(boolean echo); + + History getHistory(); } diff --git a/src/main/java/org/jboss/aesh/console/AeshConsoleImpl.java b/src/main/java/org/jboss/aesh/console/AeshConsoleImpl.java index 6f8083ba0..16dcc8a2c 100644 --- a/src/main/java/org/jboss/aesh/console/AeshConsoleImpl.java +++ b/src/main/java/org/jboss/aesh/console/AeshConsoleImpl.java @@ -56,6 +56,7 @@ import org.jboss.aesh.console.man.Man; import org.jboss.aesh.console.settings.CommandNotFoundHandler; import org.jboss.aesh.console.settings.Settings; +import org.jboss.aesh.history.History; import org.jboss.aesh.parser.AeshLine; import org.jboss.aesh.parser.Parser; import org.jboss.aesh.terminal.Shell; @@ -309,6 +310,10 @@ public ConsoleCallback getConsoleCallback() { return console.getConsoleCallback(); } + public InvocationProviders getInvocationProviders() { + return invocationProviders; + } + class AeshCompletion implements Completion { @Override @@ -425,4 +430,9 @@ else if (output != null) { } } } + + @Override + public History getHistory() { + return console.getHistory(); + } } diff --git a/src/main/java/org/jboss/aesh/console/command/invocation/AeshCommandInvocation.java b/src/main/java/org/jboss/aesh/console/command/invocation/AeshCommandInvocation.java index 6a0b8b47c..6f994bef1 100644 --- a/src/main/java/org/jboss/aesh/console/command/invocation/AeshCommandInvocation.java +++ b/src/main/java/org/jboss/aesh/console/command/invocation/AeshCommandInvocation.java @@ -19,13 +19,22 @@ */ package org.jboss.aesh.console.command.invocation; +import org.jboss.aesh.cl.CommandLine; +import org.jboss.aesh.cl.parser.CommandLineParserException; +import org.jboss.aesh.cl.validator.OptionValidatorException; import org.jboss.aesh.console.AeshConsoleImpl; import org.jboss.aesh.console.AeshContext; import org.jboss.aesh.console.ConsoleCallback; import org.jboss.aesh.console.Prompt; +import org.jboss.aesh.console.command.Command; +import org.jboss.aesh.console.command.CommandException; +import org.jboss.aesh.console.command.CommandNotFoundException; import org.jboss.aesh.console.command.CommandOperation; +import org.jboss.aesh.console.command.container.CommandContainer; import org.jboss.aesh.console.command.registry.CommandRegistry; import org.jboss.aesh.console.operator.ControlOperator; +import org.jboss.aesh.parser.AeshLine; +import org.jboss.aesh.parser.Parser; import org.jboss.aesh.terminal.Shell; /** @@ -135,4 +144,30 @@ public void setEcho(boolean echo) { aeshConsole.setEcho(echo); } + @Override + public Command getPopulatedCommand(String commandLine) throws CommandNotFoundException, + CommandException, CommandLineParserException, OptionValidatorException { + if (commandLine == null || commandLine.isEmpty()) { + return null; + } + + AeshLine aeshLine = Parser.findAllWords(commandLine); + if (aeshLine.getWords().isEmpty()) { + return null; + } + String opName = aeshLine.getWords().get(0); + CommandContainer container = aeshConsole.getCommandRegistry(). + getCommand(opName, commandLine); + if (container == null) { + throw new CommandNotFoundException("No command handler for '" + opName + "'."); + } + CommandLine line = container.getParser().parse(commandLine, false); + if (line == null) { + throw new CommandException("Invalid Command " + commandLine); + } + line.getParser().getCommandPopulator().populateObject(line, + aeshConsole.getInvocationProviders(), getAeshContext(), true); + return line.getParser().getCommand(); + } + } diff --git a/src/main/java/org/jboss/aesh/console/command/invocation/CommandInvocation.java b/src/main/java/org/jboss/aesh/console/command/invocation/CommandInvocation.java index 878d872a0..01d58581f 100644 --- a/src/main/java/org/jboss/aesh/console/command/invocation/CommandInvocation.java +++ b/src/main/java/org/jboss/aesh/console/command/invocation/CommandInvocation.java @@ -19,8 +19,13 @@ */ package org.jboss.aesh.console.command.invocation; +import org.jboss.aesh.cl.parser.CommandLineParserException; +import org.jboss.aesh.cl.validator.OptionValidatorException; import org.jboss.aesh.console.AeshContext; import org.jboss.aesh.console.Prompt; +import org.jboss.aesh.console.command.Command; +import org.jboss.aesh.console.command.CommandException; +import org.jboss.aesh.console.command.CommandNotFoundException; import org.jboss.aesh.console.command.CommandOperation; import org.jboss.aesh.console.command.registry.CommandRegistry; import org.jboss.aesh.console.operator.ControlOperator; @@ -143,4 +148,21 @@ public interface CommandInvocation { * @param echo state */ void setEcho(boolean echo); + + /** + * Retrieve the command from a command line. The arguments in the command + * line are injected into the Command fields. + * + * @param commandLine The input command. + * @return The command or null if the string was not mapping onto a Command. + * @throws org.jboss.aesh.console.command.CommandNotFoundException If the + * command is not found in the registry. + * @throws org.jboss.aesh.console.command.CommandException If the Command is + * not properly structured. + * @throws org.jboss.aesh.cl.parser.CommandLineParserException If the line + * parsing fails. + * @throws org.jboss.aesh.cl.validator.OptionValidatorException + */ + Command getPopulatedCommand(String commandLine) throws CommandNotFoundException, + CommandException, CommandLineParserException, OptionValidatorException; } diff --git a/src/test/java/org/jboss/aesh/console/aesh/AeshCommandInvocationServiceTest.java b/src/test/java/org/jboss/aesh/console/aesh/AeshCommandInvocationServiceTest.java index 1e1d38a44..dc38c34a1 100644 --- a/src/test/java/org/jboss/aesh/console/aesh/AeshCommandInvocationServiceTest.java +++ b/src/test/java/org/jboss/aesh/console/aesh/AeshCommandInvocationServiceTest.java @@ -46,7 +46,10 @@ import java.io.PipedInputStream; import java.io.PipedOutputStream; import java.io.PrintStream; +import org.jboss.aesh.cl.parser.CommandLineParserException; +import org.jboss.aesh.cl.validator.OptionValidatorException; import org.jboss.aesh.console.command.CommandException; +import org.jboss.aesh.console.command.CommandNotFoundException; import static org.junit.Assert.assertTrue; @@ -209,6 +212,12 @@ public void setEcho(boolean interactive) { public String getFoo() { return "FOO"; } + + @Override + public Command getPopulatedCommand(String commandLine) throws CommandNotFoundException, + CommandException, CommandLineParserException, OptionValidatorException { + return commandInvocation.getPopulatedCommand(commandLine); + } } class FooCommandInvocationProvider implements CommandInvocationProvider {