argue-cli 1.1.0 → 2.0.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 +5 -5
- package/README.md +150 -66
- package/dist/args.d.ts +17 -0
- package/dist/args.d.ts.map +1 -0
- package/dist/argv.d.ts +14 -0
- package/dist/argv.d.ts.map +1 -0
- package/dist/core.d.ts +19 -0
- package/dist/core.d.ts.map +1 -0
- package/dist/index.cjs +208 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +195 -0
- package/dist/index.js.map +1 -0
- package/dist/options.d.ts +9 -0
- package/dist/options.d.ts.map +1 -0
- package/dist/types.d.ts +14 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/utils.d.ts +24 -0
- package/dist/utils.d.ts.map +1 -0
- package/package.json +33 -29
- package/.npmignore +0 -37
- package/.travis.yml +0 -12
- package/lib/lib.js +0 -416
- package/src/lib.js +0 -393
- package/test/end.js +0 -18
- package/test/expect.js +0 -59
- package/test/mocha.opts +0 -2
- package/test/options-equal.js +0 -172
- package/test/options.js +0 -349
- package/test/read.js +0 -25
- package/test/strict-options-equal.js +0 -132
- package/test/strict-options.js +0 -269
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
The MIT License (MIT)
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2016 TrigenSoftware
|
|
3
|
+
Copyright (c) 2016 - present, TrigenSoftware
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
|
@@ -9,13 +9,13 @@ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
9
9
|
copies of the Software, and to permit persons to whom the Software is
|
|
10
10
|
furnished to do so, subject to the following conditions:
|
|
11
11
|
|
|
12
|
-
The above copyright notice and this permission notice shall be included in
|
|
13
|
-
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
14
|
|
|
15
15
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
16
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
17
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
18
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
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
|
-
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,80 +1,164 @@
|
|
|
1
|
-
[](https://nodei.co/npm/argue-cli/)
|
|
2
|
-
|
|
3
1
|
# argue-cli
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
2
|
+
|
|
3
|
+
[![NPM version][npm]][npm-url]
|
|
4
|
+
[![Node version][node]][node-url]
|
|
5
|
+
[![Dependencies status][deps]][deps-url]
|
|
6
|
+
[![Build status][build]][build-url]
|
|
7
|
+
[![Bundle size][size]][size-url]
|
|
8
|
+
|
|
9
|
+
[npm]: https://img.shields.io/npm/v/argue-cli.svg
|
|
10
|
+
[npm-url]: https://www.npmjs.com/package/argue-cli
|
|
11
|
+
|
|
12
|
+
[node]: https://img.shields.io/node/v/argue-cli.svg
|
|
13
|
+
[node-url]: https://nodejs.org
|
|
14
|
+
|
|
15
|
+
[deps]: https://img.shields.io/librariesio/release/npm/argue-cli
|
|
16
|
+
[deps-url]: https://libraries.io/npm/argue-cli/tree
|
|
17
|
+
|
|
18
|
+
[build]: https://img.shields.io/github/workflow/status/TrigenSoftware/Argue/CI.svg
|
|
19
|
+
[build-url]: https://github.com/TrigenSoftware/Argue/actions
|
|
20
|
+
|
|
21
|
+
[size]: https://img.shields.io/bundlephobia/minzip/argue-cli
|
|
22
|
+
[size-url]: https://bundlephobia.com/package/argue-cli
|
|
23
|
+
|
|
24
|
+
A thin and strongly typed CLI argument parser for Node.js.
|
|
25
|
+
|
|
26
|
+
## Usage
|
|
27
|
+
|
|
28
|
+
1. Install
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
# yarn
|
|
32
|
+
yarn add argue-cli
|
|
33
|
+
# pnpm
|
|
34
|
+
pnpm add argue-cli
|
|
35
|
+
# npm
|
|
36
|
+
npm i argue-cli
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
2. Import in your code and use it!
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
import { read, end, expect, alias, option, readOptions } from 'argue-cli'
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Expect and read one of the commands
|
|
46
|
+
*/
|
|
47
|
+
const command = expect(
|
|
48
|
+
alias('install', 'i'),
|
|
49
|
+
'remove'
|
|
50
|
+
)
|
|
51
|
+
let options = {}
|
|
52
|
+
|
|
53
|
+
if (command === 'install') {
|
|
54
|
+
/**
|
|
55
|
+
* Read passed options
|
|
56
|
+
*/
|
|
57
|
+
options = readOptions(
|
|
58
|
+
option(alias('save', 'S'), Boolean),
|
|
59
|
+
option(alias('saveDev', 'save-dev', 'D'), Boolean),
|
|
60
|
+
option('workspace', String)
|
|
61
|
+
)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Read next argument
|
|
66
|
+
*/
|
|
67
|
+
const packageName = read()
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Expect end of the arguments
|
|
71
|
+
*/
|
|
72
|
+
end()
|
|
73
|
+
|
|
74
|
+
/* ... */
|
|
17
75
|
```
|
|
18
76
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
77
|
+
## API
|
|
78
|
+
|
|
79
|
+
<table>
|
|
80
|
+
<thead>
|
|
81
|
+
<tr>
|
|
82
|
+
<th>Method</th>
|
|
83
|
+
<th>Description</th>
|
|
84
|
+
</tr>
|
|
85
|
+
</thead>
|
|
86
|
+
<tbody>
|
|
87
|
+
<tr>
|
|
88
|
+
<td>
|
|
89
|
+
|
|
90
|
+
```ts
|
|
91
|
+
function read(): string
|
|
24
92
|
```
|
|
25
93
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
94
|
+
</td>
|
|
95
|
+
<td>
|
|
96
|
+
Read next argument. Throws error if no next argument.
|
|
97
|
+
</td>
|
|
98
|
+
</tr>
|
|
99
|
+
<tr>
|
|
100
|
+
<td>
|
|
101
|
+
|
|
102
|
+
```ts
|
|
103
|
+
function end(): void
|
|
30
104
|
```
|
|
31
105
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
])
|
|
106
|
+
</td>
|
|
107
|
+
<td>
|
|
108
|
+
Expectation of the end. Throws an error if there are more arguments left.
|
|
109
|
+
</td>
|
|
110
|
+
</tr>
|
|
111
|
+
<tr>
|
|
112
|
+
<td>
|
|
113
|
+
|
|
114
|
+
```ts
|
|
115
|
+
function expect(...argRefs: ArgRef[]): string
|
|
43
116
|
```
|
|
44
117
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
)
|
|
118
|
+
</td>
|
|
119
|
+
<td>
|
|
120
|
+
Expect one of the given arguments.
|
|
121
|
+
</td>
|
|
122
|
+
</tr>
|
|
123
|
+
<tr>
|
|
124
|
+
<td>
|
|
125
|
+
|
|
126
|
+
```ts
|
|
127
|
+
function alias(name: string, ...aliases: string[]): AliasArgRef
|
|
55
128
|
```
|
|
56
129
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
])
|
|
130
|
+
</td>
|
|
131
|
+
<td>
|
|
132
|
+
Describe argument with aliases.
|
|
133
|
+
</td>
|
|
134
|
+
</tr>
|
|
135
|
+
<tr>
|
|
136
|
+
<td>
|
|
137
|
+
|
|
138
|
+
```ts
|
|
139
|
+
function option(argRef: ArgRef, type: PrimitiveConstructor): OptionReader
|
|
68
140
|
```
|
|
69
141
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
)
|
|
142
|
+
</td>
|
|
143
|
+
<td>
|
|
144
|
+
Describe option with value.
|
|
145
|
+
</td>
|
|
146
|
+
</tr>
|
|
147
|
+
<tr>
|
|
148
|
+
<td>
|
|
149
|
+
|
|
150
|
+
```ts
|
|
151
|
+
function readOptions(...optionReaders: OptionReader[]): OptionResult
|
|
80
152
|
```
|
|
153
|
+
|
|
154
|
+
</td>
|
|
155
|
+
<td>
|
|
156
|
+
Read options from arguments.
|
|
157
|
+
</td>
|
|
158
|
+
</tr>
|
|
159
|
+
</tbody>
|
|
160
|
+
</table>
|
|
161
|
+
|
|
162
|
+
## TypeScript
|
|
163
|
+
|
|
164
|
+
In [API section](#API) types are described in a simplified way. Detailed example of the types you can see [here](test/argue.test-d.ts).
|
package/dist/args.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { AliasArgRef, ArgRef, PrimitiveConstructor, OptionsReaderState, OptionsReaderNext, OptionResult } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Describe argument with aliases.
|
|
4
|
+
* @param name - Main name.
|
|
5
|
+
* @param alias - Alias name.
|
|
6
|
+
* @param restAliases - Rest aliases.
|
|
7
|
+
* @returns Description of argument with aliases..
|
|
8
|
+
*/
|
|
9
|
+
export declare function alias<T extends string>(name: T, alias: string, ...restAliases: string[]): AliasArgRef<T>;
|
|
10
|
+
/**
|
|
11
|
+
* Describe option with value.
|
|
12
|
+
* @param argRef - Option name.
|
|
13
|
+
* @param type - Value type.
|
|
14
|
+
* @returns Option reader.
|
|
15
|
+
*/
|
|
16
|
+
export declare function option<T extends string, K extends PrimitiveConstructor>(argRef: ArgRef<T>, type: K): (option: string, next: OptionsReaderNext, options: OptionsReaderState) => OptionResult<T, K> | null;
|
|
17
|
+
//# sourceMappingURL=args.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"args.d.ts","sourceRoot":"","sources":["../src/args.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,WAAW,EACX,MAAM,EACN,oBAAoB,EACpB,kBAAkB,EAClB,iBAAiB,EACjB,YAAY,EACb,MAAM,SAAS,CAAA;AAGhB;;;;;;GAMG;AACH,wBAAgB,KAAK,CAAC,CAAC,SAAS,MAAM,EAAE,IAAI,EAAE,CAAC,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,WAAW,EAAE,MAAM,EAAE,GAAG,WAAW,CAAC,CAAC,CAAC,CAKxG;AAED;;;;;GAKG;AACH,wBAAgB,MAAM,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,oBAAoB,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,YA8B/E,MAAM,QAAQ,iBAAiB,WAAW,kBAAkB,+BA6B/E"}
|
package/dist/argv.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal state of arguments.
|
|
3
|
+
*/
|
|
4
|
+
export declare const argv: string[];
|
|
5
|
+
/**
|
|
6
|
+
* Reset arguments state.
|
|
7
|
+
*/
|
|
8
|
+
export declare function resetArgs(): void;
|
|
9
|
+
/**
|
|
10
|
+
* Set arguments state.
|
|
11
|
+
* @param args
|
|
12
|
+
*/
|
|
13
|
+
export declare function setArgs(...args: string[]): void;
|
|
14
|
+
//# sourceMappingURL=argv.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"argv.d.ts","sourceRoot":"","sources":["../src/argv.ts"],"names":[],"mappings":"AAGA;;GAEG;AACH,eAAO,MAAM,IAAI,UAAuC,CAAA;AAExD;;GAEG;AACH,wBAAgB,SAAS,SAGxB;AAED;;;GAGG;AACH,wBAAgB,OAAO,CAAC,GAAG,IAAI,EAAE,MAAM,EAAE,QAGxC"}
|
package/dist/core.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { ArgRef } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Read next argument.
|
|
4
|
+
* Throws error if no next argument.
|
|
5
|
+
* @returns Next argument.
|
|
6
|
+
*/
|
|
7
|
+
export declare function read(): string;
|
|
8
|
+
/**
|
|
9
|
+
* Expectation of the end.
|
|
10
|
+
* Throws an error if there are more arguments left.
|
|
11
|
+
*/
|
|
12
|
+
export declare function end(): void;
|
|
13
|
+
/**
|
|
14
|
+
* Expect one of the given arguments.
|
|
15
|
+
* @param argRefs
|
|
16
|
+
* @returns Expected full argument name.
|
|
17
|
+
*/
|
|
18
|
+
export declare function expect<T extends string>(...argRefs: [...ArgRef<T>[]]): T;
|
|
19
|
+
//# sourceMappingURL=core.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"core.d.ts","sourceRoot":"","sources":["../src/core.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAA;AAGhC;;;;GAIG;AACH,wBAAgB,IAAI,WAQnB;AAED;;;GAGG;AACH,wBAAgB,GAAG,SAIlB;AAED;;;;GAIG;AACH,wBAAgB,MAAM,CAAC,CAAC,SAAS,MAAM,EAAE,GAAG,OAAO,EAAE,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,KASpE"}
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
const ARGV_START_INDEX = 2;
|
|
6
|
+
/**
|
|
7
|
+
* Internal state of arguments.
|
|
8
|
+
*/ const argv = process.argv.slice(ARGV_START_INDEX);
|
|
9
|
+
/**
|
|
10
|
+
* Reset arguments state.
|
|
11
|
+
*/ function resetArgs() {
|
|
12
|
+
argv.splice(0, argv.length);
|
|
13
|
+
argv.push(...process.argv.slice(ARGV_START_INDEX));
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Set arguments state.
|
|
17
|
+
* @param args
|
|
18
|
+
*/ function setArgs(...args) {
|
|
19
|
+
argv.splice(0, argv.length);
|
|
20
|
+
argv.push(...args);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Match reference with argument.
|
|
25
|
+
* @param argRef
|
|
26
|
+
* @param arg
|
|
27
|
+
* @returns Full name or null.
|
|
28
|
+
*/ function matchArgName(argRef, arg) {
|
|
29
|
+
if (typeof argRef === 'string') {
|
|
30
|
+
return argRef === arg ? argRef : null;
|
|
31
|
+
}
|
|
32
|
+
if (argRef.name === arg) {
|
|
33
|
+
return argRef.name;
|
|
34
|
+
}
|
|
35
|
+
return argRef.aliases.includes(arg) ? argRef.name : null;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Find full argument name in references.
|
|
39
|
+
* @param argRefs
|
|
40
|
+
* @param arg
|
|
41
|
+
* @returns Found argument's full name or null.
|
|
42
|
+
*/ function findArgName(argRefs, arg) {
|
|
43
|
+
let argRef;
|
|
44
|
+
let argName;
|
|
45
|
+
for (argRef of argRefs){
|
|
46
|
+
argName = matchArgName(argRef, arg);
|
|
47
|
+
if (argName) {
|
|
48
|
+
return argName;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Read next argument.
|
|
56
|
+
* Throws error if no next argument.
|
|
57
|
+
* @returns Next argument.
|
|
58
|
+
*/ function read() {
|
|
59
|
+
const value = argv.shift();
|
|
60
|
+
if (!value) {
|
|
61
|
+
throw new Error('Unexpected end of arguments');
|
|
62
|
+
}
|
|
63
|
+
return value;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Expectation of the end.
|
|
67
|
+
* Throws an error if there are more arguments left.
|
|
68
|
+
*/ function end() {
|
|
69
|
+
if (argv.length) {
|
|
70
|
+
throw new Error(`Unexpected argument "${argv[0]}"`);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Expect one of the given arguments.
|
|
75
|
+
* @param argRefs
|
|
76
|
+
* @returns Expected full argument name.
|
|
77
|
+
*/ function expect(...argRefs) {
|
|
78
|
+
const arg = read();
|
|
79
|
+
const argName = findArgName(argRefs, arg);
|
|
80
|
+
if (!argName) {
|
|
81
|
+
throw new Error(`Unexpected argument "${arg}"`);
|
|
82
|
+
}
|
|
83
|
+
return argName;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Describe argument with aliases.
|
|
88
|
+
* @param name - Main name.
|
|
89
|
+
* @param alias - Alias name.
|
|
90
|
+
* @param restAliases - Rest aliases.
|
|
91
|
+
* @returns Description of argument with aliases..
|
|
92
|
+
*/ function alias(name, alias1, ...restAliases) {
|
|
93
|
+
return {
|
|
94
|
+
name,
|
|
95
|
+
aliases: [
|
|
96
|
+
alias1,
|
|
97
|
+
...restAliases
|
|
98
|
+
]
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Describe option with value.
|
|
103
|
+
* @param argRef - Option name.
|
|
104
|
+
* @param type - Value type.
|
|
105
|
+
* @returns Option reader.
|
|
106
|
+
*/ function option(argRef, type) {
|
|
107
|
+
if (type === String) {
|
|
108
|
+
return (option1, next)=>{
|
|
109
|
+
const argName = matchArgName(argRef, option1);
|
|
110
|
+
if (argName) {
|
|
111
|
+
return {
|
|
112
|
+
[argName]: next(true)
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
return null;
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
if (type === Number) {
|
|
119
|
+
return (option2, next)=>{
|
|
120
|
+
const argName = matchArgName(argRef, option2);
|
|
121
|
+
if (argName) {
|
|
122
|
+
return {
|
|
123
|
+
[argName]: parseFloat(next(true))
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
return null;
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
if (type === Array) {
|
|
130
|
+
return (option3, next, options)=>{
|
|
131
|
+
const argName = matchArgName(argRef, option3);
|
|
132
|
+
if (argName) {
|
|
133
|
+
const prevValues = options[argName];
|
|
134
|
+
const values = next(true).split(',');
|
|
135
|
+
return {
|
|
136
|
+
[argName]: Array.isArray(prevValues) ? prevValues.concat(values) : values
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
return null;
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
return (option4)=>{
|
|
143
|
+
const argName = matchArgName(argRef, option4);
|
|
144
|
+
if (argName) {
|
|
145
|
+
return {
|
|
146
|
+
[argName]: true
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
return null;
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function isOption(arg) {
|
|
154
|
+
return /^--?[^-].*/.test(arg);
|
|
155
|
+
}
|
|
156
|
+
function removePrefix(arg) {
|
|
157
|
+
return arg.replace(/^--?/, '');
|
|
158
|
+
}
|
|
159
|
+
function createOptionReader(optionReaders) {
|
|
160
|
+
const optionReader = optionReaders.reduceRight((readNextOption, readOption)=>{
|
|
161
|
+
if (!readNextOption) {
|
|
162
|
+
return readOption;
|
|
163
|
+
}
|
|
164
|
+
var ref;
|
|
165
|
+
return (option, next, options)=>(ref = readOption(option, next, options)) !== null && ref !== void 0 ? ref : readNextOption(option, next, options)
|
|
166
|
+
;
|
|
167
|
+
}, null);
|
|
168
|
+
return optionReader;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Read options from arguments.
|
|
172
|
+
* @param optionReaders
|
|
173
|
+
* @returns Options with values.
|
|
174
|
+
*/ function readOptions(...optionReaders) {
|
|
175
|
+
if (!argv.length || !optionReaders.length) {
|
|
176
|
+
return {};
|
|
177
|
+
}
|
|
178
|
+
const readOption = createOptionReader(optionReaders);
|
|
179
|
+
const options = {};
|
|
180
|
+
let i = 0;
|
|
181
|
+
let arg = argv[i];
|
|
182
|
+
const next = (required = false)=>{
|
|
183
|
+
arg = argv[++i];
|
|
184
|
+
if (required && !arg) {
|
|
185
|
+
throw new Error('Unexpected end of arguments');
|
|
186
|
+
}
|
|
187
|
+
return arg;
|
|
188
|
+
};
|
|
189
|
+
// eslint-disable-next-line no-unmodified-loop-condition
|
|
190
|
+
while(arg){
|
|
191
|
+
if (isOption(arg)) {
|
|
192
|
+
Object.assign(options, readOption === null || readOption === void 0 ? void 0 : readOption(removePrefix(arg), next, options));
|
|
193
|
+
}
|
|
194
|
+
next();
|
|
195
|
+
}
|
|
196
|
+
return options;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
exports.alias = alias;
|
|
200
|
+
exports.argv = argv;
|
|
201
|
+
exports.end = end;
|
|
202
|
+
exports.expect = expect;
|
|
203
|
+
exports.option = option;
|
|
204
|
+
exports.read = read;
|
|
205
|
+
exports.readOptions = readOptions;
|
|
206
|
+
exports.resetArgs = resetArgs;
|
|
207
|
+
exports.setArgs = setArgs;
|
|
208
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../src/argv.ts","../src/utils.ts","../src/core.ts","../src/args.ts","../src/options.ts"],"sourcesContent":["\nconst ARGV_START_INDEX = 2\n\n/**\n * Internal state of arguments.\n */\nexport const argv = process.argv.slice(ARGV_START_INDEX)\n\n/**\n * Reset arguments state.\n */\nexport function resetArgs() {\n argv.splice(0, argv.length)\n argv.push(...process.argv.slice(ARGV_START_INDEX))\n}\n\n/**\n * Set arguments state.\n * @param args\n */\nexport function setArgs(...args: string[]) {\n argv.splice(0, argv.length)\n argv.push(...args)\n}\n","\nimport { ArgRef } from './types'\n\ntype FlatMerge<T> = T extends infer U ? { [K in keyof U]: U[K] } : never\n\nexport type Merge<T extends readonly [...unknown[]]> = T extends [infer L, ...infer R]\n ? FlatMerge<NonNullable<L> & Merge<R>>\n : unknown\n\nexport type UnionMerge<T extends readonly [...unknown[]]> = T extends [infer L, ...infer R]\n ? L | Merge<R>\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n : any\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\ntype AnyFunction = (...args: any[]) => any\n\nexport type ReturnTypes<T extends readonly [...AnyFunction[]]> = T extends [infer L, ...infer R]\n ? L extends AnyFunction\n ? R extends AnyFunction[]\n ? [ReturnType<L>, ...ReturnTypes<R>]\n : []\n : []\n : []\n\n/**\n * Match reference with argument.\n * @param argRef\n * @param arg\n * @returns Full name or null.\n */\nexport function matchArgName<T extends string>(argRef: ArgRef<T>, arg: string) {\n if (typeof argRef === 'string') {\n return argRef === arg\n ? argRef\n : null\n }\n\n if (argRef.name === arg) {\n return argRef.name\n }\n\n return argRef.aliases.includes(arg)\n ? argRef.name\n : null\n}\n\n/**\n * Find full argument name in references.\n * @param argRefs\n * @param arg\n * @returns Found argument's full name or null.\n */\nexport function findArgName<T extends string>(argRefs: [...ArgRef<T>[]], arg: string) {\n let argRef: ArgRef<T>\n let argName: T | null\n\n for (argRef of argRefs) {\n argName = matchArgName(argRef, arg)\n\n if (argName) {\n return argName\n }\n }\n\n return null\n}\n","import { argv } from './argv'\nimport { ArgRef } from './types'\nimport { findArgName } from './utils'\n\n/**\n * Read next argument.\n * Throws error if no next argument.\n * @returns Next argument.\n */\nexport function read() {\n const value = argv.shift()\n\n if (!value) {\n throw new Error('Unexpected end of arguments')\n }\n\n return value\n}\n\n/**\n * Expectation of the end.\n * Throws an error if there are more arguments left.\n */\nexport function end() {\n if (argv.length) {\n throw new Error(`Unexpected argument \"${argv[0]}\"`)\n }\n}\n\n/**\n * Expect one of the given arguments.\n * @param argRefs\n * @returns Expected full argument name.\n */\nexport function expect<T extends string>(...argRefs: [...ArgRef<T>[]]) {\n const arg = read()\n const argName = findArgName(argRefs, arg)\n\n if (!argName) {\n throw new Error(`Unexpected argument \"${arg}\"`)\n }\n\n return argName\n}\n","import {\n AliasArgRef,\n ArgRef,\n PrimitiveConstructor,\n OptionsReaderState,\n OptionsReaderNext,\n OptionResult\n} from './types'\nimport { matchArgName } from './utils'\n\n/**\n * Describe argument with aliases.\n * @param name - Main name.\n * @param alias - Alias name.\n * @param restAliases - Rest aliases.\n * @returns Description of argument with aliases..\n */\nexport function alias<T extends string>(name: T, alias: string, ...restAliases: string[]): AliasArgRef<T> {\n return {\n name,\n aliases: [alias, ...restAliases]\n }\n}\n\n/**\n * Describe option with value.\n * @param argRef - Option name.\n * @param type - Value type.\n * @returns Option reader.\n */\nexport function option<T extends string, K extends PrimitiveConstructor>(argRef: ArgRef<T>, type: K) {\n if (type === String) {\n return (option: string, next: OptionsReaderNext) => {\n const argName = matchArgName(argRef, option)\n\n if (argName) {\n return {\n [argName]: next(true)\n } as OptionResult<T, K>\n }\n\n return null\n }\n }\n\n if (type === Number) {\n return (option: string, next: OptionsReaderNext) => {\n const argName = matchArgName(argRef, option)\n\n if (argName) {\n return {\n [argName]: parseFloat(next(true))\n } as OptionResult<T, K>\n }\n\n return null\n }\n }\n\n if (type === Array) {\n return (option: string, next: OptionsReaderNext, options: OptionsReaderState) => {\n const argName = matchArgName(argRef, option)\n\n if (argName) {\n const prevValues = options[argName]\n const values = next(true).split(',')\n\n return {\n [argName]: Array.isArray(prevValues)\n ? prevValues.concat(values)\n : values\n } as OptionResult<T, K>\n }\n\n return null\n }\n }\n\n return (option: string) => {\n const argName = matchArgName(argRef, option)\n\n if (argName) {\n return {\n [argName]: true\n } as OptionResult<T, K>\n }\n\n return null\n }\n}\n","import { argv } from './argv'\nimport { OptionReader } from './types'\nimport { Merge, ReturnTypes, UnionMerge } from './utils'\n\nfunction isOption(arg: string) {\n return /^--?[^-].*/.test(arg)\n}\n\nfunction removePrefix(arg: string) {\n return arg.replace(/^--?/, '')\n}\n\nfunction createOptionReader<T extends OptionReader[]>(optionReaders: [...T]) {\n const optionReader = optionReaders.reduceRight<OptionReader | null>(\n (readNextOption, readOption) => {\n if (!readNextOption) {\n return readOption\n }\n\n return (option, next, options) => readOption(option, next, options) ?? readNextOption(option, next, options)\n },\n null\n )\n\n return optionReader as OptionReader<UnionMerge<ReturnTypes<T>>> | null\n}\n\n/**\n * Read options from arguments.\n * @param optionReaders\n * @returns Options with values.\n */\nexport function readOptions<T extends OptionReader[]>(...optionReaders: [...T]): Partial<Merge<ReturnTypes<T>>> {\n if (!argv.length || !optionReaders.length) {\n return {}\n }\n\n const readOption = createOptionReader(optionReaders)\n const options = {}\n let i = 0\n let arg = argv[i]\n const next = (required = false) => {\n arg = argv[++i]\n\n if (required && !arg) {\n throw new Error('Unexpected end of arguments')\n }\n\n return arg\n }\n\n // eslint-disable-next-line no-unmodified-loop-condition\n while (arg) {\n if (isOption(arg)) {\n Object.assign(options, readOption?.(removePrefix(arg), next, options))\n }\n\n next()\n }\n\n return options\n}\n"],"names":["ARGV_START_INDEX","argv","process","slice","resetArgs","splice","length","push","setArgs","args","matchArgName","argRef","arg","name","aliases","includes","findArgName","argRefs","argName","read","value","shift","Error","end","expect","alias","restAliases","option","type","String","next","Number","parseFloat","Array","options","prevValues","values","split","isArray","concat","isOption","test","removePrefix","replace","createOptionReader","optionReaders","optionReader","reduceRight","readNextOption","readOption","readOptions","i","required","Object","assign"],"mappings":";;;;AACA,MAAMA,gBAAgB,GAAG,CAAC;AAE1B;;IAGY,MAACC,IAAI,GAAGC,OAAO,CAACD,IAAI,CAACE,KAAK,CAACH,gBAAgB;AAEvD;;aAGgBI,SAAS,GAAG;IAC1BH,IAAI,CAACI,MAAM,CAAC,CAAC,EAAEJ,IAAI,CAACK,MAAM;IAC1BL,IAAI,CAACM,IAAI,IAAIL,OAAO,CAACD,IAAI,CAACE,KAAK,CAACH,gBAAgB;AAClD,CAAC;AAED;;;aAIgBQ,OAAO,IAAIC,IAAI,EAAY;IACzCR,IAAI,CAACI,MAAM,CAAC,CAAC,EAAEJ,IAAI,CAACK,MAAM;IAC1BL,IAAI,CAACM,IAAI,IAAIE,IAAI;AACnB;;ACEA;;;;;aAMgBC,YAAY,CAAmBC,MAAiB,EAAEC,GAAW,EAAE;IAC7E,IAAI,OAAOD,MAAM,KAAK,UAAU;QAC9B,OAAOA,MAAM,KAAKC,GAAG,GACjBD,MAAM,GACN,IAAI;KACT;IAED,IAAIA,MAAM,CAACE,IAAI,KAAKD,GAAG,EAAE;QACvB,OAAOD,MAAM,CAACE,IAAI;KACnB;IAED,OAAOF,MAAM,CAACG,OAAO,CAACC,QAAQ,CAACH,GAAG,IAC9BD,MAAM,CAACE,IAAI,GACX,IAAI;AACV,CAAC;AAED;;;;;aAMgBG,WAAW,CAAmBC,OAAyB,EAAEL,GAAW,EAAE;IACpF,IAAID,MAAM;IACV,IAAIO,OAAO;IAEX,KAAKP,MAAM,IAAIM,OAAO,CAAE;QACtBC,OAAO,GAAGR,YAAY,CAACC,MAAM,EAAEC,GAAG;QAElC,IAAIM,OAAO,EAAE;YACX,OAAOA,OAAO;SACf;KACF;IAED,OAAO,IAAI;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