【问题标题】:Getting values from Deno stdin从 Deno 标准输入获取值
【发布时间】:2020-01-20 23:50:25
【问题描述】:

我们如何在 Deno 中从标准输入中获取值?

我不知道如何使用Deno.stdin

一个例子将不胜感激。

【问题讨论】:

  • 就这么简单:const stdin = new TextDecoder().decode(await Deno.readAll(Deno.stdin));
  • 自从写了上面的评论后,Deno.readAll 已被弃用,并以readAll 的形式移至deno.land/std/io/util.ts

标签: deno


【解决方案1】:

我们可以在 deno 中使用 prompt。

const input = prompt('Please enter input');

如果输入必须是数字。我们可以使用Number.parseInt(input);

【讨论】:

    【解决方案2】:

    Deno.stdinFile 类型,因此您可以通过提供Uint8Array 作为缓冲区并调用Deno.stdin.read(buf) 来读取它

    window.onload = async function main() {
      const buf = new Uint8Array(1024);
      /* Reading into `buf` from start.
       * buf.subarray(0, n) is the read result.
       * If n is instead Deno.EOF, then it means that stdin is closed.
       */
      const n = await Deno.stdin.read(buf); 
      if (n == Deno.EOF) {
        console.log("Standard input closed")
      } else {
        console.log("READ:", new TextDecoder().decode(buf.subarray(0, n)));
      }
    }
    

    【讨论】:

    • 这不一定读取(整个)单行。例如,当从文件重定向标准输入时,它将读取一个大块而不仅仅是一行。如果行很长,它的读数会少于整行。此外,它可能不一定会读取整个 utf-8 符文,因此可能会破坏文本编码。
    【解决方案3】:

    一个简单的确认,可以用yn来回答:

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

    或者如果你想提示用户输入字符串

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

    【讨论】:

    • 很遗憾,不再适用于 Deno 1.0
    • @phil294 它使用的是0.51.0std,这已经被弃用了。在答案中颠倒了版本,它现在应该可以工作了。
    【解决方案4】:

    我有一个 100% 纯 Deno 的解决方案,但没有经过深入测试

    async function ask(question: string = '', stdin = Deno.stdin, stdout = Deno.stdout) {
      const buf = new Uint8Array(1024);
    
      // Write question to console
      await stdout.write(new TextEncoder().encode(question));
    
      // Read console's input into answer
      const n = <number>await stdin.read(buf);
      const answer = new TextDecoder().decode(buf.subarray(0, n));
    
      return answer.trim();
    }
    
    const answer = await ask(`Tell me your name? `);
    console.log(`Your name is ${answer}`);
    

    以上部分代码取自Kevin Qian的回答

    【讨论】:

    • 这不一定是整行,见我的其他评论。
    【解决方案5】:

    我建议使用Input-Deno 模块。这是文档中的一个示例:

    // For a single question:
    const input = new InputLoop();
    const nodeName = await input.question('Enter the label for the node:');
            
    // output:
            
    // Enter the label for the node:
            
    // Return Value:
    // 'a'
    

    【讨论】:

      【解决方案6】:

      我有一个小型终端样本要使用 Deno TCP echo server 进行测试。是这样的:

      private input = new TextProtoReader(new BufReader(Deno.stdin));
      
      while (true) {
          const line = await this.input.readLine();
          if (line === Deno.EOF) {
              console.log(red('Bye!'));
              break;
          } else {
              // display the line
          }
      }
      

      完整的项目在Github

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-01-22
        • 1970-01-01
        • 1970-01-01
        • 2017-09-26
        • 1970-01-01
        • 2012-01-25
        • 2013-03-30
        • 2012-02-17
        相关资源
        最近更新 更多