3

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

 1 year ago
source link: https://pawelgrzybek.com/til-node-js-18-3-comes-with-command-line-arguments-parser/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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

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

Published: 2022.06.16 | 1 minutes read

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 😘

Leave a comment

Name:

Website (optional):

Twitter (optional):

GitHub (optional):

Comment:

👆 you can use Markdown here

Save my data for the next time I comment

© 2022 Copyright Pawel Grzybek. All rights reserved.
This site is built with Hugo and hosted on Netlify . The source code is hosted on GitHub.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK