argue-cli 1.2.1 → 2.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 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
- all copies or substantial portions of the Software.
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
- THE SOFTWARE.
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  [![NPM version][npm]][npm-url]
4
4
  [![Node version][node]][node-url]
5
+ [![Dependencies status][deps]][deps-url]
6
+ [![Build status][build]][build-url]
7
+ [![Coverage status][coverage]][coverage-url]
8
+ [![Bundle size][size]][size-url]
5
9
 
6
10
  [npm]: https://img.shields.io/npm/v/argue-cli.svg
7
11
  [npm-url]: https://www.npmjs.com/package/argue-cli
@@ -9,91 +13,156 @@
9
13
  [node]: https://img.shields.io/node/v/argue-cli.svg
10
14
  [node-url]: https://nodejs.org
11
15
 
12
- Node.js CLI arguments parser.
16
+ [deps]: https://img.shields.io/librariesio/release/npm/argue-cli
17
+ [deps-url]: https://libraries.io/npm/argue-cli/tree
13
18
 
14
- # Install
19
+ [build]: https://img.shields.io/github/workflow/status/TrigenSoftware/Argue/CI.svg
20
+ [build-url]: https://github.com/TrigenSoftware/Argue/actions
21
+
22
+ [coverage]: https://img.shields.io/coveralls/TrigenSoftware/Argue.svg
23
+ [coverage-url]: https://coveralls.io/r/TrigenSoftware/Argue
24
+
25
+ [size]: https://img.shields.io/bundlephobia/minzip/argue-cli
26
+ [size-url]: https://bundlephobia.com/package/argue-cli
27
+
28
+ A thin and strongly typed CLI argument parser for Node.js.
29
+
30
+ ## Usage
31
+
32
+ 1. Install
15
33
 
16
34
  ```bash
17
- npm i -S argue-cli
18
- # or
35
+ # yarn
19
36
  yarn add argue-cli
37
+ # pnpm
38
+ pnpm add argue-cli
39
+ # npm
40
+ npm i argue-cli
20
41
  ```
21
42
 
22
- # API
23
-
24
- ### expect(...names)
25
- Strict expectation one of given commands.
26
- Returns full variant of expected argument.
27
- ```js
28
- expect(
29
- {"install": "i"}, // full name and shirt name, e.g. `npm install`, `npm i`
30
- ["update", "u"], // also full name and shirt name, e.g. `npm update`, `npm u`
31
- "info" // only one variant of name
32
- );
43
+ 2. Import in your code and use it!
44
+
45
+ ```ts
46
+ import { read, end, expect, alias, option, readOptions } from 'argue-cli'
47
+
48
+ /**
49
+ * Expect and read one of the commands
50
+ */
51
+ const command = expect(
52
+ alias('install', 'i'),
53
+ 'remove'
54
+ )
55
+ let options = {}
56
+
57
+ if (command === 'install') {
58
+ /**
59
+ * Read passed options
60
+ */
61
+ options = readOptions(
62
+ option(alias('save', 'S'), Boolean),
63
+ option(alias('saveDev', 'save-dev', 'D'), Boolean),
64
+ option('workspace', String)
65
+ )
66
+ }
67
+
68
+ /**
69
+ * Read next argument
70
+ */
71
+ const packageName = read()
72
+
73
+ /**
74
+ * Expect end of the arguments
75
+ */
76
+ end()
77
+
78
+ /* ... */
33
79
  ```
34
80
 
35
- ### read()
36
- Strict reading of argument.
37
- Returns argument.
38
- ```js
39
- read(); // e.g. for `npm babel` returns "babel"
81
+ ## API
82
+
83
+ <table>
84
+ <thead>
85
+ <tr>
86
+ <th>Method</th>
87
+ <th>Description</th>
88
+ </tr>
89
+ </thead>
90
+ <tbody>
91
+ <tr>
92
+ <td>
93
+
94
+ ```ts
95
+ function read(): string
40
96
  ```
41
97
 
42
- ### end()
43
- Strict expectation of end.
44
- ```js
45
- end(); // e.g. for `npm babel` throws Error.
98
+ </td>
99
+ <td>
100
+ Read next argument. Throws error if no next argument.
101
+ </td>
102
+ </tr>
103
+ <tr>
104
+ <td>
105
+
106
+ ```ts
107
+ function end(): void
46
108
  ```
47
109
 
48
- ### strictOptions(flagsNames, optionsNames)
49
- Strict reading of flags and options.
50
- Returns fullname-value pairs object.
51
- ```js
52
- strictOptions([
53
- ["another"], // for flags array is same as object notation
54
- "verbose" // only one variant of name, e.g. `babel --verbose`
55
- ], [
56
- {"output": "o"}, // full name and shirt name, e.g. `babel --output ./main.js`, `babel -o ./main.js`
57
- ["plugins", "p"] // fullname and shirtname for array, e.g. `babel --plugins commonjs,decorators`, `babel -p commonjs,decorators`
58
- ])
110
+ </td>
111
+ <td>
112
+ Expectation of the end. Throws an error if there are more arguments left.
113
+ </td>
114
+ </tr>
115
+ <tr>
116
+ <td>
117
+
118
+ ```ts
119
+ function expect(...argRefs: ArgRef[]): string
59
120
  ```
60
121
 
61
- ### strictOptionsEqual(...names)
62
- Strict reading of options with equal sign.
63
- If option is provided without value it will interpreted as `true`.
64
- Returns fullname-value pairs object.
65
- ```js
66
- strictOptionsEqual(
67
- {"output": "o"}, // full name and shirt name, e.g. `babel --output=./main.js`, `babel -o=./main.js`
68
- ["plugins", "p"], // fullname and shirtname for array, e.g. `babel --plugins=commonjs,decorators`, `babel -p=commonjs,decorators`
69
- "verbose" // only one variant of name, e.g. `babel --verbose`
70
- )
122
+ </td>
123
+ <td>
124
+ Expect one of the given arguments.
125
+ </td>
126
+ </tr>
127
+ <tr>
128
+ <td>
129
+
130
+ ```ts
131
+ function alias(name: string, ...aliases: string[]): AliasArgRef
71
132
  ```
72
133
 
73
- ### options(flagsNames, optionsNames)
74
- Unlimited reading of flags and options.
75
- Returns fullname-value pairs object.
76
- ```js
77
- options([
78
- ["another"], // for flags array is same as object notation
79
- "verbose" // only one variant of name, e.g. `babel compile script.js --verbose`
80
- ], [
81
- {"output": "o"}, // full name and shirt name, e.g. `babel compile script.js --output ./main.js`, `babel compile script.js -o ./main.js`
82
- ["plugins", "p"] // fullname and shirtname for array, e.g. `babel --plugins commonjs,decorators compile script.js`, `babel -p commonjs,decorators compile script.js`
83
- ])
134
+ </td>
135
+ <td>
136
+ Describe argument with aliases.
137
+ </td>
138
+ </tr>
139
+ <tr>
140
+ <td>
141
+
142
+ ```ts
143
+ function option(argRef: ArgRef, type: PrimitiveConstructor): OptionReader
84
144
  ```
85
145
 
86
- ### optionsEqual(...names)
87
- Unlimited reading of options with equal sign.
88
- If option is provided without value it will interpreted as `true`.
89
- Returns fullname-value pairs object.
90
- ```js
91
- optionsEqual(
92
- {"output": "o"}, // full name and shirt name, e.g. `babel compile script.js --output=./main.js`, `babel compile script.js -o=./main.js`
93
- ["plugins", "p"], // fullname and shirtname for array, e.g. `babel --plugins=commonjs,decorators compile script.js`, `babel -p=commonjs,decorators compile script.js`
94
- "verbose" // only one variant of name, e.g. `babel compile script.js --verbose`
95
- )
146
+ </td>
147
+ <td>
148
+ Describe option with value.
149
+ </td>
150
+ </tr>
151
+ <tr>
152
+ <td>
153
+
154
+ ```ts
155
+ function readOptions(...optionReaders: OptionReader[]): OptionResult
96
156
  ```
97
157
 
98
- ---
99
- [![NPM](https://nodei.co/npm/argue-cli.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/argue-cli/)
158
+ </td>
159
+ <td>
160
+ Read options from arguments.
161
+ </td>
162
+ </tr>
163
+ </tbody>
164
+ </table>
165
+
166
+ ## TypeScript
167
+
168
+ 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, OptionsReaderRead, 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, read: OptionsReaderRead, 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,223 @@
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, read)=>{
109
+ const argName = matchArgName(argRef, option1);
110
+ if (argName) {
111
+ return {
112
+ [argName]: read()
113
+ };
114
+ }
115
+ return null;
116
+ };
117
+ }
118
+ if (type === Number) {
119
+ return (option2, read)=>{
120
+ const argName = matchArgName(argRef, option2);
121
+ if (argName) {
122
+ return {
123
+ [argName]: parseFloat(read())
124
+ };
125
+ }
126
+ return null;
127
+ };
128
+ }
129
+ if (type === Array) {
130
+ return (option3, read, options)=>{
131
+ const argName = matchArgName(argRef, option3);
132
+ if (argName) {
133
+ const prevValues = options[argName];
134
+ const values = read().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, read, options)=>(ref = readOption(option, read, options)) !== null && ref !== void 0 ? ref : readNextOption(option, read, 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) {
176
+ return {};
177
+ }
178
+ const readOption = createOptionReader(optionReaders);
179
+ if (!readOption) {
180
+ return {};
181
+ }
182
+ const options = {};
183
+ let i = 0;
184
+ let arg = argv[i];
185
+ let optionResult;
186
+ const next = ()=>{
187
+ arg = argv[++i];
188
+ };
189
+ const remove = ()=>{
190
+ argv.splice(i--, 1);
191
+ };
192
+ const read = ()=>{
193
+ next();
194
+ remove();
195
+ if (!arg) {
196
+ throw new Error('Unexpected end of arguments');
197
+ }
198
+ return arg;
199
+ };
200
+ // eslint-disable-next-line no-unmodified-loop-condition
201
+ while(arg){
202
+ if (isOption(arg)) {
203
+ optionResult = readOption(removePrefix(arg), read, options);
204
+ if (optionResult) {
205
+ remove();
206
+ Object.assign(options, optionResult);
207
+ }
208
+ }
209
+ next();
210
+ }
211
+ return options;
212
+ }
213
+
214
+ exports.alias = alias;
215
+ exports.argv = argv;
216
+ exports.end = end;
217
+ exports.expect = expect;
218
+ exports.option = option;
219
+ exports.read = read;
220
+ exports.readOptions = readOptions;
221
+ exports.resetArgs = resetArgs;
222
+ exports.setArgs = setArgs;
223
+ //# 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 OptionsReaderRead,\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, read: OptionsReaderRead) => {\n const argName = matchArgName(argRef, option)\n\n if (argName) {\n return {\n [argName]: read()\n } as OptionResult<T, K>\n }\n\n return null\n }\n }\n\n if (type === Number) {\n return (option: string, read: OptionsReaderRead) => {\n const argName = matchArgName(argRef, option)\n\n if (argName) {\n return {\n [argName]: parseFloat(read())\n } as OptionResult<T, K>\n }\n\n return null\n }\n }\n\n if (type === Array) {\n return (option: string, read: OptionsReaderRead, options: OptionsReaderState) => {\n const argName = matchArgName(argRef, option)\n\n if (argName) {\n const prevValues = options[argName]\n const values = read().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, OptionResult } 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, read, options) => readOption(option, read, options) ?? readNextOption(option, read, 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) {\n return {}\n }\n\n const readOption = createOptionReader(optionReaders)\n\n if (!readOption) {\n return {}\n }\n\n const options = {}\n let i = 0\n let arg = argv[i]\n let optionResult: OptionResult | null\n const next = () => {\n arg = argv[++i]\n }\n const remove = () => {\n argv.splice(i--, 1)\n }\n const read = () => {\n next()\n remove()\n\n if (!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 optionResult = readOption(removePrefix(arg), read, options)\n\n if (optionResult) {\n remove()\n Object.assign(options, optionResult)\n }\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","Number","parseFloat","Array","options","prevValues","values","split","isArray","concat","isOption","test","removePrefix","replace","createOptionReader","optionReaders","optionReader","reduceRight","readNextOption","readOption","readOptions","i","optionResult","next","remove","Object","assign"],"mappings":";;;;AACA,MAAMA,gBAAgB,GA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AU,GAAGL,kBAAkB,CAACC,aAAa;IAEnD,KAAKI,UAAU,EAAE;QACf,OAAO,EAAE;KACV;IAED,MAAMf,OAAO,GAAG,EAAE;IAClB,IAAIiB,CAAC,GAAG,CAAC;IACT,IAAItC,GAAG,GAAGX,IAAI,CAACiD,CAAC;IAChB,IAAIC,YAAY;IAChB,MAAMC,IAAI,OAAS;QACjBxC,GAAG,GAAGX,IAAI,GAAGiD,CAAC;KACf;IACD,MAAMG,MAAM,OAAS;QACnBpD,IAAI,CAACI,MAAM,CAAC6C,CAAC,IAAI,CAAC;KACnB;IACD,MAAM/B,IAAI,OAAS;QACjBiC,IAAI;QACJC,MAAM;QAEN,KAAKzC,GAAG,EAAE;YACR,MAAM,IAAIU,KAAK,CAAC;SACjB;QAED,OAAOV,GAAG;KACX;;UAGMA,GAAG,CAAE;QACV,IAAI2B,QAAQ,CAAC3B,GAAG,GAAG;YACjBuC,YAAY,GAAGH,UAAU,CAACP,YAAY,CAAC7B,GAAG,GAAGO,IAAI,EAAEc,OAAO;YAE1D,IAAIkB,YAAY,EAAE;gBAChBE,MAAM;gBACNC,MAAM,CAACC,MAAM,CAACtB,OAAO,EAAEkB,YAAY;aACpC;SACF;QAEDC,IAAI;KACL;IAED,OAAOnB,OAAO;AAChB;;;;;;;;;;;;"}
@@ -0,0 +1,6 @@
1
+ export * from './types';
2
+ export * from './argv';
3
+ export * from './core';
4
+ export * from './args';
5
+ export * from './options';
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAA;AACvB,cAAc,QAAQ,CAAA;AACtB,cAAc,QAAQ,CAAA;AACtB,cAAc,QAAQ,CAAA;AACtB,cAAc,WAAW,CAAA"}