How can we get values from standard input in Deno?
I don\'t know how to use Deno.stdin
.
An example would be appreciated.
A simple confirmation which can be answered with y
or n
:
import { readLines } from "https://deno.land/std@0.76.0/io/bufio.ts";
async function confirm(question) {
console.log(question);
for await (const line of readLines(Deno.stdin)) {
if (line === "y") {
return true;
} else if (line === "n") {
return false;
}
}
}
const answer = await confirm("Do you want to go on? [y/n]");
Or if you want to prompt the user for a string:
import { readLines } from "https://deno.land/std@0.76.0/io/bufio.ts";
async function promptString(question) {
console.log(question);
for await (const line of readLines(Deno.stdin)) {
return line;
}
}
const userName = await promptString("Enter your name:");