@optique/core 1.0.0-dev.908 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/annotation-state.cjs +425 -0
- package/dist/annotation-state.d.cts +24 -0
- package/dist/annotation-state.d.ts +24 -0
- package/dist/annotation-state.js +414 -0
- package/dist/annotations.cjs +2 -248
- package/dist/annotations.d.cts +2 -137
- package/dist/annotations.d.ts +2 -137
- package/dist/annotations.js +2 -238
- package/dist/completion.cjs +611 -100
- package/dist/completion.d.cts +1 -1
- package/dist/completion.d.ts +1 -1
- package/dist/completion.js +611 -100
- package/dist/constructs.cjs +3338 -827
- package/dist/constructs.d.cts +48 -7
- package/dist/constructs.d.ts +48 -7
- package/dist/constructs.js +3338 -827
- package/dist/context.cjs +0 -23
- package/dist/context.d.cts +119 -53
- package/dist/context.d.ts +119 -53
- package/dist/context.js +0 -22
- package/dist/dependency-metadata.cjs +139 -0
- package/dist/dependency-metadata.d.cts +112 -0
- package/dist/dependency-metadata.d.ts +112 -0
- package/dist/dependency-metadata.js +138 -0
- package/dist/dependency-runtime.cjs +698 -0
- package/dist/dependency-runtime.d.cts +149 -0
- package/dist/dependency-runtime.d.ts +149 -0
- package/dist/dependency-runtime.js +687 -0
- package/dist/dependency.cjs +7 -928
- package/dist/dependency.d.cts +2 -794
- package/dist/dependency.d.ts +2 -794
- package/dist/dependency.js +2 -899
- package/dist/displaywidth.cjs +44 -0
- package/dist/displaywidth.js +43 -0
- package/dist/doc.cjs +285 -23
- package/dist/doc.d.cts +57 -2
- package/dist/doc.d.ts +57 -2
- package/dist/doc.js +283 -25
- package/dist/execution-context.cjs +56 -0
- package/dist/execution-context.js +53 -0
- package/dist/extension.cjs +87 -0
- package/dist/extension.d.cts +97 -0
- package/dist/extension.d.ts +97 -0
- package/dist/extension.js +76 -0
- package/dist/facade.cjs +718 -523
- package/dist/facade.d.cts +87 -18
- package/dist/facade.d.ts +87 -18
- package/dist/facade.js +718 -523
- package/dist/index.cjs +14 -29
- package/dist/index.d.cts +10 -10
- package/dist/index.d.ts +10 -10
- package/dist/index.js +7 -7
- package/dist/input-trace.cjs +56 -0
- package/dist/input-trace.d.cts +77 -0
- package/dist/input-trace.d.ts +77 -0
- package/dist/input-trace.js +55 -0
- package/dist/internal/annotations.cjs +316 -0
- package/dist/internal/annotations.d.cts +140 -0
- package/dist/internal/annotations.d.ts +140 -0
- package/dist/internal/annotations.js +306 -0
- package/dist/internal/dependency.cjs +984 -0
- package/dist/internal/dependency.d.cts +539 -0
- package/dist/internal/dependency.d.ts +539 -0
- package/dist/internal/dependency.js +964 -0
- package/dist/{mode-dispatch.cjs → internal/mode-dispatch.cjs} +1 -3
- package/dist/{mode-dispatch.d.cts → internal/mode-dispatch.d.cts} +3 -7
- package/dist/{mode-dispatch.d.ts → internal/mode-dispatch.d.ts} +3 -7
- package/dist/{mode-dispatch.js → internal/mode-dispatch.js} +1 -3
- package/dist/internal/parser.cjs +728 -0
- package/dist/internal/parser.d.cts +947 -0
- package/dist/internal/parser.d.ts +947 -0
- package/dist/internal/parser.js +711 -0
- package/dist/message.cjs +84 -26
- package/dist/message.d.cts +49 -9
- package/dist/message.d.ts +49 -9
- package/dist/message.js +84 -27
- package/dist/modifiers.cjs +1023 -240
- package/dist/modifiers.d.cts +42 -1
- package/dist/modifiers.d.ts +42 -1
- package/dist/modifiers.js +1023 -240
- package/dist/parser.cjs +11 -463
- package/dist/parser.d.cts +3 -537
- package/dist/parser.d.ts +3 -537
- package/dist/parser.js +2 -433
- package/dist/phase2-seed.cjs +59 -0
- package/dist/phase2-seed.js +56 -0
- package/dist/primitives.cjs +557 -208
- package/dist/primitives.d.cts +10 -14
- package/dist/primitives.d.ts +10 -14
- package/dist/primitives.js +557 -208
- package/dist/program.cjs +5 -1
- package/dist/program.d.cts +5 -3
- package/dist/program.d.ts +5 -3
- package/dist/program.js +6 -1
- package/dist/suggestion.cjs +22 -8
- package/dist/suggestion.js +22 -8
- package/dist/usage-internals.cjs +3 -2
- package/dist/usage-internals.js +4 -2
- package/dist/usage.cjs +195 -40
- package/dist/usage.d.cts +92 -11
- package/dist/usage.d.ts +92 -11
- package/dist/usage.js +194 -41
- package/dist/validate.cjs +170 -0
- package/dist/validate.js +164 -0
- package/dist/valueparser.cjs +1278 -191
- package/dist/valueparser.d.cts +330 -20
- package/dist/valueparser.d.ts +330 -20
- package/dist/valueparser.js +1277 -192
- package/package.json +9 -9
|
@@ -0,0 +1,947 @@
|
|
|
1
|
+
import { ParseOptions } from "./annotations.js";
|
|
2
|
+
import { Message } from "../message.js";
|
|
3
|
+
import { Usage } from "../usage.js";
|
|
4
|
+
import { DocFragments, DocPage } from "../doc.js";
|
|
5
|
+
import { DependencyRegistryLike } from "../registry-types.js";
|
|
6
|
+
import { DeferredMap, ValueParserResult } from "../valueparser.js";
|
|
7
|
+
import { ParserDependencyMetadata } from "../dependency-metadata.js";
|
|
8
|
+
import { DependencyRuntimeContext, RuntimeNode } from "../dependency-runtime.js";
|
|
9
|
+
import { InputTrace } from "../input-trace.js";
|
|
10
|
+
|
|
11
|
+
//#region src/internal/parser.d.ts
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Represents the execution mode for parsers.
|
|
15
|
+
*
|
|
16
|
+
* - `"sync"`: Synchronous execution where methods return values directly.
|
|
17
|
+
* - `"async"`: Asynchronous execution where methods return Promises or
|
|
18
|
+
* AsyncIterables.
|
|
19
|
+
*
|
|
20
|
+
* @since 0.9.0
|
|
21
|
+
*/
|
|
22
|
+
type Mode = "sync" | "async";
|
|
23
|
+
/**
|
|
24
|
+
* Wraps a value type based on the execution mode.
|
|
25
|
+
*
|
|
26
|
+
* - In sync mode: Returns `T` directly.
|
|
27
|
+
* - In async mode: Returns `Promise<T>`.
|
|
28
|
+
*
|
|
29
|
+
* @template M The execution mode.
|
|
30
|
+
* @template T The value type to wrap.
|
|
31
|
+
* @since 0.9.0
|
|
32
|
+
*/
|
|
33
|
+
type ModeValue<M extends Mode, T> = M extends "async" ? Promise<T> : T;
|
|
34
|
+
/**
|
|
35
|
+
* Wraps an iterable type based on the execution mode.
|
|
36
|
+
*
|
|
37
|
+
* - In sync mode: Returns `Iterable<T>`.
|
|
38
|
+
* - In async mode: Returns `AsyncIterable<T>`.
|
|
39
|
+
*
|
|
40
|
+
* @template M The execution mode.
|
|
41
|
+
* @template T The element type.
|
|
42
|
+
* @since 0.9.0
|
|
43
|
+
*/
|
|
44
|
+
type ModeIterable<M extends Mode, T> = M extends "async" ? AsyncIterable<T> : Iterable<T>;
|
|
45
|
+
/**
|
|
46
|
+
* Combines multiple modes into a single mode.
|
|
47
|
+
* If any mode is `"async"`, the result is `"async"`; otherwise `"sync"`.
|
|
48
|
+
*
|
|
49
|
+
* @template T A tuple of Mode types.
|
|
50
|
+
* @since 0.9.0
|
|
51
|
+
*/
|
|
52
|
+
type CombineModes<T extends readonly Mode[]> = "async" extends T[number] ? "async" : "sync";
|
|
53
|
+
/**
|
|
54
|
+
* Represents the state passed to getDocFragments.
|
|
55
|
+
* Can be either the actual parser state or an explicit indicator
|
|
56
|
+
* that no state is available.
|
|
57
|
+
* @template TState The type of the actual state when available.
|
|
58
|
+
* @since 0.3.0
|
|
59
|
+
*/
|
|
60
|
+
type DocState<TState> = {
|
|
61
|
+
readonly kind: "available";
|
|
62
|
+
readonly state: TState;
|
|
63
|
+
} | {
|
|
64
|
+
readonly kind: "unavailable";
|
|
65
|
+
};
|
|
66
|
+
/**
|
|
67
|
+
* Parser interface for command-line argument parsing.
|
|
68
|
+
* @template M The execution mode of the parser (`"sync"` or `"async"`).
|
|
69
|
+
* @template TValue The type of the value returned by the parser.
|
|
70
|
+
* @template TState The type of the state used during parsing.
|
|
71
|
+
* @since 0.9.0 Added the `M` type parameter for sync/async mode support.
|
|
72
|
+
*/
|
|
73
|
+
interface Parser<M extends Mode = "sync", TValue = unknown, TState = unknown> {
|
|
74
|
+
/**
|
|
75
|
+
* A type tag for the result value of this parser, used for type inference.
|
|
76
|
+
* Usually this is an empty array at runtime, but it does not matter
|
|
77
|
+
* what it contains.
|
|
78
|
+
* @internal
|
|
79
|
+
*/
|
|
80
|
+
readonly $valueType: readonly TValue[];
|
|
81
|
+
/**
|
|
82
|
+
* A type tag for the state of this parser, used for type inference.
|
|
83
|
+
* Usually this is an empty array at runtime, but it does not matter
|
|
84
|
+
* what it contains.
|
|
85
|
+
* @internal
|
|
86
|
+
*/
|
|
87
|
+
readonly $stateType: readonly TState[];
|
|
88
|
+
/**
|
|
89
|
+
* The execution mode of this parser.
|
|
90
|
+
*
|
|
91
|
+
* - `"sync"`: All methods return values directly.
|
|
92
|
+
* - `"async"`: Methods return Promises or AsyncIterables.
|
|
93
|
+
*
|
|
94
|
+
* @since 0.9.0
|
|
95
|
+
*/
|
|
96
|
+
readonly mode: M;
|
|
97
|
+
/**
|
|
98
|
+
* The priority of this parser, which determines the order in which
|
|
99
|
+
* parsers are applied when multiple parsers are available. The greater
|
|
100
|
+
* the number, the higher the priority.
|
|
101
|
+
*/
|
|
102
|
+
readonly priority: number;
|
|
103
|
+
/**
|
|
104
|
+
* The usage information for this parser, which describes how
|
|
105
|
+
* to use it in command-line interfaces.
|
|
106
|
+
*/
|
|
107
|
+
readonly usage: Usage;
|
|
108
|
+
/**
|
|
109
|
+
* Names that this parser could match at the first buffer position.
|
|
110
|
+
* Used by `runParser()` to detect collisions with built-in meta
|
|
111
|
+
* features (help, version, completion).
|
|
112
|
+
*
|
|
113
|
+
* Each built-in combinator computes this from its structural semantics.
|
|
114
|
+
* Custom parser implementations must include every fixed token that
|
|
115
|
+
* the parser accepts at `argv[0]` — command names, option names, and
|
|
116
|
+
* literal values alike. For example, a parser whose usage declares
|
|
117
|
+
* `{ type: "literal", value: "serve" }` should include `"serve"` in
|
|
118
|
+
* this set. Parsers that accept *any* token (like `argument()`) should
|
|
119
|
+
* return an empty set and set {@link acceptingAnyToken} to `true`
|
|
120
|
+
* instead.
|
|
121
|
+
*
|
|
122
|
+
* @since 1.0.0
|
|
123
|
+
*/
|
|
124
|
+
readonly leadingNames: ReadonlySet<string>;
|
|
125
|
+
/**
|
|
126
|
+
* Whether this parser unconditionally consumes any positional token at
|
|
127
|
+
* the first buffer position. A parser with this flag accepts any
|
|
128
|
+
* non-option token but may still reject option-like tokens (those
|
|
129
|
+
* starting with `"-"`).
|
|
130
|
+
*
|
|
131
|
+
* In shared-buffer compositions (`tuple()`, `object()`, `merge()`,
|
|
132
|
+
* `concat()`), a catch-all parser blocks positional names (command
|
|
133
|
+
* names) from lower-priority siblings but does not block option-like
|
|
134
|
+
* names. In `conditional()`, option-like names from the default
|
|
135
|
+
* branch remain reachable even when the discriminator is a catch-all.
|
|
136
|
+
*
|
|
137
|
+
* Only `argument()` is inherently accepting-any-token; combinators
|
|
138
|
+
* like `or()` and `map()` propagate this from their children.
|
|
139
|
+
* Wrappers that can succeed without consuming (`optional()`,
|
|
140
|
+
* `withDefault()`, `multiple()` with `min = 0`) always set this
|
|
141
|
+
* to `false`.
|
|
142
|
+
*
|
|
143
|
+
* @since 1.0.0
|
|
144
|
+
*/
|
|
145
|
+
readonly acceptingAnyToken: boolean;
|
|
146
|
+
/**
|
|
147
|
+
* The initial state for this parser. This is used to initialize the
|
|
148
|
+
* state when parsing starts.
|
|
149
|
+
*/
|
|
150
|
+
readonly initialState: TState;
|
|
151
|
+
/**
|
|
152
|
+
* Internal marker for wrappers whose `{ hasCliValue: false }` states should
|
|
153
|
+
* be treated as unmatched dependency-source states during completion-time
|
|
154
|
+
* Phase 1.
|
|
155
|
+
*
|
|
156
|
+
* @internal
|
|
157
|
+
*/
|
|
158
|
+
readonly [unmatchedNonCliDependencySourceStateMarker]?: true;
|
|
159
|
+
/**
|
|
160
|
+
* Parses the input context and returns a result indicating
|
|
161
|
+
* whether the parsing was successful or not.
|
|
162
|
+
* @param context The context of the parser, which includes the input buffer
|
|
163
|
+
* and the current state.
|
|
164
|
+
* @returns A result object indicating success or failure.
|
|
165
|
+
* In async mode, returns a Promise that resolves to the result.
|
|
166
|
+
*/
|
|
167
|
+
parse(context: ParserContext<TState>): ModeValue<M, ParserResult<TState>>;
|
|
168
|
+
/**
|
|
169
|
+
* Transforms a {@link TState} into a {@link TValue}, if applicable.
|
|
170
|
+
* If the transformation is not applicable, it should return
|
|
171
|
+
* a `ValueParserResult` with `success: false` and an appropriate error
|
|
172
|
+
* message.
|
|
173
|
+
* @param state The current state of the parser, which may contain accumulated
|
|
174
|
+
* data or context needed to produce the final value.
|
|
175
|
+
* @param exec Optional shared execution context. When provided, gives the
|
|
176
|
+
* parser access to cross-cutting runtime data such as the current
|
|
177
|
+
* execution phase and dependency registry.
|
|
178
|
+
* @returns A result object indicating success or failure of
|
|
179
|
+
* the transformation. If successful, it should contain
|
|
180
|
+
* the parsed value of type {@link TValue}. If not applicable,
|
|
181
|
+
* it should return an error message.
|
|
182
|
+
* In async mode, returns a Promise that resolves to the result.
|
|
183
|
+
* @since 1.0.0 Added optional `exec` parameter.
|
|
184
|
+
*/
|
|
185
|
+
complete(state: TState, exec?: ExecutionContext): ModeValue<M, ValueParserResult<TValue>>;
|
|
186
|
+
/**
|
|
187
|
+
* Generates next-step suggestions based on the current context
|
|
188
|
+
* and an optional prefix. This can be used to provide shell completion
|
|
189
|
+
* suggestions or to guide users in constructing valid commands.
|
|
190
|
+
* @param context The context of the parser, which includes the input buffer
|
|
191
|
+
* and the current state.
|
|
192
|
+
* @param prefix A prefix string that can be used to filter suggestions.
|
|
193
|
+
* Can be an empty string if no prefix is provided.
|
|
194
|
+
* @returns An iterable of {@link Suggestion} objects, each containing
|
|
195
|
+
* a suggestion text and an optional description.
|
|
196
|
+
* In async mode, returns an AsyncIterable.
|
|
197
|
+
* @since 0.6.0
|
|
198
|
+
*/
|
|
199
|
+
suggest(context: ParserContext<TState>, prefix: string): ModeIterable<M, Suggestion>;
|
|
200
|
+
/**
|
|
201
|
+
* Generates a documentation fragment for this parser, which can be used
|
|
202
|
+
* to describe the parser's usage, description, and default value.
|
|
203
|
+
* @param state The current state of the parser, wrapped in a DocState
|
|
204
|
+
* to indicate whether the actual state is available or not.
|
|
205
|
+
* @param defaultValue An optional default value that can be used
|
|
206
|
+
* to provide a default value in the documentation.
|
|
207
|
+
* @returns {@link DocFragments} object containing documentation
|
|
208
|
+
* fragments for this parser.
|
|
209
|
+
*/
|
|
210
|
+
getDocFragments(state: DocState<TState>, defaultValue?: TValue): DocFragments;
|
|
211
|
+
/**
|
|
212
|
+
* A type-appropriate default value used as a stand-in during deferred
|
|
213
|
+
* prompt resolution. When present, combinators like `prompt()` use this
|
|
214
|
+
* value instead of an internal sentinel during two-phase parsing, so that
|
|
215
|
+
* `map()` transforms and two-pass contexts always receive a valid value
|
|
216
|
+
* of type {@link TValue}.
|
|
217
|
+
*
|
|
218
|
+
* This property is set automatically by `option()` and `argument()` from
|
|
219
|
+
* the underlying {@link ValueParser}'s `placeholder`, and propagated by
|
|
220
|
+
* combinators like `map()`, `optional()`, and `withDefault()`.
|
|
221
|
+
*
|
|
222
|
+
* @since 1.0.0
|
|
223
|
+
*/
|
|
224
|
+
readonly placeholder?: TValue;
|
|
225
|
+
/**
|
|
226
|
+
* Optional predicate that determines whether completion should be
|
|
227
|
+
* deferred for the given parser state.
|
|
228
|
+
*
|
|
229
|
+
* When present, combinator wrappers ({@link optional}, {@link withDefault},
|
|
230
|
+
* {@link group}) forward this field to the outer parser. This enables
|
|
231
|
+
* packages like *\@optique/inquirer* to detect when interactive prompting
|
|
232
|
+
* should be deferred until an outer context (like a configuration file
|
|
233
|
+
* source) has resolved.
|
|
234
|
+
*
|
|
235
|
+
* @param state The current parser state.
|
|
236
|
+
* @param exec Optional shared execution context.
|
|
237
|
+
* @returns `true` if completion should be deferred.
|
|
238
|
+
* @since 1.0.0
|
|
239
|
+
* @since 1.0.0 Added optional `exec` parameter.
|
|
240
|
+
*/
|
|
241
|
+
shouldDeferCompletion?(state: TState, exec?: ExecutionContext): boolean;
|
|
242
|
+
/**
|
|
243
|
+
* Normalizes a parsed value according to the underlying value parser's
|
|
244
|
+
* configuration. When present, {@link withDefault} calls this method
|
|
245
|
+
* on default values so that runtime defaults match the representation
|
|
246
|
+
* that the value parser's `parse()` would produce.
|
|
247
|
+
*
|
|
248
|
+
* Primitive parsers ({@link option}, {@link argument}) implement this
|
|
249
|
+
* by delegating to {@link ValueParser.normalize}. Combinator wrappers
|
|
250
|
+
* ({@link optional}, {@link withDefault}) forward it from inner parsers.
|
|
251
|
+
*
|
|
252
|
+
* Exclusive combinators ({@link or}, `longestMatch()`) and
|
|
253
|
+
* multi-source combinators (`merge()`) intentionally do *not*
|
|
254
|
+
* implement this method because the active branch or key ownership
|
|
255
|
+
* is unknown at default time.
|
|
256
|
+
*
|
|
257
|
+
* @param value The value to normalize.
|
|
258
|
+
* @returns The normalized value.
|
|
259
|
+
* @since 1.0.0
|
|
260
|
+
*/
|
|
261
|
+
normalizeValue?(value: TValue): TValue;
|
|
262
|
+
/**
|
|
263
|
+
* Optionally re-validates a value as if it had been parsed from CLI
|
|
264
|
+
* input, surfacing any constraint violations from the underlying value
|
|
265
|
+
* parser (e.g., regex patterns, numeric bounds, `choice()` values).
|
|
266
|
+
*
|
|
267
|
+
* Wrappers like `bindEnv()` and `bindConfig()` call this on fallback
|
|
268
|
+
* values — environment variables parsed by a looser env parser,
|
|
269
|
+
* configured defaults, and values loaded from config files — so that
|
|
270
|
+
* those values obey the same validation semantics as CLI input.
|
|
271
|
+
* Without it, parser constraints can be silently bypassed through
|
|
272
|
+
* fallback paths.
|
|
273
|
+
*
|
|
274
|
+
* Built-in primitive parsers ({@link option}, {@link argument})
|
|
275
|
+
* implement this method by round-tripping the value through the inner
|
|
276
|
+
* {@link ValueParser.format} and {@link ValueParser.parse} calls: the
|
|
277
|
+
* value is serialized back to a string and re-parsed, which re-runs
|
|
278
|
+
* every constraint check. Combinator wrappers ({@link optional},
|
|
279
|
+
* {@link withDefault}) forward this method from their inner parser.
|
|
280
|
+
* {@link map} intentionally does *not* forward it because the mapping
|
|
281
|
+
* function is one-way: the mapped output type no longer corresponds
|
|
282
|
+
* to the inner parser's constraints. Exclusive combinators
|
|
283
|
+
* ({@link or}, `longestMatch()`) and multi-source combinators
|
|
284
|
+
* (`merge()`, `concat()`) intentionally do not implement this method
|
|
285
|
+
* because the active branch or key ownership is unknown at validation
|
|
286
|
+
* time.
|
|
287
|
+
*
|
|
288
|
+
* Implementations must wrap any *exception* thrown by `format()` in
|
|
289
|
+
* `try`/`catch` and return the original value as a successful
|
|
290
|
+
* {@link ValueParserResult}. This specifically protects
|
|
291
|
+
* dependency-derived parsers whose factory cannot run without the
|
|
292
|
+
* current dependency value, and custom value parsers whose `format()`
|
|
293
|
+
* intentionally throws for unsupported inputs. Values that
|
|
294
|
+
* `format()` successfully serializes to a string are always re-parsed,
|
|
295
|
+
* and any resulting parse failure is propagated — they represent the
|
|
296
|
+
* bug class this method exists to surface.
|
|
297
|
+
*
|
|
298
|
+
* @param value The candidate value to validate.
|
|
299
|
+
* @returns A {@link ValueParserResult} indicating success (with the
|
|
300
|
+
* possibly-canonicalized value) or failure (with an error
|
|
301
|
+
* message). In async mode, returns a `Promise` resolving to
|
|
302
|
+
* the result.
|
|
303
|
+
* @since 1.0.0
|
|
304
|
+
*/
|
|
305
|
+
validateValue?(value: TValue): ModeValue<M, ValueParserResult<TValue>>;
|
|
306
|
+
/**
|
|
307
|
+
* Internal dependency metadata describing this parser's dependency
|
|
308
|
+
* capabilities. Used by the dependency runtime to resolve dependencies
|
|
309
|
+
* without relying on state-shape protocols.
|
|
310
|
+
* @internal
|
|
311
|
+
*/
|
|
312
|
+
readonly dependencyMetadata?: ParserDependencyMetadata;
|
|
313
|
+
/**
|
|
314
|
+
* Internal hook for top-level suggest-time dependency seeding.
|
|
315
|
+
*
|
|
316
|
+
* Wrapper parsers can expose the active parser/state pairs that should be
|
|
317
|
+
* scanned when `suggestSync()` or `suggestAsync()` builds a fresh dependency
|
|
318
|
+
* runtime. When omitted, only this parser's own dependency source metadata
|
|
319
|
+
* is considered.
|
|
320
|
+
*
|
|
321
|
+
* @param state The current parser state.
|
|
322
|
+
* @param path The path to this parser within the parse tree.
|
|
323
|
+
* @returns Runtime nodes to seed into the suggestion-time dependency runtime.
|
|
324
|
+
* @internal
|
|
325
|
+
*/
|
|
326
|
+
getSuggestRuntimeNodes?(state: TState, path: readonly PropertyKey[]): readonly RuntimeNode[];
|
|
327
|
+
}
|
|
328
|
+
/**
|
|
329
|
+
* Parser-local frame data containing the input buffer and parser state.
|
|
330
|
+
* This represents the per-parser progress during parsing, separated from
|
|
331
|
+
* cross-cutting execution context.
|
|
332
|
+
* @template TState The type of the state used during parsing.
|
|
333
|
+
* @since 1.0.0
|
|
334
|
+
*/
|
|
335
|
+
interface ParseFrame<TState> {
|
|
336
|
+
/**
|
|
337
|
+
* The array of input strings that the parser is currently processing.
|
|
338
|
+
*/
|
|
339
|
+
readonly buffer: readonly string[];
|
|
340
|
+
/**
|
|
341
|
+
* The current state of the parser, which is used to track
|
|
342
|
+
* the progress of parsing and any accumulated data.
|
|
343
|
+
*/
|
|
344
|
+
readonly state: TState;
|
|
345
|
+
/**
|
|
346
|
+
* A flag indicating whether no more options should be parsed and instead
|
|
347
|
+
* the remaining input should be treated as positional arguments.
|
|
348
|
+
*/
|
|
349
|
+
readonly optionsTerminated: boolean;
|
|
350
|
+
}
|
|
351
|
+
/**
|
|
352
|
+
* The phase of the execution pipeline.
|
|
353
|
+
* @since 1.0.0
|
|
354
|
+
*/
|
|
355
|
+
type ExecutionPhase = "parse" | "precomplete" | "resolve" | "complete" | "suggest";
|
|
356
|
+
/**
|
|
357
|
+
* Shared execution context carrying cross-cutting runtime data.
|
|
358
|
+
* This includes information that is shared across all parsers in a parse
|
|
359
|
+
* tree, such as usage information, dependency registries, and the current
|
|
360
|
+
* execution phase.
|
|
361
|
+
* @since 1.0.0
|
|
362
|
+
*/
|
|
363
|
+
interface ExecutionContext {
|
|
364
|
+
/**
|
|
365
|
+
* Usage information for the entire parser tree.
|
|
366
|
+
*/
|
|
367
|
+
readonly usage: Usage;
|
|
368
|
+
/**
|
|
369
|
+
* The current phase of the execution pipeline.
|
|
370
|
+
*/
|
|
371
|
+
readonly phase: ExecutionPhase;
|
|
372
|
+
/**
|
|
373
|
+
* The path from the root to the current parser in the parse tree.
|
|
374
|
+
* Used by constructs to track the current position during dependency
|
|
375
|
+
* resolution and completion.
|
|
376
|
+
*/
|
|
377
|
+
readonly path: readonly PropertyKey[];
|
|
378
|
+
/**
|
|
379
|
+
* Matched command names in parse order.
|
|
380
|
+
*
|
|
381
|
+
* This is tracked separately from {@link path} because parse-tree paths also
|
|
382
|
+
* include object fields and other wrapper segments. Runners use it to
|
|
383
|
+
* recover subcommand help context from partial parses.
|
|
384
|
+
*
|
|
385
|
+
* @internal
|
|
386
|
+
*/
|
|
387
|
+
readonly commandPath?: readonly string[];
|
|
388
|
+
/**
|
|
389
|
+
* Immutable trace of raw primitive inputs recorded during parsing.
|
|
390
|
+
*
|
|
391
|
+
* Primitives append trace entries keyed by {@link path}, allowing later
|
|
392
|
+
* completion phases to replay derived parsers with the resolved
|
|
393
|
+
* dependency values.
|
|
394
|
+
*
|
|
395
|
+
* @internal
|
|
396
|
+
*/
|
|
397
|
+
readonly trace?: InputTrace;
|
|
398
|
+
/**
|
|
399
|
+
* A registry containing resolved dependency values from DependencySource
|
|
400
|
+
* parsers.
|
|
401
|
+
* @since 0.10.0
|
|
402
|
+
*/
|
|
403
|
+
readonly dependencyRegistry?: DependencyRegistryLike;
|
|
404
|
+
/**
|
|
405
|
+
* The dependency runtime context for dependency resolution.
|
|
406
|
+
* Coexists with `dependencyRegistry` during the transition period.
|
|
407
|
+
* @internal
|
|
408
|
+
*/
|
|
409
|
+
readonly dependencyRuntime?: DependencyRuntimeContext;
|
|
410
|
+
/**
|
|
411
|
+
* Immutable map of pre-completed results from the parent construct's
|
|
412
|
+
* Phase 1, keyed by field name. Each construct passes its own
|
|
413
|
+
* `preCompleteAndRegisterDependencies` results directly to children
|
|
414
|
+
* in Phase 3. Children read it in their own Phase 1 to avoid
|
|
415
|
+
* re-evaluating non-idempotent default thunks, but never write to
|
|
416
|
+
* it — this prevents sibling completions from leaking into each
|
|
417
|
+
* other.
|
|
418
|
+
*
|
|
419
|
+
* Field-name keying naturally handles parser reuse across different
|
|
420
|
+
* fields (e.g., `merge(object({a: shared}), object({b: shared}))`)
|
|
421
|
+
* because each field maps to its own result regardless of whether
|
|
422
|
+
* the underlying parser instance is the same.
|
|
423
|
+
*
|
|
424
|
+
* @see https://github.com/dahlia/optique/issues/762
|
|
425
|
+
* @internal
|
|
426
|
+
*/
|
|
427
|
+
readonly preCompletedByParser?: ReadonlyMap<string | symbol, unknown>;
|
|
428
|
+
/**
|
|
429
|
+
* Field names that should be ignored when a construct seeds dependency
|
|
430
|
+
* sources from child state during completion.
|
|
431
|
+
*
|
|
432
|
+
* Used by outer `merge()` completions to suppress ambiguous duplicate
|
|
433
|
+
* keys while still allowing the child parser to finish its own value
|
|
434
|
+
* completion.
|
|
435
|
+
*
|
|
436
|
+
* @internal
|
|
437
|
+
*/
|
|
438
|
+
readonly excludedSourceFields?: ReadonlySet<string | symbol>;
|
|
439
|
+
}
|
|
440
|
+
/**
|
|
441
|
+
* Internal marker for wrappers whose `{ hasCliValue: false }` states should
|
|
442
|
+
* be treated as unmatched dependency-source states during completion-time
|
|
443
|
+
* Phase 1.
|
|
444
|
+
*
|
|
445
|
+
* Wrappers like `bindEnv()` and `bindConfig()` opt in because their missing
|
|
446
|
+
* CLI states still carry enough fallback context to pre-complete exactly
|
|
447
|
+
* once. Wrappers like `prompt()` intentionally do not opt in because
|
|
448
|
+
* prompted values are not yet registered as dependency sources.
|
|
449
|
+
*
|
|
450
|
+
* @internal
|
|
451
|
+
*/
|
|
452
|
+
declare const unmatchedNonCliDependencySourceStateMarker: unique symbol;
|
|
453
|
+
/**
|
|
454
|
+
* Internal marker for parsers that want parent-state annotations injected
|
|
455
|
+
* directly into rebuilt child states instead of relying on structural
|
|
456
|
+
* inheritance.
|
|
457
|
+
*
|
|
458
|
+
* Wrappers like `bindConfig()`, `bindEnv()`, and `prompt()` opt in because
|
|
459
|
+
* their fallback contracts depend on carrying annotations through wrapper
|
|
460
|
+
* state objects during parse, complete, and suggest.
|
|
461
|
+
*
|
|
462
|
+
* @internal
|
|
463
|
+
*/
|
|
464
|
+
|
|
465
|
+
/**
|
|
466
|
+
* The context of the parser, which includes the input buffer and the state.
|
|
467
|
+
*
|
|
468
|
+
* `ParserContext` provides structured access to shared execution context
|
|
469
|
+
* via {@link exec}, and flat access to all fields for backward
|
|
470
|
+
* compatibility.
|
|
471
|
+
*
|
|
472
|
+
* @template TState The type of the state used during parsing.
|
|
473
|
+
*/
|
|
474
|
+
interface ParserContext<TState> {
|
|
475
|
+
/**
|
|
476
|
+
* Shared execution context (usage, phase, path, dependencyRegistry).
|
|
477
|
+
*
|
|
478
|
+
* Present when the context was created via {@link createParserContext}.
|
|
479
|
+
* Later runtime work will make this field required.
|
|
480
|
+
*
|
|
481
|
+
* @since 1.0.0
|
|
482
|
+
*/
|
|
483
|
+
readonly exec?: ExecutionContext;
|
|
484
|
+
/**
|
|
485
|
+
* Immutable trace of raw primitive inputs recorded during parsing.
|
|
486
|
+
*
|
|
487
|
+
* Preserved as a flat compatibility field so wrapper parsers can forward
|
|
488
|
+
* trace data even when they rebuild the parser context without {@link exec}.
|
|
489
|
+
*
|
|
490
|
+
* @since 1.0.0
|
|
491
|
+
*/
|
|
492
|
+
readonly trace?: InputTrace;
|
|
493
|
+
/**
|
|
494
|
+
* The array of input strings that the parser is currently processing.
|
|
495
|
+
*/
|
|
496
|
+
readonly buffer: readonly string[];
|
|
497
|
+
/**
|
|
498
|
+
* The current state of the parser, which is used to track
|
|
499
|
+
* the progress of parsing and any accumulated data.
|
|
500
|
+
*/
|
|
501
|
+
readonly state: TState;
|
|
502
|
+
/**
|
|
503
|
+
* A flag indicating whether no more options should be parsed and instead
|
|
504
|
+
* the remaining input should be treated as positional arguments.
|
|
505
|
+
* This is typically set when the parser encounters a `--` in the input,
|
|
506
|
+
* which is a common convention in command-line interfaces to indicate
|
|
507
|
+
* that no further options should be processed.
|
|
508
|
+
*/
|
|
509
|
+
readonly optionsTerminated: boolean;
|
|
510
|
+
/**
|
|
511
|
+
* Usage information for the entire parser tree.
|
|
512
|
+
* Used to provide better error messages with suggestions for typos.
|
|
513
|
+
* When a parser encounters an invalid option or command, it can use
|
|
514
|
+
* this information to suggest similar valid options.
|
|
515
|
+
* @since 0.7.0
|
|
516
|
+
*/
|
|
517
|
+
readonly usage: Usage;
|
|
518
|
+
/**
|
|
519
|
+
* A registry containing resolved dependency values from DependencySource parsers.
|
|
520
|
+
* This is used during shell completion to provide suggestions based on
|
|
521
|
+
* the actual dependency values that have been parsed, rather than defaults.
|
|
522
|
+
* @since 0.10.0
|
|
523
|
+
*/
|
|
524
|
+
readonly dependencyRegistry?: DependencyRegistryLike;
|
|
525
|
+
}
|
|
526
|
+
/**
|
|
527
|
+
* Creates a {@link ParserContext} from a {@link ParseFrame} and an
|
|
528
|
+
* {@link ExecutionContext}. The returned object provides both structured
|
|
529
|
+
* access (`frame`, `exec`) and flat access (`buffer`, `state`, etc.)
|
|
530
|
+
* for backward compatibility.
|
|
531
|
+
*
|
|
532
|
+
* @template TState The type of the state used during parsing.
|
|
533
|
+
* @param frame Parser-local frame data.
|
|
534
|
+
* @param exec Shared execution context.
|
|
535
|
+
* @returns A {@link ParserContext} instance.
|
|
536
|
+
* @since 1.0.0
|
|
537
|
+
*/
|
|
538
|
+
declare function createParserContext<TState>(frame: ParseFrame<TState>, exec: ExecutionContext): ParserContext<TState>;
|
|
539
|
+
/**
|
|
540
|
+
* Represents a suggestion for command-line completion or guidance.
|
|
541
|
+
* @since 0.6.0
|
|
542
|
+
*/
|
|
543
|
+
type Suggestion = {
|
|
544
|
+
/**
|
|
545
|
+
* A literal text suggestion.
|
|
546
|
+
*/
|
|
547
|
+
readonly kind: "literal";
|
|
548
|
+
/**
|
|
549
|
+
* The suggestion text that can be used for completion or guidance.
|
|
550
|
+
*/
|
|
551
|
+
readonly text: string;
|
|
552
|
+
/**
|
|
553
|
+
* An optional description providing additional context
|
|
554
|
+
* or information about the suggestion.
|
|
555
|
+
*/
|
|
556
|
+
readonly description?: Message;
|
|
557
|
+
} | {
|
|
558
|
+
/**
|
|
559
|
+
* A file system completion suggestion that uses native shell completion.
|
|
560
|
+
*/
|
|
561
|
+
readonly kind: "file";
|
|
562
|
+
/**
|
|
563
|
+
* The current prefix/pattern for fallback when native completion is unavailable.
|
|
564
|
+
*/
|
|
565
|
+
readonly pattern?: string;
|
|
566
|
+
/**
|
|
567
|
+
* The type of file system entries to complete.
|
|
568
|
+
*/
|
|
569
|
+
readonly type: "file" | "directory" | "any";
|
|
570
|
+
/**
|
|
571
|
+
* File extensions to filter by (e.g., [".ts", ".js"]).
|
|
572
|
+
*/
|
|
573
|
+
readonly extensions?: readonly string[];
|
|
574
|
+
/**
|
|
575
|
+
* Whether to include hidden files (those starting with a dot).
|
|
576
|
+
*/
|
|
577
|
+
readonly includeHidden?: boolean;
|
|
578
|
+
/**
|
|
579
|
+
* An optional description providing additional context
|
|
580
|
+
* or information about the suggestion.
|
|
581
|
+
*/
|
|
582
|
+
readonly description?: Message;
|
|
583
|
+
};
|
|
584
|
+
/**
|
|
585
|
+
* A discriminated union type representing the result of a parser operation.
|
|
586
|
+
* It can either indicate a successful parse with the next state and context,
|
|
587
|
+
* or a failure with an error message.
|
|
588
|
+
* @template TState The type of the state after parsing. It should match with
|
|
589
|
+
* the `TState` type of the {@link Parser} interface.
|
|
590
|
+
*/
|
|
591
|
+
type ParserResult<TState> = {
|
|
592
|
+
/**
|
|
593
|
+
* Indicates that the parsing operation was successful.
|
|
594
|
+
*/
|
|
595
|
+
readonly success: true;
|
|
596
|
+
/**
|
|
597
|
+
* The next context after parsing, which includes the updated input buffer.
|
|
598
|
+
*/
|
|
599
|
+
readonly next: ParserContext<TState>;
|
|
600
|
+
/**
|
|
601
|
+
* The input elements consumed by the parser during this operation.
|
|
602
|
+
*/
|
|
603
|
+
readonly consumed: readonly string[];
|
|
604
|
+
/**
|
|
605
|
+
* When `true`, indicates that this success is tentative or
|
|
606
|
+
* speculative: the parser matched something but the match has not
|
|
607
|
+
* been confirmed yet. This covers two cases:
|
|
608
|
+
*
|
|
609
|
+
* - A zero-consuming discriminator resolved to a branch key, but
|
|
610
|
+
* the selected sub-parser has not consumed any input yet.
|
|
611
|
+
* - A {@link conditional} parser speculatively committed to a
|
|
612
|
+
* named branch that consumed tokens, before the discriminator
|
|
613
|
+
* has had a chance to confirm the choice. In this case the
|
|
614
|
+
* marker stays set across subsequent parse calls until
|
|
615
|
+
* `complete()` verifies the speculative selection.
|
|
616
|
+
*
|
|
617
|
+
* Outer combinators should not treat provisional successes as
|
|
618
|
+
* definitive. For example, {@link or} should still allow a
|
|
619
|
+
* definitive branch to take priority, and a definitive
|
|
620
|
+
* zero-consuming fallback must not be displaced by a provisional
|
|
621
|
+
* consuming hit. {@link longestMatch} may still prefer a longer
|
|
622
|
+
* provisional match over a shorter definitive one; when match
|
|
623
|
+
* lengths are equal, definitive results take priority over
|
|
624
|
+
* provisional ones.
|
|
625
|
+
*
|
|
626
|
+
* @since 1.0.0
|
|
627
|
+
*/
|
|
628
|
+
readonly provisional?: true;
|
|
629
|
+
} | {
|
|
630
|
+
/**
|
|
631
|
+
* Indicates that the parsing operation failed.
|
|
632
|
+
*/
|
|
633
|
+
readonly success: false;
|
|
634
|
+
/**
|
|
635
|
+
* The number of the consumed input elements.
|
|
636
|
+
*/
|
|
637
|
+
readonly consumed: number;
|
|
638
|
+
/**
|
|
639
|
+
* The error message describing why the parsing failed.
|
|
640
|
+
*/
|
|
641
|
+
readonly error: Message;
|
|
642
|
+
};
|
|
643
|
+
/**
|
|
644
|
+
* Infers the result value type of a {@link Parser}.
|
|
645
|
+
* @template T The {@link Parser} to infer the result value type from.
|
|
646
|
+
*/
|
|
647
|
+
type InferValue<T extends Parser<Mode, unknown, unknown>> = T["$valueType"][number];
|
|
648
|
+
/**
|
|
649
|
+
* Infers the execution mode of a {@link Parser}.
|
|
650
|
+
* @template T The {@link Parser} to infer the execution mode from.
|
|
651
|
+
* @since 0.9.0
|
|
652
|
+
*/
|
|
653
|
+
type InferMode<T extends Parser<Mode, unknown, unknown>> = T["mode"];
|
|
654
|
+
/**
|
|
655
|
+
* The result type of a whole parser operation, which can either be a successful
|
|
656
|
+
* result with a value of type `T`, or a failure with an error message.
|
|
657
|
+
* @template T The type of the value produced by the parser.
|
|
658
|
+
*/
|
|
659
|
+
type Result<T> = {
|
|
660
|
+
/**
|
|
661
|
+
* Indicates that the parsing operation was successful.
|
|
662
|
+
*/
|
|
663
|
+
readonly success: true;
|
|
664
|
+
/**
|
|
665
|
+
* The successfully parsed value of type {@link T}.
|
|
666
|
+
* This is the final result of the parsing operation after all parsers
|
|
667
|
+
* have been applied and completed.
|
|
668
|
+
*/
|
|
669
|
+
readonly value: T;
|
|
670
|
+
/**
|
|
671
|
+
* When `true`, indicates that the value contains deferred prompt
|
|
672
|
+
* placeholders. Propagated from {@link ValueParserResult.deferred}.
|
|
673
|
+
* @since 1.0.0
|
|
674
|
+
*/
|
|
675
|
+
readonly deferred?: true;
|
|
676
|
+
/**
|
|
677
|
+
* Property keys (object field names or array indices) whose values are
|
|
678
|
+
* deferred placeholders.
|
|
679
|
+
* Propagated from {@link ValueParserResult.deferredKeys}.
|
|
680
|
+
* @since 1.0.0
|
|
681
|
+
*/
|
|
682
|
+
readonly deferredKeys?: DeferredMap;
|
|
683
|
+
} | {
|
|
684
|
+
/**
|
|
685
|
+
* Indicates that the parsing operation failed.
|
|
686
|
+
*/
|
|
687
|
+
readonly success: false;
|
|
688
|
+
/**
|
|
689
|
+
* The error message describing why the parsing failed.
|
|
690
|
+
*/
|
|
691
|
+
readonly error: Message;
|
|
692
|
+
};
|
|
693
|
+
/**
|
|
694
|
+
* Parses an array of command-line arguments using the provided combined parser.
|
|
695
|
+
* This function processes the input arguments, applying the parser to each
|
|
696
|
+
* argument until all arguments are consumed or an error occurs.
|
|
697
|
+
*
|
|
698
|
+
* This function only accepts synchronous parsers. For asynchronous parsers,
|
|
699
|
+
* use {@link parseAsync}.
|
|
700
|
+
*
|
|
701
|
+
* @template T The type of the value produced by the parser.
|
|
702
|
+
* @param parser The combined {@link Parser} to use for parsing the input
|
|
703
|
+
* arguments. Must be a synchronous parser.
|
|
704
|
+
* @param args The array of command-line arguments to parse. Usually this is
|
|
705
|
+
* `process.argv.slice(2)` in Node.js or `Deno.args` in Deno.
|
|
706
|
+
* @param options Optional {@link ParseOptions} for customizing parsing behavior.
|
|
707
|
+
* @returns A {@link Result} object indicating whether the parsing was
|
|
708
|
+
* successful or not. If successful, it contains the parsed value of
|
|
709
|
+
* type `T`. If not, it contains an error message describing the
|
|
710
|
+
* failure.
|
|
711
|
+
* @throws {TypeError} When a synchronous dependency source extractor returns a
|
|
712
|
+
* thenable during completion-time dependency seeding.
|
|
713
|
+
* @since 0.9.0 Renamed from the original `parse` function which now delegates
|
|
714
|
+
* to this for sync parsers.
|
|
715
|
+
* @since 0.10.0 Added optional `options` parameter for annotations support.
|
|
716
|
+
*/
|
|
717
|
+
declare function parseSync<T>(parser: Parser<"sync", T, unknown>, args: readonly string[], options?: ParseOptions): Result<T>;
|
|
718
|
+
/**
|
|
719
|
+
* Parses an array of command-line arguments using the provided combined parser.
|
|
720
|
+
* This function processes the input arguments, applying the parser to each
|
|
721
|
+
* argument until all arguments are consumed or an error occurs.
|
|
722
|
+
*
|
|
723
|
+
* This function accepts any parser (sync or async) and always returns a Promise.
|
|
724
|
+
* For synchronous parsing with sync parsers, use {@link parseSync} instead.
|
|
725
|
+
*
|
|
726
|
+
* @template T The type of the value produced by the parser.
|
|
727
|
+
* @param parser The combined {@link Parser} to use for parsing the input
|
|
728
|
+
* arguments.
|
|
729
|
+
* @param args The array of command-line arguments to parse. Usually this is
|
|
730
|
+
* `process.argv.slice(2)` in Node.js or `Deno.args` in Deno.
|
|
731
|
+
* @param options Optional {@link ParseOptions} for customizing parsing behavior.
|
|
732
|
+
* @returns A Promise that resolves to a {@link Result} object indicating
|
|
733
|
+
* whether the parsing was successful or not.
|
|
734
|
+
* @since 0.9.0
|
|
735
|
+
* @since 0.10.0 Added optional `options` parameter for annotations support.
|
|
736
|
+
*/
|
|
737
|
+
declare function parseAsync<T>(parser: Parser<Mode, T, unknown>, args: readonly string[], options?: ParseOptions): Promise<Result<T>>;
|
|
738
|
+
/**
|
|
739
|
+
* Parses an array of command-line arguments using the provided combined parser.
|
|
740
|
+
* This function processes the input arguments, applying the parser to each
|
|
741
|
+
* argument until all arguments are consumed or an error occurs.
|
|
742
|
+
*
|
|
743
|
+
* The return type depends on the parser's mode:
|
|
744
|
+
* - Sync parsers return `Result<T>` directly.
|
|
745
|
+
* - Async parsers return `Promise<Result<T>>`.
|
|
746
|
+
*
|
|
747
|
+
* For explicit control, use {@link parseSync} or {@link parseAsync}.
|
|
748
|
+
*
|
|
749
|
+
* @template M The execution mode of the parser.
|
|
750
|
+
* @template T The type of the value produced by the parser.
|
|
751
|
+
* @param parser The combined {@link Parser} to use for parsing the input
|
|
752
|
+
* arguments.
|
|
753
|
+
* @param args The array of command-line arguments to parse. Usually this is
|
|
754
|
+
* `process.argv.slice(2)` in Node.js or `Deno.args` in Deno.
|
|
755
|
+
* @param options Optional {@link ParseOptions} for customizing parsing behavior.
|
|
756
|
+
* @returns A {@link Result} object (for sync) or Promise thereof (for async)
|
|
757
|
+
* indicating whether the parsing was successful or not.
|
|
758
|
+
* @throws {TypeError} When a synchronous dependency source extractor returns a
|
|
759
|
+
* thenable during completion-time dependency seeding.
|
|
760
|
+
* @since 0.10.0 Added optional `options` parameter for annotations support.
|
|
761
|
+
*/
|
|
762
|
+
declare function parse<M extends Mode, T>(parser: Parser<M, T, unknown>, args: readonly string[], options?: ParseOptions): ModeValue<M, Result<T>>;
|
|
763
|
+
/**
|
|
764
|
+
* Generates command-line suggestions based on current parsing state.
|
|
765
|
+
* This function processes the input arguments up to the last argument,
|
|
766
|
+
* then calls the parser's suggest method with the remaining prefix.
|
|
767
|
+
*
|
|
768
|
+
* This function only accepts synchronous parsers. For asynchronous parsers,
|
|
769
|
+
* use {@link suggestAsync}.
|
|
770
|
+
*
|
|
771
|
+
* @template T The type of the value produced by the parser.
|
|
772
|
+
* @param parser The {@link Parser} to use for generating suggestions.
|
|
773
|
+
* Must be a synchronous parser.
|
|
774
|
+
* @param args The array of command-line arguments including the partial
|
|
775
|
+
* argument to complete. The last element is treated as
|
|
776
|
+
* the prefix for suggestions.
|
|
777
|
+
* @param options Optional {@link ParseOptions} for customizing parsing behavior.
|
|
778
|
+
* @returns An array of {@link Suggestion} objects containing completion
|
|
779
|
+
* candidates.
|
|
780
|
+
* @example
|
|
781
|
+
* ```typescript
|
|
782
|
+
* const parser = object({
|
|
783
|
+
* verbose: option("-v", "--verbose"),
|
|
784
|
+
* format: option("-f", "--format", choice(["json", "yaml"]))
|
|
785
|
+
* });
|
|
786
|
+
*
|
|
787
|
+
* // Get suggestions for options starting with "--"
|
|
788
|
+
* const suggestions = suggestSync(parser, ["--"]);
|
|
789
|
+
* // Returns: [{ text: "--verbose" }, { text: "--format" }]
|
|
790
|
+
*
|
|
791
|
+
* // Get suggestions after parsing some arguments
|
|
792
|
+
* const suggestions2 = suggestSync(parser, ["-v", "--format="]);
|
|
793
|
+
* // Returns: [{ text: "--format=json" }, { text: "--format=yaml" }]
|
|
794
|
+
* ```
|
|
795
|
+
* @throws {TypeError} When a synchronous dependency source extractor returns a
|
|
796
|
+
* thenable during suggestion seeding.
|
|
797
|
+
* @since 0.6.0
|
|
798
|
+
* @since 0.9.0 Renamed from the original `suggest` function.
|
|
799
|
+
* @since 0.10.0 Added optional `options` parameter for annotations support.
|
|
800
|
+
*/
|
|
801
|
+
declare function suggestSync<T>(parser: Parser<"sync", T, unknown>, args: readonly [string, ...readonly string[]], options?: ParseOptions): readonly Suggestion[];
|
|
802
|
+
/**
|
|
803
|
+
* Returns suggest-time runtime nodes for a parser, falling back to the
|
|
804
|
+
* parser's own source metadata when it does not expose a custom hook.
|
|
805
|
+
*
|
|
806
|
+
* @param parser The parser whose suggest-time runtime nodes should be resolved.
|
|
807
|
+
* @param state The current parser state.
|
|
808
|
+
* @param path The path to this parser within the parse tree.
|
|
809
|
+
* @returns The runtime nodes used to seed suggest-time dependency resolution.
|
|
810
|
+
* @internal
|
|
811
|
+
*/
|
|
812
|
+
|
|
813
|
+
/**
|
|
814
|
+
* Generates command-line suggestions based on current parsing state.
|
|
815
|
+
* This function processes the input arguments up to the last argument,
|
|
816
|
+
* then calls the parser's suggest method with the remaining prefix.
|
|
817
|
+
*
|
|
818
|
+
* This function accepts any parser (sync or async) and always returns a Promise.
|
|
819
|
+
* For synchronous suggestion generation with sync parsers, use
|
|
820
|
+
* {@link suggestSync} instead.
|
|
821
|
+
*
|
|
822
|
+
* @template T The type of the value produced by the parser.
|
|
823
|
+
* @param parser The {@link Parser} to use for generating suggestions.
|
|
824
|
+
* @param args The array of command-line arguments including the partial
|
|
825
|
+
* argument to complete. The last element is treated as
|
|
826
|
+
* the prefix for suggestions.
|
|
827
|
+
* @param options Optional {@link ParseOptions} for customizing parsing behavior.
|
|
828
|
+
* @returns A Promise that resolves to an array of {@link Suggestion} objects
|
|
829
|
+
* containing completion candidates.
|
|
830
|
+
* @since 0.9.0
|
|
831
|
+
* @since 0.10.0 Added optional `options` parameter for annotations support.
|
|
832
|
+
*/
|
|
833
|
+
declare function suggestAsync<T>(parser: Parser<Mode, T, unknown>, args: readonly [string, ...readonly string[]], options?: ParseOptions): Promise<readonly Suggestion[]>;
|
|
834
|
+
/**
|
|
835
|
+
* Generates command-line suggestions based on current parsing state.
|
|
836
|
+
* This function processes the input arguments up to the last argument,
|
|
837
|
+
* then calls the parser's suggest method with the remaining prefix.
|
|
838
|
+
*
|
|
839
|
+
* The return type depends on the parser's mode:
|
|
840
|
+
* - Sync parsers return `readonly Suggestion[]` directly.
|
|
841
|
+
* - Async parsers return `Promise<readonly Suggestion[]>`.
|
|
842
|
+
*
|
|
843
|
+
* For explicit control, use {@link suggestSync} or {@link suggestAsync}.
|
|
844
|
+
*
|
|
845
|
+
* @template M The execution mode of the parser.
|
|
846
|
+
* @template T The type of the value produced by the parser.
|
|
847
|
+
* @param parser The {@link Parser} to use for generating suggestions.
|
|
848
|
+
* @param args The array of command-line arguments including the partial
|
|
849
|
+
* argument to complete. The last element is treated as
|
|
850
|
+
* the prefix for suggestions.
|
|
851
|
+
* @param options Optional {@link ParseOptions} for customizing parsing behavior.
|
|
852
|
+
* @returns An array of {@link Suggestion} objects (for sync) or Promise thereof
|
|
853
|
+
* (for async) containing completion candidates.
|
|
854
|
+
* @throws {TypeError} When a synchronous dependency source extractor returns a
|
|
855
|
+
* thenable during suggestion seeding.
|
|
856
|
+
* @since 0.6.0
|
|
857
|
+
* @since 0.10.0 Added optional `options` parameter for annotations support.
|
|
858
|
+
*/
|
|
859
|
+
declare function suggest<M extends Mode, T>(parser: Parser<M, T, unknown>, args: readonly [string, ...readonly string[]], options?: ParseOptions): ModeValue<M, readonly Suggestion[]>;
|
|
860
|
+
/**
|
|
861
|
+
* Generates a documentation page for a synchronous parser.
|
|
862
|
+
*
|
|
863
|
+
* This is the sync-specific version of {@link getDocPage}. It only accepts
|
|
864
|
+
* sync parsers and returns the documentation page directly (not wrapped
|
|
865
|
+
* in a Promise).
|
|
866
|
+
*
|
|
867
|
+
* @param parser The sync parser to generate documentation for.
|
|
868
|
+
* @param argsOrOptions Optional array of command-line arguments for context,
|
|
869
|
+
* or a {@link ParseOptions} object for annotations. When a
|
|
870
|
+
* `ParseOptions` is passed here, the `options` parameter is ignored.
|
|
871
|
+
* @param options Optional {@link ParseOptions} for customizing parsing
|
|
872
|
+
* behavior. Only used when `argsOrOptions` is an array or omitted.
|
|
873
|
+
* @returns A {@link DocPage} or `undefined`.
|
|
874
|
+
* @since 0.9.0
|
|
875
|
+
* @since 0.10.0 Added optional `options` parameter for annotations support.
|
|
876
|
+
* @since 1.0.0 The second parameter now also accepts a `ParseOptions` object
|
|
877
|
+
* directly.
|
|
878
|
+
*/
|
|
879
|
+
declare function getDocPageSync(parser: Parser<"sync", unknown, unknown>, argsOrOptions?: readonly string[] | ParseOptions, options?: ParseOptions): DocPage | undefined;
|
|
880
|
+
/**
|
|
881
|
+
* Generates a documentation page for any parser, returning a Promise.
|
|
882
|
+
*
|
|
883
|
+
* This function accepts parsers of any mode (sync or async) and always
|
|
884
|
+
* returns a Promise. Use this when working with parsers that may contain
|
|
885
|
+
* async value parsers.
|
|
886
|
+
*
|
|
887
|
+
* @param parser The parser to generate documentation for.
|
|
888
|
+
* @param argsOrOptions Optional array of command-line arguments for context,
|
|
889
|
+
* or a {@link ParseOptions} object for annotations. When a
|
|
890
|
+
* `ParseOptions` is passed here, the `options` parameter is ignored.
|
|
891
|
+
* @param options Optional {@link ParseOptions} for customizing parsing
|
|
892
|
+
* behavior. Only used when `argsOrOptions` is an array or omitted.
|
|
893
|
+
* @returns A Promise of {@link DocPage} or `undefined`.
|
|
894
|
+
* @since 0.9.0
|
|
895
|
+
* @since 0.10.0 Added optional `options` parameter for annotations support.
|
|
896
|
+
* @since 1.0.0 The second parameter now also accepts a `ParseOptions` object
|
|
897
|
+
* directly.
|
|
898
|
+
*/
|
|
899
|
+
declare function getDocPageAsync(parser: Parser<Mode, unknown, unknown>, argsOrOptions?: readonly string[] | ParseOptions, options?: ParseOptions): Promise<DocPage | undefined>;
|
|
900
|
+
/**
|
|
901
|
+
* Generates a documentation page for a parser based on its current state after
|
|
902
|
+
* attempting to parse the provided arguments. This function is useful for
|
|
903
|
+
* creating help documentation that reflects the current parsing context.
|
|
904
|
+
*
|
|
905
|
+
* The function works by:
|
|
906
|
+
* 1. Attempting to parse the provided arguments to determine the current state
|
|
907
|
+
* 2. Generating documentation fragments from the parser's current state
|
|
908
|
+
* 3. Organizing fragments into entries and sections
|
|
909
|
+
* 4. Resolving command usage terms based on parsed arguments
|
|
910
|
+
*
|
|
911
|
+
* For sync parsers, returns the documentation page directly.
|
|
912
|
+
* For async parsers, returns a Promise of the documentation page.
|
|
913
|
+
*
|
|
914
|
+
* @param parser The parser to generate documentation for
|
|
915
|
+
* @param argsOrOptions Optional array of command-line arguments that have been
|
|
916
|
+
* parsed so far, or a {@link ParseOptions} object for annotations.
|
|
917
|
+
* When a `ParseOptions` is passed here, the `options` parameter is
|
|
918
|
+
* ignored. Defaults to an empty array when omitted.
|
|
919
|
+
* @param options Optional {@link ParseOptions} for customizing parsing
|
|
920
|
+
* behavior. Only used when `argsOrOptions` is an array or omitted.
|
|
921
|
+
* @returns For sync parsers, returns a {@link DocPage} directly.
|
|
922
|
+
* For async parsers, returns a Promise of {@link DocPage}.
|
|
923
|
+
* Returns `undefined` if no documentation can be generated.
|
|
924
|
+
*
|
|
925
|
+
* @example
|
|
926
|
+
* ```typescript
|
|
927
|
+
* const parser = object({
|
|
928
|
+
* verbose: option("-v", "--verbose"),
|
|
929
|
+
* port: option("-p", "--port", integer())
|
|
930
|
+
* });
|
|
931
|
+
*
|
|
932
|
+
* // Get documentation for sync parser
|
|
933
|
+
* const rootDoc = getDocPage(parser);
|
|
934
|
+
*
|
|
935
|
+
* // Get documentation for async parser
|
|
936
|
+
* const asyncDoc = await getDocPage(asyncParser);
|
|
937
|
+
* ```
|
|
938
|
+
* @since 0.9.0 Updated to support async parsers.
|
|
939
|
+
* @since 0.10.0 Added optional `options` parameter for annotations support.
|
|
940
|
+
* @since 1.0.0 The second parameter now also accepts a `ParseOptions` object
|
|
941
|
+
* directly.
|
|
942
|
+
*/
|
|
943
|
+
declare function getDocPage(parser: Parser<"sync", unknown, unknown>, argsOrOptions?: readonly string[] | ParseOptions, options?: ParseOptions): DocPage | undefined;
|
|
944
|
+
declare function getDocPage(parser: Parser<"async", unknown, unknown>, argsOrOptions?: readonly string[] | ParseOptions, options?: ParseOptions): Promise<DocPage | undefined>;
|
|
945
|
+
declare function getDocPage<M extends Mode>(parser: Parser<M, unknown, unknown>, argsOrOptions?: readonly string[] | ParseOptions, options?: ParseOptions): ModeValue<M, DocPage | undefined>;
|
|
946
|
+
//#endregion
|
|
947
|
+
export { CombineModes, DocState, ExecutionContext, ExecutionPhase, InferMode, InferValue, Mode, ModeIterable, ModeValue, ParseFrame, Parser, ParserContext, ParserResult, Result, Suggestion, createParserContext, getDocPage, getDocPageAsync, getDocPageSync, parse, parseAsync, parseSync, suggest, suggestAsync, suggestSync };
|