args-tokens 0.22.1 → 0.22.2
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 +232 -1
- package/lib/index.d.ts +16 -6
- package/lib/index.js +11 -6
- package/lib/{parser-Cbxholql.d.ts → parser-DEYiqyo1.d.ts} +14 -7
- package/lib/{parser-Dr4iAGaX.js → parser-M-ayhS1h.js} +23 -15
- package/lib/parser.d.ts +1 -1
- package/lib/parser.js +1 -1
- package/lib/resolver-7JOAwfSr.d.ts +474 -0
- package/lib/{resolver-BsDoIgzu.js → resolver-DBvNkaE7.js} +13 -2
- package/lib/resolver.d.ts +2 -2
- package/lib/resolver.js +3 -3
- package/lib/{utils-N7UlhLbz.js → utils-1LQrGCWG.js} +6 -0
- package/lib/utils.d.ts +6 -0
- package/lib/utils.js +1 -1
- package/package.json +7 -6
- package/lib/resolver-BoS-UnqX.d.ts +0 -173
package/README.md
CHANGED
|
@@ -302,6 +302,237 @@ const tokens = parseArgs(['-a=1'], { allowCompatible: true }) // add `allowCompa
|
|
|
302
302
|
deepStrictEqual(tokensNode, tokens)
|
|
303
303
|
```
|
|
304
304
|
|
|
305
|
+
## ArgSchema Reference
|
|
306
|
+
|
|
307
|
+
The `ArgSchema` interface defines the configuration for command-line arguments. This schema is similar to Node.js `util.parseArgs` but with extended features.
|
|
308
|
+
|
|
309
|
+
### Schema Properties
|
|
310
|
+
|
|
311
|
+
#### `type` (required)
|
|
312
|
+
|
|
313
|
+
Type of the argument value:
|
|
314
|
+
|
|
315
|
+
- `'string'`: Text value (default if not specified)
|
|
316
|
+
- `'boolean'`: True/false flag (can be negatable with `--no-` prefix)
|
|
317
|
+
- `'number'`: Numeric value (parsed as integer or float)
|
|
318
|
+
- `'enum'`: One of predefined string values (requires `choices` property)
|
|
319
|
+
- `'positional'`: Non-option argument by position
|
|
320
|
+
- `'custom'`: Custom parsing with user-defined `parse` function
|
|
321
|
+
|
|
322
|
+
<!-- eslint-skip -->
|
|
323
|
+
|
|
324
|
+
```js
|
|
325
|
+
{
|
|
326
|
+
name: { type: 'string' }, // --name value
|
|
327
|
+
verbose: { type: 'boolean' }, // --verbose or --no-verbose
|
|
328
|
+
port: { type: 'number' }, // --port 3000
|
|
329
|
+
level: { type: 'enum', choices: ['debug', 'info'] },
|
|
330
|
+
file: { type: 'positional' }, // first positional arg
|
|
331
|
+
config: { type: 'custom', parse: JSON.parse }
|
|
332
|
+
}
|
|
333
|
+
```
|
|
334
|
+
|
|
335
|
+
#### `short` (optional)
|
|
336
|
+
|
|
337
|
+
Single character alias for the long option name. Allows users to use `-x` instead of `--extended-option`.
|
|
338
|
+
|
|
339
|
+
<!-- eslint-skip -->
|
|
340
|
+
|
|
341
|
+
```js
|
|
342
|
+
{
|
|
343
|
+
verbose: {
|
|
344
|
+
type: 'boolean',
|
|
345
|
+
short: 'v' // Enables both --verbose and -v
|
|
346
|
+
},
|
|
347
|
+
port: {
|
|
348
|
+
type: 'number',
|
|
349
|
+
short: 'p' // Enables both --port 3000 and -p 3000
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
```
|
|
353
|
+
|
|
354
|
+
#### `description` (optional)
|
|
355
|
+
|
|
356
|
+
Human-readable description used for help text generation and documentation.
|
|
357
|
+
|
|
358
|
+
<!-- eslint-skip -->
|
|
359
|
+
|
|
360
|
+
```js
|
|
361
|
+
{
|
|
362
|
+
config: {
|
|
363
|
+
type: 'string',
|
|
364
|
+
description: 'Path to configuration file'
|
|
365
|
+
},
|
|
366
|
+
timeout: {
|
|
367
|
+
type: 'number',
|
|
368
|
+
description: 'Request timeout in milliseconds'
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
```
|
|
372
|
+
|
|
373
|
+
#### `required` (optional)
|
|
374
|
+
|
|
375
|
+
Marks the argument as required. When `true`, the argument must be provided or an `ArgResolveError` will be thrown.
|
|
376
|
+
|
|
377
|
+
<!-- eslint-skip -->
|
|
378
|
+
|
|
379
|
+
```js
|
|
380
|
+
{
|
|
381
|
+
input: {
|
|
382
|
+
type: 'string',
|
|
383
|
+
required: true, // Must be provided: --input file.txt
|
|
384
|
+
description: 'Input file path'
|
|
385
|
+
},
|
|
386
|
+
source: {
|
|
387
|
+
type: 'positional',
|
|
388
|
+
required: true // First positional argument must exist
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
```
|
|
392
|
+
|
|
393
|
+
#### `multiple` (optional)
|
|
394
|
+
|
|
395
|
+
Allows the argument to accept multiple values. The resolved value becomes an array.
|
|
396
|
+
|
|
397
|
+
- For options: can be specified multiple times (`--tag foo --tag bar`)
|
|
398
|
+
- For positional: collects remaining positional arguments
|
|
399
|
+
|
|
400
|
+
<!-- eslint-skip -->
|
|
401
|
+
|
|
402
|
+
```js
|
|
403
|
+
{
|
|
404
|
+
tags: {
|
|
405
|
+
type: 'string',
|
|
406
|
+
multiple: true, // --tags foo --tags bar → ['foo', 'bar']
|
|
407
|
+
description: 'Tags to apply'
|
|
408
|
+
},
|
|
409
|
+
files: {
|
|
410
|
+
type: 'positional',
|
|
411
|
+
multiple: true // Collects all remaining positional args
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
```
|
|
415
|
+
|
|
416
|
+
#### `negatable` (optional)
|
|
417
|
+
|
|
418
|
+
Enables negation for boolean arguments using `--no-` prefix. Only applicable to `type: 'boolean'`.
|
|
419
|
+
|
|
420
|
+
<!-- eslint-skip -->
|
|
421
|
+
|
|
422
|
+
```js
|
|
423
|
+
{
|
|
424
|
+
color: {
|
|
425
|
+
type: 'boolean',
|
|
426
|
+
negatable: true,
|
|
427
|
+
default: true,
|
|
428
|
+
description: 'Enable colorized output'
|
|
429
|
+
}
|
|
430
|
+
// Usage: --color (true), --no-color (false)
|
|
431
|
+
}
|
|
432
|
+
```
|
|
433
|
+
|
|
434
|
+
#### `choices` (optional)
|
|
435
|
+
|
|
436
|
+
Array of allowed string values for enum-type arguments. Required when `type: 'enum'`.
|
|
437
|
+
|
|
438
|
+
<!-- eslint-skip -->
|
|
439
|
+
|
|
440
|
+
```js
|
|
441
|
+
{
|
|
442
|
+
logLevel: {
|
|
443
|
+
type: 'enum',
|
|
444
|
+
choices: ['debug', 'info', 'warn', 'error'],
|
|
445
|
+
default: 'info',
|
|
446
|
+
description: 'Logging verbosity level'
|
|
447
|
+
},
|
|
448
|
+
format: {
|
|
449
|
+
type: 'enum',
|
|
450
|
+
choices: ['json', 'yaml', 'toml'],
|
|
451
|
+
description: 'Output format'
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
```
|
|
455
|
+
|
|
456
|
+
#### `default` (optional)
|
|
457
|
+
|
|
458
|
+
Default value used when the argument is not provided. The type must match the argument's `type` property.
|
|
459
|
+
|
|
460
|
+
<!-- eslint-skip -->
|
|
461
|
+
|
|
462
|
+
```js
|
|
463
|
+
{
|
|
464
|
+
host: {
|
|
465
|
+
type: 'string',
|
|
466
|
+
default: 'localhost' // string default
|
|
467
|
+
},
|
|
468
|
+
verbose: {
|
|
469
|
+
type: 'boolean',
|
|
470
|
+
default: false // boolean default
|
|
471
|
+
},
|
|
472
|
+
port: {
|
|
473
|
+
type: 'number',
|
|
474
|
+
default: 8080 // number default
|
|
475
|
+
},
|
|
476
|
+
level: {
|
|
477
|
+
type: 'enum',
|
|
478
|
+
choices: ['low', 'high'],
|
|
479
|
+
default: 'low' // must be in choices
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
```
|
|
483
|
+
|
|
484
|
+
#### `toKebab` (optional)
|
|
485
|
+
|
|
486
|
+
Converts the argument name from camelCase to kebab-case for CLI usage. A property like `maxCount` becomes available as `--max-count`.
|
|
487
|
+
|
|
488
|
+
<!-- eslint-skip -->
|
|
489
|
+
|
|
490
|
+
```js
|
|
491
|
+
{
|
|
492
|
+
maxRetries: {
|
|
493
|
+
type: 'number',
|
|
494
|
+
toKebab: true, // Accessible as --max-retries
|
|
495
|
+
description: 'Maximum retry attempts'
|
|
496
|
+
},
|
|
497
|
+
enableLogging: {
|
|
498
|
+
type: 'boolean',
|
|
499
|
+
toKebab: true // Accessible as --enable-logging
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
```
|
|
503
|
+
|
|
504
|
+
#### `parse` (optional)
|
|
505
|
+
|
|
506
|
+
Custom parsing function for `type: 'custom'` arguments. Required when `type: 'custom'`. Should throw an Error if parsing fails.
|
|
507
|
+
|
|
508
|
+
<!-- eslint-skip -->
|
|
509
|
+
|
|
510
|
+
```js
|
|
511
|
+
{
|
|
512
|
+
config: {
|
|
513
|
+
type: 'custom',
|
|
514
|
+
parse: (value) => {
|
|
515
|
+
try {
|
|
516
|
+
return JSON.parse(value) // Parse JSON config
|
|
517
|
+
} catch {
|
|
518
|
+
throw new Error('Invalid JSON configuration')
|
|
519
|
+
}
|
|
520
|
+
},
|
|
521
|
+
description: 'JSON configuration object'
|
|
522
|
+
},
|
|
523
|
+
date: {
|
|
524
|
+
type: 'custom',
|
|
525
|
+
parse: (value) => {
|
|
526
|
+
const date = new Date(value)
|
|
527
|
+
if (isNaN(date.getTime())) {
|
|
528
|
+
throw new Error('Invalid date format')
|
|
529
|
+
}
|
|
530
|
+
return date
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
```
|
|
535
|
+
|
|
305
536
|
## 🙌 Contributing guidelines
|
|
306
537
|
|
|
307
538
|
If you are interested in contributing to `args-tokens`, I highly recommend checking out [the contributing guidelines](/CONTRIBUTING.md) here. You'll find all the relevant information such as [how to make a PR](/CONTRIBUTING.md#pull-request-guidelines), [how to setup development](/CONTRIBUTING.md#development-setup)) etc., there.
|
|
@@ -319,7 +550,7 @@ The development of Gunish is supported by my OSS sponsors!
|
|
|
319
550
|
|
|
320
551
|
<p align="center">
|
|
321
552
|
<a href="https://cdn.jsdelivr.net/gh/kazupon/sponsors/sponsors.svg">
|
|
322
|
-
<img alt="sponsor src=
|
|
553
|
+
<img alt="sponsor" src="https://cdn.jsdelivr.net/gh/kazupon/sponsors/sponsors.svg"/>
|
|
323
554
|
</a>
|
|
324
555
|
</p>
|
|
325
556
|
|
package/lib/index.d.ts
CHANGED
|
@@ -1,19 +1,23 @@
|
|
|
1
|
-
import { ArgToken, ParserOptions, parseArgs$1 as parseArgs } from "./parser-
|
|
2
|
-
import { ArgExplicitlyProvided, ArgResolveError$1 as ArgResolveError, ArgResolveErrorType, ArgSchema, ArgValues, Args, ResolveArgs, resolveArgs$1 as resolveArgs } from "./resolver-
|
|
1
|
+
import { ArgToken, ParserOptions, parseArgs$1 as parseArgs } from "./parser-DEYiqyo1.js";
|
|
2
|
+
import { ArgExplicitlyProvided, ArgResolveError$1 as ArgResolveError, ArgResolveErrorType, ArgSchema, ArgValues, Args, ResolveArgs, resolveArgs$1 as resolveArgs } from "./resolver-7JOAwfSr.js";
|
|
3
3
|
|
|
4
4
|
//#region src/parse.d.ts
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Parse options for {@link parse} function.
|
|
8
|
+
*
|
|
9
|
+
* @typeParam A - {@link Args | Arguments schema}, which is an object that defines the command line arguments.
|
|
8
10
|
*/
|
|
9
11
|
interface ParseOptions<A extends Args> extends ParserOptions, ResolveArgs {
|
|
10
12
|
/**
|
|
11
|
-
* Command line arguments
|
|
13
|
+
* Command line arguments.
|
|
12
14
|
*/
|
|
13
15
|
args?: A;
|
|
14
16
|
}
|
|
15
17
|
/**
|
|
16
18
|
* Parsed command line arguments.
|
|
19
|
+
*
|
|
20
|
+
* @typeParam A - {@link Args | Arguments schema}, which is an object that defines the command line arguments.
|
|
17
21
|
*/
|
|
18
22
|
type ParsedArgs<A extends Args> = {
|
|
19
23
|
/**
|
|
@@ -38,13 +42,22 @@ type ParsedArgs<A extends Args> = {
|
|
|
38
42
|
tokens: ArgToken[];
|
|
39
43
|
/**
|
|
40
44
|
* Explicit provision status, same as `explicit` in {@link resolveArgs}.
|
|
45
|
+
*
|
|
41
46
|
* Indicates which arguments were explicitly provided vs using default values.
|
|
42
47
|
*/
|
|
43
48
|
explicit: ArgExplicitlyProvided<A>;
|
|
44
49
|
};
|
|
45
50
|
/**
|
|
46
51
|
* Parse command line arguments.
|
|
52
|
+
*
|
|
47
53
|
* This function is a convenient API, that is used {@link parseArgs} and {@link resolveArgs} in internal.
|
|
54
|
+
*
|
|
55
|
+
* @typeParam A - {@link Args | Arguments schema}, which is an object that defines the command line arguments.
|
|
56
|
+
*
|
|
57
|
+
* @param args - command line arguments
|
|
58
|
+
* @param options - parse options, about details see {@link ParseOptions}
|
|
59
|
+
* @returns An object that contains the values of the arguments, positional arguments, {@link AggregateError | validation errors}, and {@link ArgToken | argument tokens}.
|
|
60
|
+
*
|
|
48
61
|
* @example
|
|
49
62
|
* ```js
|
|
50
63
|
* import { parse } from 'args-tokens'
|
|
@@ -53,9 +66,6 @@ type ParsedArgs<A extends Args> = {
|
|
|
53
66
|
* console.log('values', values)
|
|
54
67
|
* console.log('positionals', positionals)
|
|
55
68
|
* ```
|
|
56
|
-
* @param args - command line arguments
|
|
57
|
-
* @param options - parse options, about details see {@link ParseOptions}
|
|
58
|
-
* @returns An object that contains the values of the arguments, positional arguments, {@link AggregateError | validation errors}, and {@link ArgToken | argument tokens}.
|
|
59
69
|
*/
|
|
60
70
|
declare function parse<A extends Args>(args: string[], options?: ParseOptions<A>): ParsedArgs<A>;
|
|
61
71
|
//#endregion
|
package/lib/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { parseArgs } from "./parser-
|
|
2
|
-
import "./utils-
|
|
3
|
-
import { ArgResolveError, resolveArgs } from "./resolver-
|
|
1
|
+
import { parseArgs } from "./parser-M-ayhS1h.js";
|
|
2
|
+
import "./utils-1LQrGCWG.js";
|
|
3
|
+
import { ArgResolveError, resolveArgs } from "./resolver-DBvNkaE7.js";
|
|
4
4
|
|
|
5
5
|
//#region src/parse.ts
|
|
6
6
|
const DEFAULT_OPTIONS = {
|
|
@@ -15,7 +15,15 @@ const DEFAULT_OPTIONS = {
|
|
|
15
15
|
};
|
|
16
16
|
/**
|
|
17
17
|
* Parse command line arguments.
|
|
18
|
+
*
|
|
18
19
|
* This function is a convenient API, that is used {@link parseArgs} and {@link resolveArgs} in internal.
|
|
20
|
+
*
|
|
21
|
+
* @typeParam A - {@link Args | Arguments schema}, which is an object that defines the command line arguments.
|
|
22
|
+
*
|
|
23
|
+
* @param args - command line arguments
|
|
24
|
+
* @param options - parse options, about details see {@link ParseOptions}
|
|
25
|
+
* @returns An object that contains the values of the arguments, positional arguments, {@link AggregateError | validation errors}, and {@link ArgToken | argument tokens}.
|
|
26
|
+
*
|
|
19
27
|
* @example
|
|
20
28
|
* ```js
|
|
21
29
|
* import { parse } from 'args-tokens'
|
|
@@ -24,9 +32,6 @@ const DEFAULT_OPTIONS = {
|
|
|
24
32
|
* console.log('values', values)
|
|
25
33
|
* console.log('positionals', positionals)
|
|
26
34
|
* ```
|
|
27
|
-
* @param args - command line arguments
|
|
28
|
-
* @param options - parse options, about details see {@link ParseOptions}
|
|
29
|
-
* @returns An object that contains the values of the arguments, positional arguments, {@link AggregateError | validation errors}, and {@link ArgToken | argument tokens}.
|
|
30
35
|
*/
|
|
31
36
|
function parse(args, options = {}) {
|
|
32
37
|
const { args: _args, allowCompatible = false } = options;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
//#region src/parser.d.ts
|
|
2
2
|
/**
|
|
3
3
|
* Entry point of argument parser.
|
|
4
|
+
*
|
|
4
5
|
* @module
|
|
5
6
|
*/
|
|
6
7
|
/**
|
|
@@ -13,6 +14,7 @@
|
|
|
13
14
|
*/
|
|
14
15
|
/**
|
|
15
16
|
* Argument token Kind.
|
|
17
|
+
*
|
|
16
18
|
* - `option`: option token, support short option (e.g. `-x`) and long option (e.g. `--foo`)
|
|
17
19
|
* - `option-terminator`: option terminator (`--`) token, see guideline 10 in https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html
|
|
18
20
|
* - `positional`: positional token
|
|
@@ -54,12 +56,18 @@ interface ArgToken {
|
|
|
54
56
|
interface ParserOptions {
|
|
55
57
|
/**
|
|
56
58
|
* [Node.js parseArgs](https://nodejs.org/api/util.html#parseargs-tokens) tokens compatible mode.
|
|
59
|
+
*
|
|
57
60
|
* @default false
|
|
58
61
|
*/
|
|
59
62
|
allowCompatible?: boolean;
|
|
60
63
|
}
|
|
61
64
|
/**
|
|
62
65
|
* Parse command line arguments.
|
|
66
|
+
*
|
|
67
|
+
* @param args - command line arguments
|
|
68
|
+
* @param options - parse options, about details see {@link ParserOptions}
|
|
69
|
+
* @returns Argument tokens.
|
|
70
|
+
*
|
|
63
71
|
* @example
|
|
64
72
|
* ```js
|
|
65
73
|
* import { parseArgs } from 'args-tokens' // for Node.js and Bun
|
|
@@ -70,21 +78,20 @@ interface ParserOptions {
|
|
|
70
78
|
* // ...
|
|
71
79
|
* console.log('tokens:', tokens)
|
|
72
80
|
* ```
|
|
73
|
-
* @param args command line arguments
|
|
74
|
-
* @param options parse options
|
|
75
|
-
* @returns Argument tokens.
|
|
76
81
|
*/
|
|
77
82
|
declare function parseArgs(args: string[], options?: ParserOptions): ArgToken[];
|
|
78
83
|
/**
|
|
79
84
|
* Check if `arg` is a short option (e.g. `-f`).
|
|
80
|
-
*
|
|
81
|
-
* @
|
|
85
|
+
*
|
|
86
|
+
* @param arg - An argument to check
|
|
87
|
+
* @returns Whether `arg` is a short option.
|
|
82
88
|
*/
|
|
83
89
|
declare function isShortOption(arg: string): boolean;
|
|
84
90
|
/**
|
|
85
91
|
* Check if `arg` is a long option prefix (e.g. `--`).
|
|
86
|
-
*
|
|
87
|
-
* @
|
|
92
|
+
*
|
|
93
|
+
* @param arg - An argument to check
|
|
94
|
+
* @returns Whether `arg` is a long option prefix.
|
|
88
95
|
*/
|
|
89
96
|
declare function hasLongOptionPrefix(arg: string): boolean;
|
|
90
97
|
//#endregion
|
|
@@ -8,6 +8,11 @@ const SHORT_OPTION_PREFIX = HYPHEN_CHAR;
|
|
|
8
8
|
const LONG_OPTION_PREFIX = "--";
|
|
9
9
|
/**
|
|
10
10
|
* Parse command line arguments.
|
|
11
|
+
*
|
|
12
|
+
* @param args - command line arguments
|
|
13
|
+
* @param options - parse options, about details see {@link ParserOptions}
|
|
14
|
+
* @returns Argument tokens.
|
|
15
|
+
*
|
|
11
16
|
* @example
|
|
12
17
|
* ```js
|
|
13
18
|
* import { parseArgs } from 'args-tokens' // for Node.js and Bun
|
|
@@ -18,9 +23,6 @@ const LONG_OPTION_PREFIX = "--";
|
|
|
18
23
|
* // ...
|
|
19
24
|
* console.log('tokens:', tokens)
|
|
20
25
|
* ```
|
|
21
|
-
* @param args command line arguments
|
|
22
|
-
* @param options parse options
|
|
23
|
-
* @returns Argument tokens.
|
|
24
26
|
*/
|
|
25
27
|
function parseArgs(args, options = {}) {
|
|
26
28
|
const { allowCompatible = false } = options;
|
|
@@ -137,16 +139,18 @@ function parseArgs(args, options = {}) {
|
|
|
137
139
|
}
|
|
138
140
|
/**
|
|
139
141
|
* Check if `arg` is a short option (e.g. `-f`).
|
|
140
|
-
*
|
|
141
|
-
* @
|
|
142
|
+
*
|
|
143
|
+
* @param arg - An argument to check
|
|
144
|
+
* @returns Whether `arg` is a short option.
|
|
142
145
|
*/
|
|
143
146
|
function isShortOption(arg) {
|
|
144
147
|
return arg.length === 2 && arg.codePointAt(0) === HYPHEN_CODE && arg.codePointAt(1) !== HYPHEN_CODE;
|
|
145
148
|
}
|
|
146
149
|
/**
|
|
147
150
|
* Check if `arg` is a short option group (e.g. `-abc`).
|
|
148
|
-
*
|
|
149
|
-
* @
|
|
151
|
+
*
|
|
152
|
+
* @param arg - An argument to check
|
|
153
|
+
* @returns Whether `arg` is a short option group.
|
|
150
154
|
*/
|
|
151
155
|
function isShortOptionGroup(arg) {
|
|
152
156
|
if (arg.length <= 2) return false;
|
|
@@ -156,32 +160,36 @@ function isShortOptionGroup(arg) {
|
|
|
156
160
|
}
|
|
157
161
|
/**
|
|
158
162
|
* Check if `arg` is a long option (e.g. `--foo`).
|
|
159
|
-
*
|
|
160
|
-
* @
|
|
163
|
+
*
|
|
164
|
+
* @param arg - An argument to check
|
|
165
|
+
* @returns Whether `arg` is a long option.
|
|
161
166
|
*/
|
|
162
167
|
function isLongOption(arg) {
|
|
163
168
|
return hasLongOptionPrefix(arg) && !arg.includes(EQUAL_CHAR, 3);
|
|
164
169
|
}
|
|
165
170
|
/**
|
|
166
171
|
* Check if `arg` is a long option with value (e.g. `--foo=bar`).
|
|
167
|
-
*
|
|
168
|
-
* @
|
|
172
|
+
*
|
|
173
|
+
* @param arg - An argument to check
|
|
174
|
+
* @returns Whether `arg` is a long option.
|
|
169
175
|
*/
|
|
170
176
|
function isLongOptionAndValue(arg) {
|
|
171
177
|
return hasLongOptionPrefix(arg) && arg.includes(EQUAL_CHAR, 3);
|
|
172
178
|
}
|
|
173
179
|
/**
|
|
174
180
|
* Check if `arg` is a long option prefix (e.g. `--`).
|
|
175
|
-
*
|
|
176
|
-
* @
|
|
181
|
+
*
|
|
182
|
+
* @param arg - An argument to check
|
|
183
|
+
* @returns Whether `arg` is a long option prefix.
|
|
177
184
|
*/
|
|
178
185
|
function hasLongOptionPrefix(arg) {
|
|
179
186
|
return arg.length > 2 && ~arg.indexOf(LONG_OPTION_PREFIX);
|
|
180
187
|
}
|
|
181
188
|
/**
|
|
182
189
|
* Check if a `value` is an option value.
|
|
183
|
-
*
|
|
184
|
-
* @
|
|
190
|
+
*
|
|
191
|
+
* @param value - A value to check
|
|
192
|
+
* @returns Whether a `value` is an option value.
|
|
185
193
|
*/
|
|
186
194
|
function hasOptionValue(value) {
|
|
187
195
|
return !(value == null) && value.codePointAt(0) !== HYPHEN_CODE;
|
package/lib/parser.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { ArgToken, ParserOptions, hasLongOptionPrefix$1 as hasLongOptionPrefix, isShortOption$1 as isShortOption, parseArgs$1 as parseArgs } from "./parser-
|
|
1
|
+
import { ArgToken, ParserOptions, hasLongOptionPrefix$1 as hasLongOptionPrefix, isShortOption$1 as isShortOption, parseArgs$1 as parseArgs } from "./parser-DEYiqyo1.js";
|
|
2
2
|
export { ArgToken, ParserOptions, hasLongOptionPrefix, isShortOption, parseArgs };
|
package/lib/parser.js
CHANGED
|
@@ -0,0 +1,474 @@
|
|
|
1
|
+
import { ArgToken } from "./parser-DEYiqyo1.js";
|
|
2
|
+
|
|
3
|
+
//#region src/resolver.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* An argument schema definition for command-line argument parsing.
|
|
7
|
+
*
|
|
8
|
+
* This schema is similar to the schema of Node.js `util.parseArgs` but with extended features:
|
|
9
|
+
* - Additional `required` and `description` properties
|
|
10
|
+
* - Extended `type` support: 'string', 'boolean', 'number', 'enum', 'positional', 'custom'
|
|
11
|
+
* - Simplified `default` property (single type, not union types)
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* Basic string argument:
|
|
15
|
+
* ```ts
|
|
16
|
+
* const schema: ArgSchema = {
|
|
17
|
+
* type: 'string',
|
|
18
|
+
* description: 'Server hostname',
|
|
19
|
+
* default: 'localhost'
|
|
20
|
+
* }
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* Required number argument with alias:
|
|
25
|
+
* ```ts
|
|
26
|
+
* const schema: ArgSchema = {
|
|
27
|
+
* type: 'number',
|
|
28
|
+
* short: 'p',
|
|
29
|
+
* description: 'Port number to listen on',
|
|
30
|
+
* required: true
|
|
31
|
+
* }
|
|
32
|
+
* ```
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* Enum argument with choices:
|
|
36
|
+
* ```ts
|
|
37
|
+
* const schema: ArgSchema = {
|
|
38
|
+
* type: 'enum',
|
|
39
|
+
* choices: ['info', 'warn', 'error'],
|
|
40
|
+
* description: 'Logging level',
|
|
41
|
+
* default: 'info'
|
|
42
|
+
* }
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
interface ArgSchema {
|
|
46
|
+
/**
|
|
47
|
+
* Type of the argument value.
|
|
48
|
+
*
|
|
49
|
+
* - `'string'`: Text value (default if not specified)
|
|
50
|
+
* - `'boolean'`: `true`/`false` flag (can be negatable with `--no-` prefix)
|
|
51
|
+
* - `'number'`: Numeric value (parsed as integer or float)
|
|
52
|
+
* - `'enum'`: One of predefined string values (requires `choices` property)
|
|
53
|
+
* - `'positional'`: Non-option argument by position
|
|
54
|
+
* - `'custom'`: Custom parsing with user-defined `parse` function
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* Different argument types:
|
|
58
|
+
* ```ts
|
|
59
|
+
* {
|
|
60
|
+
* name: { type: 'string' }, // --name value
|
|
61
|
+
* verbose: { type: 'boolean' }, // --verbose or --no-verbose
|
|
62
|
+
* port: { type: 'number' }, // --port 3000
|
|
63
|
+
* level: { type: 'enum', choices: ['debug', 'info'] },
|
|
64
|
+
* file: { type: 'positional' }, // first positional arg
|
|
65
|
+
* config: { type: 'custom', parse: JSON.parse }
|
|
66
|
+
* }
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
type: 'string' | 'boolean' | 'number' | 'enum' | 'positional' | 'custom';
|
|
70
|
+
/**
|
|
71
|
+
* Single character alias for the long option name.
|
|
72
|
+
*
|
|
73
|
+
* As example, allows users to use `-x` instead of `--extended-option`.
|
|
74
|
+
* Only valid for non-positional argument types.
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* Short alias usage:
|
|
78
|
+
* ```ts
|
|
79
|
+
* {
|
|
80
|
+
* verbose: {
|
|
81
|
+
* type: 'boolean',
|
|
82
|
+
* short: 'v' // Enables both --verbose and -v
|
|
83
|
+
* },
|
|
84
|
+
* port: {
|
|
85
|
+
* type: 'number',
|
|
86
|
+
* short: 'p' // Enables both --port 3000 and -p 3000
|
|
87
|
+
* }
|
|
88
|
+
* }
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
short?: string;
|
|
92
|
+
/**
|
|
93
|
+
* Human-readable description of the argument's purpose.
|
|
94
|
+
*
|
|
95
|
+
* Used for help text generation and documentation.
|
|
96
|
+
* Should be concise but descriptive enough to understand the argument's role.
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* Descriptive help text:
|
|
100
|
+
* ```ts
|
|
101
|
+
* {
|
|
102
|
+
* config: {
|
|
103
|
+
* type: 'string',
|
|
104
|
+
* description: 'Path to configuration file'
|
|
105
|
+
* },
|
|
106
|
+
* timeout: {
|
|
107
|
+
* type: 'number',
|
|
108
|
+
* description: 'Request timeout in milliseconds'
|
|
109
|
+
* }
|
|
110
|
+
* }
|
|
111
|
+
* ```
|
|
112
|
+
*/
|
|
113
|
+
description?: string;
|
|
114
|
+
/**
|
|
115
|
+
* Marks the argument as required.
|
|
116
|
+
*
|
|
117
|
+
* When `true`, the argument must be provided by the user.
|
|
118
|
+
* If missing, an `ArgResolveError` with type 'required' will be thrown.
|
|
119
|
+
*
|
|
120
|
+
* Note: Only `true` is allowed (not `false`) to make intent explicit.
|
|
121
|
+
*
|
|
122
|
+
* @example
|
|
123
|
+
* Required arguments:
|
|
124
|
+
* ```ts
|
|
125
|
+
* {
|
|
126
|
+
* input: {
|
|
127
|
+
* type: 'string',
|
|
128
|
+
* required: true, // Must be provided: --input file.txt
|
|
129
|
+
* description: 'Input file path'
|
|
130
|
+
* },
|
|
131
|
+
* source: {
|
|
132
|
+
* type: 'positional',
|
|
133
|
+
* required: true // First positional argument must exist
|
|
134
|
+
* }
|
|
135
|
+
* }
|
|
136
|
+
* ```
|
|
137
|
+
*/
|
|
138
|
+
required?: true;
|
|
139
|
+
/**
|
|
140
|
+
* Allows the argument to accept multiple values.
|
|
141
|
+
*
|
|
142
|
+
* When `true`, the resolved value becomes an array.
|
|
143
|
+
* For options: can be specified multiple times (--tag foo --tag bar)
|
|
144
|
+
* For positional: collects remaining positional arguments
|
|
145
|
+
*
|
|
146
|
+
* Note: Only `true` is allowed (not `false`) to make intent explicit.
|
|
147
|
+
*
|
|
148
|
+
* @example
|
|
149
|
+
* Multiple values:
|
|
150
|
+
* ```ts
|
|
151
|
+
* {
|
|
152
|
+
* tags: {
|
|
153
|
+
* type: 'string',
|
|
154
|
+
* multiple: true, // --tags foo --tags bar → ['foo', 'bar']
|
|
155
|
+
* description: 'Tags to apply'
|
|
156
|
+
* },
|
|
157
|
+
* files: {
|
|
158
|
+
* type: 'positional',
|
|
159
|
+
* multiple: true // Collects all remaining positional args
|
|
160
|
+
* }
|
|
161
|
+
* }
|
|
162
|
+
* ```
|
|
163
|
+
*/
|
|
164
|
+
multiple?: true;
|
|
165
|
+
/**
|
|
166
|
+
* Enables negation for boolean arguments using `--no-` prefix.
|
|
167
|
+
*
|
|
168
|
+
* When `true`, allows users to explicitly set the boolean to `false`
|
|
169
|
+
* using `--no-option-name`. When `false` or omitted, only positive
|
|
170
|
+
* form is available.
|
|
171
|
+
*
|
|
172
|
+
* Only applicable to `type: 'boolean'` arguments.
|
|
173
|
+
*
|
|
174
|
+
* @example
|
|
175
|
+
* Negatable boolean:
|
|
176
|
+
* ```ts
|
|
177
|
+
* {
|
|
178
|
+
* color: {
|
|
179
|
+
* type: 'boolean',
|
|
180
|
+
* negatable: true,
|
|
181
|
+
* default: true,
|
|
182
|
+
* description: 'Enable colorized output'
|
|
183
|
+
* }
|
|
184
|
+
* // Usage: --color (true), --no-color (false)
|
|
185
|
+
* }
|
|
186
|
+
* ```
|
|
187
|
+
*/
|
|
188
|
+
negatable?: boolean;
|
|
189
|
+
/**
|
|
190
|
+
* Array of allowed string values for enum-type arguments.
|
|
191
|
+
*
|
|
192
|
+
* Required when `type: 'enum'`. The argument value must be one of these choices,
|
|
193
|
+
* otherwise an `ArgResolveError` with type 'type' will be thrown.
|
|
194
|
+
*
|
|
195
|
+
* Supports both mutable arrays and readonly arrays for type safety.
|
|
196
|
+
*
|
|
197
|
+
* @example
|
|
198
|
+
* Enum choices:
|
|
199
|
+
* ```ts
|
|
200
|
+
* {
|
|
201
|
+
* logLevel: {
|
|
202
|
+
* type: 'enum',
|
|
203
|
+
* choices: ['debug', 'info', 'warn', 'error'] as const,
|
|
204
|
+
* default: 'info',
|
|
205
|
+
* description: 'Logging verbosity level'
|
|
206
|
+
* },
|
|
207
|
+
* format: {
|
|
208
|
+
* type: 'enum',
|
|
209
|
+
* choices: ['json', 'yaml', 'toml'],
|
|
210
|
+
* description: 'Output format'
|
|
211
|
+
* }
|
|
212
|
+
* }
|
|
213
|
+
* ```
|
|
214
|
+
*/
|
|
215
|
+
choices?: string[] | readonly string[];
|
|
216
|
+
/**
|
|
217
|
+
* Default value used when the argument is not provided.
|
|
218
|
+
*
|
|
219
|
+
* The type must match the argument's `type` property:
|
|
220
|
+
* - `string` type: string default
|
|
221
|
+
* - `boolean` type: boolean default
|
|
222
|
+
* - `number` type: number default
|
|
223
|
+
* - `enum` type: must be one of the `choices` values
|
|
224
|
+
* - `positional`/`custom` type: any appropriate default
|
|
225
|
+
*
|
|
226
|
+
* @example
|
|
227
|
+
* Default values by type:
|
|
228
|
+
* ```ts
|
|
229
|
+
* {
|
|
230
|
+
* host: {
|
|
231
|
+
* type: 'string',
|
|
232
|
+
* default: 'localhost' // string default
|
|
233
|
+
* },
|
|
234
|
+
* verbose: {
|
|
235
|
+
* type: 'boolean',
|
|
236
|
+
* default: false // boolean default
|
|
237
|
+
* },
|
|
238
|
+
* port: {
|
|
239
|
+
* type: 'number',
|
|
240
|
+
* default: 8080 // number default
|
|
241
|
+
* },
|
|
242
|
+
* level: {
|
|
243
|
+
* type: 'enum',
|
|
244
|
+
* choices: ['low', 'high'],
|
|
245
|
+
* default: 'low' // must be in choices
|
|
246
|
+
* }
|
|
247
|
+
* }
|
|
248
|
+
* ```
|
|
249
|
+
*/
|
|
250
|
+
default?: string | boolean | number;
|
|
251
|
+
/**
|
|
252
|
+
* Converts the argument name from camelCase to kebab-case for CLI usage.
|
|
253
|
+
*
|
|
254
|
+
* When `true`, a property like `maxCount` becomes available as `--max-count`.
|
|
255
|
+
* This allows [CAC](https://github.com/cacjs/cac) user-friendly property names while maintaining CLI conventions.
|
|
256
|
+
*
|
|
257
|
+
* Can be overridden globally with `resolveArgs({ toKebab: true })`.
|
|
258
|
+
*
|
|
259
|
+
* Note: Only `true` is allowed (not `false`) to make intent explicit.
|
|
260
|
+
*
|
|
261
|
+
* @example
|
|
262
|
+
* Kebab-case conversion:
|
|
263
|
+
* ```ts
|
|
264
|
+
* {
|
|
265
|
+
* maxRetries: {
|
|
266
|
+
* type: 'number',
|
|
267
|
+
* toKebab: true, // Accessible as --max-retries
|
|
268
|
+
* description: 'Maximum retry attempts'
|
|
269
|
+
* },
|
|
270
|
+
* enableLogging: {
|
|
271
|
+
* type: 'boolean',
|
|
272
|
+
* toKebab: true // Accessible as --enable-logging
|
|
273
|
+
* }
|
|
274
|
+
* }
|
|
275
|
+
* ```
|
|
276
|
+
*/
|
|
277
|
+
toKebab?: true;
|
|
278
|
+
/**
|
|
279
|
+
* Custom parsing function for `type: 'custom'` arguments.
|
|
280
|
+
*
|
|
281
|
+
* Required when `type: 'custom'`. Receives the raw string value and must
|
|
282
|
+
* return the parsed result. Should throw an Error (or subclass) if parsing fails.
|
|
283
|
+
*
|
|
284
|
+
* The function's return type becomes the resolved argument type.
|
|
285
|
+
*
|
|
286
|
+
* @param value - Raw string value from command line
|
|
287
|
+
* @returns Parsed value of any type
|
|
288
|
+
* @throws Error or subclass when value is invalid
|
|
289
|
+
*
|
|
290
|
+
* @example
|
|
291
|
+
* Custom parsing functions:
|
|
292
|
+
* ```ts
|
|
293
|
+
* {
|
|
294
|
+
* config: {
|
|
295
|
+
* type: 'custom',
|
|
296
|
+
* parse: (value: string) => {
|
|
297
|
+
* try {
|
|
298
|
+
* return JSON.parse(value) // Parse JSON config
|
|
299
|
+
* } catch {
|
|
300
|
+
* throw new Error('Invalid JSON configuration')
|
|
301
|
+
* }
|
|
302
|
+
* },
|
|
303
|
+
* description: 'JSON configuration object'
|
|
304
|
+
* },
|
|
305
|
+
* date: {
|
|
306
|
+
* type: 'custom',
|
|
307
|
+
* parse: (value: string) => {
|
|
308
|
+
* const date = new Date(value)
|
|
309
|
+
* if (isNaN(date.getTime())) {
|
|
310
|
+
* throw new Error('Invalid date format')
|
|
311
|
+
* }
|
|
312
|
+
* return date
|
|
313
|
+
* }
|
|
314
|
+
* }
|
|
315
|
+
* }
|
|
316
|
+
* ```
|
|
317
|
+
*/
|
|
318
|
+
parse?: (value: string) => any;
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* An object that contains {@link ArgSchema | argument schema}.
|
|
322
|
+
*
|
|
323
|
+
* This type is used to define the structure and validation rules for command line arguments.
|
|
324
|
+
*/
|
|
325
|
+
interface Args {
|
|
326
|
+
[option: string]: ArgSchema;
|
|
327
|
+
}
|
|
328
|
+
/**
|
|
329
|
+
* An object that contains the values of the arguments.
|
|
330
|
+
*
|
|
331
|
+
* @typeParam T - Arguments which is an object that defines the command line arguments.
|
|
332
|
+
*/
|
|
333
|
+
type ArgValues<T> = T extends Args ? ResolveArgValues<T, { [Arg in keyof T]: ExtractOptionValue<T[Arg]> }> : {
|
|
334
|
+
[option: string]: string | boolean | number | (string | boolean | number)[] | undefined;
|
|
335
|
+
};
|
|
336
|
+
type IsFunction<T> = T extends ((...args: any[]) => any) ? true : false;
|
|
337
|
+
/**
|
|
338
|
+
* Extracts the value type from the argument schema.
|
|
339
|
+
*
|
|
340
|
+
* @typeParam A - Argument schema which is an object that defines command line arguments.
|
|
341
|
+
*
|
|
342
|
+
* @internal
|
|
343
|
+
*/
|
|
344
|
+
type ExtractOptionValue<A extends ArgSchema> = A['type'] extends 'string' ? ResolveOptionValue<A, string> : A['type'] extends 'boolean' ? ResolveOptionValue<A, boolean> : A['type'] extends 'number' ? ResolveOptionValue<A, number> : A['type'] extends 'positional' ? ResolveOptionValue<A, string> : A['type'] extends 'enum' ? A['choices'] extends string[] | readonly string[] ? ResolveOptionValue<A, A['choices'][number]> : never : A['type'] extends 'custom' ? IsFunction<A['parse']> extends true ? ResolveOptionValue<A, ReturnType<NonNullable<A['parse']>>> : never : ResolveOptionValue<A, string | boolean | number>;
|
|
345
|
+
type ResolveOptionValue<A extends ArgSchema, T> = A['multiple'] extends true ? T[] : T;
|
|
346
|
+
/**
|
|
347
|
+
* Resolved argument values.
|
|
348
|
+
*
|
|
349
|
+
* @typeParam A - {@link Arguments | Args} which is an object that defines the command line arguments.
|
|
350
|
+
* @typeParam V - Resolvable argument values.
|
|
351
|
+
*
|
|
352
|
+
* @internal
|
|
353
|
+
*/
|
|
354
|
+
type ResolveArgValues<A extends Args, V extends Record<keyof A, unknown>> = { -readonly [Arg in keyof A]?: V[Arg] } & FilterArgs<A, V, 'default'> & FilterArgs<A, V, 'required'> & FilterPositionalArgs<A, V> extends infer P ? { [K in keyof P]: P[K] } : never;
|
|
355
|
+
/**
|
|
356
|
+
* Filters the arguments based on their default values.
|
|
357
|
+
*
|
|
358
|
+
* @typeParam A - {@link Args | Arguments}, which is an object that defines the command line arguments.
|
|
359
|
+
* @typeParam V - Resolvable argument values.
|
|
360
|
+
* @typeParam K - Key of the {@link ArgSchema | argument schema} to filter by.
|
|
361
|
+
*
|
|
362
|
+
* @internal
|
|
363
|
+
*/
|
|
364
|
+
type FilterArgs<A extends Args, V extends Record<keyof A, unknown>, K extends keyof ArgSchema> = { [Arg in keyof A as A[Arg][K] extends {} ? Arg : never]: V[Arg] };
|
|
365
|
+
/**
|
|
366
|
+
* Filters positional arguments from the argument schema.
|
|
367
|
+
*
|
|
368
|
+
* @typeParam A - {@link Args | Arguments}, which is an object that defines the command line arguments.
|
|
369
|
+
* @typeParam V - Resolvable argument values.
|
|
370
|
+
*
|
|
371
|
+
* @internal
|
|
372
|
+
*/
|
|
373
|
+
type FilterPositionalArgs<A extends Args, V extends Record<keyof A, unknown>> = { [Arg in keyof A as A[Arg]['type'] extends 'positional' ? Arg : never]: V[Arg] };
|
|
374
|
+
/**
|
|
375
|
+
* An arguments for {@link resolveArgs | resolve arguments}.
|
|
376
|
+
*/
|
|
377
|
+
interface ResolveArgs {
|
|
378
|
+
/**
|
|
379
|
+
* Whether to group short arguments.
|
|
380
|
+
*
|
|
381
|
+
* @see guideline 5 in https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap12.html
|
|
382
|
+
*
|
|
383
|
+
* @default false
|
|
384
|
+
*/
|
|
385
|
+
shortGrouping?: boolean;
|
|
386
|
+
/**
|
|
387
|
+
* Skip positional arguments index.
|
|
388
|
+
*
|
|
389
|
+
* @default -1
|
|
390
|
+
*/
|
|
391
|
+
skipPositional?: number;
|
|
392
|
+
/**
|
|
393
|
+
* Whether to convert the argument name to kebab-case. This option is applied to all arguments as `toKebab: true`, if set to `true`.
|
|
394
|
+
*
|
|
395
|
+
* @default false
|
|
396
|
+
*/
|
|
397
|
+
toKebab?: boolean;
|
|
398
|
+
}
|
|
399
|
+
/**
|
|
400
|
+
* Tracks which arguments were explicitly provided by the user.
|
|
401
|
+
*
|
|
402
|
+
* Each property indicates whether the corresponding argument was explicitly
|
|
403
|
+
* provided (true) or is using a default value or not provided (false).
|
|
404
|
+
*
|
|
405
|
+
* @typeParam A - {@link Args | Arguments}, which is an object that defines the command line arguments.
|
|
406
|
+
*/
|
|
407
|
+
type ArgExplicitlyProvided<A extends Args> = { [K in keyof A]: boolean };
|
|
408
|
+
/**
|
|
409
|
+
* Resolve command line arguments.
|
|
410
|
+
*
|
|
411
|
+
* @typeParam A - {@link Args | Arguments}, which is an object that defines the command line arguments.
|
|
412
|
+
*
|
|
413
|
+
* @param args - An arguments that contains {@link ArgSchema | arguments schema}.
|
|
414
|
+
* @param tokens - An array of {@link ArgToken | tokens}.
|
|
415
|
+
* @param resolveArgs - An arguments that contains {@link ResolveArgs | resolve arguments}.
|
|
416
|
+
* @returns An object that contains the values of the arguments, positional arguments, rest arguments, {@link AggregateError | validation errors}, and explicit provision status.
|
|
417
|
+
*
|
|
418
|
+
* @example
|
|
419
|
+
* ```typescript
|
|
420
|
+
* // passed tokens: --port 3000
|
|
421
|
+
*
|
|
422
|
+
* const { values, explicit } = resolveArgs({
|
|
423
|
+
* port: {
|
|
424
|
+
* type: 'number',
|
|
425
|
+
* default: 8080
|
|
426
|
+
* },
|
|
427
|
+
* host: {
|
|
428
|
+
* type: 'string',
|
|
429
|
+
* default: 'localhost'
|
|
430
|
+
* }
|
|
431
|
+
* }, parsedTokens)
|
|
432
|
+
*
|
|
433
|
+
* values.port // 3000
|
|
434
|
+
* values.host // 'localhost'
|
|
435
|
+
*
|
|
436
|
+
* explicit.port // true (explicitly provided)
|
|
437
|
+
* explicit.host // false (not provided, fallback to default)
|
|
438
|
+
* ```
|
|
439
|
+
*/
|
|
440
|
+
declare function resolveArgs<A extends Args>(args: A, tokens: ArgToken[], {
|
|
441
|
+
shortGrouping,
|
|
442
|
+
skipPositional,
|
|
443
|
+
toKebab
|
|
444
|
+
}?: ResolveArgs): {
|
|
445
|
+
values: ArgValues<A>;
|
|
446
|
+
positionals: string[];
|
|
447
|
+
rest: string[];
|
|
448
|
+
error: AggregateError | undefined;
|
|
449
|
+
explicit: ArgExplicitlyProvided<A>;
|
|
450
|
+
};
|
|
451
|
+
/**
|
|
452
|
+
* An error type for {@link ArgResolveError}.
|
|
453
|
+
*/
|
|
454
|
+
type ArgResolveErrorType = 'type' | 'required';
|
|
455
|
+
/**
|
|
456
|
+
* An error that occurs when resolving arguments.
|
|
457
|
+
* This error is thrown when the argument is not valid.
|
|
458
|
+
*/
|
|
459
|
+
declare class ArgResolveError extends Error {
|
|
460
|
+
name: string;
|
|
461
|
+
schema: ArgSchema;
|
|
462
|
+
type: ArgResolveErrorType;
|
|
463
|
+
/**
|
|
464
|
+
* Create an instance of ArgResolveError.
|
|
465
|
+
*
|
|
466
|
+
* @param message - the error message
|
|
467
|
+
* @param name - the name of the argument
|
|
468
|
+
* @param type - the type of the error, either 'type' or 'required'
|
|
469
|
+
* @param schema - the argument schema that caused the error
|
|
470
|
+
*/
|
|
471
|
+
constructor(message: string, name: string, type: ArgResolveErrorType, schema: ArgSchema);
|
|
472
|
+
}
|
|
473
|
+
//#endregion
|
|
474
|
+
export { ArgExplicitlyProvided, ArgResolveError as ArgResolveError$1, ArgResolveErrorType, ArgSchema, ArgValues, Args, ExtractOptionValue, FilterArgs, FilterPositionalArgs, ResolveArgValues, ResolveArgs, resolveArgs as resolveArgs$1 };
|
|
@@ -1,10 +1,13 @@
|
|
|
1
|
-
import { hasLongOptionPrefix, isShortOption } from "./parser-
|
|
2
|
-
import { kebabnize } from "./utils-
|
|
1
|
+
import { hasLongOptionPrefix, isShortOption } from "./parser-M-ayhS1h.js";
|
|
2
|
+
import { kebabnize } from "./utils-1LQrGCWG.js";
|
|
3
3
|
|
|
4
4
|
//#region src/resolver.ts
|
|
5
5
|
const SKIP_POSITIONAL_DEFAULT = -1;
|
|
6
6
|
/**
|
|
7
7
|
* Resolve command line arguments.
|
|
8
|
+
*
|
|
9
|
+
* @typeParam A - {@link Args | Arguments}, which is an object that defines the command line arguments.
|
|
10
|
+
*
|
|
8
11
|
* @param args - An arguments that contains {@link ArgSchema | arguments schema}.
|
|
9
12
|
* @param tokens - An array of {@link ArgToken | tokens}.
|
|
10
13
|
* @param resolveArgs - An arguments that contains {@link ResolveArgs | resolve arguments}.
|
|
@@ -230,6 +233,14 @@ var ArgResolveError = class extends Error {
|
|
|
230
233
|
name;
|
|
231
234
|
schema;
|
|
232
235
|
type;
|
|
236
|
+
/**
|
|
237
|
+
* Create an instance of ArgResolveError.
|
|
238
|
+
*
|
|
239
|
+
* @param message - the error message
|
|
240
|
+
* @param name - the name of the argument
|
|
241
|
+
* @param type - the type of the error, either 'type' or 'required'
|
|
242
|
+
* @param schema - the argument schema that caused the error
|
|
243
|
+
*/
|
|
233
244
|
constructor(message, name, type, schema) {
|
|
234
245
|
super(message);
|
|
235
246
|
this.name = name;
|
package/lib/resolver.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import "./parser-
|
|
2
|
-
import { ArgExplicitlyProvided, ArgResolveError$1 as ArgResolveError, ArgResolveErrorType, ArgSchema, ArgValues, Args, ExtractOptionValue, FilterArgs, FilterPositionalArgs, ResolveArgValues, ResolveArgs, resolveArgs$1 as resolveArgs } from "./resolver-
|
|
1
|
+
import "./parser-DEYiqyo1.js";
|
|
2
|
+
import { ArgExplicitlyProvided, ArgResolveError$1 as ArgResolveError, ArgResolveErrorType, ArgSchema, ArgValues, Args, ExtractOptionValue, FilterArgs, FilterPositionalArgs, ResolveArgValues, ResolveArgs, resolveArgs$1 as resolveArgs } from "./resolver-7JOAwfSr.js";
|
|
3
3
|
export { ArgExplicitlyProvided, ArgResolveError, ArgResolveErrorType, ArgSchema, ArgValues, Args, ExtractOptionValue, FilterArgs, FilterPositionalArgs, ResolveArgValues, ResolveArgs, resolveArgs };
|
package/lib/resolver.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import "./parser-
|
|
2
|
-
import "./utils-
|
|
3
|
-
import { ArgResolveError, resolveArgs } from "./resolver-
|
|
1
|
+
import "./parser-M-ayhS1h.js";
|
|
2
|
+
import "./utils-1LQrGCWG.js";
|
|
3
|
+
import { ArgResolveError, resolveArgs } from "./resolver-DBvNkaE7.js";
|
|
4
4
|
|
|
5
5
|
export { ArgResolveError, resolveArgs };
|
|
@@ -10,6 +10,12 @@
|
|
|
10
10
|
* @author kazuya kawaguchi (a.k.a. kazupon)
|
|
11
11
|
* @license MIT
|
|
12
12
|
*/
|
|
13
|
+
/**
|
|
14
|
+
* Convert a string to kebab-case.
|
|
15
|
+
*
|
|
16
|
+
* @param str - A string to convert
|
|
17
|
+
* @returns Converted string into kebab-case.
|
|
18
|
+
*/
|
|
13
19
|
function kebabnize(str) {
|
|
14
20
|
return str.replace(/[A-Z]/g, (match, offset) => (offset > 0 ? "-" : "") + match.toLowerCase());
|
|
15
21
|
}
|
package/lib/utils.d.ts
CHANGED
|
@@ -10,6 +10,12 @@
|
|
|
10
10
|
* @author kazuya kawaguchi (a.k.a. kazupon)
|
|
11
11
|
* @license MIT
|
|
12
12
|
*/
|
|
13
|
+
/**
|
|
14
|
+
* Convert a string to kebab-case.
|
|
15
|
+
*
|
|
16
|
+
* @param str - A string to convert
|
|
17
|
+
* @returns Converted string into kebab-case.
|
|
18
|
+
*/
|
|
13
19
|
declare function kebabnize(str: string): string;
|
|
14
20
|
//#endregion
|
|
15
21
|
export { kebabnize };
|
package/lib/utils.js
CHANGED
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.22.
|
|
4
|
+
"version": "0.22.2",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "kazuya kawaguchi",
|
|
7
7
|
"email": "kawakazu80@gmail.com"
|
|
@@ -72,15 +72,16 @@
|
|
|
72
72
|
},
|
|
73
73
|
"devDependencies": {
|
|
74
74
|
"@eslint/markdown": "^6.6.0",
|
|
75
|
-
"@kazupon/eslint-config": "^0.33.
|
|
75
|
+
"@kazupon/eslint-config": "^0.33.3",
|
|
76
76
|
"@kazupon/prettier-config": "^0.1.1",
|
|
77
77
|
"@types/node": "^22.17.0",
|
|
78
|
-
"@typescript/native-preview": "7.0.0-dev.
|
|
78
|
+
"@typescript/native-preview": "7.0.0-dev.20250801.1",
|
|
79
79
|
"@vitest/eslint-plugin": "^1.3.4",
|
|
80
|
-
"bumpp": "^10.2.
|
|
80
|
+
"bumpp": "^10.2.2",
|
|
81
81
|
"deno": "^2.4.3",
|
|
82
82
|
"eslint": "^9.32.0",
|
|
83
83
|
"eslint-config-prettier": "^10.1.8",
|
|
84
|
+
"eslint-plugin-jsdoc": "^52.0.2",
|
|
84
85
|
"eslint-plugin-jsonc": "^2.20.1",
|
|
85
86
|
"eslint-plugin-promise": "^7.2.1",
|
|
86
87
|
"eslint-plugin-regexp": "^2.9.0",
|
|
@@ -94,8 +95,8 @@
|
|
|
94
95
|
"mitata": "^1.0.34",
|
|
95
96
|
"pkg-pr-new": "^0.0.54",
|
|
96
97
|
"prettier": "^3.6.2",
|
|
97
|
-
"tsdown": "^0.13.
|
|
98
|
-
"typescript": "^5.
|
|
98
|
+
"tsdown": "^0.13.1",
|
|
99
|
+
"typescript": "^5.9.2",
|
|
99
100
|
"typescript-eslint": "^8.38.0",
|
|
100
101
|
"vitest": "^3.2.4",
|
|
101
102
|
"zod": "^4.0.14"
|
|
@@ -1,173 +0,0 @@
|
|
|
1
|
-
import { ArgToken } from "./parser-Cbxholql.js";
|
|
2
|
-
|
|
3
|
-
//#region src/resolver.d.ts
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* An argument schema
|
|
7
|
-
* This schema is similar to the schema of the `node:utils`.
|
|
8
|
-
* difference is that:
|
|
9
|
-
* - `required` property and `description` property are added
|
|
10
|
-
* - `type` is not only 'string' and 'boolean', but also 'number', 'enum', 'positional', 'custom' too.
|
|
11
|
-
* - `default` property type, not support multiple types
|
|
12
|
-
*/
|
|
13
|
-
interface ArgSchema {
|
|
14
|
-
/**
|
|
15
|
-
* Type of argument.
|
|
16
|
-
*/
|
|
17
|
-
type: 'string' | 'boolean' | 'number' | 'enum' | 'positional' | 'custom';
|
|
18
|
-
/**
|
|
19
|
-
* A single character alias for the argument.
|
|
20
|
-
*/
|
|
21
|
-
short?: string;
|
|
22
|
-
/**
|
|
23
|
-
* A description of the argument.
|
|
24
|
-
*/
|
|
25
|
-
description?: string;
|
|
26
|
-
/**
|
|
27
|
-
* Whether the argument is required or not.
|
|
28
|
-
*/
|
|
29
|
-
required?: true;
|
|
30
|
-
/**
|
|
31
|
-
* Whether the argument allow multiple values or not.
|
|
32
|
-
*/
|
|
33
|
-
multiple?: true;
|
|
34
|
-
/**
|
|
35
|
-
* Whether the negatable option for `boolean` type
|
|
36
|
-
*/
|
|
37
|
-
negatable?: boolean;
|
|
38
|
-
/**
|
|
39
|
-
* The allowed values of the argument, and string only. This property is only used when the type is 'enum'.
|
|
40
|
-
*/
|
|
41
|
-
choices?: string[] | readonly string[];
|
|
42
|
-
/**
|
|
43
|
-
* The default value of the argument.
|
|
44
|
-
* if the type is 'enum', the default value must be one of the allowed values.
|
|
45
|
-
*/
|
|
46
|
-
default?: string | boolean | number;
|
|
47
|
-
/**
|
|
48
|
-
* Whether to convert the argument name to kebab-case.
|
|
49
|
-
*/
|
|
50
|
-
toKebab?: true;
|
|
51
|
-
/**
|
|
52
|
-
* A function to parse the value of the argument. if the type is 'custom', this function is required.
|
|
53
|
-
* If argument value will be invalid, this function have to throw an error.
|
|
54
|
-
* @param value
|
|
55
|
-
* @returns Parsed value
|
|
56
|
-
* @throws An Error, If the value is invalid. Error type should be `Error` or extends it
|
|
57
|
-
*/
|
|
58
|
-
parse?: (value: string) => any;
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* An object that contains {@link ArgSchema | argument schema}.
|
|
62
|
-
*/
|
|
63
|
-
interface Args {
|
|
64
|
-
[option: string]: ArgSchema;
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* An object that contains the values of the arguments.
|
|
68
|
-
*/
|
|
69
|
-
type ArgValues<T> = T extends Args ? ResolveArgValues<T, { [Arg in keyof T]: ExtractOptionValue<T[Arg]> }> : {
|
|
70
|
-
[option: string]: string | boolean | number | (string | boolean | number)[] | undefined;
|
|
71
|
-
};
|
|
72
|
-
type IsFunction<T> = T extends ((...args: any[]) => any) ? true : false;
|
|
73
|
-
/**
|
|
74
|
-
* @internal
|
|
75
|
-
*/
|
|
76
|
-
type ExtractOptionValue<A extends ArgSchema> = A['type'] extends 'string' ? ResolveOptionValue<A, string> : A['type'] extends 'boolean' ? ResolveOptionValue<A, boolean> : A['type'] extends 'number' ? ResolveOptionValue<A, number> : A['type'] extends 'positional' ? ResolveOptionValue<A, string> : A['type'] extends 'enum' ? A['choices'] extends string[] | readonly string[] ? ResolveOptionValue<A, A['choices'][number]> : never : A['type'] extends 'custom' ? IsFunction<A['parse']> extends true ? ResolveOptionValue<A, ReturnType<NonNullable<A['parse']>>> : never : ResolveOptionValue<A, string | boolean | number>;
|
|
77
|
-
type ResolveOptionValue<A extends ArgSchema, T> = A['multiple'] extends true ? T[] : T;
|
|
78
|
-
/**
|
|
79
|
-
* @internal
|
|
80
|
-
*/
|
|
81
|
-
type ResolveArgValues<A extends Args, V extends Record<keyof A, unknown>> = { -readonly [Arg in keyof A]?: V[Arg] } & FilterArgs<A, V, 'default'> & FilterArgs<A, V, 'required'> & FilterPositionalArgs<A, V> extends infer P ? { [K in keyof P]: P[K] } : never;
|
|
82
|
-
/**
|
|
83
|
-
* @internal
|
|
84
|
-
*/
|
|
85
|
-
type FilterArgs<A extends Args, V extends Record<keyof A, unknown>, K extends keyof ArgSchema> = { [Arg in keyof A as A[Arg][K] extends {} ? Arg : never]: V[Arg] };
|
|
86
|
-
/**
|
|
87
|
-
* @internal
|
|
88
|
-
*/
|
|
89
|
-
type FilterPositionalArgs<A extends Args, V extends Record<keyof A, unknown>> = { [Arg in keyof A as A[Arg]['type'] extends 'positional' ? Arg : never]: V[Arg] };
|
|
90
|
-
/**
|
|
91
|
-
* An arguments for {@link resolveArgs | resolve arguments}.
|
|
92
|
-
*/
|
|
93
|
-
interface ResolveArgs {
|
|
94
|
-
/**
|
|
95
|
-
* Whether to group short arguments.
|
|
96
|
-
* @default false
|
|
97
|
-
* @see guideline 5 in https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap12.html
|
|
98
|
-
*/
|
|
99
|
-
shortGrouping?: boolean;
|
|
100
|
-
/**
|
|
101
|
-
* Skip positional arguments index.
|
|
102
|
-
* @default -1
|
|
103
|
-
*/
|
|
104
|
-
skipPositional?: number;
|
|
105
|
-
/**
|
|
106
|
-
* Whether to convert the argument name to kebab-case. This option is applied to all arguments as `toKebab: true`, if set to `true`.
|
|
107
|
-
* @default false
|
|
108
|
-
*/
|
|
109
|
-
toKebab?: boolean;
|
|
110
|
-
}
|
|
111
|
-
/**
|
|
112
|
-
* Tracks which arguments were explicitly provided by the user.
|
|
113
|
-
*
|
|
114
|
-
* Each property indicates whether the corresponding argument was explicitly
|
|
115
|
-
* provided (true) or is using a default value or not provided (false).
|
|
116
|
-
*/
|
|
117
|
-
type ArgExplicitlyProvided<A extends Args> = { [K in keyof A]: boolean };
|
|
118
|
-
/**
|
|
119
|
-
* Resolve command line arguments.
|
|
120
|
-
* @param args - An arguments that contains {@link ArgSchema | arguments schema}.
|
|
121
|
-
* @param tokens - An array of {@link ArgToken | tokens}.
|
|
122
|
-
* @param resolveArgs - An arguments that contains {@link ResolveArgs | resolve arguments}.
|
|
123
|
-
* @returns An object that contains the values of the arguments, positional arguments, rest arguments, {@link AggregateError | validation errors}, and explicit provision status.
|
|
124
|
-
*
|
|
125
|
-
* @example
|
|
126
|
-
* ```typescript
|
|
127
|
-
* // passed tokens: --port 3000
|
|
128
|
-
*
|
|
129
|
-
* const { values, explicit } = resolveArgs({
|
|
130
|
-
* port: {
|
|
131
|
-
* type: 'number',
|
|
132
|
-
* default: 8080
|
|
133
|
-
* },
|
|
134
|
-
* host: {
|
|
135
|
-
* type: 'string',
|
|
136
|
-
* default: 'localhost'
|
|
137
|
-
* }
|
|
138
|
-
* }, parsedTokens)
|
|
139
|
-
*
|
|
140
|
-
* values.port // 3000
|
|
141
|
-
* values.host // 'localhost'
|
|
142
|
-
*
|
|
143
|
-
* explicit.port // true (explicitly provided)
|
|
144
|
-
* explicit.host // false (not provided, fallback to default)
|
|
145
|
-
* ```
|
|
146
|
-
*/
|
|
147
|
-
declare function resolveArgs<A extends Args>(args: A, tokens: ArgToken[], {
|
|
148
|
-
shortGrouping,
|
|
149
|
-
skipPositional,
|
|
150
|
-
toKebab
|
|
151
|
-
}?: ResolveArgs): {
|
|
152
|
-
values: ArgValues<A>;
|
|
153
|
-
positionals: string[];
|
|
154
|
-
rest: string[];
|
|
155
|
-
error: AggregateError | undefined;
|
|
156
|
-
explicit: ArgExplicitlyProvided<A>;
|
|
157
|
-
};
|
|
158
|
-
/**
|
|
159
|
-
* An error type for {@link ArgResolveError}.
|
|
160
|
-
*/
|
|
161
|
-
type ArgResolveErrorType = 'type' | 'required';
|
|
162
|
-
/**
|
|
163
|
-
* An error that occurs when resolving arguments.
|
|
164
|
-
* This error is thrown when the argument is not valid.
|
|
165
|
-
*/
|
|
166
|
-
declare class ArgResolveError extends Error {
|
|
167
|
-
name: string;
|
|
168
|
-
schema: ArgSchema;
|
|
169
|
-
type: ArgResolveErrorType;
|
|
170
|
-
constructor(message: string, name: string, type: ArgResolveErrorType, schema: ArgSchema);
|
|
171
|
-
}
|
|
172
|
-
//#endregion
|
|
173
|
-
export { ArgExplicitlyProvided, ArgResolveError as ArgResolveError$1, ArgResolveErrorType, ArgSchema, ArgValues, Args, ExtractOptionValue, FilterArgs, FilterPositionalArgs, ResolveArgValues, ResolveArgs, resolveArgs as resolveArgs$1 };
|