Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions docs/modules/parseargs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
description: Modern alternatives to CLI argument parsing packages using Node.js built-in util.parseArgs
---

# Replacements for argument parsers

## `util.parseArgs` (native, Node.js)

[`util.parseArgs`](https://nodejs.org/api/util.html#utilparseargsconfig) is built into Node.js (since 18.3.0 and 16.17.0) and can replace many common CLI parsing libraries such as `minimist`, `mri`, `arg`, `meow`, `yargs-parser`, `yargs`, `commander`, and `sade`.

Example:

```ts
import { parseArgs } from 'node:util'

const { values, positionals } = parseArgs({
args: process.argv.slice(2),
options: {
force: { type: 'boolean', short: 'f' },
output: { type: 'string', short: 'o' },
},
allowPositionals: true,
})
```

> [!NOTE]
> `parseArgs` only supports `string` and `boolean` types and does not provide subcommand routing, auto-generated help, or validation. If your CLI relies heavily on these features, evaluate whether the dependency savings justify the added code.
54 changes: 54 additions & 0 deletions manifests/preferred.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
"replacements": ["node:stream"],
"url": {"type": "e18e", "id": "through"}
},
"arg": {
"type": "module",
"moduleName": "arg",
"replacements": ["util.parseArgs"],
"url": {"type": "e18e", "id": "parseargs"}
},
"axios": {
"type": "module",
"moduleName": "axios",
Expand Down Expand Up @@ -84,6 +90,12 @@
"replacements": ["util.styleText", "picocolors", "ansis"],
"url": {"type": "e18e", "id": "chalk"}
},
"commander": {
"type": "module",
"moduleName": "commander",
"replacements": ["util.parseArgs"],
"url": {"type": "e18e", "id": "parseargs"}
},
"core-util-is": {
"type": "module",
"moduleName": "core-util-is",
Expand Down Expand Up @@ -2304,6 +2316,18 @@
"replacements": ["node:crypto"],
"url": {"type": "e18e", "id": "md5"}
},
"meow": {
"type": "module",
"moduleName": "meow",
"replacements": ["util.parseArgs"],
"url": {"type": "e18e", "id": "parseargs"}
},
"minimist": {
"type": "module",
"moduleName": "minimist",
"replacements": ["util.parseArgs"],
"url": {"type": "e18e", "id": "parseargs"}
},
"mkdirp": {
"type": "module",
"moduleName": "mkdirp",
Expand All @@ -2316,6 +2340,12 @@
"replacements": ["day.js", "date-fns", "luxon", "Date"],
"url": {"type": "e18e", "id": "moment"}
},
"mri": {
"type": "module",
"moduleName": "mri",
"replacements": ["util.parseArgs"],
"url": {"type": "e18e", "id": "parseargs"}
},
"node-fetch": {
"type": "module",
"moduleName": "node-fetch",
Expand Down Expand Up @@ -2464,6 +2494,12 @@
"replacements": ["fs.rm", "fs.rmdir", "premove"],
"url": {"type": "e18e", "id": "rimraf"}
},
"sade": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure sade should be in here.

From what I remember it uses mri under the hood to do arguments parsing. But it exists to provide help text etc on top of that.

In a future pr, we should probably add sade as a replacement itself for some packages in fact

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does use mri, yes

"type": "module",
"moduleName": "sade",
"replacements": ["util.parseArgs"],
"url": {"type": "e18e", "id": "parseargs"}
},
"set-value": {
"type": "module",
"moduleName": "set-value",
Expand Down Expand Up @@ -2559,6 +2595,18 @@
"moduleName": "xmldom",
"replacements": ["@xmldom/xmldom"],
"url": {"type": "e18e", "id": "xmldom"}
},
"yargs": {
"type": "module",
"moduleName": "yargs",
"replacements": ["util.parseArgs"],
"url": {"type": "e18e", "id": "parseargs"}
},
"yargs-parser": {
"type": "module",
"moduleName": "yargs-parser",
"replacements": ["util.parseArgs"],
"url": {"type": "e18e", "id": "parseargs"}
}
},
"replacements": {
Expand Down Expand Up @@ -3259,6 +3307,12 @@
"nodeFeatureId": {"moduleName": "node:util", "exportName": "types"},
"url": {"type": "node", "id": "api/util.html#utiltypes"}
},
"util.parseArgs": {
"id": "util.parseArgs",
"type": "native",
"nodeFeatureId": {"moduleName": "node:util", "exportName": "parseArgs"},
"url": {"type": "node", "id": "api/util.html#utilparseargsconfig"}
},
"webpack.output.clean": {
"id": "webpack.output.clean",
"type": "documented",
Expand Down
Loading