Readline and terminal interfaces
A CLI should treat input as data and define explicit output and exit behavior. Readline consumes line-oriented input; `util.parseArgs` parses command-line options without interpreting them as shell code. Use `node:readline/promises` or the readline async iterator for finite lines, and close the interface in finally. Input may come from a pipe instead of a terminal; test `process.stdin.isTTY` before prompting or adding terminal escape sequences. Keep machine-readable JSON on stdout and diagnostics on stderr. `parseArgs` supports typed string and boolean options, defaults and strict parsing. Validate business values after parsing: a string option is not automatically a valid numeric limit. `node:repl` serves interactive development, not untrusted input execution. ICU-backed Intl formatting depends on locale data; use explicit locale for human output and stable JSON for automation. Do not print prompts or color escapes into JSON output, and do not pass raw CLI arguments to a shell. Sources: https://nodejs.org/download/release/v22.16.0/docs/api/util.html; https://nodejs.org/download/release/v22.16.0/docs/api/readline.html.
Formal Syntax & Structure
parseArgs({ args, options }); createInterface({ input, terminal })Working Example
import {createInterface} from 'node:readline/promises';import {Readable} from 'node:stream';const rl=createInterface({input:Readable.from(['tea\ncoffee\n\n']),terminal:false});const names=[];try{for await(const line of rl)if(line.trim())names.push(line.trim());}finally{rl.close();}console.log(JSON.stringify(names));