@optique/core 0.5.0-dev.79 → 0.5.0-dev.80
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/constructs.cjs +754 -0
- package/dist/constructs.d.cts +1100 -0
- package/dist/constructs.d.ts +1100 -0
- package/dist/constructs.js +748 -0
- package/dist/facade.cjs +29 -26
- package/dist/facade.js +4 -1
- package/dist/index.cjs +20 -17
- package/dist/index.d.cts +5 -2
- package/dist/index.d.ts +5 -2
- package/dist/index.js +4 -1
- package/dist/modifiers.cjs +300 -0
- package/dist/modifiers.d.cts +192 -0
- package/dist/modifiers.d.ts +192 -0
- package/dist/modifiers.js +296 -0
- package/dist/parser.cjs +20 -1581
- package/dist/parser.d.cts +6 -1279
- package/dist/parser.d.ts +6 -1279
- package/dist/parser.js +4 -1565
- package/dist/primitives.cjs +595 -0
- package/dist/primitives.d.cts +274 -0
- package/dist/primitives.d.ts +274 -0
- package/dist/primitives.js +591 -0
- package/dist/valueparser.cjs +28 -28
- package/dist/valueparser.d.cts +144 -0
- package/dist/valueparser.d.ts +144 -0
- package/dist/valueparser.js +28 -28
- package/package.json +25 -1
package/dist/facade.cjs
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
const require_message = require('./message.cjs');
|
|
2
|
+
const require_constructs = require('./constructs.cjs');
|
|
2
3
|
const require_usage = require('./usage.cjs');
|
|
3
4
|
const require_doc = require('./doc.cjs');
|
|
5
|
+
const require_modifiers = require('./modifiers.cjs');
|
|
4
6
|
const require_valueparser = require('./valueparser.cjs');
|
|
7
|
+
const require_primitives = require('./primitives.cjs');
|
|
5
8
|
const require_parser = require('./parser.cjs');
|
|
6
9
|
|
|
7
10
|
//#region src/facade.ts
|
|
@@ -9,16 +12,16 @@ const require_parser = require('./parser.cjs');
|
|
|
9
12
|
* Creates help parsers based on the specified mode.
|
|
10
13
|
*/
|
|
11
14
|
function createHelpParser(mode) {
|
|
12
|
-
const helpCommand =
|
|
13
|
-
const helpOption =
|
|
14
|
-
const _contextualHelpParser =
|
|
15
|
-
help:
|
|
16
|
-
version:
|
|
17
|
-
commands:
|
|
15
|
+
const helpCommand = require_primitives.command("help", require_modifiers.multiple(require_primitives.argument(require_valueparser.string({ metavar: "COMMAND" }))), { description: require_message.message`Show help information.` });
|
|
16
|
+
const helpOption = require_primitives.flag("--help", { description: require_message.message`Show help information.` });
|
|
17
|
+
const _contextualHelpParser = require_constructs.object({
|
|
18
|
+
help: require_primitives.constant(true),
|
|
19
|
+
version: require_primitives.constant(false),
|
|
20
|
+
commands: require_modifiers.multiple(require_primitives.argument(require_valueparser.string({
|
|
18
21
|
metavar: "COMMAND",
|
|
19
22
|
pattern: /^[^-].*$/
|
|
20
23
|
}))),
|
|
21
|
-
__help:
|
|
24
|
+
__help: require_primitives.flag("--help")
|
|
22
25
|
});
|
|
23
26
|
switch (mode) {
|
|
24
27
|
case "command": return {
|
|
@@ -42,8 +45,8 @@ function createHelpParser(mode) {
|
|
|
42
45
|
* Creates version parsers based on the specified mode.
|
|
43
46
|
*/
|
|
44
47
|
function createVersionParser(mode) {
|
|
45
|
-
const versionCommand =
|
|
46
|
-
const versionOption =
|
|
48
|
+
const versionCommand = require_primitives.command("version", require_constructs.object({}), { description: require_message.message`Show version information.` });
|
|
49
|
+
const versionOption = require_primitives.flag("--version", { description: require_message.message`Show version information.` });
|
|
47
50
|
switch (mode) {
|
|
48
51
|
case "command": return {
|
|
49
52
|
versionCommand,
|
|
@@ -191,26 +194,26 @@ function combineWithHelpVersion(originalParser, helpParsers, versionParsers) {
|
|
|
191
194
|
};
|
|
192
195
|
parsers.push(lenientVersionParser);
|
|
193
196
|
}
|
|
194
|
-
if (versionParsers.versionCommand) parsers.push(
|
|
195
|
-
help:
|
|
196
|
-
version:
|
|
197
|
+
if (versionParsers.versionCommand) parsers.push(require_constructs.object({
|
|
198
|
+
help: require_primitives.constant(false),
|
|
199
|
+
version: require_primitives.constant(true),
|
|
197
200
|
result: versionParsers.versionCommand,
|
|
198
|
-
helpFlag: helpParsers.helpOption ?
|
|
201
|
+
helpFlag: helpParsers.helpOption ? require_modifiers.optional(helpParsers.helpOption) : require_primitives.constant(false)
|
|
199
202
|
}));
|
|
200
|
-
if (helpParsers.helpCommand) parsers.push(
|
|
201
|
-
help:
|
|
202
|
-
version:
|
|
203
|
+
if (helpParsers.helpCommand) parsers.push(require_constructs.object({
|
|
204
|
+
help: require_primitives.constant(true),
|
|
205
|
+
version: require_primitives.constant(false),
|
|
203
206
|
commands: helpParsers.helpCommand
|
|
204
207
|
}));
|
|
205
208
|
if (helpParsers.contextualHelpParser) parsers.push(helpParsers.contextualHelpParser);
|
|
206
|
-
parsers.push(
|
|
207
|
-
help:
|
|
208
|
-
version:
|
|
209
|
+
parsers.push(require_constructs.object({
|
|
210
|
+
help: require_primitives.constant(false),
|
|
211
|
+
version: require_primitives.constant(false),
|
|
209
212
|
result: originalParser
|
|
210
213
|
}));
|
|
211
214
|
if (parsers.length === 1) return parsers[0];
|
|
212
|
-
else if (parsers.length === 2) return
|
|
213
|
-
else return
|
|
215
|
+
else if (parsers.length === 2) return require_constructs.longestMatch(parsers[0], parsers[1]);
|
|
216
|
+
else return require_constructs.longestMatch(...parsers);
|
|
214
217
|
}
|
|
215
218
|
/**
|
|
216
219
|
* Classifies the parsing result into a discriminated union for cleaner handling.
|
|
@@ -320,17 +323,17 @@ function run(parser, programName, args, options = {}) {
|
|
|
320
323
|
if (helpAsCommand && versionAsCommand) {
|
|
321
324
|
const tempHelpParsers = createHelpParser(help);
|
|
322
325
|
const tempVersionParsers = createVersionParser(version);
|
|
323
|
-
if (tempHelpParsers.helpCommand && tempVersionParsers.versionCommand) helpGeneratorParser =
|
|
324
|
-
else if (tempHelpParsers.helpCommand) helpGeneratorParser =
|
|
325
|
-
else if (tempVersionParsers.versionCommand) helpGeneratorParser =
|
|
326
|
+
if (tempHelpParsers.helpCommand && tempVersionParsers.versionCommand) helpGeneratorParser = require_constructs.longestMatch(parser, tempHelpParsers.helpCommand, tempVersionParsers.versionCommand);
|
|
327
|
+
else if (tempHelpParsers.helpCommand) helpGeneratorParser = require_constructs.longestMatch(parser, tempHelpParsers.helpCommand);
|
|
328
|
+
else if (tempVersionParsers.versionCommand) helpGeneratorParser = require_constructs.longestMatch(parser, tempVersionParsers.versionCommand);
|
|
326
329
|
else helpGeneratorParser = parser;
|
|
327
330
|
} else if (helpAsCommand) {
|
|
328
331
|
const tempHelpParsers = createHelpParser(help);
|
|
329
|
-
if (tempHelpParsers.helpCommand) helpGeneratorParser =
|
|
332
|
+
if (tempHelpParsers.helpCommand) helpGeneratorParser = require_constructs.longestMatch(parser, tempHelpParsers.helpCommand);
|
|
330
333
|
else helpGeneratorParser = parser;
|
|
331
334
|
} else if (versionAsCommand) {
|
|
332
335
|
const tempVersionParsers = createVersionParser(version);
|
|
333
|
-
if (tempVersionParsers.versionCommand) helpGeneratorParser =
|
|
336
|
+
if (tempVersionParsers.versionCommand) helpGeneratorParser = require_constructs.longestMatch(parser, tempVersionParsers.versionCommand);
|
|
334
337
|
else helpGeneratorParser = parser;
|
|
335
338
|
} else helpGeneratorParser = parser;
|
|
336
339
|
const doc = require_parser.getDocPage(helpGeneratorParser, classified.commands);
|
package/dist/facade.js
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import { formatMessage, message } from "./message.js";
|
|
2
|
+
import { longestMatch, object } from "./constructs.js";
|
|
2
3
|
import { formatUsage } from "./usage.js";
|
|
3
4
|
import { formatDocPage } from "./doc.js";
|
|
5
|
+
import { multiple, optional } from "./modifiers.js";
|
|
4
6
|
import { string } from "./valueparser.js";
|
|
5
|
-
import { argument, command, constant, flag
|
|
7
|
+
import { argument, command, constant, flag } from "./primitives.js";
|
|
8
|
+
import { getDocPage, parse } from "./parser.js";
|
|
6
9
|
|
|
7
10
|
//#region src/facade.ts
|
|
8
11
|
/**
|
package/dist/index.cjs
CHANGED
|
@@ -1,49 +1,52 @@
|
|
|
1
1
|
const require_message = require('./message.cjs');
|
|
2
|
+
const require_constructs = require('./constructs.cjs');
|
|
2
3
|
const require_usage = require('./usage.cjs');
|
|
3
4
|
const require_doc = require('./doc.cjs');
|
|
5
|
+
const require_modifiers = require('./modifiers.cjs');
|
|
4
6
|
const require_valueparser = require('./valueparser.cjs');
|
|
7
|
+
const require_primitives = require('./primitives.cjs');
|
|
5
8
|
const require_parser = require('./parser.cjs');
|
|
6
9
|
const require_facade = require('./facade.cjs');
|
|
7
10
|
|
|
8
11
|
exports.RunError = require_facade.RunError;
|
|
9
|
-
exports.WithDefaultError =
|
|
10
|
-
exports.argument =
|
|
12
|
+
exports.WithDefaultError = require_modifiers.WithDefaultError;
|
|
13
|
+
exports.argument = require_primitives.argument;
|
|
11
14
|
exports.choice = require_valueparser.choice;
|
|
12
|
-
exports.command =
|
|
13
|
-
exports.concat =
|
|
14
|
-
exports.constant =
|
|
15
|
+
exports.command = require_primitives.command;
|
|
16
|
+
exports.concat = require_constructs.concat;
|
|
17
|
+
exports.constant = require_primitives.constant;
|
|
15
18
|
exports.envVar = require_message.envVar;
|
|
16
|
-
exports.flag =
|
|
19
|
+
exports.flag = require_primitives.flag;
|
|
17
20
|
exports.float = require_valueparser.float;
|
|
18
21
|
exports.formatDocPage = require_doc.formatDocPage;
|
|
19
22
|
exports.formatMessage = require_message.formatMessage;
|
|
20
23
|
exports.formatUsage = require_usage.formatUsage;
|
|
21
24
|
exports.formatUsageTerm = require_usage.formatUsageTerm;
|
|
22
25
|
exports.getDocPage = require_parser.getDocPage;
|
|
23
|
-
exports.group =
|
|
26
|
+
exports.group = require_constructs.group;
|
|
24
27
|
exports.integer = require_valueparser.integer;
|
|
25
28
|
exports.isValueParser = require_valueparser.isValueParser;
|
|
26
29
|
exports.locale = require_valueparser.locale;
|
|
27
|
-
exports.longestMatch =
|
|
28
|
-
exports.map =
|
|
29
|
-
exports.merge =
|
|
30
|
+
exports.longestMatch = require_constructs.longestMatch;
|
|
31
|
+
exports.map = require_modifiers.map;
|
|
32
|
+
exports.merge = require_constructs.merge;
|
|
30
33
|
exports.message = require_message.message;
|
|
31
34
|
exports.metavar = require_message.metavar;
|
|
32
|
-
exports.multiple =
|
|
35
|
+
exports.multiple = require_modifiers.multiple;
|
|
33
36
|
exports.normalizeUsage = require_usage.normalizeUsage;
|
|
34
|
-
exports.object =
|
|
35
|
-
exports.option =
|
|
37
|
+
exports.object = require_constructs.object;
|
|
38
|
+
exports.option = require_primitives.option;
|
|
36
39
|
exports.optionName = require_message.optionName;
|
|
37
40
|
exports.optionNames = require_message.optionNames;
|
|
38
|
-
exports.optional =
|
|
39
|
-
exports.or =
|
|
41
|
+
exports.optional = require_modifiers.optional;
|
|
42
|
+
exports.or = require_constructs.or;
|
|
40
43
|
exports.parse = require_parser.parse;
|
|
41
44
|
exports.run = require_facade.run;
|
|
42
45
|
exports.string = require_valueparser.string;
|
|
43
46
|
exports.text = require_message.text;
|
|
44
|
-
exports.tuple =
|
|
47
|
+
exports.tuple = require_constructs.tuple;
|
|
45
48
|
exports.url = require_valueparser.url;
|
|
46
49
|
exports.uuid = require_valueparser.uuid;
|
|
47
50
|
exports.value = require_message.value;
|
|
48
51
|
exports.values = require_message.values;
|
|
49
|
-
exports.withDefault =
|
|
52
|
+
exports.withDefault = require_modifiers.withDefault;
|
package/dist/index.d.cts
CHANGED
|
@@ -2,6 +2,9 @@ import { Message, MessageFormatOptions, MessageTerm, envVar, formatMessage, mess
|
|
|
2
2
|
import { OptionName, Usage, UsageFormatOptions, UsageTerm, UsageTermFormatOptions, formatUsage, formatUsageTerm, normalizeUsage } from "./usage.cjs";
|
|
3
3
|
import { DocEntry, DocFragment, DocFragments, DocPage, DocPageFormatOptions, DocSection, ShowDefaultOptions, formatDocPage } from "./doc.cjs";
|
|
4
4
|
import { ChoiceOptions, FloatOptions, IntegerOptionsBigInt, IntegerOptionsNumber, LocaleOptions, StringOptions, UrlOptions, Uuid, UuidOptions, ValueParser, ValueParserResult, choice, float, integer, isValueParser, locale, string, url, uuid } from "./valueparser.cjs";
|
|
5
|
-
import {
|
|
5
|
+
import { MultipleErrorOptions, MultipleOptions, WithDefaultError, WithDefaultOptions, map, multiple, optional, withDefault } from "./modifiers.cjs";
|
|
6
|
+
import { ArgumentErrorOptions, ArgumentOptions, CommandErrorOptions, CommandOptions, FlagErrorOptions, FlagOptions, OptionErrorOptions, OptionOptions, argument, command, constant, flag, option } from "./primitives.cjs";
|
|
7
|
+
import { DocState, InferValue, Parser, ParserContext, ParserResult, Result, getDocPage, parse } from "./parser.cjs";
|
|
8
|
+
import { LongestMatchErrorOptions, LongestMatchOptions, ObjectErrorOptions, ObjectOptions, OrErrorOptions, OrOptions, concat, group, longestMatch, merge, object, or, tuple } from "./constructs.cjs";
|
|
6
9
|
import { RunError, RunOptions, run } from "./facade.cjs";
|
|
7
|
-
export { ArgumentOptions, ChoiceOptions, CommandOptions, DocEntry, DocFragment, DocFragments, DocPage, DocPageFormatOptions, DocSection, DocState, FlagOptions, FloatOptions, InferValue, IntegerOptionsBigInt, IntegerOptionsNumber, LocaleOptions, Message, MessageFormatOptions, MessageTerm, MultipleOptions, OptionName, OptionOptions, Parser, ParserContext, ParserResult, Result, RunError, RunOptions, ShowDefaultOptions, StringOptions, UrlOptions, Usage, UsageFormatOptions, UsageTerm, UsageTermFormatOptions, Uuid, UuidOptions, ValueParser, ValueParserResult, WithDefaultError, WithDefaultOptions, argument, choice, command, concat, constant, envVar, flag, float, formatDocPage, formatMessage, formatUsage, formatUsageTerm, getDocPage, group, integer, isValueParser, locale, longestMatch, map, merge, message, metavar, multiple, normalizeUsage, object, option, optionName, optionNames, optional, or, parse, run, string, text, tuple, url, uuid, value, values, withDefault };
|
|
10
|
+
export { ArgumentErrorOptions, ArgumentOptions, ChoiceOptions, CommandErrorOptions, CommandOptions, DocEntry, DocFragment, DocFragments, DocPage, DocPageFormatOptions, DocSection, DocState, FlagErrorOptions, FlagOptions, FloatOptions, InferValue, IntegerOptionsBigInt, IntegerOptionsNumber, LocaleOptions, LongestMatchErrorOptions, LongestMatchOptions, Message, MessageFormatOptions, MessageTerm, MultipleErrorOptions, MultipleOptions, ObjectErrorOptions, ObjectOptions, OptionErrorOptions, OptionName, OptionOptions, OrErrorOptions, OrOptions, Parser, ParserContext, ParserResult, Result, RunError, RunOptions, ShowDefaultOptions, StringOptions, UrlOptions, Usage, UsageFormatOptions, UsageTerm, UsageTermFormatOptions, Uuid, UuidOptions, ValueParser, ValueParserResult, WithDefaultError, WithDefaultOptions, argument, choice, command, concat, constant, envVar, flag, float, formatDocPage, formatMessage, formatUsage, formatUsageTerm, getDocPage, group, integer, isValueParser, locale, longestMatch, map, merge, message, metavar, multiple, normalizeUsage, object, option, optionName, optionNames, optional, or, parse, run, string, text, tuple, url, uuid, value, values, withDefault };
|
package/dist/index.d.ts
CHANGED
|
@@ -2,6 +2,9 @@ import { Message, MessageFormatOptions, MessageTerm, envVar, formatMessage, mess
|
|
|
2
2
|
import { OptionName, Usage, UsageFormatOptions, UsageTerm, UsageTermFormatOptions, formatUsage, formatUsageTerm, normalizeUsage } from "./usage.js";
|
|
3
3
|
import { DocEntry, DocFragment, DocFragments, DocPage, DocPageFormatOptions, DocSection, ShowDefaultOptions, formatDocPage } from "./doc.js";
|
|
4
4
|
import { ChoiceOptions, FloatOptions, IntegerOptionsBigInt, IntegerOptionsNumber, LocaleOptions, StringOptions, UrlOptions, Uuid, UuidOptions, ValueParser, ValueParserResult, choice, float, integer, isValueParser, locale, string, url, uuid } from "./valueparser.js";
|
|
5
|
-
import {
|
|
5
|
+
import { MultipleErrorOptions, MultipleOptions, WithDefaultError, WithDefaultOptions, map, multiple, optional, withDefault } from "./modifiers.js";
|
|
6
|
+
import { ArgumentErrorOptions, ArgumentOptions, CommandErrorOptions, CommandOptions, FlagErrorOptions, FlagOptions, OptionErrorOptions, OptionOptions, argument, command, constant, flag, option } from "./primitives.js";
|
|
7
|
+
import { DocState, InferValue, Parser, ParserContext, ParserResult, Result, getDocPage, parse } from "./parser.js";
|
|
8
|
+
import { LongestMatchErrorOptions, LongestMatchOptions, ObjectErrorOptions, ObjectOptions, OrErrorOptions, OrOptions, concat, group, longestMatch, merge, object, or, tuple } from "./constructs.js";
|
|
6
9
|
import { RunError, RunOptions, run } from "./facade.js";
|
|
7
|
-
export { ArgumentOptions, ChoiceOptions, CommandOptions, DocEntry, DocFragment, DocFragments, DocPage, DocPageFormatOptions, DocSection, DocState, FlagOptions, FloatOptions, InferValue, IntegerOptionsBigInt, IntegerOptionsNumber, LocaleOptions, Message, MessageFormatOptions, MessageTerm, MultipleOptions, OptionName, OptionOptions, Parser, ParserContext, ParserResult, Result, RunError, RunOptions, ShowDefaultOptions, StringOptions, UrlOptions, Usage, UsageFormatOptions, UsageTerm, UsageTermFormatOptions, Uuid, UuidOptions, ValueParser, ValueParserResult, WithDefaultError, WithDefaultOptions, argument, choice, command, concat, constant, envVar, flag, float, formatDocPage, formatMessage, formatUsage, formatUsageTerm, getDocPage, group, integer, isValueParser, locale, longestMatch, map, merge, message, metavar, multiple, normalizeUsage, object, option, optionName, optionNames, optional, or, parse, run, string, text, tuple, url, uuid, value, values, withDefault };
|
|
10
|
+
export { ArgumentErrorOptions, ArgumentOptions, ChoiceOptions, CommandErrorOptions, CommandOptions, DocEntry, DocFragment, DocFragments, DocPage, DocPageFormatOptions, DocSection, DocState, FlagErrorOptions, FlagOptions, FloatOptions, InferValue, IntegerOptionsBigInt, IntegerOptionsNumber, LocaleOptions, LongestMatchErrorOptions, LongestMatchOptions, Message, MessageFormatOptions, MessageTerm, MultipleErrorOptions, MultipleOptions, ObjectErrorOptions, ObjectOptions, OptionErrorOptions, OptionName, OptionOptions, OrErrorOptions, OrOptions, Parser, ParserContext, ParserResult, Result, RunError, RunOptions, ShowDefaultOptions, StringOptions, UrlOptions, Usage, UsageFormatOptions, UsageTerm, UsageTermFormatOptions, Uuid, UuidOptions, ValueParser, ValueParserResult, WithDefaultError, WithDefaultOptions, argument, choice, command, concat, constant, envVar, flag, float, formatDocPage, formatMessage, formatUsage, formatUsageTerm, getDocPage, group, integer, isValueParser, locale, longestMatch, map, merge, message, metavar, multiple, normalizeUsage, object, option, optionName, optionNames, optional, or, parse, run, string, text, tuple, url, uuid, value, values, withDefault };
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import { envVar, formatMessage, message, metavar, optionName, optionNames, text, value, values } from "./message.js";
|
|
2
|
+
import { concat, group, longestMatch, merge, object, or, tuple } from "./constructs.js";
|
|
2
3
|
import { formatUsage, formatUsageTerm, normalizeUsage } from "./usage.js";
|
|
3
4
|
import { formatDocPage } from "./doc.js";
|
|
5
|
+
import { WithDefaultError, map, multiple, optional, withDefault } from "./modifiers.js";
|
|
4
6
|
import { choice, float, integer, isValueParser, locale, string, url, uuid } from "./valueparser.js";
|
|
5
|
-
import {
|
|
7
|
+
import { argument, command, constant, flag, option } from "./primitives.js";
|
|
8
|
+
import { getDocPage, parse } from "./parser.js";
|
|
6
9
|
import { RunError, run } from "./facade.js";
|
|
7
10
|
|
|
8
11
|
export { RunError, WithDefaultError, argument, choice, command, concat, constant, envVar, flag, float, formatDocPage, formatMessage, formatUsage, formatUsageTerm, getDocPage, group, integer, isValueParser, locale, longestMatch, map, merge, message, metavar, multiple, normalizeUsage, object, option, optionName, optionNames, optional, or, parse, run, string, text, tuple, url, uuid, value, values, withDefault };
|
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
const require_message = require('./message.cjs');
|
|
2
|
+
|
|
3
|
+
//#region src/modifiers.ts
|
|
4
|
+
/**
|
|
5
|
+
* Creates a parser that makes another parser optional, allowing it to succeed
|
|
6
|
+
* without consuming input if the wrapped parser fails to match.
|
|
7
|
+
* If the wrapped parser succeeds, this returns its value.
|
|
8
|
+
* If the wrapped parser fails, this returns `undefined` without consuming input.
|
|
9
|
+
* @template TValue The type of the value returned by the wrapped parser.
|
|
10
|
+
* @template TState The type of the state used by the wrapped parser.
|
|
11
|
+
* @param parser The {@link Parser} to make optional.
|
|
12
|
+
* @returns A {@link Parser} that produces either the result of the wrapped parser
|
|
13
|
+
* or `undefined` if the wrapped parser fails to match.
|
|
14
|
+
*/
|
|
15
|
+
function optional(parser) {
|
|
16
|
+
return {
|
|
17
|
+
$valueType: [],
|
|
18
|
+
$stateType: [],
|
|
19
|
+
priority: parser.priority,
|
|
20
|
+
usage: [{
|
|
21
|
+
type: "optional",
|
|
22
|
+
terms: parser.usage
|
|
23
|
+
}],
|
|
24
|
+
initialState: void 0,
|
|
25
|
+
parse(context) {
|
|
26
|
+
const result = parser.parse({
|
|
27
|
+
...context,
|
|
28
|
+
state: typeof context.state === "undefined" ? parser.initialState : context.state[0]
|
|
29
|
+
});
|
|
30
|
+
if (result.success) return {
|
|
31
|
+
success: true,
|
|
32
|
+
next: {
|
|
33
|
+
...result.next,
|
|
34
|
+
state: [result.next.state]
|
|
35
|
+
},
|
|
36
|
+
consumed: result.consumed
|
|
37
|
+
};
|
|
38
|
+
return result;
|
|
39
|
+
},
|
|
40
|
+
complete(state) {
|
|
41
|
+
if (typeof state === "undefined") return {
|
|
42
|
+
success: true,
|
|
43
|
+
value: void 0
|
|
44
|
+
};
|
|
45
|
+
return parser.complete(state[0]);
|
|
46
|
+
},
|
|
47
|
+
getDocFragments(state, defaultValue) {
|
|
48
|
+
const innerState = state.kind === "unavailable" ? { kind: "unavailable" } : state.state === void 0 ? { kind: "unavailable" } : {
|
|
49
|
+
kind: "available",
|
|
50
|
+
state: state.state[0]
|
|
51
|
+
};
|
|
52
|
+
return parser.getDocFragments(innerState, defaultValue);
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Error type for structured error messages in {@link withDefault} default value callbacks.
|
|
58
|
+
* Unlike regular errors that only support string messages, this error type accepts
|
|
59
|
+
* a {@link Message} object that supports rich formatting, colors, and structured content.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```typescript
|
|
63
|
+
* withDefault(option("--url", url()), () => {
|
|
64
|
+
* if (!process.env.INSTANCE_URL) {
|
|
65
|
+
* throw new WithDefaultError(
|
|
66
|
+
* message`Environment variable ${envVar("INSTANCE_URL")} is not set.`
|
|
67
|
+
* );
|
|
68
|
+
* }
|
|
69
|
+
* return new URL(process.env.INSTANCE_URL);
|
|
70
|
+
* })
|
|
71
|
+
* ```
|
|
72
|
+
*
|
|
73
|
+
* @since 0.5.0
|
|
74
|
+
*/
|
|
75
|
+
var WithDefaultError = class extends Error {
|
|
76
|
+
/**
|
|
77
|
+
* The structured message associated with this error.
|
|
78
|
+
*/
|
|
79
|
+
errorMessage;
|
|
80
|
+
/**
|
|
81
|
+
* Creates a new WithDefaultError with a structured message.
|
|
82
|
+
* @param message The structured {@link Message} describing the error.
|
|
83
|
+
*/
|
|
84
|
+
constructor(message$1) {
|
|
85
|
+
super(require_message.formatMessage(message$1));
|
|
86
|
+
this.errorMessage = message$1;
|
|
87
|
+
this.name = "WithDefaultError";
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
function withDefault(parser, defaultValue, options) {
|
|
91
|
+
return {
|
|
92
|
+
$valueType: [],
|
|
93
|
+
$stateType: [],
|
|
94
|
+
priority: parser.priority,
|
|
95
|
+
usage: [{
|
|
96
|
+
type: "optional",
|
|
97
|
+
terms: parser.usage
|
|
98
|
+
}],
|
|
99
|
+
initialState: void 0,
|
|
100
|
+
parse(context) {
|
|
101
|
+
const result = parser.parse({
|
|
102
|
+
...context,
|
|
103
|
+
state: typeof context.state === "undefined" ? parser.initialState : context.state[0]
|
|
104
|
+
});
|
|
105
|
+
if (result.success) return {
|
|
106
|
+
success: true,
|
|
107
|
+
next: {
|
|
108
|
+
...result.next,
|
|
109
|
+
state: [result.next.state]
|
|
110
|
+
},
|
|
111
|
+
consumed: result.consumed
|
|
112
|
+
};
|
|
113
|
+
return result;
|
|
114
|
+
},
|
|
115
|
+
complete(state) {
|
|
116
|
+
if (typeof state === "undefined") try {
|
|
117
|
+
const value = typeof defaultValue === "function" ? defaultValue() : defaultValue;
|
|
118
|
+
return {
|
|
119
|
+
success: true,
|
|
120
|
+
value
|
|
121
|
+
};
|
|
122
|
+
} catch (error) {
|
|
123
|
+
return {
|
|
124
|
+
success: false,
|
|
125
|
+
error: error instanceof WithDefaultError ? error.errorMessage : require_message.message`${require_message.text(String(error))}`
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
return parser.complete(state[0]);
|
|
129
|
+
},
|
|
130
|
+
getDocFragments(state, upperDefaultValue) {
|
|
131
|
+
const innerState = state.kind === "unavailable" ? { kind: "unavailable" } : state.state === void 0 ? { kind: "unavailable" } : {
|
|
132
|
+
kind: "available",
|
|
133
|
+
state: state.state[0]
|
|
134
|
+
};
|
|
135
|
+
const actualDefaultValue = upperDefaultValue != null ? upperDefaultValue : typeof defaultValue === "function" ? defaultValue() : defaultValue;
|
|
136
|
+
const fragments = parser.getDocFragments(innerState, actualDefaultValue);
|
|
137
|
+
if (options?.message) {
|
|
138
|
+
const modifiedFragments = fragments.fragments.map((fragment) => {
|
|
139
|
+
if (fragment.type === "entry") return {
|
|
140
|
+
...fragment,
|
|
141
|
+
default: options.message
|
|
142
|
+
};
|
|
143
|
+
return fragment;
|
|
144
|
+
});
|
|
145
|
+
return {
|
|
146
|
+
...fragments,
|
|
147
|
+
fragments: modifiedFragments
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
return fragments;
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Creates a parser that transforms the result value of another parser using
|
|
156
|
+
* a mapping function. This enables value transformation while preserving
|
|
157
|
+
* the original parser's parsing logic and state management.
|
|
158
|
+
*
|
|
159
|
+
* The `map()` function is useful for:
|
|
160
|
+
* - Converting parsed values to different types
|
|
161
|
+
* - Applying transformations like string formatting or boolean inversion
|
|
162
|
+
* - Computing derived values from parsed input
|
|
163
|
+
* - Creating reusable transformations that can be applied to any parser
|
|
164
|
+
*
|
|
165
|
+
* @template T The type of the value produced by the original parser.
|
|
166
|
+
* @template U The type of the value produced by the mapping function.
|
|
167
|
+
* @template TState The type of the state used by the original parser.
|
|
168
|
+
* @param parser The {@link Parser} whose result will be transformed.
|
|
169
|
+
* @param transform A function that transforms the parsed value from type T to type U.
|
|
170
|
+
* @returns A {@link Parser} that produces the transformed value of type U
|
|
171
|
+
* while preserving the original parser's state type and parsing behavior.
|
|
172
|
+
*
|
|
173
|
+
* @example
|
|
174
|
+
* ```typescript
|
|
175
|
+
* // Transform boolean flag to its inverse
|
|
176
|
+
* const parser = object({
|
|
177
|
+
* disallow: map(option("--allow"), b => !b)
|
|
178
|
+
* });
|
|
179
|
+
*
|
|
180
|
+
* // Transform string to uppercase
|
|
181
|
+
* const upperParser = map(argument(string()), s => s.toUpperCase());
|
|
182
|
+
*
|
|
183
|
+
* // Transform number to formatted string
|
|
184
|
+
* const prefixedParser = map(option("-n", integer()), n => `value: ${n}`);
|
|
185
|
+
* ```
|
|
186
|
+
*/
|
|
187
|
+
function map(parser, transform) {
|
|
188
|
+
return {
|
|
189
|
+
$valueType: [],
|
|
190
|
+
$stateType: parser.$stateType,
|
|
191
|
+
priority: parser.priority,
|
|
192
|
+
usage: parser.usage,
|
|
193
|
+
initialState: parser.initialState,
|
|
194
|
+
parse: parser.parse.bind(parser),
|
|
195
|
+
complete(state) {
|
|
196
|
+
const result = parser.complete(state);
|
|
197
|
+
if (result.success) return {
|
|
198
|
+
success: true,
|
|
199
|
+
value: transform(result.value)
|
|
200
|
+
};
|
|
201
|
+
return result;
|
|
202
|
+
},
|
|
203
|
+
getDocFragments(state, _defaultValue) {
|
|
204
|
+
return parser.getDocFragments(state, void 0);
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Creates a parser that allows multiple occurrences of a given parser.
|
|
210
|
+
* This parser can be used to parse multiple values of the same type,
|
|
211
|
+
* such as multiple command-line arguments or options.
|
|
212
|
+
* @template TValue The type of the value that the parser produces.
|
|
213
|
+
* @template TState The type of the state used by the parser.
|
|
214
|
+
* @param parser The {@link Parser} to apply multiple times.
|
|
215
|
+
* @param options Optional configuration for the parser,
|
|
216
|
+
* allowing you to specify the minimum and maximum number of
|
|
217
|
+
* occurrences allowed.
|
|
218
|
+
* @returns A {@link Parser} that produces an array of values
|
|
219
|
+
* of type {@link TValue} and an array of states
|
|
220
|
+
* of type {@link TState}.
|
|
221
|
+
*/
|
|
222
|
+
function multiple(parser, options = {}) {
|
|
223
|
+
const { min = 0, max = Infinity } = options;
|
|
224
|
+
return {
|
|
225
|
+
$valueType: [],
|
|
226
|
+
$stateType: [],
|
|
227
|
+
priority: parser.priority,
|
|
228
|
+
usage: [{
|
|
229
|
+
type: "multiple",
|
|
230
|
+
terms: parser.usage,
|
|
231
|
+
min
|
|
232
|
+
}],
|
|
233
|
+
initialState: [],
|
|
234
|
+
parse(context) {
|
|
235
|
+
let added = context.state.length < 1;
|
|
236
|
+
let result = parser.parse({
|
|
237
|
+
...context,
|
|
238
|
+
state: context.state.at(-1) ?? parser.initialState
|
|
239
|
+
});
|
|
240
|
+
if (!result.success) if (!added) {
|
|
241
|
+
result = parser.parse({
|
|
242
|
+
...context,
|
|
243
|
+
state: parser.initialState
|
|
244
|
+
});
|
|
245
|
+
if (!result.success) return result;
|
|
246
|
+
added = true;
|
|
247
|
+
} else return result;
|
|
248
|
+
return {
|
|
249
|
+
success: true,
|
|
250
|
+
next: {
|
|
251
|
+
...result.next,
|
|
252
|
+
state: [...added ? context.state : context.state.slice(0, -1), result.next.state]
|
|
253
|
+
},
|
|
254
|
+
consumed: result.consumed
|
|
255
|
+
};
|
|
256
|
+
},
|
|
257
|
+
complete(state) {
|
|
258
|
+
const result = [];
|
|
259
|
+
for (const s of state) {
|
|
260
|
+
const valueResult = parser.complete(s);
|
|
261
|
+
if (valueResult.success) result.push(valueResult.value);
|
|
262
|
+
else return {
|
|
263
|
+
success: false,
|
|
264
|
+
error: valueResult.error
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
if (result.length < min) {
|
|
268
|
+
const customMessage = options.errors?.tooFew;
|
|
269
|
+
return {
|
|
270
|
+
success: false,
|
|
271
|
+
error: customMessage ? typeof customMessage === "function" ? customMessage(min, result.length) : customMessage : require_message.message`Expected at least ${require_message.text(min.toLocaleString("en"))} values, but got only ${require_message.text(result.length.toLocaleString("en"))}.`
|
|
272
|
+
};
|
|
273
|
+
} else if (result.length > max) {
|
|
274
|
+
const customMessage = options.errors?.tooMany;
|
|
275
|
+
return {
|
|
276
|
+
success: false,
|
|
277
|
+
error: customMessage ? typeof customMessage === "function" ? customMessage(max, result.length) : customMessage : require_message.message`Expected at most ${require_message.text(max.toLocaleString("en"))} values, but got ${require_message.text(result.length.toLocaleString("en"))}.`
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
return {
|
|
281
|
+
success: true,
|
|
282
|
+
value: result
|
|
283
|
+
};
|
|
284
|
+
},
|
|
285
|
+
getDocFragments(state, defaultValue) {
|
|
286
|
+
const innerState = state.kind === "unavailable" ? { kind: "unavailable" } : state.state.length > 0 ? {
|
|
287
|
+
kind: "available",
|
|
288
|
+
state: state.state.at(-1)
|
|
289
|
+
} : { kind: "unavailable" };
|
|
290
|
+
return parser.getDocFragments(innerState, defaultValue != null && defaultValue.length > 0 ? defaultValue[0] : void 0);
|
|
291
|
+
}
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
//#endregion
|
|
296
|
+
exports.WithDefaultError = WithDefaultError;
|
|
297
|
+
exports.map = map;
|
|
298
|
+
exports.multiple = multiple;
|
|
299
|
+
exports.optional = optional;
|
|
300
|
+
exports.withDefault = withDefault;
|