Skip to content

Instantly share code, notes, and snippets.

@marcogrcr
Last active April 10, 2025 14:49
Show Gist options
  • Save marcogrcr/9bb43c35819be66692471b01105e0a56 to your computer and use it in GitHub Desktop.
Save marcogrcr/9bb43c35819be66692471b01105e0a56 to your computer and use it in GitHub Desktop.
Confirms a y/n in a CLI
import { createInterface } from "node:readline";
const rl = createInterface({ input: process.stdin, terminal: false });
process.stdout.write("Please confirm with y/n: ");
for await (const line of rl) {
if (line === "y") {
break;
}
if (line === "n") {
process.exit(0);
}
process.stdout.write("Invalid input. Please confirm with y/n: ");
}
rl.close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment