从 Deno stdin 获取值

问题描述 投票:0回答:7

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

我不知道如何使用

Deno.stdin

举个例子,我们将不胜感激。

deno
7个回答
19
投票

我们可以在 deno 中使用提示。

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

如果输入必须是数字。我们可以使用

Number.parseInt(input)
;


11
投票

Deno.stdin
的类型为
File
,因此您可以通过提供
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)));
  }
}

9
投票

一个简单的确认,可以用

y
n
回答:

import { readLines } from "https://deno.land/std/io/buffer.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/io/buffer.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:");

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 Qi

的回答

2
投票

我建议使用 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'

0
投票

我有一个小型终端示例,可以使用 Deno TCP echo 服务器 进行测试。类似于:

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


0
投票

读取所有输入以计算行数和字数:

import { toArrayBuffer } from "https://deno.land/[email protected]/streams/mod.ts";

const inputData = new TextDecoder().decode(
  await toArrayBuffer(Deno.stdin.readable),
);

// https://stackoverflow.com/a/8488881/547223
const numberOfLines = inputData.split(/\r\n|\r|\n/).length;

// https://stackoverflow.com/a/18679657/547223
const numberOfWords = inputData.trim().split(/\s+/).length;

console.log(numberOfLines);
console.log(numberOfWords);

如果您想创建一个脚本,例如从标准输入读取文件内容:

$ cat some_file.txt | util.ts
28
70
© www.soinside.com 2019 - 2024. All rights reserved.