@optique/core 0.9.0-dev.202 → 0.9.0-dev.204
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/index.cjs +2 -0
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/parser.cjs +58 -27
- package/dist/parser.d.cts +39 -7
- package/dist/parser.d.ts +39 -7
- package/dist/parser.js +57 -28
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -35,6 +35,8 @@ exports.formatMessage = require_message.formatMessage;
|
|
|
35
35
|
exports.formatUsage = require_usage.formatUsage;
|
|
36
36
|
exports.formatUsageTerm = require_usage.formatUsageTerm;
|
|
37
37
|
exports.getDocPage = require_parser.getDocPage;
|
|
38
|
+
exports.getDocPageAsync = require_parser.getDocPageAsync;
|
|
39
|
+
exports.getDocPageSync = require_parser.getDocPageSync;
|
|
38
40
|
exports.group = require_constructs.group;
|
|
39
41
|
exports.integer = require_valueparser.integer;
|
|
40
42
|
exports.isNonEmptyString = require_nonempty.isNonEmptyString;
|
package/dist/index.d.cts
CHANGED
|
@@ -5,8 +5,8 @@ import { DocEntry, DocFragment, DocFragments, DocPage, DocPageFormatOptions, Doc
|
|
|
5
5
|
import { ChoiceOptions, ChoiceOptionsBase, ChoiceOptionsNumber, ChoiceOptionsString, FloatOptions, IntegerOptionsBigInt, IntegerOptionsNumber, LocaleOptions, StringOptions, UrlOptions, Uuid, UuidOptions, ValueParser, ValueParserResult, choice, float, integer, isValueParser, locale, string, url, uuid } from "./valueparser.cjs";
|
|
6
6
|
import { MultipleErrorOptions, MultipleOptions, WithDefaultError, WithDefaultOptions, map, multiple, optional, withDefault } from "./modifiers.cjs";
|
|
7
7
|
import { ArgumentErrorOptions, ArgumentOptions, CommandErrorOptions, CommandOptions, FlagErrorOptions, FlagOptions, OptionErrorOptions, OptionOptions, PassThroughFormat, PassThroughOptions, argument, command, constant, flag, option, passThrough } from "./primitives.cjs";
|
|
8
|
-
import { CombineModes, DocState, InferMode, InferValue, Mode, ModeIterable, ModeValue, Parser, ParserContext, ParserResult, Result, Suggestion, getDocPage, parse, parseAsync, parseSync, suggest, suggestAsync, suggestSync } from "./parser.cjs";
|
|
8
|
+
import { CombineModes, DocState, InferMode, InferValue, Mode, ModeIterable, ModeValue, Parser, ParserContext, ParserResult, Result, Suggestion, getDocPage, getDocPageAsync, getDocPageSync, parse, parseAsync, parseSync, suggest, suggestAsync, suggestSync } from "./parser.cjs";
|
|
9
9
|
import { ConditionalErrorOptions, ConditionalOptions, DuplicateOptionError, LongestMatchErrorOptions, LongestMatchOptions, MergeOptions, NoMatchContext, ObjectErrorOptions, ObjectOptions, OrErrorOptions, OrOptions, TupleOptions, concat, conditional, group, longestMatch, merge, object, or, tuple } from "./constructs.cjs";
|
|
10
10
|
import { ShellCompletion, bash, fish, nu, pwsh, zsh } from "./completion.cjs";
|
|
11
11
|
import { RunError, RunOptions, RunParserError, run, runParser } from "./facade.cjs";
|
|
12
|
-
export { ArgumentErrorOptions, ArgumentOptions, ChoiceOptions, ChoiceOptionsBase, ChoiceOptionsNumber, ChoiceOptionsString, CombineModes, CommandErrorOptions, CommandOptions, ConditionalErrorOptions, ConditionalOptions, DocEntry, DocFragment, DocFragments, DocPage, DocPageFormatOptions, DocSection, DocState, DuplicateOptionError, FlagErrorOptions, FlagOptions, FloatOptions, InferMode, InferValue, IntegerOptionsBigInt, IntegerOptionsNumber, LocaleOptions, LongestMatchErrorOptions, LongestMatchOptions, MergeOptions, Message, MessageFormatOptions, MessageTerm, Mode, ModeIterable, ModeValue, MultipleErrorOptions, MultipleOptions, NoMatchContext, NonEmptyString, ObjectErrorOptions, ObjectOptions, OptionErrorOptions, OptionName, OptionOptions, OrErrorOptions, OrOptions, Parser, ParserContext, ParserResult, PassThroughFormat, PassThroughOptions, Result, RunError, RunOptions, RunParserError, ShellCompletion, ShowDefaultOptions, StringOptions, Suggestion, TupleOptions, UrlOptions, Usage, UsageFormatOptions, UsageTerm, UsageTermFormatOptions, Uuid, UuidOptions, ValueParser, ValueParserResult, WithDefaultError, WithDefaultOptions, argument, bash, choice, command, commandLine, concat, conditional, constant, ensureNonEmptyString, envVar, extractArgumentMetavars, extractCommandNames, extractOptionNames, fish, flag, float, formatDocPage, formatMessage, formatUsage, formatUsageTerm, getDocPage, group, integer, isNonEmptyString, isValueParser, locale, longestMatch, map, merge, message, metavar, multiple, normalizeUsage, nu, object, option, optionName, optionNames, optional, or, parse, parseAsync, parseSync, passThrough, pwsh, run, runParser, string, suggest, suggestAsync, suggestSync, text, tuple, url, uuid, value, values, withDefault, zsh };
|
|
12
|
+
export { ArgumentErrorOptions, ArgumentOptions, ChoiceOptions, ChoiceOptionsBase, ChoiceOptionsNumber, ChoiceOptionsString, CombineModes, CommandErrorOptions, CommandOptions, ConditionalErrorOptions, ConditionalOptions, DocEntry, DocFragment, DocFragments, DocPage, DocPageFormatOptions, DocSection, DocState, DuplicateOptionError, FlagErrorOptions, FlagOptions, FloatOptions, InferMode, InferValue, IntegerOptionsBigInt, IntegerOptionsNumber, LocaleOptions, LongestMatchErrorOptions, LongestMatchOptions, MergeOptions, Message, MessageFormatOptions, MessageTerm, Mode, ModeIterable, ModeValue, MultipleErrorOptions, MultipleOptions, NoMatchContext, NonEmptyString, ObjectErrorOptions, ObjectOptions, OptionErrorOptions, OptionName, OptionOptions, OrErrorOptions, OrOptions, Parser, ParserContext, ParserResult, PassThroughFormat, PassThroughOptions, Result, RunError, RunOptions, RunParserError, ShellCompletion, ShowDefaultOptions, StringOptions, Suggestion, TupleOptions, UrlOptions, Usage, UsageFormatOptions, UsageTerm, UsageTermFormatOptions, Uuid, UuidOptions, ValueParser, ValueParserResult, WithDefaultError, WithDefaultOptions, argument, bash, choice, command, commandLine, concat, conditional, constant, ensureNonEmptyString, envVar, extractArgumentMetavars, extractCommandNames, extractOptionNames, fish, flag, float, formatDocPage, formatMessage, formatUsage, formatUsageTerm, getDocPage, getDocPageAsync, getDocPageSync, group, integer, isNonEmptyString, isValueParser, locale, longestMatch, map, merge, message, metavar, multiple, normalizeUsage, nu, object, option, optionName, optionNames, optional, or, parse, parseAsync, parseSync, passThrough, pwsh, run, runParser, string, suggest, suggestAsync, suggestSync, text, tuple, url, uuid, value, values, withDefault, zsh };
|
package/dist/index.d.ts
CHANGED
|
@@ -5,8 +5,8 @@ import { DocEntry, DocFragment, DocFragments, DocPage, DocPageFormatOptions, Doc
|
|
|
5
5
|
import { ChoiceOptions, ChoiceOptionsBase, ChoiceOptionsNumber, ChoiceOptionsString, FloatOptions, IntegerOptionsBigInt, IntegerOptionsNumber, LocaleOptions, StringOptions, UrlOptions, Uuid, UuidOptions, ValueParser, ValueParserResult, choice, float, integer, isValueParser, locale, string, url, uuid } from "./valueparser.js";
|
|
6
6
|
import { MultipleErrorOptions, MultipleOptions, WithDefaultError, WithDefaultOptions, map, multiple, optional, withDefault } from "./modifiers.js";
|
|
7
7
|
import { ArgumentErrorOptions, ArgumentOptions, CommandErrorOptions, CommandOptions, FlagErrorOptions, FlagOptions, OptionErrorOptions, OptionOptions, PassThroughFormat, PassThroughOptions, argument, command, constant, flag, option, passThrough } from "./primitives.js";
|
|
8
|
-
import { CombineModes, DocState, InferMode, InferValue, Mode, ModeIterable, ModeValue, Parser, ParserContext, ParserResult, Result, Suggestion, getDocPage, parse, parseAsync, parseSync, suggest, suggestAsync, suggestSync } from "./parser.js";
|
|
8
|
+
import { CombineModes, DocState, InferMode, InferValue, Mode, ModeIterable, ModeValue, Parser, ParserContext, ParserResult, Result, Suggestion, getDocPage, getDocPageAsync, getDocPageSync, parse, parseAsync, parseSync, suggest, suggestAsync, suggestSync } from "./parser.js";
|
|
9
9
|
import { ConditionalErrorOptions, ConditionalOptions, DuplicateOptionError, LongestMatchErrorOptions, LongestMatchOptions, MergeOptions, NoMatchContext, ObjectErrorOptions, ObjectOptions, OrErrorOptions, OrOptions, TupleOptions, concat, conditional, group, longestMatch, merge, object, or, tuple } from "./constructs.js";
|
|
10
10
|
import { ShellCompletion, bash, fish, nu, pwsh, zsh } from "./completion.js";
|
|
11
11
|
import { RunError, RunOptions, RunParserError, run, runParser } from "./facade.js";
|
|
12
|
-
export { ArgumentErrorOptions, ArgumentOptions, ChoiceOptions, ChoiceOptionsBase, ChoiceOptionsNumber, ChoiceOptionsString, CombineModes, CommandErrorOptions, CommandOptions, ConditionalErrorOptions, ConditionalOptions, DocEntry, DocFragment, DocFragments, DocPage, DocPageFormatOptions, DocSection, DocState, DuplicateOptionError, FlagErrorOptions, FlagOptions, FloatOptions, InferMode, InferValue, IntegerOptionsBigInt, IntegerOptionsNumber, LocaleOptions, LongestMatchErrorOptions, LongestMatchOptions, MergeOptions, Message, MessageFormatOptions, MessageTerm, Mode, ModeIterable, ModeValue, MultipleErrorOptions, MultipleOptions, NoMatchContext, NonEmptyString, ObjectErrorOptions, ObjectOptions, OptionErrorOptions, OptionName, OptionOptions, OrErrorOptions, OrOptions, Parser, ParserContext, ParserResult, PassThroughFormat, PassThroughOptions, Result, RunError, RunOptions, RunParserError, ShellCompletion, ShowDefaultOptions, StringOptions, Suggestion, TupleOptions, UrlOptions, Usage, UsageFormatOptions, UsageTerm, UsageTermFormatOptions, Uuid, UuidOptions, ValueParser, ValueParserResult, WithDefaultError, WithDefaultOptions, argument, bash, choice, command, commandLine, concat, conditional, constant, ensureNonEmptyString, envVar, extractArgumentMetavars, extractCommandNames, extractOptionNames, fish, flag, float, formatDocPage, formatMessage, formatUsage, formatUsageTerm, getDocPage, group, integer, isNonEmptyString, isValueParser, locale, longestMatch, map, merge, message, metavar, multiple, normalizeUsage, nu, object, option, optionName, optionNames, optional, or, parse, parseAsync, parseSync, passThrough, pwsh, run, runParser, string, suggest, suggestAsync, suggestSync, text, tuple, url, uuid, value, values, withDefault, zsh };
|
|
12
|
+
export { ArgumentErrorOptions, ArgumentOptions, ChoiceOptions, ChoiceOptionsBase, ChoiceOptionsNumber, ChoiceOptionsString, CombineModes, CommandErrorOptions, CommandOptions, ConditionalErrorOptions, ConditionalOptions, DocEntry, DocFragment, DocFragments, DocPage, DocPageFormatOptions, DocSection, DocState, DuplicateOptionError, FlagErrorOptions, FlagOptions, FloatOptions, InferMode, InferValue, IntegerOptionsBigInt, IntegerOptionsNumber, LocaleOptions, LongestMatchErrorOptions, LongestMatchOptions, MergeOptions, Message, MessageFormatOptions, MessageTerm, Mode, ModeIterable, ModeValue, MultipleErrorOptions, MultipleOptions, NoMatchContext, NonEmptyString, ObjectErrorOptions, ObjectOptions, OptionErrorOptions, OptionName, OptionOptions, OrErrorOptions, OrOptions, Parser, ParserContext, ParserResult, PassThroughFormat, PassThroughOptions, Result, RunError, RunOptions, RunParserError, ShellCompletion, ShowDefaultOptions, StringOptions, Suggestion, TupleOptions, UrlOptions, Usage, UsageFormatOptions, UsageTerm, UsageTermFormatOptions, Uuid, UuidOptions, ValueParser, ValueParserResult, WithDefaultError, WithDefaultOptions, argument, bash, choice, command, commandLine, concat, conditional, constant, ensureNonEmptyString, envVar, extractArgumentMetavars, extractCommandNames, extractOptionNames, fish, flag, float, formatDocPage, formatMessage, formatUsage, formatUsageTerm, getDocPage, getDocPageAsync, getDocPageSync, group, integer, isNonEmptyString, isValueParser, locale, longestMatch, map, merge, message, metavar, multiple, normalizeUsage, nu, object, option, optionName, optionNames, optional, or, parse, parseAsync, parseSync, passThrough, pwsh, run, runParser, string, suggest, suggestAsync, suggestSync, text, tuple, url, uuid, value, values, withDefault, zsh };
|
package/dist/index.js
CHANGED
|
@@ -7,7 +7,7 @@ import { WithDefaultError, map, multiple, optional, withDefault } from "./modifi
|
|
|
7
7
|
import { ensureNonEmptyString, isNonEmptyString } from "./nonempty.js";
|
|
8
8
|
import { choice, float, integer, isValueParser, locale, string, url, uuid } from "./valueparser.js";
|
|
9
9
|
import { argument, command, constant, flag, option, passThrough } from "./primitives.js";
|
|
10
|
-
import { getDocPage, parse, parseAsync, parseSync, suggest, suggestAsync, suggestSync } from "./parser.js";
|
|
10
|
+
import { getDocPage, getDocPageAsync, getDocPageSync, parse, parseAsync, parseSync, suggest, suggestAsync, suggestSync } from "./parser.js";
|
|
11
11
|
import { RunError, RunParserError, run, runParser } from "./facade.js";
|
|
12
12
|
|
|
13
|
-
export { DuplicateOptionError, RunError, RunParserError, WithDefaultError, argument, bash, choice, command, commandLine, concat, conditional, constant, ensureNonEmptyString, envVar, extractArgumentMetavars, extractCommandNames, extractOptionNames, fish, flag, float, formatDocPage, formatMessage, formatUsage, formatUsageTerm, getDocPage, group, integer, isNonEmptyString, isValueParser, locale, longestMatch, map, merge, message, metavar, multiple, normalizeUsage, nu, object, option, optionName, optionNames, optional, or, parse, parseAsync, parseSync, passThrough, pwsh, run, runParser, string, suggest, suggestAsync, suggestSync, text, tuple, url, uuid, value, values, withDefault, zsh };
|
|
13
|
+
export { DuplicateOptionError, RunError, RunParserError, WithDefaultError, argument, bash, choice, command, commandLine, concat, conditional, constant, ensureNonEmptyString, envVar, extractArgumentMetavars, extractCommandNames, extractOptionNames, fish, flag, float, formatDocPage, formatMessage, formatUsage, formatUsageTerm, getDocPage, getDocPageAsync, getDocPageSync, group, integer, isNonEmptyString, isValueParser, locale, longestMatch, map, merge, message, metavar, multiple, normalizeUsage, nu, object, option, optionName, optionNames, optional, or, parse, parseAsync, parseSync, passThrough, pwsh, run, runParser, string, suggest, suggestAsync, suggestSync, text, tuple, url, uuid, value, values, withDefault, zsh };
|
package/dist/parser.cjs
CHANGED
|
@@ -264,39 +264,44 @@ function findCommandInExclusive(term, commandName) {
|
|
|
264
264
|
return null;
|
|
265
265
|
}
|
|
266
266
|
/**
|
|
267
|
-
* Generates a documentation page for a parser
|
|
268
|
-
* attempting to parse the provided arguments. This function is useful for
|
|
269
|
-
* creating help documentation that reflects the current parsing context.
|
|
267
|
+
* Generates a documentation page for a synchronous parser.
|
|
270
268
|
*
|
|
271
|
-
*
|
|
272
|
-
*
|
|
273
|
-
*
|
|
274
|
-
* 3. Organizing fragments into entries and sections
|
|
275
|
-
* 4. Resolving command usage terms based on parsed arguments
|
|
269
|
+
* This is the sync-specific version of {@link getDocPage}. It only accepts
|
|
270
|
+
* sync parsers and returns the documentation page directly (not wrapped
|
|
271
|
+
* in a Promise).
|
|
276
272
|
*
|
|
277
|
-
* @param parser The parser to generate documentation for
|
|
278
|
-
* @param args Optional array of command-line arguments
|
|
279
|
-
*
|
|
280
|
-
*
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
*
|
|
287
|
-
* const parser = object({
|
|
288
|
-
* verbose: option("-v", "--verbose"),
|
|
289
|
-
* port: option("-p", "--port", integer())
|
|
290
|
-
* });
|
|
273
|
+
* @param parser The sync parser to generate documentation for.
|
|
274
|
+
* @param args Optional array of command-line arguments for context.
|
|
275
|
+
* @returns A {@link DocPage} or `undefined`.
|
|
276
|
+
* @since 0.9.0
|
|
277
|
+
*/
|
|
278
|
+
function getDocPageSync(parser, args = []) {
|
|
279
|
+
return getDocPageSyncImpl(parser, args);
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Generates a documentation page for any parser, returning a Promise.
|
|
291
283
|
*
|
|
292
|
-
*
|
|
293
|
-
*
|
|
284
|
+
* This function accepts parsers of any mode (sync or async) and always
|
|
285
|
+
* returns a Promise. Use this when working with parsers that may contain
|
|
286
|
+
* async value parsers.
|
|
294
287
|
*
|
|
295
|
-
*
|
|
296
|
-
*
|
|
297
|
-
*
|
|
288
|
+
* @param parser The parser to generate documentation for.
|
|
289
|
+
* @param args Optional array of command-line arguments for context.
|
|
290
|
+
* @returns A Promise of {@link DocPage} or `undefined`.
|
|
291
|
+
* @since 0.9.0
|
|
298
292
|
*/
|
|
293
|
+
function getDocPageAsync(parser, args = []) {
|
|
294
|
+
if (parser.$mode === "sync") return Promise.resolve(getDocPageSyncImpl(parser, args));
|
|
295
|
+
return getDocPageAsyncImpl(parser, args);
|
|
296
|
+
}
|
|
299
297
|
function getDocPage(parser, args = []) {
|
|
298
|
+
if (parser.$mode === "sync") return getDocPageSyncImpl(parser, args);
|
|
299
|
+
return getDocPageAsyncImpl(parser, args);
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* Internal sync implementation of getDocPage.
|
|
303
|
+
*/
|
|
304
|
+
function getDocPageSyncImpl(parser, args) {
|
|
300
305
|
let context = {
|
|
301
306
|
buffer: args,
|
|
302
307
|
optionsTerminated: false,
|
|
@@ -308,6 +313,30 @@ function getDocPage(parser, args = []) {
|
|
|
308
313
|
if (!result.success) break;
|
|
309
314
|
context = result.next;
|
|
310
315
|
} while (context.buffer.length > 0);
|
|
316
|
+
return buildDocPage(parser, context, args);
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* Internal async implementation of getDocPage.
|
|
320
|
+
*/
|
|
321
|
+
async function getDocPageAsyncImpl(parser, args) {
|
|
322
|
+
let context = {
|
|
323
|
+
buffer: args,
|
|
324
|
+
optionsTerminated: false,
|
|
325
|
+
state: parser.initialState,
|
|
326
|
+
usage: parser.usage
|
|
327
|
+
};
|
|
328
|
+
do {
|
|
329
|
+
const result = await parser.parse(context);
|
|
330
|
+
if (!result.success) break;
|
|
331
|
+
context = result.next;
|
|
332
|
+
} while (context.buffer.length > 0);
|
|
333
|
+
return buildDocPage(parser, context, args);
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* Builds a DocPage from the parser and context.
|
|
337
|
+
* Shared by both sync and async implementations.
|
|
338
|
+
*/
|
|
339
|
+
function buildDocPage(parser, context, args) {
|
|
311
340
|
const { description, fragments, footer } = parser.getDocFragments({
|
|
312
341
|
kind: "available",
|
|
313
342
|
state: context.state
|
|
@@ -351,6 +380,8 @@ exports.conditional = require_constructs.conditional;
|
|
|
351
380
|
exports.constant = require_primitives.constant;
|
|
352
381
|
exports.flag = require_primitives.flag;
|
|
353
382
|
exports.getDocPage = getDocPage;
|
|
383
|
+
exports.getDocPageAsync = getDocPageAsync;
|
|
384
|
+
exports.getDocPageSync = getDocPageSync;
|
|
354
385
|
exports.group = require_constructs.group;
|
|
355
386
|
exports.longestMatch = require_constructs.longestMatch;
|
|
356
387
|
exports.map = require_modifiers.map;
|
package/dist/parser.d.cts
CHANGED
|
@@ -440,6 +440,32 @@ declare function suggestAsync<T>(parser: Parser<Mode, T, unknown>, args: readonl
|
|
|
440
440
|
* @since 0.6.0
|
|
441
441
|
*/
|
|
442
442
|
declare function suggest<M extends Mode, T>(parser: Parser<M, T, unknown>, args: readonly [string, ...readonly string[]]): ModeValue<M, readonly Suggestion[]>;
|
|
443
|
+
/**
|
|
444
|
+
* Generates a documentation page for a synchronous parser.
|
|
445
|
+
*
|
|
446
|
+
* This is the sync-specific version of {@link getDocPage}. It only accepts
|
|
447
|
+
* sync parsers and returns the documentation page directly (not wrapped
|
|
448
|
+
* in a Promise).
|
|
449
|
+
*
|
|
450
|
+
* @param parser The sync parser to generate documentation for.
|
|
451
|
+
* @param args Optional array of command-line arguments for context.
|
|
452
|
+
* @returns A {@link DocPage} or `undefined`.
|
|
453
|
+
* @since 0.9.0
|
|
454
|
+
*/
|
|
455
|
+
declare function getDocPageSync(parser: Parser<"sync", unknown, unknown>, args?: readonly string[]): DocPage | undefined;
|
|
456
|
+
/**
|
|
457
|
+
* Generates a documentation page for any parser, returning a Promise.
|
|
458
|
+
*
|
|
459
|
+
* This function accepts parsers of any mode (sync or async) and always
|
|
460
|
+
* returns a Promise. Use this when working with parsers that may contain
|
|
461
|
+
* async value parsers.
|
|
462
|
+
*
|
|
463
|
+
* @param parser The parser to generate documentation for.
|
|
464
|
+
* @param args Optional array of command-line arguments for context.
|
|
465
|
+
* @returns A Promise of {@link DocPage} or `undefined`.
|
|
466
|
+
* @since 0.9.0
|
|
467
|
+
*/
|
|
468
|
+
declare function getDocPageAsync(parser: Parser<Mode, unknown, unknown>, args?: readonly string[]): Promise<DocPage | undefined>;
|
|
443
469
|
/**
|
|
444
470
|
* Generates a documentation page for a parser based on its current state after
|
|
445
471
|
* attempting to parse the provided arguments. This function is useful for
|
|
@@ -451,13 +477,16 @@ declare function suggest<M extends Mode, T>(parser: Parser<M, T, unknown>, args:
|
|
|
451
477
|
* 3. Organizing fragments into entries and sections
|
|
452
478
|
* 4. Resolving command usage terms based on parsed arguments
|
|
453
479
|
*
|
|
480
|
+
* For sync parsers, returns the documentation page directly.
|
|
481
|
+
* For async parsers, returns a Promise of the documentation page.
|
|
482
|
+
*
|
|
454
483
|
* @param parser The parser to generate documentation for
|
|
455
484
|
* @param args Optional array of command-line arguments that have been parsed
|
|
456
485
|
* so far. Defaults to an empty array. This is used to determine
|
|
457
486
|
* the current parsing context and generate contextual documentation.
|
|
458
|
-
* @returns
|
|
459
|
-
*
|
|
460
|
-
* generated.
|
|
487
|
+
* @returns For sync parsers, returns a {@link DocPage} directly.
|
|
488
|
+
* For async parsers, returns a Promise of {@link DocPage}.
|
|
489
|
+
* Returns `undefined` if no documentation can be generated.
|
|
461
490
|
*
|
|
462
491
|
* @example
|
|
463
492
|
* ```typescript
|
|
@@ -466,13 +495,16 @@ declare function suggest<M extends Mode, T>(parser: Parser<M, T, unknown>, args:
|
|
|
466
495
|
* port: option("-p", "--port", integer())
|
|
467
496
|
* });
|
|
468
497
|
*
|
|
469
|
-
* // Get documentation for
|
|
498
|
+
* // Get documentation for sync parser
|
|
470
499
|
* const rootDoc = getDocPage(parser);
|
|
471
500
|
*
|
|
472
|
-
* // Get documentation
|
|
473
|
-
* const
|
|
501
|
+
* // Get documentation for async parser
|
|
502
|
+
* const asyncDoc = await getDocPage(asyncParser);
|
|
474
503
|
* ```
|
|
504
|
+
* @since 0.9.0 Updated to support async parsers.
|
|
475
505
|
*/
|
|
476
506
|
declare function getDocPage(parser: Parser<"sync", unknown, unknown>, args?: readonly string[]): DocPage | undefined;
|
|
507
|
+
declare function getDocPage(parser: Parser<"async", unknown, unknown>, args?: readonly string[]): Promise<DocPage | undefined>;
|
|
508
|
+
declare function getDocPage<M extends Mode>(parser: Parser<M, unknown, unknown>, args?: readonly string[]): ModeValue<M, DocPage | undefined>;
|
|
477
509
|
//#endregion
|
|
478
|
-
export { ArgumentErrorOptions, ArgumentOptions, CombineModes, CommandErrorOptions, CommandOptions, ConditionalErrorOptions, ConditionalOptions, DocState, DuplicateOptionError, FlagErrorOptions, FlagOptions, InferMode, InferValue, LongestMatchErrorOptions, LongestMatchOptions, MergeOptions, Mode, ModeIterable, ModeValue, MultipleErrorOptions, MultipleOptions, NoMatchContext, ObjectErrorOptions, ObjectOptions, OptionErrorOptions, OptionOptions, OrErrorOptions, OrOptions, Parser, ParserContext, ParserResult, PassThroughFormat, PassThroughOptions, Result, Suggestion, TupleOptions, WithDefaultError, WithDefaultOptions, 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 };
|
|
510
|
+
export { ArgumentErrorOptions, ArgumentOptions, CombineModes, CommandErrorOptions, CommandOptions, ConditionalErrorOptions, ConditionalOptions, DocState, DuplicateOptionError, FlagErrorOptions, FlagOptions, InferMode, InferValue, LongestMatchErrorOptions, LongestMatchOptions, MergeOptions, Mode, ModeIterable, ModeValue, MultipleErrorOptions, MultipleOptions, NoMatchContext, ObjectErrorOptions, ObjectOptions, OptionErrorOptions, OptionOptions, OrErrorOptions, OrOptions, Parser, ParserContext, ParserResult, PassThroughFormat, PassThroughOptions, Result, Suggestion, TupleOptions, WithDefaultError, WithDefaultOptions, argument, command, concat, conditional, constant, flag, getDocPage, getDocPageAsync, getDocPageSync, group, longestMatch, map, merge, multiple, object, option, optional, or, parse, parseAsync, parseSync, passThrough, suggest, suggestAsync, suggestSync, tuple, withDefault };
|
package/dist/parser.d.ts
CHANGED
|
@@ -440,6 +440,32 @@ declare function suggestAsync<T>(parser: Parser<Mode, T, unknown>, args: readonl
|
|
|
440
440
|
* @since 0.6.0
|
|
441
441
|
*/
|
|
442
442
|
declare function suggest<M extends Mode, T>(parser: Parser<M, T, unknown>, args: readonly [string, ...readonly string[]]): ModeValue<M, readonly Suggestion[]>;
|
|
443
|
+
/**
|
|
444
|
+
* Generates a documentation page for a synchronous parser.
|
|
445
|
+
*
|
|
446
|
+
* This is the sync-specific version of {@link getDocPage}. It only accepts
|
|
447
|
+
* sync parsers and returns the documentation page directly (not wrapped
|
|
448
|
+
* in a Promise).
|
|
449
|
+
*
|
|
450
|
+
* @param parser The sync parser to generate documentation for.
|
|
451
|
+
* @param args Optional array of command-line arguments for context.
|
|
452
|
+
* @returns A {@link DocPage} or `undefined`.
|
|
453
|
+
* @since 0.9.0
|
|
454
|
+
*/
|
|
455
|
+
declare function getDocPageSync(parser: Parser<"sync", unknown, unknown>, args?: readonly string[]): DocPage | undefined;
|
|
456
|
+
/**
|
|
457
|
+
* Generates a documentation page for any parser, returning a Promise.
|
|
458
|
+
*
|
|
459
|
+
* This function accepts parsers of any mode (sync or async) and always
|
|
460
|
+
* returns a Promise. Use this when working with parsers that may contain
|
|
461
|
+
* async value parsers.
|
|
462
|
+
*
|
|
463
|
+
* @param parser The parser to generate documentation for.
|
|
464
|
+
* @param args Optional array of command-line arguments for context.
|
|
465
|
+
* @returns A Promise of {@link DocPage} or `undefined`.
|
|
466
|
+
* @since 0.9.0
|
|
467
|
+
*/
|
|
468
|
+
declare function getDocPageAsync(parser: Parser<Mode, unknown, unknown>, args?: readonly string[]): Promise<DocPage | undefined>;
|
|
443
469
|
/**
|
|
444
470
|
* Generates a documentation page for a parser based on its current state after
|
|
445
471
|
* attempting to parse the provided arguments. This function is useful for
|
|
@@ -451,13 +477,16 @@ declare function suggest<M extends Mode, T>(parser: Parser<M, T, unknown>, args:
|
|
|
451
477
|
* 3. Organizing fragments into entries and sections
|
|
452
478
|
* 4. Resolving command usage terms based on parsed arguments
|
|
453
479
|
*
|
|
480
|
+
* For sync parsers, returns the documentation page directly.
|
|
481
|
+
* For async parsers, returns a Promise of the documentation page.
|
|
482
|
+
*
|
|
454
483
|
* @param parser The parser to generate documentation for
|
|
455
484
|
* @param args Optional array of command-line arguments that have been parsed
|
|
456
485
|
* so far. Defaults to an empty array. This is used to determine
|
|
457
486
|
* the current parsing context and generate contextual documentation.
|
|
458
|
-
* @returns
|
|
459
|
-
*
|
|
460
|
-
* generated.
|
|
487
|
+
* @returns For sync parsers, returns a {@link DocPage} directly.
|
|
488
|
+
* For async parsers, returns a Promise of {@link DocPage}.
|
|
489
|
+
* Returns `undefined` if no documentation can be generated.
|
|
461
490
|
*
|
|
462
491
|
* @example
|
|
463
492
|
* ```typescript
|
|
@@ -466,13 +495,16 @@ declare function suggest<M extends Mode, T>(parser: Parser<M, T, unknown>, args:
|
|
|
466
495
|
* port: option("-p", "--port", integer())
|
|
467
496
|
* });
|
|
468
497
|
*
|
|
469
|
-
* // Get documentation for
|
|
498
|
+
* // Get documentation for sync parser
|
|
470
499
|
* const rootDoc = getDocPage(parser);
|
|
471
500
|
*
|
|
472
|
-
* // Get documentation
|
|
473
|
-
* const
|
|
501
|
+
* // Get documentation for async parser
|
|
502
|
+
* const asyncDoc = await getDocPage(asyncParser);
|
|
474
503
|
* ```
|
|
504
|
+
* @since 0.9.0 Updated to support async parsers.
|
|
475
505
|
*/
|
|
476
506
|
declare function getDocPage(parser: Parser<"sync", unknown, unknown>, args?: readonly string[]): DocPage | undefined;
|
|
507
|
+
declare function getDocPage(parser: Parser<"async", unknown, unknown>, args?: readonly string[]): Promise<DocPage | undefined>;
|
|
508
|
+
declare function getDocPage<M extends Mode>(parser: Parser<M, unknown, unknown>, args?: readonly string[]): ModeValue<M, DocPage | undefined>;
|
|
477
509
|
//#endregion
|
|
478
|
-
export { ArgumentErrorOptions, ArgumentOptions, CombineModes, CommandErrorOptions, CommandOptions, ConditionalErrorOptions, ConditionalOptions, DocState, DuplicateOptionError, FlagErrorOptions, FlagOptions, InferMode, InferValue, LongestMatchErrorOptions, LongestMatchOptions, MergeOptions, Mode, ModeIterable, ModeValue, MultipleErrorOptions, MultipleOptions, NoMatchContext, ObjectErrorOptions, ObjectOptions, OptionErrorOptions, OptionOptions, OrErrorOptions, OrOptions, Parser, ParserContext, ParserResult, PassThroughFormat, PassThroughOptions, Result, Suggestion, TupleOptions, WithDefaultError, WithDefaultOptions, 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 };
|
|
510
|
+
export { ArgumentErrorOptions, ArgumentOptions, CombineModes, CommandErrorOptions, CommandOptions, ConditionalErrorOptions, ConditionalOptions, DocState, DuplicateOptionError, FlagErrorOptions, FlagOptions, InferMode, InferValue, LongestMatchErrorOptions, LongestMatchOptions, MergeOptions, Mode, ModeIterable, ModeValue, MultipleErrorOptions, MultipleOptions, NoMatchContext, ObjectErrorOptions, ObjectOptions, OptionErrorOptions, OptionOptions, OrErrorOptions, OrOptions, Parser, ParserContext, ParserResult, PassThroughFormat, PassThroughOptions, Result, Suggestion, TupleOptions, WithDefaultError, WithDefaultOptions, argument, command, concat, conditional, constant, flag, getDocPage, getDocPageAsync, getDocPageSync, group, longestMatch, map, merge, multiple, object, option, optional, or, parse, parseAsync, parseSync, passThrough, suggest, suggestAsync, suggestSync, tuple, withDefault };
|
package/dist/parser.js
CHANGED
|
@@ -264,39 +264,44 @@ function findCommandInExclusive(term, commandName) {
|
|
|
264
264
|
return null;
|
|
265
265
|
}
|
|
266
266
|
/**
|
|
267
|
-
* Generates a documentation page for a parser
|
|
268
|
-
* attempting to parse the provided arguments. This function is useful for
|
|
269
|
-
* creating help documentation that reflects the current parsing context.
|
|
267
|
+
* Generates a documentation page for a synchronous parser.
|
|
270
268
|
*
|
|
271
|
-
*
|
|
272
|
-
*
|
|
273
|
-
*
|
|
274
|
-
* 3. Organizing fragments into entries and sections
|
|
275
|
-
* 4. Resolving command usage terms based on parsed arguments
|
|
269
|
+
* This is the sync-specific version of {@link getDocPage}. It only accepts
|
|
270
|
+
* sync parsers and returns the documentation page directly (not wrapped
|
|
271
|
+
* in a Promise).
|
|
276
272
|
*
|
|
277
|
-
* @param parser The parser to generate documentation for
|
|
278
|
-
* @param args Optional array of command-line arguments
|
|
279
|
-
*
|
|
280
|
-
*
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
*
|
|
287
|
-
* const parser = object({
|
|
288
|
-
* verbose: option("-v", "--verbose"),
|
|
289
|
-
* port: option("-p", "--port", integer())
|
|
290
|
-
* });
|
|
273
|
+
* @param parser The sync parser to generate documentation for.
|
|
274
|
+
* @param args Optional array of command-line arguments for context.
|
|
275
|
+
* @returns A {@link DocPage} or `undefined`.
|
|
276
|
+
* @since 0.9.0
|
|
277
|
+
*/
|
|
278
|
+
function getDocPageSync(parser, args = []) {
|
|
279
|
+
return getDocPageSyncImpl(parser, args);
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Generates a documentation page for any parser, returning a Promise.
|
|
291
283
|
*
|
|
292
|
-
*
|
|
293
|
-
*
|
|
284
|
+
* This function accepts parsers of any mode (sync or async) and always
|
|
285
|
+
* returns a Promise. Use this when working with parsers that may contain
|
|
286
|
+
* async value parsers.
|
|
294
287
|
*
|
|
295
|
-
*
|
|
296
|
-
*
|
|
297
|
-
*
|
|
288
|
+
* @param parser The parser to generate documentation for.
|
|
289
|
+
* @param args Optional array of command-line arguments for context.
|
|
290
|
+
* @returns A Promise of {@link DocPage} or `undefined`.
|
|
291
|
+
* @since 0.9.0
|
|
298
292
|
*/
|
|
293
|
+
function getDocPageAsync(parser, args = []) {
|
|
294
|
+
if (parser.$mode === "sync") return Promise.resolve(getDocPageSyncImpl(parser, args));
|
|
295
|
+
return getDocPageAsyncImpl(parser, args);
|
|
296
|
+
}
|
|
299
297
|
function getDocPage(parser, args = []) {
|
|
298
|
+
if (parser.$mode === "sync") return getDocPageSyncImpl(parser, args);
|
|
299
|
+
return getDocPageAsyncImpl(parser, args);
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* Internal sync implementation of getDocPage.
|
|
303
|
+
*/
|
|
304
|
+
function getDocPageSyncImpl(parser, args) {
|
|
300
305
|
let context = {
|
|
301
306
|
buffer: args,
|
|
302
307
|
optionsTerminated: false,
|
|
@@ -308,6 +313,30 @@ function getDocPage(parser, args = []) {
|
|
|
308
313
|
if (!result.success) break;
|
|
309
314
|
context = result.next;
|
|
310
315
|
} while (context.buffer.length > 0);
|
|
316
|
+
return buildDocPage(parser, context, args);
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* Internal async implementation of getDocPage.
|
|
320
|
+
*/
|
|
321
|
+
async function getDocPageAsyncImpl(parser, args) {
|
|
322
|
+
let context = {
|
|
323
|
+
buffer: args,
|
|
324
|
+
optionsTerminated: false,
|
|
325
|
+
state: parser.initialState,
|
|
326
|
+
usage: parser.usage
|
|
327
|
+
};
|
|
328
|
+
do {
|
|
329
|
+
const result = await parser.parse(context);
|
|
330
|
+
if (!result.success) break;
|
|
331
|
+
context = result.next;
|
|
332
|
+
} while (context.buffer.length > 0);
|
|
333
|
+
return buildDocPage(parser, context, args);
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* Builds a DocPage from the parser and context.
|
|
337
|
+
* Shared by both sync and async implementations.
|
|
338
|
+
*/
|
|
339
|
+
function buildDocPage(parser, context, args) {
|
|
311
340
|
const { description, fragments, footer } = parser.getDocFragments({
|
|
312
341
|
kind: "available",
|
|
313
342
|
state: context.state
|
|
@@ -342,4 +371,4 @@ function getDocPage(parser, args = []) {
|
|
|
342
371
|
}
|
|
343
372
|
|
|
344
373
|
//#endregion
|
|
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 };
|
|
374
|
+
export { DuplicateOptionError, WithDefaultError, argument, command, concat, conditional, constant, flag, getDocPage, getDocPageAsync, getDocPageSync, group, longestMatch, map, merge, multiple, object, option, optional, or, parse, parseAsync, parseSync, passThrough, suggest, suggestAsync, suggestSync, tuple, withDefault };
|