args-tokens 0.3.2 → 0.4.1
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/README.md +84 -0
- package/lib/cjs/index.d.ts +5 -2
- package/lib/cjs/index.js +5 -16
- package/lib/cjs/parse.d.ts +40 -0
- package/lib/cjs/parse.js +35 -0
- package/lib/cjs/parser.d.ts +3 -3
- package/lib/esm/index.d.ts +5 -2
- package/lib/esm/index.js +2 -1
- package/lib/esm/parse.d.ts +40 -0
- package/lib/esm/parse.js +32 -0
- package/lib/esm/parser.d.ts +3 -3
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -153,6 +153,10 @@ bun add args-tokens
|
|
|
153
153
|
|
|
154
154
|
## 🚀 Usage
|
|
155
155
|
|
|
156
|
+
### Parse args to tokens
|
|
157
|
+
|
|
158
|
+
`parseArgs` will transform arguments into tokens. This function is useful if you want to analyze arguments yourself based on the tokens. It's faster than `node:util` parseArgs because it only focuses on token transformation.
|
|
159
|
+
|
|
156
160
|
```js
|
|
157
161
|
import { parseArgs } from 'args-tokens' // for Node.js and Bun
|
|
158
162
|
// import { parseArgs } from 'jsr:@kazupon/args-tokens' // for Deno
|
|
@@ -163,6 +167,86 @@ const tokens = parseArgs(['--foo', 'bar', '-x', '--bar=baz'])
|
|
|
163
167
|
console.log('tokens:', tokens)
|
|
164
168
|
```
|
|
165
169
|
|
|
170
|
+
## Resolve args value with tokens and arg option schema
|
|
171
|
+
|
|
172
|
+
`resolveArgs` is a useful function when you want to resolve values from the tokens obtained by `parseArgs`.
|
|
173
|
+
|
|
174
|
+
```js
|
|
175
|
+
import { parseArgs, resolveArgs } from 'args-tokens' // for Node.js and Bun
|
|
176
|
+
// import { parseArgs, resolveArgs } from 'jsr:@kazupon/args-tokens' // for Deno
|
|
177
|
+
|
|
178
|
+
const args = ['dev', '-p=9131', '--host=example.com', '--mode=production']
|
|
179
|
+
const tokens = parseArgs(args)
|
|
180
|
+
const { values, positionals } = resolveArgs(
|
|
181
|
+
{
|
|
182
|
+
help: {
|
|
183
|
+
type: 'boolean',
|
|
184
|
+
short: 'h'
|
|
185
|
+
},
|
|
186
|
+
version: {
|
|
187
|
+
type: 'boolean',
|
|
188
|
+
short: 'v'
|
|
189
|
+
},
|
|
190
|
+
port: {
|
|
191
|
+
type: 'number',
|
|
192
|
+
short: 'p',
|
|
193
|
+
default: 8080
|
|
194
|
+
},
|
|
195
|
+
mode: {
|
|
196
|
+
type: 'string',
|
|
197
|
+
short: 'm'
|
|
198
|
+
},
|
|
199
|
+
host: {
|
|
200
|
+
type: 'string',
|
|
201
|
+
short: 'o',
|
|
202
|
+
required: true
|
|
203
|
+
}
|
|
204
|
+
},
|
|
205
|
+
tokens
|
|
206
|
+
)
|
|
207
|
+
console.log('values:', values)
|
|
208
|
+
console.log('positionals:', positionals)
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
## Convenient argument parsing
|
|
212
|
+
|
|
213
|
+
Using the `parse,` you can transform the arguments into tokens and resolve the argument values once:
|
|
214
|
+
|
|
215
|
+
```js
|
|
216
|
+
import { parse } from 'args-tokens' // for Node.js and Bun
|
|
217
|
+
// import { parse } from 'jsr:@kazupon/args-tokens' // for Deno
|
|
218
|
+
|
|
219
|
+
const args = ['dev', '-p=9131', '--host=example.com', '--mode=production']
|
|
220
|
+
const { values, positionals } = parse(args, {
|
|
221
|
+
options: {
|
|
222
|
+
help: {
|
|
223
|
+
type: 'boolean',
|
|
224
|
+
short: 'h'
|
|
225
|
+
},
|
|
226
|
+
version: {
|
|
227
|
+
type: 'boolean',
|
|
228
|
+
short: 'v'
|
|
229
|
+
},
|
|
230
|
+
port: {
|
|
231
|
+
type: 'number',
|
|
232
|
+
short: 'p',
|
|
233
|
+
default: 8080
|
|
234
|
+
},
|
|
235
|
+
mode: {
|
|
236
|
+
type: 'string',
|
|
237
|
+
short: 'm'
|
|
238
|
+
},
|
|
239
|
+
host: {
|
|
240
|
+
type: 'string',
|
|
241
|
+
short: 'o',
|
|
242
|
+
required: true
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
})
|
|
246
|
+
console.log('values:', values)
|
|
247
|
+
console.log('positionals:', positionals)
|
|
248
|
+
```
|
|
249
|
+
|
|
166
250
|
## Node.js `parseArgs` tokens compatible
|
|
167
251
|
|
|
168
252
|
If you want to use the same short options tokens as returned Node.js `parseArgs`, you can use `allowCompatible` parse option on `parseArgs`:
|
package/lib/cjs/index.d.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
export { parse } from './parse.js';
|
|
1
2
|
export { parseArgs } from './parser.js';
|
|
2
|
-
export
|
|
3
|
-
export type {
|
|
3
|
+
export { resolveArgs } from './resolver.js';
|
|
4
|
+
export type { ParsedArgs, ParseOptions } from './parse';
|
|
5
|
+
export type { ArgToken, ParserOptions } from './parser';
|
|
6
|
+
export type { ArgOptionSchema } from './resolver';
|
package/lib/cjs/index.js
CHANGED
|
@@ -1,20 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.parseArgs = void 0;
|
|
3
|
+
exports.resolveArgs = exports.parseArgs = exports.parse = void 0;
|
|
4
|
+
var parse_js_1 = require("./parse.js");
|
|
5
|
+
Object.defineProperty(exports, "parse", { enumerable: true, get: function () { return parse_js_1.parse; } });
|
|
18
6
|
var parser_js_1 = require("./parser.js");
|
|
19
7
|
Object.defineProperty(exports, "parseArgs", { enumerable: true, get: function () { return parser_js_1.parseArgs; } });
|
|
20
|
-
|
|
8
|
+
var resolver_js_1 = require("./resolver.js");
|
|
9
|
+
Object.defineProperty(exports, "resolveArgs", { enumerable: true, get: function () { return resolver_js_1.resolveArgs; } });
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { ParserOptions } from './parser';
|
|
2
|
+
import type { ArgOptions, ArgValues } from './resolver';
|
|
3
|
+
/**
|
|
4
|
+
* Parse options for {@link parse} function
|
|
5
|
+
*/
|
|
6
|
+
export interface ParseOptions<O extends ArgOptions> extends ParserOptions {
|
|
7
|
+
/**
|
|
8
|
+
* Command line options, about details see {@link ArgOptions}
|
|
9
|
+
*/
|
|
10
|
+
options?: O;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Parsed command line arguments
|
|
14
|
+
*/
|
|
15
|
+
export type ParsedArgs<T extends ArgOptions> = {
|
|
16
|
+
/**
|
|
17
|
+
* Parsed values, same as `values` in {@link resolveArgs}
|
|
18
|
+
*/
|
|
19
|
+
values: ArgValues<T>;
|
|
20
|
+
/**
|
|
21
|
+
* Positional arguments, same as `positionals` in {@link resolveArgs}
|
|
22
|
+
*/
|
|
23
|
+
positionals: string[];
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Parse command line arguments
|
|
27
|
+
* @description This function is a convenient API, that is used {@link parseArgs} and {@link resolveArgs} in internal.
|
|
28
|
+
* @example
|
|
29
|
+
* ```js
|
|
30
|
+
* import { parse } from 'args-tokens'
|
|
31
|
+
*
|
|
32
|
+
* const { values, positionals } = parse(process.argv.slice(2))
|
|
33
|
+
* console.log('values', values)
|
|
34
|
+
* console.log('positionals', positionals)
|
|
35
|
+
* ```
|
|
36
|
+
* @param args command line arguments
|
|
37
|
+
* @param options parse options, about details see {@link ParseOptions}
|
|
38
|
+
* @returns parsed values
|
|
39
|
+
*/
|
|
40
|
+
export declare function parse<O extends ArgOptions>(args: string[], options?: ParseOptions<O>): ParsedArgs<O>;
|
package/lib/cjs/parse.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parse = parse;
|
|
4
|
+
const parser_js_1 = require("./parser.js");
|
|
5
|
+
const resolver_js_1 = require("./resolver.js");
|
|
6
|
+
const DEFAULT_OPTIONS = {
|
|
7
|
+
help: {
|
|
8
|
+
type: 'boolean',
|
|
9
|
+
short: 'h'
|
|
10
|
+
},
|
|
11
|
+
version: {
|
|
12
|
+
type: 'boolean',
|
|
13
|
+
short: 'v'
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Parse command line arguments
|
|
18
|
+
* @description This function is a convenient API, that is used {@link parseArgs} and {@link resolveArgs} in internal.
|
|
19
|
+
* @example
|
|
20
|
+
* ```js
|
|
21
|
+
* import { parse } from 'args-tokens'
|
|
22
|
+
*
|
|
23
|
+
* const { values, positionals } = parse(process.argv.slice(2))
|
|
24
|
+
* console.log('values', values)
|
|
25
|
+
* console.log('positionals', positionals)
|
|
26
|
+
* ```
|
|
27
|
+
* @param args command line arguments
|
|
28
|
+
* @param options parse options, about details see {@link ParseOptions}
|
|
29
|
+
* @returns parsed values
|
|
30
|
+
*/
|
|
31
|
+
function parse(args, options = {}) {
|
|
32
|
+
const { options: argOptions, allowCompatible = false } = options;
|
|
33
|
+
const tokens = (0, parser_js_1.parseArgs)(args, { allowCompatible });
|
|
34
|
+
return (0, resolver_js_1.resolveArgs)(argOptions || DEFAULT_OPTIONS, tokens);
|
|
35
|
+
}
|
package/lib/cjs/parser.d.ts
CHANGED
|
@@ -36,9 +36,9 @@ export interface ArgToken {
|
|
|
36
36
|
inlineValue?: boolean;
|
|
37
37
|
}
|
|
38
38
|
/**
|
|
39
|
-
*
|
|
39
|
+
* Parser Options
|
|
40
40
|
*/
|
|
41
|
-
export interface
|
|
41
|
+
export interface ParserOptions {
|
|
42
42
|
/**
|
|
43
43
|
* [Node.js parseArgs](https://nodejs.org/api/util.html#parseargs-tokens) tokens compatible mode
|
|
44
44
|
* @default false
|
|
@@ -61,7 +61,7 @@ export interface ParseOptions {
|
|
|
61
61
|
* @param options parse options
|
|
62
62
|
* @returns argument tokens
|
|
63
63
|
*/
|
|
64
|
-
export declare function parseArgs(args: string[], options?:
|
|
64
|
+
export declare function parseArgs(args: string[], options?: ParserOptions): ArgToken[];
|
|
65
65
|
/**
|
|
66
66
|
* Check if `arg` is a short option (e.g. `-f`)
|
|
67
67
|
* @param arg the argument to check
|
package/lib/esm/index.d.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
export { parse } from './parse.js';
|
|
1
2
|
export { parseArgs } from './parser.js';
|
|
2
|
-
export
|
|
3
|
-
export type {
|
|
3
|
+
export { resolveArgs } from './resolver.js';
|
|
4
|
+
export type { ParsedArgs, ParseOptions } from './parse';
|
|
5
|
+
export type { ArgToken, ParserOptions } from './parser';
|
|
6
|
+
export type { ArgOptionSchema } from './resolver';
|
package/lib/esm/index.js
CHANGED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { ParserOptions } from './parser';
|
|
2
|
+
import type { ArgOptions, ArgValues } from './resolver';
|
|
3
|
+
/**
|
|
4
|
+
* Parse options for {@link parse} function
|
|
5
|
+
*/
|
|
6
|
+
export interface ParseOptions<O extends ArgOptions> extends ParserOptions {
|
|
7
|
+
/**
|
|
8
|
+
* Command line options, about details see {@link ArgOptions}
|
|
9
|
+
*/
|
|
10
|
+
options?: O;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Parsed command line arguments
|
|
14
|
+
*/
|
|
15
|
+
export type ParsedArgs<T extends ArgOptions> = {
|
|
16
|
+
/**
|
|
17
|
+
* Parsed values, same as `values` in {@link resolveArgs}
|
|
18
|
+
*/
|
|
19
|
+
values: ArgValues<T>;
|
|
20
|
+
/**
|
|
21
|
+
* Positional arguments, same as `positionals` in {@link resolveArgs}
|
|
22
|
+
*/
|
|
23
|
+
positionals: string[];
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Parse command line arguments
|
|
27
|
+
* @description This function is a convenient API, that is used {@link parseArgs} and {@link resolveArgs} in internal.
|
|
28
|
+
* @example
|
|
29
|
+
* ```js
|
|
30
|
+
* import { parse } from 'args-tokens'
|
|
31
|
+
*
|
|
32
|
+
* const { values, positionals } = parse(process.argv.slice(2))
|
|
33
|
+
* console.log('values', values)
|
|
34
|
+
* console.log('positionals', positionals)
|
|
35
|
+
* ```
|
|
36
|
+
* @param args command line arguments
|
|
37
|
+
* @param options parse options, about details see {@link ParseOptions}
|
|
38
|
+
* @returns parsed values
|
|
39
|
+
*/
|
|
40
|
+
export declare function parse<O extends ArgOptions>(args: string[], options?: ParseOptions<O>): ParsedArgs<O>;
|
package/lib/esm/parse.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { parseArgs } from './parser.js';
|
|
2
|
+
import { resolveArgs } from './resolver.js';
|
|
3
|
+
const DEFAULT_OPTIONS = {
|
|
4
|
+
help: {
|
|
5
|
+
type: 'boolean',
|
|
6
|
+
short: 'h'
|
|
7
|
+
},
|
|
8
|
+
version: {
|
|
9
|
+
type: 'boolean',
|
|
10
|
+
short: 'v'
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Parse command line arguments
|
|
15
|
+
* @description This function is a convenient API, that is used {@link parseArgs} and {@link resolveArgs} in internal.
|
|
16
|
+
* @example
|
|
17
|
+
* ```js
|
|
18
|
+
* import { parse } from 'args-tokens'
|
|
19
|
+
*
|
|
20
|
+
* const { values, positionals } = parse(process.argv.slice(2))
|
|
21
|
+
* console.log('values', values)
|
|
22
|
+
* console.log('positionals', positionals)
|
|
23
|
+
* ```
|
|
24
|
+
* @param args command line arguments
|
|
25
|
+
* @param options parse options, about details see {@link ParseOptions}
|
|
26
|
+
* @returns parsed values
|
|
27
|
+
*/
|
|
28
|
+
export function parse(args, options = {}) {
|
|
29
|
+
const { options: argOptions, allowCompatible = false } = options;
|
|
30
|
+
const tokens = parseArgs(args, { allowCompatible });
|
|
31
|
+
return resolveArgs(argOptions || DEFAULT_OPTIONS, tokens);
|
|
32
|
+
}
|
package/lib/esm/parser.d.ts
CHANGED
|
@@ -36,9 +36,9 @@ export interface ArgToken {
|
|
|
36
36
|
inlineValue?: boolean;
|
|
37
37
|
}
|
|
38
38
|
/**
|
|
39
|
-
*
|
|
39
|
+
* Parser Options
|
|
40
40
|
*/
|
|
41
|
-
export interface
|
|
41
|
+
export interface ParserOptions {
|
|
42
42
|
/**
|
|
43
43
|
* [Node.js parseArgs](https://nodejs.org/api/util.html#parseargs-tokens) tokens compatible mode
|
|
44
44
|
* @default false
|
|
@@ -61,7 +61,7 @@ export interface ParseOptions {
|
|
|
61
61
|
* @param options parse options
|
|
62
62
|
* @returns argument tokens
|
|
63
63
|
*/
|
|
64
|
-
export declare function parseArgs(args: string[], options?:
|
|
64
|
+
export declare function parseArgs(args: string[], options?: ParserOptions): ArgToken[];
|
|
65
65
|
/**
|
|
66
66
|
* Check if `arg` is a short option (e.g. `-f`)
|
|
67
67
|
* @param arg the argument to check
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "args-tokens",
|
|
3
3
|
"description": "parseArgs tokens compatibility and more high-performance parser",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.4.1",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "kazuya kawaguchi",
|
|
7
7
|
"email": "kawakazu80@gmail.com"
|
|
@@ -106,7 +106,7 @@
|
|
|
106
106
|
]
|
|
107
107
|
},
|
|
108
108
|
"scripts": {
|
|
109
|
-
"bench:mitata": "node --expose-gc bench/
|
|
109
|
+
"bench:mitata": "node --expose-gc bench/mitata.js",
|
|
110
110
|
"bench:vitest": "vitest bench --run",
|
|
111
111
|
"build": "pnpm run --parallel --color \"/^build:/\"",
|
|
112
112
|
"build:cjs": "tsc -p ./tsconfig.cjs.json",
|