Getting values from Deno stdin

后端 未结 5 1088
梦毁少年i
梦毁少年i 2021-02-14 11:43

How can we get values from standard input in Deno?

I don\'t know how to use Deno.stdin.

An example would be appreciated.

5条回答
  •  借酒劲吻你
    2021-02-14 12:03

    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:");
    

提交回复
热议问题