@optique/core 0.9.0-dev.201 → 0.9.0-dev.202
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/dist/constructs.cjs +1337 -589
- package/dist/constructs.d.cts +182 -62
- package/dist/constructs.d.ts +182 -62
- package/dist/constructs.js +1337 -589
- package/dist/facade.cjs +128 -138
- package/dist/facade.d.cts +4 -2
- package/dist/facade.d.ts +4 -2
- package/dist/facade.js +129 -139
- package/dist/index.cjs +4 -0
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/modifiers.cjs +228 -79
- package/dist/modifiers.d.cts +11 -6
- package/dist/modifiers.d.ts +11 -6
- package/dist/modifiers.js +228 -79
- package/dist/parser.cjs +158 -5
- package/dist/parser.d.cts +164 -12
- package/dist/parser.d.ts +164 -12
- package/dist/parser.js +155 -6
- package/dist/primitives.cjs +242 -97
- package/dist/primitives.d.cts +14 -10
- package/dist/primitives.d.ts +14 -10
- package/dist/primitives.js +242 -97
- package/dist/valueparser.cjs +10 -1
- package/dist/valueparser.d.cts +29 -16
- package/dist/valueparser.d.ts +29 -16
- package/dist/valueparser.js +10 -1
- package/package.json +1 -1
package/dist/parser.js
CHANGED
|
@@ -9,17 +9,23 @@ import { argument, command, constant, flag, option, passThrough } from "./primit
|
|
|
9
9
|
* Parses an array of command-line arguments using the provided combined parser.
|
|
10
10
|
* This function processes the input arguments, applying the parser to each
|
|
11
11
|
* argument until all arguments are consumed or an error occurs.
|
|
12
|
+
*
|
|
13
|
+
* This function only accepts synchronous parsers. For asynchronous parsers,
|
|
14
|
+
* use {@link parseAsync}.
|
|
15
|
+
*
|
|
12
16
|
* @template T The type of the value produced by the parser.
|
|
13
17
|
* @param parser The combined {@link Parser} to use for parsing the input
|
|
14
|
-
* arguments.
|
|
18
|
+
* arguments. Must be a synchronous parser.
|
|
15
19
|
* @param args The array of command-line arguments to parse. Usually this is
|
|
16
20
|
* `process.argv.slice(2)` in Node.js or `Deno.args` in Deno.
|
|
17
21
|
* @returns A {@link Result} object indicating whether the parsing was
|
|
18
22
|
* successful or not. If successful, it contains the parsed value of
|
|
19
23
|
* type `T`. If not, it contains an error message describing the
|
|
20
24
|
* failure.
|
|
25
|
+
* @since 0.9.0 Renamed from the original `parse` function which now delegates
|
|
26
|
+
* to this for sync parsers.
|
|
21
27
|
*/
|
|
22
|
-
function
|
|
28
|
+
function parseSync(parser, args) {
|
|
23
29
|
let context = {
|
|
24
30
|
buffer: args,
|
|
25
31
|
optionsTerminated: false,
|
|
@@ -49,11 +55,86 @@ function parse(parser, args) {
|
|
|
49
55
|
};
|
|
50
56
|
}
|
|
51
57
|
/**
|
|
58
|
+
* Parses an array of command-line arguments using the provided combined parser.
|
|
59
|
+
* This function processes the input arguments, applying the parser to each
|
|
60
|
+
* argument until all arguments are consumed or an error occurs.
|
|
61
|
+
*
|
|
62
|
+
* This function accepts any parser (sync or async) and always returns a Promise.
|
|
63
|
+
* For synchronous parsing with sync parsers, use {@link parseSync} instead.
|
|
64
|
+
*
|
|
65
|
+
* @template T The type of the value produced by the parser.
|
|
66
|
+
* @param parser The combined {@link Parser} to use for parsing the input
|
|
67
|
+
* arguments.
|
|
68
|
+
* @param args The array of command-line arguments to parse. Usually this is
|
|
69
|
+
* `process.argv.slice(2)` in Node.js or `Deno.args` in Deno.
|
|
70
|
+
* @returns A Promise that resolves to a {@link Result} object indicating
|
|
71
|
+
* whether the parsing was successful or not.
|
|
72
|
+
* @since 0.9.0
|
|
73
|
+
*/
|
|
74
|
+
async function parseAsync(parser, args) {
|
|
75
|
+
let context = {
|
|
76
|
+
buffer: args,
|
|
77
|
+
optionsTerminated: false,
|
|
78
|
+
state: parser.initialState,
|
|
79
|
+
usage: parser.usage
|
|
80
|
+
};
|
|
81
|
+
do {
|
|
82
|
+
const result = await parser.parse(context);
|
|
83
|
+
if (!result.success) return {
|
|
84
|
+
success: false,
|
|
85
|
+
error: result.error
|
|
86
|
+
};
|
|
87
|
+
const previousBuffer = context.buffer;
|
|
88
|
+
context = result.next;
|
|
89
|
+
if (context.buffer.length > 0 && context.buffer.length === previousBuffer.length && context.buffer.every((item, i) => item === previousBuffer[i])) return {
|
|
90
|
+
success: false,
|
|
91
|
+
error: message`Unexpected option or argument: ${context.buffer[0]}.`
|
|
92
|
+
};
|
|
93
|
+
} while (context.buffer.length > 0);
|
|
94
|
+
const endResult = await parser.complete(context.state);
|
|
95
|
+
return endResult.success ? {
|
|
96
|
+
success: true,
|
|
97
|
+
value: endResult.value
|
|
98
|
+
} : {
|
|
99
|
+
success: false,
|
|
100
|
+
error: endResult.error
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Parses an array of command-line arguments using the provided combined parser.
|
|
105
|
+
* This function processes the input arguments, applying the parser to each
|
|
106
|
+
* argument until all arguments are consumed or an error occurs.
|
|
107
|
+
*
|
|
108
|
+
* The return type depends on the parser's mode:
|
|
109
|
+
* - Sync parsers return `Result<T>` directly.
|
|
110
|
+
* - Async parsers return `Promise<Result<T>>`.
|
|
111
|
+
*
|
|
112
|
+
* For explicit control, use {@link parseSync} or {@link parseAsync}.
|
|
113
|
+
*
|
|
114
|
+
* @template M The execution mode of the parser.
|
|
115
|
+
* @template T The type of the value produced by the parser.
|
|
116
|
+
* @param parser The combined {@link Parser} to use for parsing the input
|
|
117
|
+
* arguments.
|
|
118
|
+
* @param args The array of command-line arguments to parse. Usually this is
|
|
119
|
+
* `process.argv.slice(2)` in Node.js or `Deno.args` in Deno.
|
|
120
|
+
* @returns A {@link Result} object (for sync) or Promise thereof (for async)
|
|
121
|
+
* indicating whether the parsing was successful or not.
|
|
122
|
+
*/
|
|
123
|
+
function parse(parser, args) {
|
|
124
|
+
if (parser.$mode === "async") return parseAsync(parser, args);
|
|
125
|
+
return parseSync(parser, args);
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
52
128
|
* Generates command-line suggestions based on current parsing state.
|
|
53
129
|
* This function processes the input arguments up to the last argument,
|
|
54
130
|
* then calls the parser's suggest method with the remaining prefix.
|
|
131
|
+
*
|
|
132
|
+
* This function only accepts synchronous parsers. For asynchronous parsers,
|
|
133
|
+
* use {@link suggestAsync}.
|
|
134
|
+
*
|
|
55
135
|
* @template T The type of the value produced by the parser.
|
|
56
136
|
* @param parser The {@link Parser} to use for generating suggestions.
|
|
137
|
+
* Must be a synchronous parser.
|
|
57
138
|
* @param args The array of command-line arguments including the partial
|
|
58
139
|
* argument to complete. The last element is treated as
|
|
59
140
|
* the prefix for suggestions.
|
|
@@ -67,16 +148,17 @@ function parse(parser, args) {
|
|
|
67
148
|
* });
|
|
68
149
|
*
|
|
69
150
|
* // Get suggestions for options starting with "--"
|
|
70
|
-
* const suggestions =
|
|
151
|
+
* const suggestions = suggestSync(parser, ["--"]);
|
|
71
152
|
* // Returns: [{ text: "--verbose" }, { text: "--format" }]
|
|
72
153
|
*
|
|
73
154
|
* // Get suggestions after parsing some arguments
|
|
74
|
-
* const suggestions2 =
|
|
155
|
+
* const suggestions2 = suggestSync(parser, ["-v", "--format="]);
|
|
75
156
|
* // Returns: [{ text: "--format=json" }, { text: "--format=yaml" }]
|
|
76
157
|
* ```
|
|
77
158
|
* @since 0.6.0
|
|
159
|
+
* @since 0.9.0 Renamed from the original `suggest` function.
|
|
78
160
|
*/
|
|
79
|
-
function
|
|
161
|
+
function suggestSync(parser, args) {
|
|
80
162
|
const allButLast = args.slice(0, -1);
|
|
81
163
|
const prefix = args[args.length - 1];
|
|
82
164
|
let context = {
|
|
@@ -95,6 +177,73 @@ function suggest(parser, args) {
|
|
|
95
177
|
return Array.from(parser.suggest(context, prefix));
|
|
96
178
|
}
|
|
97
179
|
/**
|
|
180
|
+
* Generates command-line suggestions based on current parsing state.
|
|
181
|
+
* This function processes the input arguments up to the last argument,
|
|
182
|
+
* then calls the parser's suggest method with the remaining prefix.
|
|
183
|
+
*
|
|
184
|
+
* This function accepts any parser (sync or async) and always returns a Promise.
|
|
185
|
+
* For synchronous suggestion generation with sync parsers, use
|
|
186
|
+
* {@link suggestSync} instead.
|
|
187
|
+
*
|
|
188
|
+
* @template T The type of the value produced by the parser.
|
|
189
|
+
* @param parser The {@link Parser} to use for generating suggestions.
|
|
190
|
+
* @param args The array of command-line arguments including the partial
|
|
191
|
+
* argument to complete. The last element is treated as
|
|
192
|
+
* the prefix for suggestions.
|
|
193
|
+
* @returns A Promise that resolves to an array of {@link Suggestion} objects
|
|
194
|
+
* containing completion candidates.
|
|
195
|
+
* @since 0.9.0
|
|
196
|
+
*/
|
|
197
|
+
async function suggestAsync(parser, args) {
|
|
198
|
+
const allButLast = args.slice(0, -1);
|
|
199
|
+
const prefix = args[args.length - 1];
|
|
200
|
+
let context = {
|
|
201
|
+
buffer: allButLast,
|
|
202
|
+
optionsTerminated: false,
|
|
203
|
+
state: parser.initialState,
|
|
204
|
+
usage: parser.usage
|
|
205
|
+
};
|
|
206
|
+
while (context.buffer.length > 0) {
|
|
207
|
+
const result = await parser.parse(context);
|
|
208
|
+
if (!result.success) {
|
|
209
|
+
const suggestions$1 = [];
|
|
210
|
+
for await (const suggestion of parser.suggest(context, prefix)) suggestions$1.push(suggestion);
|
|
211
|
+
return suggestions$1;
|
|
212
|
+
}
|
|
213
|
+
const previousBuffer = context.buffer;
|
|
214
|
+
context = result.next;
|
|
215
|
+
if (context.buffer.length > 0 && context.buffer.length === previousBuffer.length && context.buffer.every((item, i) => item === previousBuffer[i])) return [];
|
|
216
|
+
}
|
|
217
|
+
const suggestions = [];
|
|
218
|
+
for await (const suggestion of parser.suggest(context, prefix)) suggestions.push(suggestion);
|
|
219
|
+
return suggestions;
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Generates command-line suggestions based on current parsing state.
|
|
223
|
+
* This function processes the input arguments up to the last argument,
|
|
224
|
+
* then calls the parser's suggest method with the remaining prefix.
|
|
225
|
+
*
|
|
226
|
+
* The return type depends on the parser's mode:
|
|
227
|
+
* - Sync parsers return `readonly Suggestion[]` directly.
|
|
228
|
+
* - Async parsers return `Promise<readonly Suggestion[]>`.
|
|
229
|
+
*
|
|
230
|
+
* For explicit control, use {@link suggestSync} or {@link suggestAsync}.
|
|
231
|
+
*
|
|
232
|
+
* @template M The execution mode of the parser.
|
|
233
|
+
* @template T The type of the value produced by the parser.
|
|
234
|
+
* @param parser The {@link Parser} to use for generating suggestions.
|
|
235
|
+
* @param args The array of command-line arguments including the partial
|
|
236
|
+
* argument to complete. The last element is treated as
|
|
237
|
+
* the prefix for suggestions.
|
|
238
|
+
* @returns An array of {@link Suggestion} objects (for sync) or Promise thereof
|
|
239
|
+
* (for async) containing completion candidates.
|
|
240
|
+
* @since 0.6.0
|
|
241
|
+
*/
|
|
242
|
+
function suggest(parser, args) {
|
|
243
|
+
if (parser.$mode === "async") return suggestAsync(parser, args);
|
|
244
|
+
return suggestSync(parser, args);
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
98
247
|
* Recursively searches for a command within nested exclusive usage terms.
|
|
99
248
|
* When the command is found, returns the expanded usage terms for that command.
|
|
100
249
|
*
|
|
@@ -193,4 +342,4 @@ function getDocPage(parser, args = []) {
|
|
|
193
342
|
}
|
|
194
343
|
|
|
195
344
|
//#endregion
|
|
196
|
-
export { DuplicateOptionError, WithDefaultError, argument, command, concat, conditional, constant, flag, getDocPage, group, longestMatch, map, merge, multiple, object, option, optional, or, parse, passThrough, suggest, tuple, withDefault };
|
|
345
|
+
export { DuplicateOptionError, WithDefaultError, argument, command, concat, conditional, constant, flag, getDocPage, group, longestMatch, map, merge, multiple, object, option, optional, or, parse, parseAsync, parseSync, passThrough, suggest, suggestAsync, suggestSync, tuple, withDefault };
|