9

I would like to build a CLI application using Deno however I can't find a module that allows me to keep prompting the user for interaction similar to command line applications to the REPL module on Node.js

Any suggestions?

2 Answers2

6

You can use std/io to build a 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()
}

You can build from here, process each line, add commands and so on.

Marcos Casagrande
  • 37,983
  • 8
  • 84
  • 98
1

If you just want one line,you can do this

import { readLines } from "https://raw.githubusercontent.com/denoland/deno/master/std/io/bufio.ts";

const word = (await readLines(Deno.stdin).next()).value.trim()

console.log(`You typed: ${word}`)
D:\WorkSpace\VSCode\deno-play>deno run -A main.ts
hello
You typed: hello

Current denoland lib Commits on Jun 19, 2020

NomadCoder
  • 11
  • 2