How to get command line input?

I just tried to write a very simple cli program using rescript and node.

How to read command line input like scanf/cin in cpp?

Ja can follow this tutorial Using stdout, stdin, and stderr in Node.js - LogRocket Blog

Probably you have to create bindings for the functions you will use. Here are some threads and the documentation can help you to write these.

There are already bindings for this rescript-nodejs/Process.res at 66a558001767c9dfd4d2a2fd6c7e56fd1530d60a · TheSpyder/rescript-nodejs · GitHub

1 Like

I know this was over a year ago but in trying to find a solution for ReScript Bun this question is the only thing in the forum so thought I’d post a solution I cobbled together after a lot of googling. I’m using rescript-bun but I guess a similar solution should work for rescript-nodejs.

let {close, question} = module(Readline.Interface)
let rl = Readline.make({
  input: %raw(`process.stdin`),
  output: %raw(`process.stdout`),
})

let answer = await Promise.make((resolve, _reject) => {
  rl->question("? ", resolve)
})

answer->Console.log
rl->close

I was struggling to find what was a RescriptBun.Stream.Readable.subtype<RescriptBun.Buffer.t, 'rtype> for the input and output fields, so I found a similar solution in an old ReasonML forum that used the %raw solution to provide process.stdin/stdout.

I found out via stackoverflow how to get the command line input value out by wrapping the question call in a promise.

1 Like