CLI REPL with Deno
Asked Answered
W

2

8

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?

Wardieu answered 21/5, 2020 at 23:14 Comment(0)
S
5

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.

Sailplane answered 21/5, 2020 at 23:22 Comment(1)
Nice, but is there anything similar to cmd2 in python?Aalii
S
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

Sweaty answered 19/6, 2020 at 11:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.