Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CLI REPL with Deno

Tags:

deno

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?

like image 908
Thomas Magane Avatar asked May 21 '20 23:05

Thomas Magane


2 Answers

You can use std/io to build a REPL.

import { readLines } from "https://deno.land/[email protected]/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.

like image 98
Marcos Casagrande Avatar answered Nov 15 '22 17:11

Marcos Casagrande


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

like image 1
NomadCoder Avatar answered Nov 15 '22 17:11

NomadCoder