Skip to content
Merged
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
45 changes: 45 additions & 0 deletions docs/modules/parseargs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
description: Modern alternatives to CLI argument parsing packages using Node.js built-in util.parseArgs
---

# Replacements for argument parsers

## `util.parseArgs` (native, since Node.js 16.x)

[`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 options parsing libraries.

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. If you'd like to support stronger types, one of the other options may be a better fit.

## `mri`

[`mri`](https://github.com/lukeed/mri) is a minimalistic argument parser that supports both short and long options, as well as positional arguments.

Example:

```ts
import mri from 'mri'

const options = mri(process.argv.slice(2), {
alias: {
f: 'force',
o: 'output'
},
boolean: ['force']
})
```
29 changes: 29 additions & 0 deletions manifests/preferred.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@
"replacements": ["fetch", "ofetch", "ky"],
"url": {"type": "e18e", "id": "fetch"}
},
"arg": {
"type": "module",
"moduleName": "arg",
"replacements": ["util.parseArgs", "mri"],
"url": {"type": "e18e", "id": "parseargs"}
},
"axios": {
"type": "module",
"moduleName": "axios",
Expand Down Expand Up @@ -2418,6 +2424,12 @@
"replacements": ["node:crypto"],
"url": {"type": "e18e", "id": "md5"}
},
"minimist": {
"type": "module",
"moduleName": "minimist",
"replacements": ["util.parseArgs", "mri"],
"url": {"type": "e18e", "id": "parseargs"}
},
"mkdirp": {
"type": "module",
"moduleName": "mkdirp",
Expand Down Expand Up @@ -2767,6 +2779,12 @@
"moduleName": "xmldom",
"replacements": ["@xmldom/xmldom"],
"url": {"type": "e18e", "id": "xmldom"}
},
"yargs-parser": {
"type": "module",
"moduleName": "yargs-parser",
"replacements": ["util.parseArgs", "mri"],
"url": {"type": "e18e", "id": "parseargs"}
}
},
"replacements": {
Expand Down Expand Up @@ -3212,6 +3230,11 @@
"type": "documented",
"replacementModule": "milliparsec"
},
"mri": {
"id": "mri",
"type": "documented",
"replacementModule": "mri"
},
"nano-staged": {
"id": "nano-staged",
"type": "documented",
Expand Down Expand Up @@ -3461,6 +3484,12 @@
"id": "api/util.html#utilisdeepstrictequalval1-val2-options"
}
},
"util.parseArgs": {
"id": "util.parseArgs",
"type": "native",
"nodeFeatureId": {"moduleName": "node:util", "exportName": "parseArgs"},
"url": {"type": "node", "id": "api/util.html#utilparseargsconfig"}
},
"util.stripVTControlCharacters": {
"id": "util.stripVTControlCharacters",
"type": "native",
Expand Down