Skip to content

Commit aecab99

Browse files
bjnewmanBenjamin Newman43081jghostdevv
authored
feat: add cli arg parser replacements (#274)
* docs: add util.parseArgs replacements for CLI argument parsers * docs: drop cli builders * chore: update readme * chore: remove redundant url --------- Co-authored-by: Benjamin Newman <[email protected]> Co-authored-by: James Garbutt <[email protected]> Co-authored-by: Willow (GHOST) <[email protected]>
1 parent 63cf8fd commit aecab99

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

docs/modules/parseargs.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
description: Modern alternatives to CLI argument parsing packages using Node.js built-in util.parseArgs
3+
---
4+
5+
# Replacements for argument parsers
6+
7+
## `util.parseArgs` (native, since Node.js 16.x)
8+
9+
[`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.
10+
11+
Example:
12+
13+
```ts
14+
import { parseArgs } from 'node:util'
15+
16+
const { values, positionals } = parseArgs({
17+
args: process.argv.slice(2),
18+
options: {
19+
force: { type: 'boolean', short: 'f' },
20+
output: { type: 'string', short: 'o' }
21+
},
22+
allowPositionals: true
23+
})
24+
```
25+
26+
> [!NOTE]
27+
> `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.
28+
29+
## `mri`
30+
31+
[`mri`](https://github.com/lukeed/mri) is a minimalistic argument parser that supports both short and long options, as well as positional arguments.
32+
33+
Example:
34+
35+
```ts
36+
import mri from 'mri'
37+
38+
const options = mri(process.argv.slice(2), {
39+
alias: {
40+
f: 'force',
41+
o: 'output'
42+
},
43+
boolean: ['force']
44+
})
45+
```

manifests/preferred.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@
3636
"replacements": ["fetch", "ofetch", "ky"],
3737
"url": {"type": "e18e", "id": "fetch"}
3838
},
39+
"arg": {
40+
"type": "module",
41+
"moduleName": "arg",
42+
"replacements": ["util.parseArgs", "mri"],
43+
"url": {"type": "e18e", "id": "parseargs"}
44+
},
3945
"axios": {
4046
"type": "module",
4147
"moduleName": "axios",
@@ -2418,6 +2424,12 @@
24182424
"replacements": ["node:crypto"],
24192425
"url": {"type": "e18e", "id": "md5"}
24202426
},
2427+
"minimist": {
2428+
"type": "module",
2429+
"moduleName": "minimist",
2430+
"replacements": ["util.parseArgs", "mri"],
2431+
"url": {"type": "e18e", "id": "parseargs"}
2432+
},
24212433
"mkdirp": {
24222434
"type": "module",
24232435
"moduleName": "mkdirp",
@@ -2767,6 +2779,12 @@
27672779
"moduleName": "xmldom",
27682780
"replacements": ["@xmldom/xmldom"],
27692781
"url": {"type": "e18e", "id": "xmldom"}
2782+
},
2783+
"yargs-parser": {
2784+
"type": "module",
2785+
"moduleName": "yargs-parser",
2786+
"replacements": ["util.parseArgs", "mri"],
2787+
"url": {"type": "e18e", "id": "parseargs"}
27702788
}
27712789
},
27722790
"replacements": {
@@ -3212,6 +3230,11 @@
32123230
"type": "documented",
32133231
"replacementModule": "milliparsec"
32143232
},
3233+
"mri": {
3234+
"id": "mri",
3235+
"type": "documented",
3236+
"replacementModule": "mri"
3237+
},
32153238
"nano-staged": {
32163239
"id": "nano-staged",
32173240
"type": "documented",
@@ -3461,6 +3484,12 @@
34613484
"id": "api/util.html#utilisdeepstrictequalval1-val2-options"
34623485
}
34633486
},
3487+
"util.parseArgs": {
3488+
"id": "util.parseArgs",
3489+
"type": "native",
3490+
"nodeFeatureId": {"moduleName": "node:util", "exportName": "parseArgs"},
3491+
"url": {"type": "node", "id": "api/util.html#utilparseargsconfig"}
3492+
},
34643493
"util.stripVTControlCharacters": {
34653494
"id": "util.stripVTControlCharacters",
34663495
"type": "native",

0 commit comments

Comments
 (0)