Skip to content
Theme:

TIL — Node.js 18.3 comes with command-line arguments parser

Node.js 18.3 comes with a new command-line arguments parser. Having such a common feature built into a runtime is so handy. Of course, it is neither as powerful as popular packages like yargs, minimist or argparse, nor stable at this stage, but in many cases, it is a good enough solution. Have a look!

node greet.mjs --name Dan -c
# Dan is cool
node greet.mjs -n Pawel
# Pawel is not cool
import { parseArgs } from "node:util";

const {
  values: { name, cool },
} = parseArgs({
  options: {
    name: {
      type: "string",
      short: "n",
    },
    cool: {
      type: "boolean",
      short: "c",
    },
  },
});

console.log(`${name} is ${cool ? "cool" : "not cool"}`);

It is nice, isn’t it?! Until next time keep on building great stuff 😘

Comments

  • T
    Thor G.

    Thanks for making me aware! Since Node.js v20, it seems no longer experimental.

    👆 you can use Markdown here

    Your comment is awaiting moderation. Thanks!

Leave a comment

👆 you can use Markdown here

Your comment is awaiting moderation. Thanks!