-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsoleExample.js
117 lines (103 loc) · 2.19 KB
/
consoleExample.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { CommandHandler } from "../src/index.js";
/*
This example is a simple console commands handler
*/
let username = "guest";
let handler = new CommandHandler({
prefix: "",
buildArguments: (build) => [build.args],
});
handler.on("invalidUsage", ({ command, errors }) => {
console.log("/!\\ Invalid Usage!");
console.log("Usage: " + handler.prettyPrint(command));
console.log(errors.map((x) => "- " + x.message).join("\n"));
});
handler.on("failedChecks", ({ checks }) => {
console.log("(x) Error: Failed Checks:");
console.log(checks.map((x) => "- " + x.message).join("\n"));
});
// -- commands --
handler.registerCommand({
name: "help",
desc: "Shows commands",
async run(args) {
handler.Commands.forEach((cmd) => {
console.log("> " + cmd.name + " : " + cmd.desc);
if (cmd.args && cmd.args.length)
console.log(" Usage: " + handler.prettyPrint(cmd));
});
},
});
handler.registerCommand({
name: "say",
desc: "Repeats your words",
args: [
{
type: "text",
rest: true,
},
],
async run([text]) {
// Because rest: true, it all gets collected to the first element
console.log(text);
},
});
handler.registerCommand({
name: "add",
desc: "Add two numbers",
args: [
{
type: "number",
name: "a",
},
{
type: "number",
name: "b",
},
],
async run([a, b]) {
let sum = a + b;
console.log(a + " + " + b + " = " + sum);
},
});
handler.registerCommand({
name: "exit",
desc: "Exit this example",
async run() {
console.log("OK, bye!");
process.exit();
},
});
handler.registerCommand({
name: "su",
desc: "Switch user",
args: ["uname:string"],
async run([uname]) {
username = uname;
console.log("Welcome back, " + username + "!");
},
});
handler.registerCommand({
name: "make_sandwich",
desc: "No (unless you're 'root')",
checks: [
async () => {
if (username == "root") {
// Okay.
return { pass: true };
} else {
return { pass: false, message: "What? Make it yourself." };
}
},
],
async run() {
console.log("Ok, heres your virtual sandwich:");
console.log(" 🥪 ");
},
});
var stdin = process.openStdin();
stdin.addListener("data", (d) => {
let input = d.toString().trim();
handler.run(input);
});
handler.run("help");