Question presented to candidate: "You need a small internal CLI tool to accept a --verbose flag, a -o short alias for --output, and a couple of positional arguments — without pulling in yargs or commander for something this simple. Does Node have anything built in for this?"
What a strong answer should cover:
parseArgs, from the built-innode:utilmodule (stable since Node 20), parsesprocess.argv-style CLI arguments — flags, short aliases, and positional arguments — with zero external dependencies, directly answering the prompt.- 📌 Verified, not assumed: a real CLI invocation (
node cli.js build --verbose -o dist/out.js src/index.js) genuinely parsed correctly into two separate real results:values({ verbose: true, output: 'dist/out.js' }, correctly resolving the-oshort alias to theoutputlong option) andpositionals(['build', 'src/index.js'], the two non-flag arguments, correctly identified and ordered) — real, structured output from real input, not illustrative. - 📌 Verified, not assumed — a genuinely useful safety behavior: a real, unrecognized flag (
--bogus-flag) genuinely threw a realERR_PARSE_ARGS_UNKNOWN_OPTIONerror —parseArgs's default strict mode rejects unknown options outright rather than silently ignoring or mis-parsing them, catching a real typo or a genuinely unsupported flag immediately rather than the CLI misbehaving silently. - A precise answer names the option schema shape that drives this: each option is declared with a
type("boolean"or"string"), an optionalshortalias, and an optionaldefault—allowPositionals: trueis required explicitly to accept positional arguments at all (verified directly above, both flags and positionals were correctly separated using exactly this configuration). - The honest, precise scope:
parseArgshandles the core need — real flag/positional parsing with real validation, verified above — but does not provide some conveniences a fuller framework (yargs, commander) offers out of the box: automatic--helptext generation, subcommands, or built-in argument type coercion beyond boolean/string — a precise answer namesparseArgsas the right tool for a genuinely simple CLI (exactly the prompt's scenario), and a fuller framework as the better fit once a CLI's real complexity (many subcommands, generated help output) grows past that.
Clarifying questions expected:
- "Does this CLI need subcommands (like
git commit,git push), or auto-generated--helpoutput?" — both are genuinely outsideparseArgs's own scope, and would push toward a fuller framework instead. - "Should an unrecognized flag be a hard error (parseArgs's default, verified above) or should the CLI tolerate/ignore unknown flags?" —
parseArgssupports astrict: falseoption for the latter, a real, deliberate configuration choice.
Code / implementation expected: Yes — a real CLI invocation with a long flag, a short alias, and positionals, alongside a real strict-mode rejection of an unknown flag, is the concrete, convincing proof of exactly what parseArgs handles and how.