-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtinyCLI.js
More file actions
37 lines (31 loc) · 1 KB
/
tinyCLI.js
File metadata and controls
37 lines (31 loc) · 1 KB
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
// Example from https://nodejs.org/api/readline.html#readline_example_tiny_cli
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: "> "
});
var commands = {
"hello": () => {return "world!\n";},
"look": () => {return "It seems someone spat here.\n";},
"taste": (s) => {return "You lick the " + s.join(" ")+ ".\n";},
"quit": () => {rl.close(); },
"exit": () => {rl.close(); },
"win": () => {process.stdout.write("YOU WIN!\n"); rl.close();},
};
function f(line) {
let message = "Say what? I might have heard '" + line.trim() + "'.\n";
let input = line.trim().toLowerCase().split(" ");
if (input[0] in commands) {
message = commands[input[0]](input.slice(1));
}
return message;
}
rl.on("line", (line) => {
var message = f(line);
process.stdout.write(message);
rl.prompt();
}).on("close", () => {
process.stdout.write("Have a great day!\n\n");
process.exit(0);
});