带有 Deno 的 CLI REPL

Tho*_*ane 8 deno

我想使用 Deno 构建一个 CLI 应用程序,但是我找不到一个模块可以让我不断提示用户进行类似于命令行应用程序到 Node.js 上的 REPL 模块的交互

有什么建议?

Mar*_*nde 5

您可以使用它std/io来构建 REPL。

import { readLines } from "https://deno.land/std@v0.52.0/io/bufio.ts";


async function read() {
   // Listen to stdin input, once a new line is entered return
   for await(const line of readLines(Deno.stdin)) {
      console.log('Received', line)
      return line;
   }
}

console.log('Start typing');
while(true) {
        await read()
}
Run Code Online (Sandbox Code Playgroud)

您可以从这里构建、处理每一行、添加命令等。