Skip to the solution
EasyBackend7 min read

What is the node:util parseArgs helper for building CLIs?

223 views

The problem

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-in node:util module (stable since Node 20), parses process.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 -o short alias to the output long option) and positionals (['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 real ERR_PARSE_ARGS_UNKNOWN_OPTION error β€” 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 optional short alias, and an optional default β€” allowPositionals: true is 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: parseArgs handles 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 --help text generation, subcommands, or built-in argument type coercion beyond boolean/string β€” a precise answer names parseArgs as 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 --help output?" β€” both are genuinely outside parseArgs'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?" β€” parseArgs supports a strict: false option 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.

  • nodejs
  • cli
  • parseargs
  • utilities

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

7 min read Β· 8 sections

Read the code

JavaScriptReal node:util parseArgs: genuine flag/alias/positional parsing, and a real strict-mode rejection of an unknown flag
const { parseArgs } = require("node:util");

const { values, positionals } = parseArgs({
  args: process.argv.slice(2),
  options: {
    verbose: { type: "boolean", short: "v", default: false },
    output: { type: "string", short: "o" },
  },
  allowPositionals: true,
});

console.log("values:", values);
console.log("positionals:", positionals);

// $ node cli.js build --verbose -o dist/out.js src/index.js
// values: { verbose: true, output: 'dist/out.js' }
// positionals: [ 'build', 'src/index.js' ]

// $ node cli.js --bogus-flag
// TypeError [ERR_PARSE_ARGS_UNKNOWN_OPTION]: Unknown option '--bogus-flag'.
// To specify a positional argument starting with a '-', place it at the end
// of the command after '--', as in '-- "--bogus-flag"

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 31 of 152 in Node.js

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

All Node.js questions
Up nextEasy

What is the difference between unit, integration, and end-to-end tests for a Node.js API?

Continue