Skip to content

Create a Simple CLI Argument Parser

Turn process.argv flags of the form --key=value into an object, treating a bare --key as true.

Language: Node.js

Added on: 21st August 2026

const args = process.argv.slice(2);
const flags = Object.fromEntries(args.map((a) => {
  const [k, v = "true"] = a.replace(/^--/, "").split("=");
  return [k, v];
}));

console.log(flags);

Caveats

Only handles --key and --key=value. No short flags, quoting, or repeated keys (later values overwrite). Positional arguments become keys too.