From 3ddd062d60d621971af59b480ba70e8bf9e705f1 Mon Sep 17 00:00:00 2001 From: Ben Culkin Date: Sat, 21 Nov 2020 18:04:20 -0500 Subject: Rudimentary CLI for new die implementation --- .../bjc/dicelang/neodice/commands/Command.java | 88 ++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 dice/src/example/java/bjc/dicelang/neodice/commands/Command.java (limited to 'dice/src/example/java/bjc/dicelang/neodice/commands/Command.java') diff --git a/dice/src/example/java/bjc/dicelang/neodice/commands/Command.java b/dice/src/example/java/bjc/dicelang/neodice/commands/Command.java new file mode 100644 index 0000000..8460104 --- /dev/null +++ b/dice/src/example/java/bjc/dicelang/neodice/commands/Command.java @@ -0,0 +1,88 @@ +package bjc.dicelang.neodice.commands; + +import java.util.*; +import java.util.function.*; + +import bjc.dicelang.neodice.*; +import bjc.dicelang.neodice.statements.*; + +/** + * A single CLI command. + * + * @author Ben Culkin + * + */ +@FunctionalInterface +public interface Command { + /** + * Execute this command. + * + * @param words The remaining input. + * @param state The current state. + * + * @return The result of executing this command. + */ + public StatementValue execute(Iterator words, DieBoxCLI state); + + /** + * Get the 'short help' or usage summary for this command. + * + * @return The short help for this command. + */ + default String shortHelp() { + return "no short help"; + } + + /** + * Get the 'long help' or detailed usage for this command. + * + * @return The long help for this command. + */ + default String longHelp() { + return "no long help"; + } + + /** + * Create a new command, backed by a function. + * + * @param executor The function which backs the command. + * @param shortHelp The short help string. + * @param longHelp The long help string. + * + * @return A command backed by the function, with help. + */ + static Command newCommand( + BiFunction, DieBoxCLI, StatementValue> executor, + String shortHelp, String longHelp) { + return new FunctionalCommand(executor, shortHelp, longHelp); + } +} + +class FunctionalCommand implements Command { + private final BiFunction, DieBoxCLI, StatementValue> executor; + private final String shortHelp; + private final String longHelp; + + public FunctionalCommand( + BiFunction, DieBoxCLI, StatementValue> executor, String shortHelp, + String longHelp) { + this.executor = executor; + this.shortHelp = shortHelp; + this.longHelp = longHelp; + } + + @Override + public StatementValue execute(Iterator words, DieBoxCLI state) { + return executor.apply(words, state); + } + + @Override + public String shortHelp() { + return shortHelp; + } + + @Override + public String longHelp() { + return longHelp; + } +} \ No newline at end of file -- cgit v1.2.3