Community JavaScript Snippet

Prompt for y/N Without readline Cruft

Every CLI script needs a confirm() one day. The full Inquirer ceremony is overkill. Here is the small wrapper I copy into deploy scripts when I need 'are you sure? [y/N]' that defaults to no.

Prompt for y/N Without readline Cruft

Every CLI script needs a confirm() one day. The full Inquirer ceremony is overkill. Here is the small wrapper I copy into deploy scripts when I need 'are you sure? [y/N]' that defaults to no.

JavaScript
Frontend
2 snippets
input-output
code-template
petrawilson

By @petrawilson

April 25, 2026

·

Updated May 20, 2026

1,007 views

33

Rate

Splitting the parsing logic from the IO is the design choice that makes this prompt testable. interpretAnswer is a pure function: same input always produces the same output, so the matrix of edge cases (empty string, mixed case, whitespace, junk) is cheap to verify in unit tests. The default-answer parameter is what gives the prompt the right semantics for destructive operations: confirm('Drop database?', { default: false }) defaults to no, so a fat-finger Enter does not nuke the DB. The [y/N] capitalization in promptHint is a convention every veteran terminal user reads as 'default is no'.