| Interface | Description |
|---|---|
| CommandType |
Marker interface for shell command types.
|
| CommandVerifier |
Implementors verifies the passed command.
|
| MainCommandType |
Marker interface for shell main command types.
|
| Parser |
Parses input line from interactive shell.
|
| Scanner |
Scans the input line from an interactive shell.
|
| SubCommandType |
Marker interface for shell sub command types.
|
| Class | Description |
|---|---|
| DefaultParser |
Default parses implementation.
|
| DefaultScanner |
Default implementation for scanning the input line from an interactive shell.
|
| LiteralCommandMap |
Maps the literal string of an command to its enum type.
|
| NullCommandVerifier |
Verifies nothing.
|
| Parsers |
Factory to create parsers.
|
| Scanners |
Factory to create scanners.
|
| ShellCommand |
Describes a parsed mainCommand of the interactive shell.
|
| Exception | Description |
|---|---|
| SyntaxException |
Signals syntax errors in the input scanned from an interactive shell.
|
This package is a part of the open-source Commons
Contains:First define main and sub command types.
// CHECKSTYLE:OFF public enum MyMainType implements MainCommandType {
HELP("help"),
FOO("foo"),
EXIT("exit");
// Literal command string used in shell.
private final String literal;
private NeuronMainType(final String name) {
this.literal = name;
}
{@literal @}Override
public String toString() {
return literal;
}
}
public enum MySubType implements SubCommandType {
NONE(""), // Default for main commands w/o sub commands.
BAR("bar"),
BAZ("baz");
// Literal sub command string used in shell.
private final String literal;
private NeuronSubType(final String name) {
this.literal = name;
}
{@literal @}Override
public String toString() {
return literal;
}
}
// CHECKSTYLE:ON
And then define a literal to command map:
// CHECKSTYLE:OFF public class MyLiteralCommandMap extends LiteralCommandMap {
public MyLiteralCommandMap() {
super(MySubType.NONE); // Default for main commands w/o sub commands.
}
{@literal @}Override
protected void initCommandMap(Map<String, MainCommandType> map) {
for (final MyMainType t : MyMainType.values()) {
map.put(t.toString(), t);
}
}
{@literal @}Override
protected void initSubCommandMap(Map<String, SubCommandType> map) {
for (final MySubType t : MySubType.values()) {
if (t.toString().isEmpty()) {
continue; // Ignore to do not recognize empty strings as sub command in parser.
}
map.put(t.toString(), t);
}
}
}
// CHECKSTYLE:ON
Now you can implement a simple REPL:
// CHECKSTYLE:OFF Parser parser = Parsers.newParser(new MyLiteralCommandMap());
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
while (true) {
System.out.println("prompt> ");
final String inputLine = input.readLine();
try {
final ShellCommand cmd = parser.parse(inputLine);
// Do something with the shell command...
if (shellCmd.getCommand() == NeuronMainType.EXIT) {
break;
}
} catch (SyntaxException ex) {
System.out.println("Error: " + ex.getMessage());
}
}
input.close();
// CHECKSTYLE:ON
If you want to verify the parsed commands, e.g. check if a command has a proper subcommand or arguments
you must implement CommandVerifier and pass it to the
parser factory.
Copyright © 2014 Sven Strittmatter. All Rights Reserved.