args-tokens 0.4.0 → 0.5.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/lib/cjs/index.d.ts +2 -2
- package/lib/cjs/index.js +3 -16
- package/lib/cjs/parse.d.ts +2 -3
- package/lib/cjs/resolver.js +46 -12
- package/lib/esm/index.d.ts +2 -2
- package/lib/esm/index.js +1 -1
- package/lib/esm/parse.d.ts +2 -3
- package/lib/esm/resolver.js +46 -12
- package/package.json +1 -1
- package/lib/cjs/resolver.test-d.d.ts +0 -1
- package/lib/cjs/resolver.test-d.js +0 -6
- package/lib/esm/resolver.test-d.d.ts +0 -1
- package/lib/esm/resolver.test-d.js +0 -4
package/lib/cjs/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export
|
|
1
|
+
export { parse } from './parse.js';
|
|
2
2
|
export { parseArgs } from './parser.js';
|
|
3
3
|
export { resolveArgs } from './resolver.js';
|
|
4
|
-
export type
|
|
4
|
+
export type { ParsedArgs, ParseOptions } from './parse';
|
|
5
5
|
export type { ArgToken, ParserOptions } from './parser';
|
|
6
6
|
export type { ArgOptionSchema } from './resolver';
|
package/lib/cjs/index.js
CHANGED
|
@@ -1,21 +1,8 @@
|
|
|
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.resolveArgs = exports.parseArgs = void 0;
|
|
18
|
-
|
|
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; } });
|
|
19
6
|
var parser_js_1 = require("./parser.js");
|
|
20
7
|
Object.defineProperty(exports, "parseArgs", { enumerable: true, get: function () { return parser_js_1.parseArgs; } });
|
|
21
8
|
var resolver_js_1 = require("./resolver.js");
|
package/lib/cjs/parse.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import type { ArgOptions, ArgValues } from './resolver';
|
|
|
3
3
|
/**
|
|
4
4
|
* Parse options for {@link parse} function
|
|
5
5
|
*/
|
|
6
|
-
interface ParseOptions<O extends ArgOptions> extends ParserOptions {
|
|
6
|
+
export interface ParseOptions<O extends ArgOptions> extends ParserOptions {
|
|
7
7
|
/**
|
|
8
8
|
* Command line options, about details see {@link ArgOptions}
|
|
9
9
|
*/
|
|
@@ -12,7 +12,7 @@ interface ParseOptions<O extends ArgOptions> extends ParserOptions {
|
|
|
12
12
|
/**
|
|
13
13
|
* Parsed command line arguments
|
|
14
14
|
*/
|
|
15
|
-
type ParsedArgs<T extends ArgOptions> = {
|
|
15
|
+
export type ParsedArgs<T extends ArgOptions> = {
|
|
16
16
|
/**
|
|
17
17
|
* Parsed values, same as `values` in {@link resolveArgs}
|
|
18
18
|
*/
|
|
@@ -38,4 +38,3 @@ type ParsedArgs<T extends ArgOptions> = {
|
|
|
38
38
|
* @returns parsed values
|
|
39
39
|
*/
|
|
40
40
|
export declare function parse<O extends ArgOptions>(args: string[], options?: ParseOptions<O>): ParsedArgs<O>;
|
|
41
|
-
export {};
|
package/lib/cjs/resolver.js
CHANGED
|
@@ -9,9 +9,10 @@ function resolveArgs(options, tokens) {
|
|
|
9
9
|
const positionals = [];
|
|
10
10
|
const longOptionTokens = [];
|
|
11
11
|
const shortOptionTokens = [];
|
|
12
|
+
let currentLongOption;
|
|
12
13
|
let currentShortOption;
|
|
13
14
|
const expandableShortOptions = [];
|
|
14
|
-
function
|
|
15
|
+
function toShortValue() {
|
|
15
16
|
if (expandableShortOptions.length === 0) {
|
|
16
17
|
return undefined;
|
|
17
18
|
}
|
|
@@ -21,28 +22,44 @@ function resolveArgs(options, tokens) {
|
|
|
21
22
|
return value;
|
|
22
23
|
}
|
|
23
24
|
}
|
|
24
|
-
function
|
|
25
|
+
function applyLongOptionValue(value = undefined) {
|
|
26
|
+
if (currentLongOption) {
|
|
27
|
+
currentLongOption.value = value;
|
|
28
|
+
longOptionTokens.push({ ...currentLongOption });
|
|
29
|
+
currentLongOption = undefined;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
function applyShortOptionValue(value = undefined) {
|
|
25
33
|
if (currentShortOption) {
|
|
26
|
-
currentShortOption.value =
|
|
34
|
+
currentShortOption.value = value || toShortValue();
|
|
27
35
|
shortOptionTokens.push({ ...currentShortOption });
|
|
28
36
|
currentShortOption = undefined;
|
|
29
37
|
}
|
|
30
38
|
}
|
|
31
39
|
/**
|
|
32
|
-
*
|
|
40
|
+
* analyze phase to resolve value
|
|
41
|
+
* separate tokens into positionals, long and short options, after that resolve values
|
|
33
42
|
*/
|
|
34
43
|
// eslint-disable-next-line unicorn/no-for-loop
|
|
35
44
|
for (let i = 0; i < tokens.length; i++) {
|
|
36
45
|
const token = tokens[i];
|
|
37
46
|
if (token.kind === 'positional') {
|
|
38
47
|
positionals.push(token.value);
|
|
39
|
-
|
|
48
|
+
// check if previous option is not resolved
|
|
49
|
+
applyLongOptionValue(token.value);
|
|
50
|
+
applyShortOptionValue(token.value);
|
|
40
51
|
}
|
|
41
52
|
else if (token.kind === 'option') {
|
|
42
53
|
if (token.rawName) {
|
|
43
54
|
if ((0, parser_js_1.hasLongOptionPrefix)(token.rawName)) {
|
|
44
|
-
|
|
45
|
-
|
|
55
|
+
if (token.inlineValue) {
|
|
56
|
+
longOptionTokens.push({ ...token });
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
currentLongOption = { ...token };
|
|
60
|
+
}
|
|
61
|
+
// check if previous short option is not resolved
|
|
62
|
+
applyShortOptionValue();
|
|
46
63
|
}
|
|
47
64
|
else if ((0, parser_js_1.isShortOption)(token.rawName)) {
|
|
48
65
|
if (currentShortOption) {
|
|
@@ -50,13 +67,17 @@ function resolveArgs(options, tokens) {
|
|
|
50
67
|
expandableShortOptions.push({ ...token });
|
|
51
68
|
}
|
|
52
69
|
else {
|
|
53
|
-
currentShortOption.value =
|
|
70
|
+
currentShortOption.value = toShortValue();
|
|
54
71
|
shortOptionTokens.push({ ...currentShortOption });
|
|
55
72
|
currentShortOption = { ...token };
|
|
56
73
|
}
|
|
74
|
+
// check if previous long option is not resolved
|
|
75
|
+
applyLongOptionValue();
|
|
57
76
|
}
|
|
58
77
|
else {
|
|
59
78
|
currentShortOption = { ...token };
|
|
79
|
+
// check if previous long option is not resolved
|
|
80
|
+
applyLongOptionValue();
|
|
60
81
|
}
|
|
61
82
|
}
|
|
62
83
|
}
|
|
@@ -67,15 +88,20 @@ function resolveArgs(options, tokens) {
|
|
|
67
88
|
shortOptionTokens.push({ ...currentShortOption });
|
|
68
89
|
currentShortOption = undefined;
|
|
69
90
|
}
|
|
91
|
+
// check if previous long option is not resolved
|
|
92
|
+
applyLongOptionValue();
|
|
70
93
|
}
|
|
71
94
|
}
|
|
72
95
|
else {
|
|
73
|
-
|
|
96
|
+
// check if previous option is not resolved
|
|
97
|
+
applyLongOptionValue();
|
|
98
|
+
applyShortOptionValue();
|
|
74
99
|
}
|
|
75
100
|
}
|
|
76
101
|
/**
|
|
77
|
-
* check if the last short option is not resolved
|
|
102
|
+
* check if the last long or short option is not resolved
|
|
78
103
|
*/
|
|
104
|
+
applyLongOptionValue();
|
|
79
105
|
applyShortOptionValue();
|
|
80
106
|
/**
|
|
81
107
|
* resolve values
|
|
@@ -90,7 +116,11 @@ function resolveArgs(options, tokens) {
|
|
|
90
116
|
// eslint-disable-next-line unicorn/no-null
|
|
91
117
|
if (option === token.name && token.rawName != null && (0, parser_js_1.hasLongOptionPrefix)(token.rawName)) {
|
|
92
118
|
validateRequire(token, option, schema);
|
|
93
|
-
if (schema.type
|
|
119
|
+
if (schema.type === 'boolean') {
|
|
120
|
+
// NOTE: re-set value to undefined, because long boolean type option is set on analyze phase
|
|
121
|
+
token.value = undefined;
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
94
124
|
validateValue(token, option, schema);
|
|
95
125
|
}
|
|
96
126
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
|
|
@@ -105,7 +135,11 @@ function resolveArgs(options, tokens) {
|
|
|
105
135
|
// eslint-disable-next-line unicorn/no-null
|
|
106
136
|
if (schema.short === token.name && token.rawName != null && (0, parser_js_1.isShortOption)(token.rawName)) {
|
|
107
137
|
validateRequire(token, option, schema);
|
|
108
|
-
if (schema.type
|
|
138
|
+
if (schema.type === 'boolean') {
|
|
139
|
+
// NOTE: re-set value to undefined, because short boolean type option is set on analyze phase
|
|
140
|
+
token.value = undefined;
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
109
143
|
validateValue(token, option, schema);
|
|
110
144
|
}
|
|
111
145
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
|
package/lib/esm/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export
|
|
1
|
+
export { parse } from './parse.js';
|
|
2
2
|
export { parseArgs } from './parser.js';
|
|
3
3
|
export { resolveArgs } from './resolver.js';
|
|
4
|
-
export type
|
|
4
|
+
export type { ParsedArgs, ParseOptions } from './parse';
|
|
5
5
|
export type { ArgToken, ParserOptions } from './parser';
|
|
6
6
|
export type { ArgOptionSchema } from './resolver';
|
package/lib/esm/index.js
CHANGED
package/lib/esm/parse.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import type { ArgOptions, ArgValues } from './resolver';
|
|
|
3
3
|
/**
|
|
4
4
|
* Parse options for {@link parse} function
|
|
5
5
|
*/
|
|
6
|
-
interface ParseOptions<O extends ArgOptions> extends ParserOptions {
|
|
6
|
+
export interface ParseOptions<O extends ArgOptions> extends ParserOptions {
|
|
7
7
|
/**
|
|
8
8
|
* Command line options, about details see {@link ArgOptions}
|
|
9
9
|
*/
|
|
@@ -12,7 +12,7 @@ interface ParseOptions<O extends ArgOptions> extends ParserOptions {
|
|
|
12
12
|
/**
|
|
13
13
|
* Parsed command line arguments
|
|
14
14
|
*/
|
|
15
|
-
type ParsedArgs<T extends ArgOptions> = {
|
|
15
|
+
export type ParsedArgs<T extends ArgOptions> = {
|
|
16
16
|
/**
|
|
17
17
|
* Parsed values, same as `values` in {@link resolveArgs}
|
|
18
18
|
*/
|
|
@@ -38,4 +38,3 @@ type ParsedArgs<T extends ArgOptions> = {
|
|
|
38
38
|
* @returns parsed values
|
|
39
39
|
*/
|
|
40
40
|
export declare function parse<O extends ArgOptions>(args: string[], options?: ParseOptions<O>): ParsedArgs<O>;
|
|
41
|
-
export {};
|
package/lib/esm/resolver.js
CHANGED
|
@@ -6,9 +6,10 @@ export function resolveArgs(options, tokens) {
|
|
|
6
6
|
const positionals = [];
|
|
7
7
|
const longOptionTokens = [];
|
|
8
8
|
const shortOptionTokens = [];
|
|
9
|
+
let currentLongOption;
|
|
9
10
|
let currentShortOption;
|
|
10
11
|
const expandableShortOptions = [];
|
|
11
|
-
function
|
|
12
|
+
function toShortValue() {
|
|
12
13
|
if (expandableShortOptions.length === 0) {
|
|
13
14
|
return undefined;
|
|
14
15
|
}
|
|
@@ -18,28 +19,44 @@ export function resolveArgs(options, tokens) {
|
|
|
18
19
|
return value;
|
|
19
20
|
}
|
|
20
21
|
}
|
|
21
|
-
function
|
|
22
|
+
function applyLongOptionValue(value = undefined) {
|
|
23
|
+
if (currentLongOption) {
|
|
24
|
+
currentLongOption.value = value;
|
|
25
|
+
longOptionTokens.push({ ...currentLongOption });
|
|
26
|
+
currentLongOption = undefined;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
function applyShortOptionValue(value = undefined) {
|
|
22
30
|
if (currentShortOption) {
|
|
23
|
-
currentShortOption.value =
|
|
31
|
+
currentShortOption.value = value || toShortValue();
|
|
24
32
|
shortOptionTokens.push({ ...currentShortOption });
|
|
25
33
|
currentShortOption = undefined;
|
|
26
34
|
}
|
|
27
35
|
}
|
|
28
36
|
/**
|
|
29
|
-
*
|
|
37
|
+
* analyze phase to resolve value
|
|
38
|
+
* separate tokens into positionals, long and short options, after that resolve values
|
|
30
39
|
*/
|
|
31
40
|
// eslint-disable-next-line unicorn/no-for-loop
|
|
32
41
|
for (let i = 0; i < tokens.length; i++) {
|
|
33
42
|
const token = tokens[i];
|
|
34
43
|
if (token.kind === 'positional') {
|
|
35
44
|
positionals.push(token.value);
|
|
36
|
-
|
|
45
|
+
// check if previous option is not resolved
|
|
46
|
+
applyLongOptionValue(token.value);
|
|
47
|
+
applyShortOptionValue(token.value);
|
|
37
48
|
}
|
|
38
49
|
else if (token.kind === 'option') {
|
|
39
50
|
if (token.rawName) {
|
|
40
51
|
if (hasLongOptionPrefix(token.rawName)) {
|
|
41
|
-
|
|
42
|
-
|
|
52
|
+
if (token.inlineValue) {
|
|
53
|
+
longOptionTokens.push({ ...token });
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
currentLongOption = { ...token };
|
|
57
|
+
}
|
|
58
|
+
// check if previous short option is not resolved
|
|
59
|
+
applyShortOptionValue();
|
|
43
60
|
}
|
|
44
61
|
else if (isShortOption(token.rawName)) {
|
|
45
62
|
if (currentShortOption) {
|
|
@@ -47,13 +64,17 @@ export function resolveArgs(options, tokens) {
|
|
|
47
64
|
expandableShortOptions.push({ ...token });
|
|
48
65
|
}
|
|
49
66
|
else {
|
|
50
|
-
currentShortOption.value =
|
|
67
|
+
currentShortOption.value = toShortValue();
|
|
51
68
|
shortOptionTokens.push({ ...currentShortOption });
|
|
52
69
|
currentShortOption = { ...token };
|
|
53
70
|
}
|
|
71
|
+
// check if previous long option is not resolved
|
|
72
|
+
applyLongOptionValue();
|
|
54
73
|
}
|
|
55
74
|
else {
|
|
56
75
|
currentShortOption = { ...token };
|
|
76
|
+
// check if previous long option is not resolved
|
|
77
|
+
applyLongOptionValue();
|
|
57
78
|
}
|
|
58
79
|
}
|
|
59
80
|
}
|
|
@@ -64,15 +85,20 @@ export function resolveArgs(options, tokens) {
|
|
|
64
85
|
shortOptionTokens.push({ ...currentShortOption });
|
|
65
86
|
currentShortOption = undefined;
|
|
66
87
|
}
|
|
88
|
+
// check if previous long option is not resolved
|
|
89
|
+
applyLongOptionValue();
|
|
67
90
|
}
|
|
68
91
|
}
|
|
69
92
|
else {
|
|
70
|
-
|
|
93
|
+
// check if previous option is not resolved
|
|
94
|
+
applyLongOptionValue();
|
|
95
|
+
applyShortOptionValue();
|
|
71
96
|
}
|
|
72
97
|
}
|
|
73
98
|
/**
|
|
74
|
-
* check if the last short option is not resolved
|
|
99
|
+
* check if the last long or short option is not resolved
|
|
75
100
|
*/
|
|
101
|
+
applyLongOptionValue();
|
|
76
102
|
applyShortOptionValue();
|
|
77
103
|
/**
|
|
78
104
|
* resolve values
|
|
@@ -87,7 +113,11 @@ export function resolveArgs(options, tokens) {
|
|
|
87
113
|
// eslint-disable-next-line unicorn/no-null
|
|
88
114
|
if (option === token.name && token.rawName != null && hasLongOptionPrefix(token.rawName)) {
|
|
89
115
|
validateRequire(token, option, schema);
|
|
90
|
-
if (schema.type
|
|
116
|
+
if (schema.type === 'boolean') {
|
|
117
|
+
// NOTE: re-set value to undefined, because long boolean type option is set on analyze phase
|
|
118
|
+
token.value = undefined;
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
91
121
|
validateValue(token, option, schema);
|
|
92
122
|
}
|
|
93
123
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
|
|
@@ -102,7 +132,11 @@ export function resolveArgs(options, tokens) {
|
|
|
102
132
|
// eslint-disable-next-line unicorn/no-null
|
|
103
133
|
if (schema.short === token.name && token.rawName != null && isShortOption(token.rawName)) {
|
|
104
134
|
validateRequire(token, option, schema);
|
|
105
|
-
if (schema.type
|
|
135
|
+
if (schema.type === 'boolean') {
|
|
136
|
+
// NOTE: re-set value to undefined, because short boolean type option is set on analyze phase
|
|
137
|
+
token.value = undefined;
|
|
138
|
+
}
|
|
139
|
+
else {
|
|
106
140
|
validateValue(token, option, schema);
|
|
107
141
|
}
|
|
108
142
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
|
package/package.json
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|