@optique/core 0.4.1 → 0.5.0-dev.63

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,6 +6,7 @@ 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;
package/dist/index.d.cts CHANGED
@@ -2,6 +2,6 @@ import { Message, MessageFormatOptions, MessageTerm, formatMessage, message, met
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, 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,6 @@ import { Message, MessageFormatOptions, MessageTerm, formatMessage, message, met
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, 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
@@ -2,7 +2,7 @@ import { formatMessage, message, metavar, optionName, optionNames, text, value,
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, 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/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 ${text("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 ${text("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 ${text("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 ${text("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.4.1",
3
+ "version": "0.5.0-dev.63+48ac6884",
4
4
  "description": "Type-safe combinatorial command-line interface parser",
5
5
  "keywords": [
6
6
  "CLI",