Skip to content

Commit 53ecc2d

Browse files
authored
docs: add upstream docs to README (#113)
Copy current proposed documentation into README so can discuss changes over PR in the usual way.
1 parent aeb642d commit 53ecc2d

File tree

1 file changed

+91
-16
lines changed

1 file changed

+91
-16
lines changed

β€ŽREADME.md

Lines changed: 91 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,89 @@
33

44
[![Coverage][coverage-image]][coverage-url]
55

6-
>
7-
> 🚨 THIS REPO IS AN EARLY WIP -- DO NOT USE ... yet 🚨
8-
>
9-
10-
Polyfill of future proposal to the [nodejs/tooling](https://github.com/nodejs/tooling) repo for `util.parseArgs()`
11-
12-
### Scope
13-
14-
It is already possible to build great arg parsing modules on top of what Node.js provides; the prickly API is abstracted away by these modules. Thus, process.parseArgs() is not necessarily intended for library authors; it is intended for developers of simple CLI tools, ad-hoc scripts, deployed Node.js applications, and learning materials.
15-
16-
It is exceedingly difficult to provide an API which would both be friendly to these Node.js users while being extensible enough for libraries to build upon. We chose to prioritize these use cases because these are currently not well-served by Node.js' API.
6+
Polyfill of proposal for `util.parseArgs()`
7+
8+
## `util.parseArgs([config])`
9+
10+
<!-- YAML
11+
added: REPLACEME
12+
-->
13+
14+
> Stability: 1 - Experimental
15+
16+
* `config` {Object} Used to provide arguments for parsing and to configure
17+
the parser. `config` supports the following properties:
18+
* `args` {string\[]} array of argument strings. **Default:** `process.argv`
19+
with `execPath` and `filename` removed.
20+
* `options` {Object} Used to describe arguments known to the parser.
21+
Keys of `options` are the long names of options and values are an
22+
{Object} accepting the following properties:
23+
* `type` {string} Type of argument, which must be either `boolean` or `string`.
24+
**Default:** `boolean`.
25+
* `multiple` {boolean} Whether this option can be provided multiple
26+
times. If `true`, all values will be collected in an array. If
27+
`false`, values for the option are last-wins. **Default:** `false`.
28+
* `short` {string} A single character alias for the option.
29+
* `strict`: {boolean} Should an error be thrown when unknown arguments
30+
are encountered, or when arguments are passed that do not match the
31+
`type` configured in `options`.
32+
**Default:** `true`.
33+
34+
* Returns: {Object} An {Object} representing the parsed command line
35+
arguments:
36+
* `values` {Object} With properties and {string} or {boolean} values
37+
corresponding to parsed options passed.
38+
* `positionals` {string\[]}, containing positional arguments.
39+
40+
Provides a higher level API for command-line argument parsing than interacting
41+
with `process.argv` directly.
42+
43+
```mjs
44+
import { parseArgs } from 'util';
45+
const args = ['-f', '--bar', 'b'];
46+
const options = {
47+
foo: {
48+
type: 'boolean',
49+
short: 'f'
50+
},
51+
bar: {
52+
type: 'string'
53+
}
54+
};
55+
const {
56+
values,
57+
positionals
58+
} = parseArgs({ args, options });
59+
```
1760

18-
### Links & Resources
61+
```cjs
62+
const { parseArgs } = require('util');
63+
const args = ['-f', '--bar', 'b'];
64+
const options = {
65+
foo: {
66+
type: 'boolean',
67+
short: 'f'
68+
},
69+
bar: {
70+
type: 'string'
71+
}
72+
};
73+
const {
74+
values,
75+
positionals
76+
} = parseArgs({ args, options });
77+
```
1978

20-
* [Initial Tooling Issue](https://github.com/nodejs/tooling/issues/19)
21-
* [Initial Proposal](https://github.com/nodejs/node/pull/35015)
79+
`util.parseArgs` is experimental and behavior may change. Join the
80+
conversation in [pkgjs/parseargs][] to contribute to the design.
2281

23-
----
82+
-----
2483

2584
<!-- omit in toc -->
2685
## Table of Contents
86+
- [`util.parseArgs([config])`](#utilparseargsconfig)
87+
- [Scope](#scope)
88+
- [Links & Resources](#links--resources)
2789
- [πŸš€ Getting Started](#-getting-started)
2890
- [πŸ™Œ Contributing](#-contributing)
2991
- [πŸ’‘ `process.mainArgs` Proposal](#-processmainargs-proposal)
@@ -32,7 +94,13 @@ It is exceedingly difficult to provide an API which would both be friendly to th
3294
- [πŸ“ƒ Examples](#-examples)
3395
- [F.A.Qs](#faqs)
3496

35-
----
97+
-----
98+
99+
## Scope
100+
101+
It is already possible to build great arg parsing modules on top of what Node.js provides; the prickly API is abstracted away by these modules. Thus, process.parseArgs() is not necessarily intended for library authors; it is intended for developers of simple CLI tools, ad-hoc scripts, deployed Node.js applications, and learning materials.
102+
103+
It is exceedingly difficult to provide an API which would both be friendly to these Node.js users while being extensible enough for libraries to build upon. We chose to prioritize these use cases because these are currently not well-served by Node.js' API.
36104

37105
## πŸš€ Getting Started
38106

@@ -211,5 +279,12 @@ const { values, positionals } = parseArgs({ strict: false, args, options });
211279
- Is `-` a positional? ie, `bash some-test.sh | tap -`
212280
- yes
213281

282+
## Links & Resources
283+
284+
* [Initial Tooling Issue](https://github.com/nodejs/tooling/issues/19)
285+
* [Initial Proposal](https://github.com/nodejs/node/pull/35015)
286+
* [parseArgs Proposal](https://github.com/nodejs/node/pull/42675)
287+
214288
[coverage-image]: https://img.shields.io/nycrc/pkgjs/parseargs
215289
[coverage-url]: https://github.com/pkgjs/parseargs/blob/main/.nycrc
290+
[pkgjs/parseargs]: https://github.com/pkgjs/parseargs

0 commit comments

Comments
Β (0)