@optique/core 0.5.0-dev.62 → 0.5.0-dev.64

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -6,11 +6,13 @@ const require_parser = require('./parser.cjs');
6
6
  const require_facade = require('./facade.cjs');
7
7
 
8
8
  exports.RunError = require_facade.RunError;
9
+ exports.WithDefaultError = require_parser.WithDefaultError;
9
10
  exports.argument = require_parser.argument;
10
11
  exports.choice = require_valueparser.choice;
11
12
  exports.command = require_parser.command;
12
13
  exports.concat = require_parser.concat;
13
14
  exports.constant = require_parser.constant;
15
+ exports.envVar = require_message.envVar;
14
16
  exports.flag = require_parser.flag;
15
17
  exports.float = require_valueparser.float;
16
18
  exports.formatDocPage = require_doc.formatDocPage;
package/dist/index.d.cts CHANGED
@@ -1,7 +1,7 @@
1
- import { Message, MessageFormatOptions, MessageTerm, formatMessage, message, metavar, optionName, optionNames, text, value, values } from "./message.cjs";
1
+ import { Message, MessageFormatOptions, MessageTerm, envVar, formatMessage, message, metavar, optionName, optionNames, text, value, values } from "./message.cjs";
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 { ArgumentOptions, CommandOptions, DocState, FlagOptions, InferValue, MultipleOptions, OptionOptions, Parser, ParserContext, ParserResult, Result, argument, command, concat, constant, flag, getDocPage, group, longestMatch, map, merge, multiple, object, option, optional, or, parse, tuple, withDefault } from "./parser.cjs";
5
+ import { ArgumentOptions, CommandOptions, DocState, FlagOptions, InferValue, MultipleOptions, OptionOptions, Parser, ParserContext, ParserResult, Result, WithDefaultError, argument, command, concat, constant, flag, getDocPage, group, longestMatch, map, merge, multiple, object, option, optional, or, parse, tuple, withDefault } from "./parser.cjs";
6
6
  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, argument, choice, command, concat, constant, 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 };
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, 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
@@ -1,7 +1,7 @@
1
- import { Message, MessageFormatOptions, MessageTerm, formatMessage, message, metavar, optionName, optionNames, text, value, values } from "./message.js";
1
+ import { Message, MessageFormatOptions, MessageTerm, envVar, formatMessage, message, metavar, optionName, optionNames, text, value, values } from "./message.js";
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 { ArgumentOptions, CommandOptions, DocState, FlagOptions, InferValue, MultipleOptions, OptionOptions, Parser, ParserContext, ParserResult, Result, argument, command, concat, constant, flag, getDocPage, group, longestMatch, map, merge, multiple, object, option, optional, or, parse, tuple, withDefault } from "./parser.js";
5
+ import { ArgumentOptions, CommandOptions, DocState, FlagOptions, InferValue, MultipleOptions, OptionOptions, Parser, ParserContext, ParserResult, Result, WithDefaultError, argument, command, concat, constant, flag, getDocPage, group, longestMatch, map, merge, multiple, object, option, optional, or, parse, tuple, withDefault } from "./parser.js";
6
6
  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, argument, choice, command, concat, constant, 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 };
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, 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,8 @@
1
- import { formatMessage, message, metavar, optionName, optionNames, text, value, values } from "./message.js";
1
+ import { envVar, formatMessage, message, metavar, optionName, optionNames, text, value, values } from "./message.js";
2
2
  import { formatUsage, formatUsageTerm, normalizeUsage } from "./usage.js";
3
3
  import { formatDocPage } from "./doc.js";
4
4
  import { choice, float, integer, isValueParser, locale, string, url, uuid } from "./valueparser.js";
5
- import { argument, command, concat, constant, flag, getDocPage, group, longestMatch, map, merge, multiple, object, option, optional, or, parse, tuple, withDefault } from "./parser.js";
5
+ import { WithDefaultError, argument, command, concat, constant, flag, getDocPage, group, longestMatch, map, merge, multiple, object, option, optional, or, parse, tuple, withDefault } from "./parser.js";
6
6
  import { RunError, run } from "./facade.js";
7
7
 
8
- export { RunError, argument, choice, command, concat, constant, 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 };
8
+ 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 };
package/dist/message.cjs CHANGED
@@ -113,6 +113,19 @@ function values(values$1) {
113
113
  };
114
114
  }
115
115
  /**
116
+ * Creates a {@link MessageTerm} for an environment variable.
117
+ * @param envVar The environment variable name, which is a string that represents
118
+ * an environment variable. For example, `"PATH"` or `"API_URL"`.
119
+ * @returns A {@link MessageTerm} representing the environment variable.
120
+ * @since 0.5.0
121
+ */
122
+ function envVar(envVar$1) {
123
+ return {
124
+ type: "envVar",
125
+ envVar: envVar$1
126
+ };
127
+ }
128
+ /**
116
129
  * Formats a {@link Message} into a human-readable string for
117
130
  * the terminal.
118
131
  * @param msg The message to format, which is an array of
@@ -176,7 +189,13 @@ function formatMessage(msg, options = {}) {
176
189
  width: value$1.length
177
190
  };
178
191
  }
179
- else throw new TypeError(`Invalid MessageTerm type: ${term["type"]}.`);
192
+ else if (term.type === "envVar") {
193
+ const envVar$1 = useQuotes ? `\`${term.envVar}\`` : term.envVar;
194
+ yield {
195
+ text: useColors ? `\x1b[1;4m${envVar$1}\x1b[0m` : envVar$1,
196
+ width: envVar$1.length
197
+ };
198
+ } else throw new TypeError(`Invalid MessageTerm type: ${term["type"]}.`);
180
199
  }
181
200
  let output = "";
182
201
  let totalWidth = 0;
@@ -192,6 +211,7 @@ function formatMessage(msg, options = {}) {
192
211
  }
193
212
 
194
213
  //#endregion
214
+ exports.envVar = envVar;
195
215
  exports.formatMessage = formatMessage;
196
216
  exports.message = message;
197
217
  exports.metavar = metavar;
@@ -89,6 +89,21 @@ type MessageTerm =
89
89
  * representations of consecutive values. For example, `["42", "hello"]`.
90
90
  */
91
91
  readonly values: readonly string[];
92
+ }
93
+ /**
94
+ * An environment variable term in the message, which represents
95
+ * an environment variable name.
96
+ * @since 0.5.0
97
+ */ | {
98
+ /**
99
+ * The type of the term, which is `"envVar"` for an environment variable.
100
+ */
101
+ readonly type: "envVar";
102
+ /**
103
+ * The environment variable name, which is a string that represents
104
+ * an environment variable. For example, `"PATH"` or `"API_URL"`.
105
+ */
106
+ readonly envVar: string;
92
107
  };
93
108
  /**
94
109
  * Type representing a message that can include styled/colored values.
@@ -160,6 +175,14 @@ declare function value(value: string): MessageTerm;
160
175
  * @returns A {@link MessageTerm} representing the list of values.
161
176
  */
162
177
  declare function values(values: readonly string[]): MessageTerm;
178
+ /**
179
+ * Creates a {@link MessageTerm} for an environment variable.
180
+ * @param envVar The environment variable name, which is a string that represents
181
+ * an environment variable. For example, `"PATH"` or `"API_URL"`.
182
+ * @returns A {@link MessageTerm} representing the environment variable.
183
+ * @since 0.5.0
184
+ */
185
+ declare function envVar(envVar: string): MessageTerm;
163
186
  /**
164
187
  * Options for the {@link formatMessage} function.
165
188
  */
@@ -196,4 +219,4 @@ interface MessageFormatOptions {
196
219
  */
197
220
  declare function formatMessage(msg: Message, options?: MessageFormatOptions): string;
198
221
  //#endregion
199
- export { Message, MessageFormatOptions, MessageTerm, formatMessage, message, metavar, optionName, optionNames, text, value, values };
222
+ export { Message, MessageFormatOptions, MessageTerm, envVar, formatMessage, message, metavar, optionName, optionNames, text, value, values };
package/dist/message.d.ts CHANGED
@@ -89,6 +89,21 @@ type MessageTerm =
89
89
  * representations of consecutive values. For example, `["42", "hello"]`.
90
90
  */
91
91
  readonly values: readonly string[];
92
+ }
93
+ /**
94
+ * An environment variable term in the message, which represents
95
+ * an environment variable name.
96
+ * @since 0.5.0
97
+ */ | {
98
+ /**
99
+ * The type of the term, which is `"envVar"` for an environment variable.
100
+ */
101
+ readonly type: "envVar";
102
+ /**
103
+ * The environment variable name, which is a string that represents
104
+ * an environment variable. For example, `"PATH"` or `"API_URL"`.
105
+ */
106
+ readonly envVar: string;
92
107
  };
93
108
  /**
94
109
  * Type representing a message that can include styled/colored values.
@@ -160,6 +175,14 @@ declare function value(value: string): MessageTerm;
160
175
  * @returns A {@link MessageTerm} representing the list of values.
161
176
  */
162
177
  declare function values(values: readonly string[]): MessageTerm;
178
+ /**
179
+ * Creates a {@link MessageTerm} for an environment variable.
180
+ * @param envVar The environment variable name, which is a string that represents
181
+ * an environment variable. For example, `"PATH"` or `"API_URL"`.
182
+ * @returns A {@link MessageTerm} representing the environment variable.
183
+ * @since 0.5.0
184
+ */
185
+ declare function envVar(envVar: string): MessageTerm;
163
186
  /**
164
187
  * Options for the {@link formatMessage} function.
165
188
  */
@@ -196,4 +219,4 @@ interface MessageFormatOptions {
196
219
  */
197
220
  declare function formatMessage(msg: Message, options?: MessageFormatOptions): string;
198
221
  //#endregion
199
- export { Message, MessageFormatOptions, MessageTerm, formatMessage, message, metavar, optionName, optionNames, text, value, values };
222
+ export { Message, MessageFormatOptions, MessageTerm, envVar, formatMessage, message, metavar, optionName, optionNames, text, value, values };
package/dist/message.js CHANGED
@@ -112,6 +112,19 @@ function values(values$1) {
112
112
  };
113
113
  }
114
114
  /**
115
+ * Creates a {@link MessageTerm} for an environment variable.
116
+ * @param envVar The environment variable name, which is a string that represents
117
+ * an environment variable. For example, `"PATH"` or `"API_URL"`.
118
+ * @returns A {@link MessageTerm} representing the environment variable.
119
+ * @since 0.5.0
120
+ */
121
+ function envVar(envVar$1) {
122
+ return {
123
+ type: "envVar",
124
+ envVar: envVar$1
125
+ };
126
+ }
127
+ /**
115
128
  * Formats a {@link Message} into a human-readable string for
116
129
  * the terminal.
117
130
  * @param msg The message to format, which is an array of
@@ -175,7 +188,13 @@ function formatMessage(msg, options = {}) {
175
188
  width: value$1.length
176
189
  };
177
190
  }
178
- else throw new TypeError(`Invalid MessageTerm type: ${term["type"]}.`);
191
+ else if (term.type === "envVar") {
192
+ const envVar$1 = useQuotes ? `\`${term.envVar}\`` : term.envVar;
193
+ yield {
194
+ text: useColors ? `\x1b[1;4m${envVar$1}\x1b[0m` : envVar$1,
195
+ width: envVar$1.length
196
+ };
197
+ } else throw new TypeError(`Invalid MessageTerm type: ${term["type"]}.`);
179
198
  }
180
199
  let output = "";
181
200
  let totalWidth = 0;
@@ -191,4 +210,4 @@ function formatMessage(msg, options = {}) {
191
210
  }
192
211
 
193
212
  //#endregion
194
- export { formatMessage, message, metavar, optionName, optionNames, text, value, values };
213
+ export { envVar, formatMessage, message, metavar, optionName, optionNames, text, value, values };
package/dist/parser.cjs CHANGED
@@ -523,6 +523,40 @@ function optional(parser) {
523
523
  };
524
524
  }
525
525
  /**
526
+ * Error type for structured error messages in {@link withDefault} default value callbacks.
527
+ * Unlike regular errors that only support string messages, this error type accepts
528
+ * a {@link Message} object that supports rich formatting, colors, and structured content.
529
+ *
530
+ * @example
531
+ * ```typescript
532
+ * withDefault(option("--url", url()), () => {
533
+ * if (!process.env.INSTANCE_URL) {
534
+ * throw new WithDefaultError(
535
+ * message`Environment variable ${envVar("INSTANCE_URL")} is not set.`
536
+ * );
537
+ * }
538
+ * return new URL(process.env.INSTANCE_URL);
539
+ * })
540
+ * ```
541
+ *
542
+ * @since 0.5.0
543
+ */
544
+ var WithDefaultError = class extends Error {
545
+ /**
546
+ * The structured message associated with this error.
547
+ */
548
+ errorMessage;
549
+ /**
550
+ * Creates a new WithDefaultError with a structured message.
551
+ * @param message The structured {@link Message} describing the error.
552
+ */
553
+ constructor(message$1) {
554
+ super(require_message.formatMessage(message$1));
555
+ this.errorMessage = message$1;
556
+ this.name = "WithDefaultError";
557
+ }
558
+ };
559
+ /**
526
560
  * Creates a parser that makes another parser use a default value when it fails
527
561
  * to match or consume input. This is similar to {@link optional}, but instead
528
562
  * of returning `undefined` when the wrapped parser doesn't match, it returns
@@ -563,10 +597,18 @@ function withDefault(parser, defaultValue) {
563
597
  return result;
564
598
  },
565
599
  complete(state) {
566
- if (typeof state === "undefined") return {
567
- success: true,
568
- value: typeof defaultValue === "function" ? defaultValue() : defaultValue
569
- };
600
+ if (typeof state === "undefined") try {
601
+ const value = typeof defaultValue === "function" ? defaultValue() : defaultValue;
602
+ return {
603
+ success: true,
604
+ value
605
+ };
606
+ } catch (error) {
607
+ return {
608
+ success: false,
609
+ error: error instanceof WithDefaultError ? error.errorMessage : require_message.message`${require_message.text(String(error))}`
610
+ };
611
+ }
570
612
  return parser.complete(state[0]);
571
613
  },
572
614
  getDocFragments(state, upperDefaultValue) {
@@ -1645,6 +1687,7 @@ function getDocPage(parser, args = []) {
1645
1687
  }
1646
1688
 
1647
1689
  //#endregion
1690
+ exports.WithDefaultError = WithDefaultError;
1648
1691
  exports.argument = argument;
1649
1692
  exports.command = command;
1650
1693
  exports.concat = concat;
package/dist/parser.d.cts CHANGED
@@ -276,6 +276,36 @@ declare function argument<T>(valueParser: ValueParser<T>, options?: ArgumentOpti
276
276
  * or `undefined` if the wrapped parser fails to match.
277
277
  */
278
278
  declare function optional<TValue, TState>(parser: Parser<TValue, TState>): Parser<TValue | undefined, [TState] | undefined>;
279
+ /**
280
+ * Error type for structured error messages in {@link withDefault} default value callbacks.
281
+ * Unlike regular errors that only support string messages, this error type accepts
282
+ * a {@link Message} object that supports rich formatting, colors, and structured content.
283
+ *
284
+ * @example
285
+ * ```typescript
286
+ * withDefault(option("--url", url()), () => {
287
+ * if (!process.env.INSTANCE_URL) {
288
+ * throw new WithDefaultError(
289
+ * message`Environment variable ${envVar("INSTANCE_URL")} is not set.`
290
+ * );
291
+ * }
292
+ * return new URL(process.env.INSTANCE_URL);
293
+ * })
294
+ * ```
295
+ *
296
+ * @since 0.5.0
297
+ */
298
+ declare class WithDefaultError extends Error {
299
+ /**
300
+ * The structured message associated with this error.
301
+ */
302
+ readonly errorMessage: Message;
303
+ /**
304
+ * Creates a new WithDefaultError with a structured message.
305
+ * @param message The structured {@link Message} describing the error.
306
+ */
307
+ constructor(message: Message);
308
+ }
279
309
  /**
280
310
  * Creates a parser that makes another parser use a default value when it fails
281
311
  * to match or consume input. This is similar to {@link optional}, but instead
@@ -1431,4 +1461,4 @@ declare function group<TValue, TState>(label: string, parser: Parser<TValue, TSt
1431
1461
  */
1432
1462
  declare function getDocPage(parser: Parser<unknown, unknown>, args?: readonly string[]): DocPage | undefined;
1433
1463
  //#endregion
1434
- export { ArgumentOptions, CommandOptions, DocState, FlagOptions, InferValue, MultipleOptions, OptionOptions, Parser, ParserContext, ParserResult, Result, argument, command, concat, constant, flag, getDocPage, group, longestMatch, map, merge, multiple, object, option, optional, or, parse, tuple, withDefault };
1464
+ export { ArgumentOptions, CommandOptions, DocState, FlagOptions, InferValue, MultipleOptions, OptionOptions, Parser, ParserContext, ParserResult, Result, WithDefaultError, argument, command, concat, constant, flag, getDocPage, group, longestMatch, map, merge, multiple, object, option, optional, or, parse, tuple, withDefault };
package/dist/parser.d.ts CHANGED
@@ -276,6 +276,36 @@ declare function argument<T>(valueParser: ValueParser<T>, options?: ArgumentOpti
276
276
  * or `undefined` if the wrapped parser fails to match.
277
277
  */
278
278
  declare function optional<TValue, TState>(parser: Parser<TValue, TState>): Parser<TValue | undefined, [TState] | undefined>;
279
+ /**
280
+ * Error type for structured error messages in {@link withDefault} default value callbacks.
281
+ * Unlike regular errors that only support string messages, this error type accepts
282
+ * a {@link Message} object that supports rich formatting, colors, and structured content.
283
+ *
284
+ * @example
285
+ * ```typescript
286
+ * withDefault(option("--url", url()), () => {
287
+ * if (!process.env.INSTANCE_URL) {
288
+ * throw new WithDefaultError(
289
+ * message`Environment variable ${envVar("INSTANCE_URL")} is not set.`
290
+ * );
291
+ * }
292
+ * return new URL(process.env.INSTANCE_URL);
293
+ * })
294
+ * ```
295
+ *
296
+ * @since 0.5.0
297
+ */
298
+ declare class WithDefaultError extends Error {
299
+ /**
300
+ * The structured message associated with this error.
301
+ */
302
+ readonly errorMessage: Message;
303
+ /**
304
+ * Creates a new WithDefaultError with a structured message.
305
+ * @param message The structured {@link Message} describing the error.
306
+ */
307
+ constructor(message: Message);
308
+ }
279
309
  /**
280
310
  * Creates a parser that makes another parser use a default value when it fails
281
311
  * to match or consume input. This is similar to {@link optional}, but instead
@@ -1431,4 +1461,4 @@ declare function group<TValue, TState>(label: string, parser: Parser<TValue, TSt
1431
1461
  */
1432
1462
  declare function getDocPage(parser: Parser<unknown, unknown>, args?: readonly string[]): DocPage | undefined;
1433
1463
  //#endregion
1434
- export { ArgumentOptions, CommandOptions, DocState, FlagOptions, InferValue, MultipleOptions, OptionOptions, Parser, ParserContext, ParserResult, Result, argument, command, concat, constant, flag, getDocPage, group, longestMatch, map, merge, multiple, object, option, optional, or, parse, tuple, withDefault };
1464
+ export { ArgumentOptions, CommandOptions, DocState, FlagOptions, InferValue, MultipleOptions, OptionOptions, Parser, ParserContext, ParserResult, Result, WithDefaultError, argument, command, concat, constant, flag, getDocPage, group, longestMatch, map, merge, multiple, object, option, optional, or, parse, tuple, withDefault };
package/dist/parser.js CHANGED
@@ -1,4 +1,4 @@
1
- import { message, metavar, optionName, optionNames, text, values } from "./message.js";
1
+ import { formatMessage, message, metavar, optionName, optionNames, text, values } from "./message.js";
2
2
  import { normalizeUsage } from "./usage.js";
3
3
  import { isValueParser } from "./valueparser.js";
4
4
 
@@ -523,6 +523,40 @@ function optional(parser) {
523
523
  };
524
524
  }
525
525
  /**
526
+ * Error type for structured error messages in {@link withDefault} default value callbacks.
527
+ * Unlike regular errors that only support string messages, this error type accepts
528
+ * a {@link Message} object that supports rich formatting, colors, and structured content.
529
+ *
530
+ * @example
531
+ * ```typescript
532
+ * withDefault(option("--url", url()), () => {
533
+ * if (!process.env.INSTANCE_URL) {
534
+ * throw new WithDefaultError(
535
+ * message`Environment variable ${envVar("INSTANCE_URL")} is not set.`
536
+ * );
537
+ * }
538
+ * return new URL(process.env.INSTANCE_URL);
539
+ * })
540
+ * ```
541
+ *
542
+ * @since 0.5.0
543
+ */
544
+ var WithDefaultError = class extends Error {
545
+ /**
546
+ * The structured message associated with this error.
547
+ */
548
+ errorMessage;
549
+ /**
550
+ * Creates a new WithDefaultError with a structured message.
551
+ * @param message The structured {@link Message} describing the error.
552
+ */
553
+ constructor(message$1) {
554
+ super(formatMessage(message$1));
555
+ this.errorMessage = message$1;
556
+ this.name = "WithDefaultError";
557
+ }
558
+ };
559
+ /**
526
560
  * Creates a parser that makes another parser use a default value when it fails
527
561
  * to match or consume input. This is similar to {@link optional}, but instead
528
562
  * of returning `undefined` when the wrapped parser doesn't match, it returns
@@ -563,10 +597,18 @@ function withDefault(parser, defaultValue) {
563
597
  return result;
564
598
  },
565
599
  complete(state) {
566
- if (typeof state === "undefined") return {
567
- success: true,
568
- value: typeof defaultValue === "function" ? defaultValue() : defaultValue
569
- };
600
+ if (typeof state === "undefined") try {
601
+ const value = typeof defaultValue === "function" ? defaultValue() : defaultValue;
602
+ return {
603
+ success: true,
604
+ value
605
+ };
606
+ } catch (error) {
607
+ return {
608
+ success: false,
609
+ error: error instanceof WithDefaultError ? error.errorMessage : message`${text(String(error))}`
610
+ };
611
+ }
570
612
  return parser.complete(state[0]);
571
613
  },
572
614
  getDocFragments(state, upperDefaultValue) {
@@ -1645,4 +1687,4 @@ function getDocPage(parser, args = []) {
1645
1687
  }
1646
1688
 
1647
1689
  //#endregion
1648
- export { argument, command, concat, constant, flag, getDocPage, group, longestMatch, map, merge, multiple, object, option, optional, or, parse, tuple, withDefault };
1690
+ export { WithDefaultError, argument, command, concat, constant, flag, getDocPage, group, longestMatch, map, merge, multiple, object, option, optional, or, parse, tuple, withDefault };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@optique/core",
3
- "version": "0.5.0-dev.62+985a811b",
3
+ "version": "0.5.0-dev.64+84de5850",
4
4
  "description": "Type-safe combinatorial command-line interface parser",
5
5
  "keywords": [
6
6
  "CLI",