import roj.ui.CLIUtil;
import roj.ui.terminal.Argument;
import roj.ui.terminal.CommandConsole;
import roj.ui.terminal.CommandImpl;
import static roj.ui.terminal.CommandNode.argument;
import static roj.ui.terminal.CommandNode.literal;
/**
* 代码来源roj.mod.FMDMain
*/
public final class Example {
public static CommandConsole console = new CommandConsole("");
@SuppressWarnings("fallthrough")
public static void main(String[] args) throws Exception {
CommandConsole c = console;
// 简单的指令
c.register(literal("reflect").executes(ctx -> ReflectTool.start(!isCLI)));
c.register(literal("auto").then(argument("auto", Argument.bool()).executes(
ctx -> AutoCompile.setEnabled(ctx.argument("auto", Boolean.class))
)));
// 复杂一点的Argument
CommandImpl cDeobf = ctx -> {};
c.register(literal("deobf").executes(cDeobf)
.then(argument("reverse", Argument.string("mcp2srg", "srg2mcp")).executes(cDeobf)));
// 变长参数
c.register(literal("build").then(
argument("flags", Argument.stringFlags("zl", "showErrorCode", "noupdate")).executes(ctx -> {
List<String> flags = Helpers.cast(ctx.argument("flags", List.class));
Map<String, Object> map = new MyHashMap<>();
for (String flag : flags) map.put(flag, "");
build(map);
})));
// 甚至是自定义参数
Argument.ArgSetOf<File> dynamicProject = new Argument.ArgSetOf<>(1, new MyHashMap<>()) {
@Override
protected void updateChoices() {
choice.clear();
IOUtil.findAllFiles(CONFIG_DIR, (f) -> {
String name = f.getName().toLowerCase();
if (name.endsWith(".json")) choice.put(name.substring(0, name.length() - 5), f);
return false;
});
}
};
// prompt (new的参数也是)
c.setPrompt("114514 > ");
// 别忘了
CLIUtil.setConsole(c);
// Console线程也是daemon的
LockSupport.park();
}
}