@optique/core 0.9.0-dev.202 → 0.9.0-dev.206
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/facade.cjs +90 -36
- package/dist/facade.d.cts +37 -1
- package/dist/facade.d.ts +37 -1
- package/dist/facade.js +88 -36
- package/dist/index.cjs +4 -0
- package/dist/index.d.cts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +3 -3
- 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/facade.cjs
CHANGED
|
@@ -404,7 +404,7 @@ function handleCompletion(completionArgs, programName, parser, completionParser,
|
|
|
404
404
|
return callOnCompletion(0);
|
|
405
405
|
}
|
|
406
406
|
function runParser(parser, programName, args, options = {}) {
|
|
407
|
-
|
|
407
|
+
const { colors, maxWidth, showDefault, aboveError = "usage", onError = () => {
|
|
408
408
|
throw new RunParserError("Failed to parse command line arguments.");
|
|
409
409
|
}, stderr = console.error, stdout = console.log, brief, description, footer } = options;
|
|
410
410
|
const helpMode = options.help?.mode ?? "option";
|
|
@@ -500,31 +500,35 @@ function runParser(parser, programName, args, options = {}) {
|
|
|
500
500
|
else if (commandParsers.length === 2) helpGeneratorParser = require_constructs.longestMatch(commandParsers[0], commandParsers[1]);
|
|
501
501
|
else helpGeneratorParser = require_constructs.longestMatch(...commandParsers);
|
|
502
502
|
}
|
|
503
|
-
const
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
503
|
+
const displayHelp = (doc) => {
|
|
504
|
+
if (doc != null) {
|
|
505
|
+
const isMetaCommandHelp = (completionName === "singular" || completionName === "both" ? requestedCommand === "completion" : false) || (completionName === "plural" || completionName === "both" ? requestedCommand === "completions" : false) || requestedCommand === "help" || requestedCommand === "version";
|
|
506
|
+
const augmentedDoc = {
|
|
507
|
+
...doc,
|
|
508
|
+
brief: !isMetaCommandHelp ? brief ?? doc.brief : doc.brief,
|
|
509
|
+
description: !isMetaCommandHelp ? description ?? doc.description : doc.description,
|
|
510
|
+
footer: !isMetaCommandHelp ? footer ?? doc.footer : doc.footer
|
|
511
|
+
};
|
|
512
|
+
stdout(require_doc.formatDocPage(programName, augmentedDoc, {
|
|
513
|
+
colors,
|
|
514
|
+
maxWidth,
|
|
515
|
+
showDefault
|
|
516
|
+
}));
|
|
517
|
+
}
|
|
518
|
+
try {
|
|
519
|
+
return onHelp(0);
|
|
520
|
+
} catch {
|
|
521
|
+
return onHelp();
|
|
522
|
+
}
|
|
523
|
+
};
|
|
524
|
+
const docOrPromise = require_parser.getDocPage(helpGeneratorParser, classified.commands);
|
|
525
|
+
if (docOrPromise instanceof Promise) return docOrPromise.then(displayHelp);
|
|
526
|
+
return displayHelp(docOrPromise);
|
|
523
527
|
}
|
|
524
528
|
case "error": {
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
if (doc == null)
|
|
529
|
+
const displayError = (doc, currentAboveError) => {
|
|
530
|
+
let effectiveAboveError = currentAboveError;
|
|
531
|
+
if (effectiveAboveError === "help") if (doc == null) effectiveAboveError = "usage";
|
|
528
532
|
else {
|
|
529
533
|
const augmentedDoc = {
|
|
530
534
|
...doc,
|
|
@@ -538,18 +542,25 @@ function runParser(parser, programName, args, options = {}) {
|
|
|
538
542
|
showDefault
|
|
539
543
|
}));
|
|
540
544
|
}
|
|
545
|
+
if (effectiveAboveError === "usage") stderr(`Usage: ${indentLines(require_usage.formatUsage(programName, augmentedParser.usage, {
|
|
546
|
+
colors,
|
|
547
|
+
maxWidth: maxWidth == null ? void 0 : maxWidth - 7,
|
|
548
|
+
expandCommands: true
|
|
549
|
+
}), 7)}`);
|
|
550
|
+
const errorMessage = require_message.formatMessage(classified.error, {
|
|
551
|
+
colors,
|
|
552
|
+
quotes: !colors
|
|
553
|
+
});
|
|
554
|
+
stderr(`Error: ${errorMessage}`);
|
|
555
|
+
return onError(1);
|
|
556
|
+
};
|
|
557
|
+
if (aboveError === "help") {
|
|
558
|
+
const parserForDoc = args.length < 1 ? augmentedParser : parser;
|
|
559
|
+
const docOrPromise = require_parser.getDocPage(parserForDoc, args);
|
|
560
|
+
if (docOrPromise instanceof Promise) return docOrPromise.then((doc) => displayError(doc, aboveError));
|
|
561
|
+
return displayError(docOrPromise, aboveError);
|
|
541
562
|
}
|
|
542
|
-
|
|
543
|
-
colors,
|
|
544
|
-
maxWidth: maxWidth == null ? void 0 : maxWidth - 7,
|
|
545
|
-
expandCommands: true
|
|
546
|
-
}), 7)}`);
|
|
547
|
-
const errorMessage = require_message.formatMessage(classified.error, {
|
|
548
|
-
colors,
|
|
549
|
-
quotes: !colors
|
|
550
|
-
});
|
|
551
|
-
stderr(`Error: ${errorMessage}`);
|
|
552
|
-
return onError(1);
|
|
563
|
+
return displayError(void 0, aboveError);
|
|
553
564
|
}
|
|
554
565
|
default: throw new RunParserError("Unexpected parse result type");
|
|
555
566
|
}
|
|
@@ -561,6 +572,47 @@ function runParser(parser, programName, args, options = {}) {
|
|
|
561
572
|
}
|
|
562
573
|
}
|
|
563
574
|
/**
|
|
575
|
+
* Runs a synchronous command-line parser with the given options.
|
|
576
|
+
*
|
|
577
|
+
* This is a type-safe version of {@link runParser} that only accepts sync
|
|
578
|
+
* parsers. Use this when you know your parser is sync-only to get direct
|
|
579
|
+
* return values without Promise wrappers.
|
|
580
|
+
*
|
|
581
|
+
* @template TParser The sync parser type being executed.
|
|
582
|
+
* @template THelp The return type of the onHelp callback.
|
|
583
|
+
* @template TError The return type of the onError callback.
|
|
584
|
+
* @param parser The synchronous command-line parser to execute.
|
|
585
|
+
* @param programName The name of the program for help messages.
|
|
586
|
+
* @param args The command-line arguments to parse.
|
|
587
|
+
* @param options Configuration options for customizing behavior.
|
|
588
|
+
* @returns The parsed result if successful.
|
|
589
|
+
* @since 0.9.0
|
|
590
|
+
*/
|
|
591
|
+
function runParserSync(parser, programName, args, options) {
|
|
592
|
+
return runParser(parser, programName, args, options);
|
|
593
|
+
}
|
|
594
|
+
/**
|
|
595
|
+
* Runs any command-line parser asynchronously with the given options.
|
|
596
|
+
*
|
|
597
|
+
* This function accepts parsers of any mode (sync or async) and always
|
|
598
|
+
* returns a Promise. Use this when working with parsers that may contain
|
|
599
|
+
* async value parsers.
|
|
600
|
+
*
|
|
601
|
+
* @template TParser The parser type being executed.
|
|
602
|
+
* @template THelp The return type of the onHelp callback.
|
|
603
|
+
* @template TError The return type of the onError callback.
|
|
604
|
+
* @param parser The command-line parser to execute.
|
|
605
|
+
* @param programName The name of the program for help messages.
|
|
606
|
+
* @param args The command-line arguments to parse.
|
|
607
|
+
* @param options Configuration options for customizing behavior.
|
|
608
|
+
* @returns A Promise of the parsed result if successful.
|
|
609
|
+
* @since 0.9.0
|
|
610
|
+
*/
|
|
611
|
+
function runParserAsync(parser, programName, args, options) {
|
|
612
|
+
const result = runParser(parser, programName, args, options);
|
|
613
|
+
return Promise.resolve(result);
|
|
614
|
+
}
|
|
615
|
+
/**
|
|
564
616
|
* @deprecated Use `runParser()` instead. This export will be removed in
|
|
565
617
|
* a future major version. The name `run` conflicts with
|
|
566
618
|
* `@optique/run`'s `run()` function, causing IDE autocomplete
|
|
@@ -590,4 +642,6 @@ function indentLines(text$1, indent) {
|
|
|
590
642
|
exports.RunError = RunError;
|
|
591
643
|
exports.RunParserError = RunParserError;
|
|
592
644
|
exports.run = run;
|
|
593
|
-
exports.runParser = runParser;
|
|
645
|
+
exports.runParser = runParser;
|
|
646
|
+
exports.runParserAsync = runParserAsync;
|
|
647
|
+
exports.runParserSync = runParserSync;
|
package/dist/facade.d.cts
CHANGED
|
@@ -217,6 +217,42 @@ interface RunOptions<THelp, TError> {
|
|
|
217
217
|
declare function runParser<TParser extends Parser<"sync", unknown, unknown>, THelp = void, TError = never>(parser: TParser, programName: string, args: readonly string[], options?: RunOptions<THelp, TError>): InferValue<TParser>;
|
|
218
218
|
declare function runParser<TParser extends Parser<"async", unknown, unknown>, THelp = void, TError = never>(parser: TParser, programName: string, args: readonly string[], options?: RunOptions<THelp, TError>): Promise<InferValue<TParser>>;
|
|
219
219
|
declare function runParser<TParser extends Parser<Mode, unknown, unknown>, THelp = void, TError = never>(parser: TParser, programName: string, args: readonly string[], options?: RunOptions<THelp, TError>): ModeValue<InferMode<TParser>, InferValue<TParser>>;
|
|
220
|
+
/**
|
|
221
|
+
* Runs a synchronous command-line parser with the given options.
|
|
222
|
+
*
|
|
223
|
+
* This is a type-safe version of {@link runParser} that only accepts sync
|
|
224
|
+
* parsers. Use this when you know your parser is sync-only to get direct
|
|
225
|
+
* return values without Promise wrappers.
|
|
226
|
+
*
|
|
227
|
+
* @template TParser The sync parser type being executed.
|
|
228
|
+
* @template THelp The return type of the onHelp callback.
|
|
229
|
+
* @template TError The return type of the onError callback.
|
|
230
|
+
* @param parser The synchronous command-line parser to execute.
|
|
231
|
+
* @param programName The name of the program for help messages.
|
|
232
|
+
* @param args The command-line arguments to parse.
|
|
233
|
+
* @param options Configuration options for customizing behavior.
|
|
234
|
+
* @returns The parsed result if successful.
|
|
235
|
+
* @since 0.9.0
|
|
236
|
+
*/
|
|
237
|
+
declare function runParserSync<TParser extends Parser<"sync", unknown, unknown>, THelp = void, TError = never>(parser: TParser, programName: string, args: readonly string[], options?: RunOptions<THelp, TError>): InferValue<TParser>;
|
|
238
|
+
/**
|
|
239
|
+
* Runs any command-line parser asynchronously with the given options.
|
|
240
|
+
*
|
|
241
|
+
* This function accepts parsers of any mode (sync or async) and always
|
|
242
|
+
* returns a Promise. Use this when working with parsers that may contain
|
|
243
|
+
* async value parsers.
|
|
244
|
+
*
|
|
245
|
+
* @template TParser The parser type being executed.
|
|
246
|
+
* @template THelp The return type of the onHelp callback.
|
|
247
|
+
* @template TError The return type of the onError callback.
|
|
248
|
+
* @param parser The command-line parser to execute.
|
|
249
|
+
* @param programName The name of the program for help messages.
|
|
250
|
+
* @param args The command-line arguments to parse.
|
|
251
|
+
* @param options Configuration options for customizing behavior.
|
|
252
|
+
* @returns A Promise of the parsed result if successful.
|
|
253
|
+
* @since 0.9.0
|
|
254
|
+
*/
|
|
255
|
+
declare function runParserAsync<TParser extends Parser<Mode, unknown, unknown>, THelp = void, TError = never>(parser: TParser, programName: string, args: readonly string[], options?: RunOptions<THelp, TError>): Promise<InferValue<TParser>>;
|
|
220
256
|
/**
|
|
221
257
|
* @deprecated Use `runParser()` instead. This export will be removed in
|
|
222
258
|
* a future major version. The name `run` conflicts with
|
|
@@ -237,4 +273,4 @@ declare class RunParserError extends Error {
|
|
|
237
273
|
*/
|
|
238
274
|
declare const RunError: typeof RunParserError;
|
|
239
275
|
//#endregion
|
|
240
|
-
export { RunError, RunOptions, RunParserError, run, runParser };
|
|
276
|
+
export { RunError, RunOptions, RunParserError, run, runParser, runParserAsync, runParserSync };
|
package/dist/facade.d.ts
CHANGED
|
@@ -217,6 +217,42 @@ interface RunOptions<THelp, TError> {
|
|
|
217
217
|
declare function runParser<TParser extends Parser<"sync", unknown, unknown>, THelp = void, TError = never>(parser: TParser, programName: string, args: readonly string[], options?: RunOptions<THelp, TError>): InferValue<TParser>;
|
|
218
218
|
declare function runParser<TParser extends Parser<"async", unknown, unknown>, THelp = void, TError = never>(parser: TParser, programName: string, args: readonly string[], options?: RunOptions<THelp, TError>): Promise<InferValue<TParser>>;
|
|
219
219
|
declare function runParser<TParser extends Parser<Mode, unknown, unknown>, THelp = void, TError = never>(parser: TParser, programName: string, args: readonly string[], options?: RunOptions<THelp, TError>): ModeValue<InferMode<TParser>, InferValue<TParser>>;
|
|
220
|
+
/**
|
|
221
|
+
* Runs a synchronous command-line parser with the given options.
|
|
222
|
+
*
|
|
223
|
+
* This is a type-safe version of {@link runParser} that only accepts sync
|
|
224
|
+
* parsers. Use this when you know your parser is sync-only to get direct
|
|
225
|
+
* return values without Promise wrappers.
|
|
226
|
+
*
|
|
227
|
+
* @template TParser The sync parser type being executed.
|
|
228
|
+
* @template THelp The return type of the onHelp callback.
|
|
229
|
+
* @template TError The return type of the onError callback.
|
|
230
|
+
* @param parser The synchronous command-line parser to execute.
|
|
231
|
+
* @param programName The name of the program for help messages.
|
|
232
|
+
* @param args The command-line arguments to parse.
|
|
233
|
+
* @param options Configuration options for customizing behavior.
|
|
234
|
+
* @returns The parsed result if successful.
|
|
235
|
+
* @since 0.9.0
|
|
236
|
+
*/
|
|
237
|
+
declare function runParserSync<TParser extends Parser<"sync", unknown, unknown>, THelp = void, TError = never>(parser: TParser, programName: string, args: readonly string[], options?: RunOptions<THelp, TError>): InferValue<TParser>;
|
|
238
|
+
/**
|
|
239
|
+
* Runs any command-line parser asynchronously with the given options.
|
|
240
|
+
*
|
|
241
|
+
* This function accepts parsers of any mode (sync or async) and always
|
|
242
|
+
* returns a Promise. Use this when working with parsers that may contain
|
|
243
|
+
* async value parsers.
|
|
244
|
+
*
|
|
245
|
+
* @template TParser The parser type being executed.
|
|
246
|
+
* @template THelp The return type of the onHelp callback.
|
|
247
|
+
* @template TError The return type of the onError callback.
|
|
248
|
+
* @param parser The command-line parser to execute.
|
|
249
|
+
* @param programName The name of the program for help messages.
|
|
250
|
+
* @param args The command-line arguments to parse.
|
|
251
|
+
* @param options Configuration options for customizing behavior.
|
|
252
|
+
* @returns A Promise of the parsed result if successful.
|
|
253
|
+
* @since 0.9.0
|
|
254
|
+
*/
|
|
255
|
+
declare function runParserAsync<TParser extends Parser<Mode, unknown, unknown>, THelp = void, TError = never>(parser: TParser, programName: string, args: readonly string[], options?: RunOptions<THelp, TError>): Promise<InferValue<TParser>>;
|
|
220
256
|
/**
|
|
221
257
|
* @deprecated Use `runParser()` instead. This export will be removed in
|
|
222
258
|
* a future major version. The name `run` conflicts with
|
|
@@ -237,4 +273,4 @@ declare class RunParserError extends Error {
|
|
|
237
273
|
*/
|
|
238
274
|
declare const RunError: typeof RunParserError;
|
|
239
275
|
//#endregion
|
|
240
|
-
export { RunError, RunOptions, RunParserError, run, runParser };
|
|
276
|
+
export { RunError, RunOptions, RunParserError, run, runParser, runParserAsync, runParserSync };
|
package/dist/facade.js
CHANGED
|
@@ -404,7 +404,7 @@ function handleCompletion(completionArgs, programName, parser, completionParser,
|
|
|
404
404
|
return callOnCompletion(0);
|
|
405
405
|
}
|
|
406
406
|
function runParser(parser, programName, args, options = {}) {
|
|
407
|
-
|
|
407
|
+
const { colors, maxWidth, showDefault, aboveError = "usage", onError = () => {
|
|
408
408
|
throw new RunParserError("Failed to parse command line arguments.");
|
|
409
409
|
}, stderr = console.error, stdout = console.log, brief, description, footer } = options;
|
|
410
410
|
const helpMode = options.help?.mode ?? "option";
|
|
@@ -500,31 +500,35 @@ function runParser(parser, programName, args, options = {}) {
|
|
|
500
500
|
else if (commandParsers.length === 2) helpGeneratorParser = longestMatch(commandParsers[0], commandParsers[1]);
|
|
501
501
|
else helpGeneratorParser = longestMatch(...commandParsers);
|
|
502
502
|
}
|
|
503
|
-
const
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
503
|
+
const displayHelp = (doc) => {
|
|
504
|
+
if (doc != null) {
|
|
505
|
+
const isMetaCommandHelp = (completionName === "singular" || completionName === "both" ? requestedCommand === "completion" : false) || (completionName === "plural" || completionName === "both" ? requestedCommand === "completions" : false) || requestedCommand === "help" || requestedCommand === "version";
|
|
506
|
+
const augmentedDoc = {
|
|
507
|
+
...doc,
|
|
508
|
+
brief: !isMetaCommandHelp ? brief ?? doc.brief : doc.brief,
|
|
509
|
+
description: !isMetaCommandHelp ? description ?? doc.description : doc.description,
|
|
510
|
+
footer: !isMetaCommandHelp ? footer ?? doc.footer : doc.footer
|
|
511
|
+
};
|
|
512
|
+
stdout(formatDocPage(programName, augmentedDoc, {
|
|
513
|
+
colors,
|
|
514
|
+
maxWidth,
|
|
515
|
+
showDefault
|
|
516
|
+
}));
|
|
517
|
+
}
|
|
518
|
+
try {
|
|
519
|
+
return onHelp(0);
|
|
520
|
+
} catch {
|
|
521
|
+
return onHelp();
|
|
522
|
+
}
|
|
523
|
+
};
|
|
524
|
+
const docOrPromise = getDocPage(helpGeneratorParser, classified.commands);
|
|
525
|
+
if (docOrPromise instanceof Promise) return docOrPromise.then(displayHelp);
|
|
526
|
+
return displayHelp(docOrPromise);
|
|
523
527
|
}
|
|
524
528
|
case "error": {
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
if (doc == null)
|
|
529
|
+
const displayError = (doc, currentAboveError) => {
|
|
530
|
+
let effectiveAboveError = currentAboveError;
|
|
531
|
+
if (effectiveAboveError === "help") if (doc == null) effectiveAboveError = "usage";
|
|
528
532
|
else {
|
|
529
533
|
const augmentedDoc = {
|
|
530
534
|
...doc,
|
|
@@ -538,18 +542,25 @@ function runParser(parser, programName, args, options = {}) {
|
|
|
538
542
|
showDefault
|
|
539
543
|
}));
|
|
540
544
|
}
|
|
545
|
+
if (effectiveAboveError === "usage") stderr(`Usage: ${indentLines(formatUsage(programName, augmentedParser.usage, {
|
|
546
|
+
colors,
|
|
547
|
+
maxWidth: maxWidth == null ? void 0 : maxWidth - 7,
|
|
548
|
+
expandCommands: true
|
|
549
|
+
}), 7)}`);
|
|
550
|
+
const errorMessage = formatMessage(classified.error, {
|
|
551
|
+
colors,
|
|
552
|
+
quotes: !colors
|
|
553
|
+
});
|
|
554
|
+
stderr(`Error: ${errorMessage}`);
|
|
555
|
+
return onError(1);
|
|
556
|
+
};
|
|
557
|
+
if (aboveError === "help") {
|
|
558
|
+
const parserForDoc = args.length < 1 ? augmentedParser : parser;
|
|
559
|
+
const docOrPromise = getDocPage(parserForDoc, args);
|
|
560
|
+
if (docOrPromise instanceof Promise) return docOrPromise.then((doc) => displayError(doc, aboveError));
|
|
561
|
+
return displayError(docOrPromise, aboveError);
|
|
541
562
|
}
|
|
542
|
-
|
|
543
|
-
colors,
|
|
544
|
-
maxWidth: maxWidth == null ? void 0 : maxWidth - 7,
|
|
545
|
-
expandCommands: true
|
|
546
|
-
}), 7)}`);
|
|
547
|
-
const errorMessage = formatMessage(classified.error, {
|
|
548
|
-
colors,
|
|
549
|
-
quotes: !colors
|
|
550
|
-
});
|
|
551
|
-
stderr(`Error: ${errorMessage}`);
|
|
552
|
-
return onError(1);
|
|
563
|
+
return displayError(void 0, aboveError);
|
|
553
564
|
}
|
|
554
565
|
default: throw new RunParserError("Unexpected parse result type");
|
|
555
566
|
}
|
|
@@ -561,6 +572,47 @@ function runParser(parser, programName, args, options = {}) {
|
|
|
561
572
|
}
|
|
562
573
|
}
|
|
563
574
|
/**
|
|
575
|
+
* Runs a synchronous command-line parser with the given options.
|
|
576
|
+
*
|
|
577
|
+
* This is a type-safe version of {@link runParser} that only accepts sync
|
|
578
|
+
* parsers. Use this when you know your parser is sync-only to get direct
|
|
579
|
+
* return values without Promise wrappers.
|
|
580
|
+
*
|
|
581
|
+
* @template TParser The sync parser type being executed.
|
|
582
|
+
* @template THelp The return type of the onHelp callback.
|
|
583
|
+
* @template TError The return type of the onError callback.
|
|
584
|
+
* @param parser The synchronous command-line parser to execute.
|
|
585
|
+
* @param programName The name of the program for help messages.
|
|
586
|
+
* @param args The command-line arguments to parse.
|
|
587
|
+
* @param options Configuration options for customizing behavior.
|
|
588
|
+
* @returns The parsed result if successful.
|
|
589
|
+
* @since 0.9.0
|
|
590
|
+
*/
|
|
591
|
+
function runParserSync(parser, programName, args, options) {
|
|
592
|
+
return runParser(parser, programName, args, options);
|
|
593
|
+
}
|
|
594
|
+
/**
|
|
595
|
+
* Runs any command-line parser asynchronously with the given options.
|
|
596
|
+
*
|
|
597
|
+
* This function accepts parsers of any mode (sync or async) and always
|
|
598
|
+
* returns a Promise. Use this when working with parsers that may contain
|
|
599
|
+
* async value parsers.
|
|
600
|
+
*
|
|
601
|
+
* @template TParser The parser type being executed.
|
|
602
|
+
* @template THelp The return type of the onHelp callback.
|
|
603
|
+
* @template TError The return type of the onError callback.
|
|
604
|
+
* @param parser The command-line parser to execute.
|
|
605
|
+
* @param programName The name of the program for help messages.
|
|
606
|
+
* @param args The command-line arguments to parse.
|
|
607
|
+
* @param options Configuration options for customizing behavior.
|
|
608
|
+
* @returns A Promise of the parsed result if successful.
|
|
609
|
+
* @since 0.9.0
|
|
610
|
+
*/
|
|
611
|
+
function runParserAsync(parser, programName, args, options) {
|
|
612
|
+
const result = runParser(parser, programName, args, options);
|
|
613
|
+
return Promise.resolve(result);
|
|
614
|
+
}
|
|
615
|
+
/**
|
|
564
616
|
* @deprecated Use `runParser()` instead. This export will be removed in
|
|
565
617
|
* a future major version. The name `run` conflicts with
|
|
566
618
|
* `@optique/run`'s `run()` function, causing IDE autocomplete
|
|
@@ -587,4 +639,4 @@ function indentLines(text$1, indent) {
|
|
|
587
639
|
}
|
|
588
640
|
|
|
589
641
|
//#endregion
|
|
590
|
-
export { RunError, RunParserError, run, runParser };
|
|
642
|
+
export { RunError, RunParserError, run, runParser, runParserAsync, runParserSync };
|
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;
|
|
@@ -61,6 +63,8 @@ exports.passThrough = require_primitives.passThrough;
|
|
|
61
63
|
exports.pwsh = require_completion.pwsh;
|
|
62
64
|
exports.run = require_facade.run;
|
|
63
65
|
exports.runParser = require_facade.runParser;
|
|
66
|
+
exports.runParserAsync = require_facade.runParserAsync;
|
|
67
|
+
exports.runParserSync = require_facade.runParserSync;
|
|
64
68
|
exports.string = require_valueparser.string;
|
|
65
69
|
exports.suggest = require_parser.suggest;
|
|
66
70
|
exports.suggestAsync = require_parser.suggestAsync;
|
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
|
-
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 };
|
|
11
|
+
import { RunError, RunOptions, RunParserError, run, runParser, runParserAsync, runParserSync } 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, 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, runParserAsync, runParserSync, 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
|
-
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 };
|
|
11
|
+
import { RunError, RunOptions, RunParserError, run, runParser, runParserAsync, runParserSync } 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, 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, runParserAsync, runParserSync, 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";
|
|
11
|
-
import { RunError, RunParserError, run, runParser } from "./facade.js";
|
|
10
|
+
import { getDocPage, getDocPageAsync, getDocPageSync, parse, parseAsync, parseSync, suggest, suggestAsync, suggestSync } from "./parser.js";
|
|
11
|
+
import { RunError, RunParserError, run, runParser, runParserAsync, runParserSync } 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, runParserAsync, runParserSync, 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 };
|