yarsg 18.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +182 -0
- package/browser.d.ts +5 -0
- package/browser.mjs +7 -0
- package/build/lib/argsert.js +62 -0
- package/build/lib/command.js +479 -0
- package/build/lib/completion-templates.js +57 -0
- package/build/lib/completion.js +243 -0
- package/build/lib/middleware.js +88 -0
- package/build/lib/parse-command.js +32 -0
- package/build/lib/typings/common-types.js +9 -0
- package/build/lib/typings/yargs-parser-types.js +1 -0
- package/build/lib/usage.js +584 -0
- package/build/lib/utils/apply-extends.js +59 -0
- package/build/lib/utils/is-promise.js +5 -0
- package/build/lib/utils/levenshtein.js +34 -0
- package/build/lib/utils/maybe-async-result.js +17 -0
- package/build/lib/utils/obj-filter.js +10 -0
- package/build/lib/utils/process-argv.js +17 -0
- package/build/lib/utils/set-blocking.js +12 -0
- package/build/lib/utils/which-module.js +10 -0
- package/build/lib/validation.js +305 -0
- package/build/lib/yargs-factory.js +1517 -0
- package/build/lib/yerror.js +9 -0
- package/helpers/helpers.mjs +10 -0
- package/helpers/package.json +3 -0
- package/index.mjs +10 -0
- package/lib/platform-shims/browser.mjs +99 -0
- package/lib/platform-shims/esm.mjs +67 -0
- package/locales/be.json +46 -0
- package/locales/cs.json +51 -0
- package/locales/de.json +46 -0
- package/locales/en.json +55 -0
- package/locales/es.json +46 -0
- package/locales/fi.json +49 -0
- package/locales/fr.json +53 -0
- package/locales/he.json +55 -0
- package/locales/hi.json +49 -0
- package/locales/hu.json +46 -0
- package/locales/id.json +50 -0
- package/locales/it.json +46 -0
- package/locales/ja.json +51 -0
- package/locales/ko.json +49 -0
- package/locales/nb.json +44 -0
- package/locales/nl.json +49 -0
- package/locales/nn.json +44 -0
- package/locales/pirate.json +13 -0
- package/locales/pl.json +49 -0
- package/locales/pt.json +45 -0
- package/locales/pt_BR.json +48 -0
- package/locales/ru.json +51 -0
- package/locales/th.json +46 -0
- package/locales/tr.json +48 -0
- package/locales/uk_UA.json +51 -0
- package/locales/uz.json +52 -0
- package/locales/zh_CN.json +48 -0
- package/locales/zh_TW.json +51 -0
- package/package.json +102 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright 2010 James Halliday (mail@substack.net); Modified work Copyright 2014 Contributors (ben@npmjs.com)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img width="250" src="https://raw.githubusercontent.com/yargs/yargs/main/yargs-logo.png">
|
|
3
|
+
</p>
|
|
4
|
+
<h1 align="center"> Yargs </h1>
|
|
5
|
+
<p align="center">
|
|
6
|
+
<b >Yargs be a node.js library fer hearties tryin' ter parse optstrings</b>
|
|
7
|
+
</p>
|
|
8
|
+
|
|
9
|
+
<br>
|
|
10
|
+
|
|
11
|
+

|
|
12
|
+
[![NPM version][npm-image]][npm-url]
|
|
13
|
+
[![js-standard-style][standard-image]][standard-url]
|
|
14
|
+
[![Coverage][coverage-image]][coverage-url]
|
|
15
|
+
[![Conventional Commits][conventional-commits-image]][conventional-commits-url]
|
|
16
|
+
|
|
17
|
+
## Description
|
|
18
|
+
Yargs helps you build interactive command line tools, by parsing arguments and generating an elegant user interface.
|
|
19
|
+
|
|
20
|
+
It gives you:
|
|
21
|
+
|
|
22
|
+
* commands and (grouped) options (`my-program.js serve --port=5000`).
|
|
23
|
+
* a dynamically generated help menu based on your arguments:
|
|
24
|
+
|
|
25
|
+
```
|
|
26
|
+
mocha [spec..]
|
|
27
|
+
|
|
28
|
+
Run tests with Mocha
|
|
29
|
+
|
|
30
|
+
Commands
|
|
31
|
+
mocha inspect [spec..] Run tests with Mocha [default]
|
|
32
|
+
mocha init <path> create a client-side Mocha setup at <path>
|
|
33
|
+
|
|
34
|
+
Rules & Behavior
|
|
35
|
+
--allow-uncaught Allow uncaught errors to propagate [boolean]
|
|
36
|
+
--async-only, -A Require all tests to use a callback (async) or
|
|
37
|
+
return a Promise [boolean]
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
* generate completion scripts for Bash and Zsh for your command
|
|
41
|
+
* and [tons more](/docs/api.md).
|
|
42
|
+
|
|
43
|
+
## Installation
|
|
44
|
+
|
|
45
|
+
Stable version:
|
|
46
|
+
```bash
|
|
47
|
+
npm i yargs
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Bleeding edge version with the most recent features:
|
|
51
|
+
```bash
|
|
52
|
+
npm i yargs@next
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Usage
|
|
56
|
+
|
|
57
|
+
### Simple Example
|
|
58
|
+
|
|
59
|
+
```javascript
|
|
60
|
+
#!/usr/bin/env node
|
|
61
|
+
import yargs from 'yargs';
|
|
62
|
+
import { hideBin } from 'yargs/helpers';
|
|
63
|
+
const argv = yargs(hideBin(process.argv)).parse()
|
|
64
|
+
|
|
65
|
+
if (argv.ships > 3 && argv.distance < 53.5) {
|
|
66
|
+
console.log('Plunder more riffiwobbles!')
|
|
67
|
+
} else {
|
|
68
|
+
console.log('Retreat from the xupptumblers!')
|
|
69
|
+
}
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
$ ./plunder.js --ships=4 --distance=22
|
|
74
|
+
Plunder more riffiwobbles!
|
|
75
|
+
|
|
76
|
+
$ ./plunder.js --ships 12 --distance 98.7
|
|
77
|
+
Retreat from the xupptumblers!
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
> Note: `hideBin` is a shorthand for `process.argv.slice(2)`. It has the benefit that it takes into account variations in some environments, e.g., [Electron](https://github.com/electron/electron/issues/4690).
|
|
81
|
+
|
|
82
|
+
### Complex Example
|
|
83
|
+
|
|
84
|
+
```javascript
|
|
85
|
+
#!/usr/bin/env node
|
|
86
|
+
import yargs from 'yargs';
|
|
87
|
+
import { hideBin } from 'yargs/helpers';
|
|
88
|
+
|
|
89
|
+
yargs(hideBin(process.argv))
|
|
90
|
+
.command('serve [port]', 'start the server', (yargs) => {
|
|
91
|
+
return yargs
|
|
92
|
+
.positional('port', {
|
|
93
|
+
describe: 'port to bind on',
|
|
94
|
+
default: 5000
|
|
95
|
+
})
|
|
96
|
+
}, (argv) => {
|
|
97
|
+
if (argv.verbose) console.info(`start server on :${argv.port}`)
|
|
98
|
+
serve(argv.port)
|
|
99
|
+
})
|
|
100
|
+
.option('verbose', {
|
|
101
|
+
alias: 'v',
|
|
102
|
+
type: 'boolean',
|
|
103
|
+
description: 'Run with verbose logging'
|
|
104
|
+
})
|
|
105
|
+
.parse()
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
Run the example above with `--help` to see the help for the application.
|
|
109
|
+
|
|
110
|
+
## Supported Platforms
|
|
111
|
+
|
|
112
|
+
### TypeScript
|
|
113
|
+
|
|
114
|
+
yargs has type definitions at [@types/yargs][type-definitions].
|
|
115
|
+
|
|
116
|
+
```
|
|
117
|
+
npm i @types/yargs --save-dev
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
See usage examples in [docs](/docs/typescript.md).
|
|
121
|
+
|
|
122
|
+
### Deno
|
|
123
|
+
|
|
124
|
+
As of `v16`, `yargs` supports [Deno](https://github.com/denoland/deno):
|
|
125
|
+
|
|
126
|
+
```typescript
|
|
127
|
+
import yargs from 'https://deno.land/x/yargs@v17.7.2-deno/deno.ts'
|
|
128
|
+
import { Arguments } from 'https://deno.land/x/yargs@v17.7.2-deno/deno-types.ts'
|
|
129
|
+
|
|
130
|
+
yargs(Deno.args)
|
|
131
|
+
.command('download <files...>', 'download a list of files', (yargs: any) => {
|
|
132
|
+
return yargs.positional('files', {
|
|
133
|
+
describe: 'a list of files to do something with'
|
|
134
|
+
})
|
|
135
|
+
}, (argv: Arguments) => {
|
|
136
|
+
console.info(argv)
|
|
137
|
+
})
|
|
138
|
+
.strictCommands()
|
|
139
|
+
.demandCommand(1)
|
|
140
|
+
.parse()
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
> Note: If you use version tags in url then you also have to add `-deno` flag on the end, like `@17.7.2-deno`
|
|
144
|
+
|
|
145
|
+
### Usage in Browser
|
|
146
|
+
|
|
147
|
+
See examples of using yargs in the browser in [docs](/docs/browser.md).
|
|
148
|
+
|
|
149
|
+
## Documentation
|
|
150
|
+
|
|
151
|
+
### Table of Contents
|
|
152
|
+
|
|
153
|
+
* [Yargs' API](/docs/api.md)
|
|
154
|
+
* [Examples](/docs/examples.md)
|
|
155
|
+
* [Parsing Tricks](/docs/tricks.md)
|
|
156
|
+
* [Stop the Parser](/docs/tricks.md#stop)
|
|
157
|
+
* [Negating Boolean Arguments](/docs/tricks.md#negate)
|
|
158
|
+
* [Numbers](/docs/tricks.md#numbers)
|
|
159
|
+
* [Arrays](/docs/tricks.md#arrays)
|
|
160
|
+
* [Objects](/docs/tricks.md#objects)
|
|
161
|
+
* [Quotes](/docs/tricks.md#quotes)
|
|
162
|
+
* [Advanced Topics](/docs/advanced.md)
|
|
163
|
+
* [Composing Your App Using Commands](/docs/advanced.md#commands)
|
|
164
|
+
* [Building Configurable CLI Apps](/docs/advanced.md#configuration)
|
|
165
|
+
* [Customizing Yargs' Parser](/docs/advanced.md#customizing)
|
|
166
|
+
* [Contributing](/contributing.md)
|
|
167
|
+
|
|
168
|
+
## Supported Node.js Versions
|
|
169
|
+
|
|
170
|
+
Libraries in this ecosystem make a best effort to track
|
|
171
|
+
[Node.js' release schedule](https://nodejs.org/en/about/releases/). Here's [a
|
|
172
|
+
post on why we think this is important](https://medium.com/the-node-js-collection/maintainers-should-consider-following-node-js-release-schedule-ab08ed4de71a).
|
|
173
|
+
|
|
174
|
+
[npm-url]: https://www.npmjs.com/package/yargs
|
|
175
|
+
[npm-image]: https://img.shields.io/npm/v/yargs.svg
|
|
176
|
+
[standard-image]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg
|
|
177
|
+
[standard-url]: http://standardjs.com/
|
|
178
|
+
[conventional-commits-image]: https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg
|
|
179
|
+
[conventional-commits-url]: https://conventionalcommits.org/
|
|
180
|
+
[type-definitions]: https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/yargs
|
|
181
|
+
[coverage-image]: https://img.shields.io/nycrc/yargs/yargs
|
|
182
|
+
[coverage-url]: https://github.com/yargs/yargs/blob/main/.nycrc
|
package/browser.d.ts
ADDED
package/browser.mjs
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { YError } from './yerror.js';
|
|
2
|
+
import { parseCommand } from './parse-command.js';
|
|
3
|
+
const positionName = ['first', 'second', 'third', 'fourth', 'fifth', 'sixth'];
|
|
4
|
+
export function argsert(arg1, arg2, arg3) {
|
|
5
|
+
function parseArgs() {
|
|
6
|
+
return typeof arg1 === 'object'
|
|
7
|
+
? [{ demanded: [], optional: [] }, arg1, arg2]
|
|
8
|
+
: [
|
|
9
|
+
parseCommand(`cmd ${arg1}`),
|
|
10
|
+
arg2,
|
|
11
|
+
arg3,
|
|
12
|
+
];
|
|
13
|
+
}
|
|
14
|
+
try {
|
|
15
|
+
let position = 0;
|
|
16
|
+
const [parsed, callerArguments, _length] = parseArgs();
|
|
17
|
+
const args = [].slice.call(callerArguments);
|
|
18
|
+
while (args.length && args[args.length - 1] === undefined)
|
|
19
|
+
args.pop();
|
|
20
|
+
const length = _length || args.length;
|
|
21
|
+
if (length < parsed.demanded.length) {
|
|
22
|
+
throw new YError(`Not enough arguments provided. Expected ${parsed.demanded.length} but received ${args.length}.`);
|
|
23
|
+
}
|
|
24
|
+
const totalCommands = parsed.demanded.length + parsed.optional.length;
|
|
25
|
+
if (length > totalCommands) {
|
|
26
|
+
throw new YError(`Too many arguments provided. Expected max ${totalCommands} but received ${length}.`);
|
|
27
|
+
}
|
|
28
|
+
parsed.demanded.forEach(demanded => {
|
|
29
|
+
const arg = args.shift();
|
|
30
|
+
const observedType = guessType(arg);
|
|
31
|
+
const matchingTypes = demanded.cmd.filter(type => type === observedType || type === '*');
|
|
32
|
+
if (matchingTypes.length === 0)
|
|
33
|
+
argumentTypeError(observedType, demanded.cmd, position);
|
|
34
|
+
position += 1;
|
|
35
|
+
});
|
|
36
|
+
parsed.optional.forEach(optional => {
|
|
37
|
+
if (args.length === 0)
|
|
38
|
+
return;
|
|
39
|
+
const arg = args.shift();
|
|
40
|
+
const observedType = guessType(arg);
|
|
41
|
+
const matchingTypes = optional.cmd.filter(type => type === observedType || type === '*');
|
|
42
|
+
if (matchingTypes.length === 0)
|
|
43
|
+
argumentTypeError(observedType, optional.cmd, position);
|
|
44
|
+
position += 1;
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
catch (err) {
|
|
48
|
+
console.warn(err.stack);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
function guessType(arg) {
|
|
52
|
+
if (Array.isArray(arg)) {
|
|
53
|
+
return 'array';
|
|
54
|
+
}
|
|
55
|
+
else if (arg === null) {
|
|
56
|
+
return 'null';
|
|
57
|
+
}
|
|
58
|
+
return typeof arg;
|
|
59
|
+
}
|
|
60
|
+
function argumentTypeError(observedType, allowedTypes, position) {
|
|
61
|
+
throw new YError(`Invalid ${positionName[position] || 'manyith'} argument. Expected ${allowedTypes.join(' or ')} but received ${observedType}.`);
|
|
62
|
+
}
|