Skip to content

Instantly share code, notes, and snippets.

@stalep
Created April 11, 2018 13:53
Show Gist options
  • Save stalep/a9e6fd8417a43f84b5bd46bca40048b9 to your computer and use it in GitHub Desktop.
Save stalep/a9e6fd8417a43f84b5bd46bca40048b9 to your computer and use it in GitHub Desktop.
import org.aesh.command.AeshCommandRuntimeBuilder;
import org.aesh.command.Command;
import org.aesh.command.CommandDefinition;
import org.aesh.command.CommandException;
import org.aesh.command.CommandNotFoundException;
import org.aesh.command.CommandResult;
import org.aesh.command.CommandRuntime;
import org.aesh.command.impl.registry.AeshCommandRegistryBuilder;
import org.aesh.command.invocation.CommandInvocation;
import org.aesh.command.option.Option;
import org.aesh.command.parser.CommandLineParserException;
import org.aesh.command.registry.CommandRegistry;
import org.aesh.command.validator.CommandValidatorException;
import org.aesh.command.validator.OptionValidatorException;
import java.io.IOException;
public class RuntimeExample {
public static void main(String[] args) throws CommandLineParserException {
CommandRegistry registry = new AeshCommandRegistryBuilder()
.command(TestConsoleCommand.class)
.create();
CommandRuntime runtime = AeshCommandRuntimeBuilder
.builder()
.commandRegistry(registry)
.build();
String input = "test --bar FOO";
try {
runtime.executeCommand(input);
}
catch (CommandNotFoundException e) {
System.out.println("Command not found: "+input);
}
catch (OptionValidatorException | CommandException | CommandValidatorException | IOException | InterruptedException e) {
e.printStackTrace();
}
}
@CommandDefinition(name = "test", description = "testing")
public static class TestConsoleCommand implements Command {
@Option
private String bar;
@Override
public CommandResult execute(CommandInvocation commandInvocation) {
commandInvocation.print("bar is: "+bar);
return CommandResult.SUCCESS;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment