@optique/core 1.4.0-dev.2627 → 1.4.0-dev.2630

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.
@@ -885,11 +885,12 @@ function flag(...args) {
885
885
  }
886
886
  const prefixes = optionNames$1.filter((name) => name.startsWith("--") || name.startsWith("/") || name.startsWith("-") && name.length > 2 && !/^-[^-]$/u.test(name)).map((name) => name.startsWith("/") ? `${name}:` : `${name}=`);
887
887
  for (const prefix of prefixes) if (context.buffer[0].startsWith(prefix)) {
888
+ const optionName$1 = prefix.slice(0, -1);
888
889
  const value = context.buffer[0].slice(prefix.length);
889
890
  return {
890
891
  success: false,
891
892
  consumed: 1,
892
- error: require_message.message`Flag ${require_message.optionName(prefix.slice(0, -1))} does not accept a value, but got: ${value}.`
893
+ error: options.errors?.unexpectedValue ? typeof options.errors.unexpectedValue === "function" ? options.errors.unexpectedValue(optionName$1, value) : options.errors.unexpectedValue : require_message.message`Flag ${require_message.optionName(optionName$1)} does not accept a value, but got: ${value}.`
893
894
  };
894
895
  }
895
896
  const shortOptions = optionNames$1.filter((name) => name.match(/^-[^-]$/u));
@@ -196,6 +196,14 @@ interface FlagErrorOptions {
196
196
  * Can be a static message or a function that receives the token.
197
197
  */
198
198
  duplicate?: Message | ((token: string) => Message);
199
+ /**
200
+ * Custom error message when a flag receives an unexpected value.
201
+ * Can be a static message or a function that receives the matched option
202
+ * name and the value after the separator, including an empty value.
203
+ *
204
+ * @since 1.4.0
205
+ */
206
+ readonly unexpectedValue?: Message | ((optionName: string, value: string) => Message);
199
207
  /**
200
208
  * Custom error message when no matching flag is found.
201
209
  * Can be a static message or a function that receives:
@@ -196,6 +196,14 @@ interface FlagErrorOptions {
196
196
  * Can be a static message or a function that receives the token.
197
197
  */
198
198
  duplicate?: Message | ((token: string) => Message);
199
+ /**
200
+ * Custom error message when a flag receives an unexpected value.
201
+ * Can be a static message or a function that receives the matched option
202
+ * name and the value after the separator, including an empty value.
203
+ *
204
+ * @since 1.4.0
205
+ */
206
+ readonly unexpectedValue?: Message | ((optionName: string, value: string) => Message);
199
207
  /**
200
208
  * Custom error message when no matching flag is found.
201
209
  * Can be a static message or a function that receives:
@@ -885,11 +885,12 @@ function flag(...args) {
885
885
  }
886
886
  const prefixes = optionNames$1.filter((name) => name.startsWith("--") || name.startsWith("/") || name.startsWith("-") && name.length > 2 && !/^-[^-]$/u.test(name)).map((name) => name.startsWith("/") ? `${name}:` : `${name}=`);
887
887
  for (const prefix of prefixes) if (context.buffer[0].startsWith(prefix)) {
888
+ const optionName$1 = prefix.slice(0, -1);
888
889
  const value = context.buffer[0].slice(prefix.length);
889
890
  return {
890
891
  success: false,
891
892
  consumed: 1,
892
- error: message`Flag ${optionName(prefix.slice(0, -1))} does not accept a value, but got: ${value}.`
893
+ error: options.errors?.unexpectedValue ? typeof options.errors.unexpectedValue === "function" ? options.errors.unexpectedValue(optionName$1, value) : options.errors.unexpectedValue : message`Flag ${optionName(optionName$1)} does not accept a value, but got: ${value}.`
893
894
  };
894
895
  }
895
896
  const shortOptions = optionNames$1.filter((name) => name.match(/^-[^-]$/u));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@optique/core",
3
- "version": "1.4.0-dev.2627",
3
+ "version": "1.4.0-dev.2630",
4
4
  "description": "Type-safe combinatorial command-line interface parser",
5
5
  "keywords": [
6
6
  "CLI",
@@ -237,7 +237,7 @@
237
237
  },
238
238
  "sideEffects": false,
239
239
  "devDependencies": {
240
- "@optique/env": "1.4.0-dev.2627+4dd892c0",
240
+ "@optique/env": "1.4.0-dev.2630+2e4e647b",
241
241
  "@types/node": "^24.0.0",
242
242
  "fast-check": "^4.7.0",
243
243
  "tsdown": "^0.13.0",
@@ -34,12 +34,12 @@ Core rules
34
34
  It groups untitled entries only on pages with visible commands. See
35
35
  <https://optique.dev/cookbook.md#command-and-option-headings-in-help>.
36
36
  - Let TypeScript infer results unless another API needs a separate interface.
37
- - Parsers are usually required. `optional(p)` yields `undefined`, and
38
- `withDefault(p, value)` supplies a fallback. Use
39
- `withDefault(flag("--name"), false)` for optional Boolean flags.
37
+ - Parsers usually require input. `optional(p)` yields `undefined`; use
38
+ `withDefault(p, value)` or `withDefault(flag("-v"), false)` for fallbacks.
40
39
  - Use semantic `message` helpers for descriptions and errors. Since 1.3.0,
41
40
  customize terminal output with `theme` or `messageFormatter`, preserving
42
41
  `initialWidth`, quoting, and width options. Keep canonical errors unthemed.
42
+ `flag()` supports static/callback `errors.unexpectedValue` since 1.4.0.
43
43
  - Use value parsers such as `integer()`, `choice()`, `biject()`, `regExp()`,
44
44
  `url()`, `origin()`, and `uuid()` instead of validating raw strings after
45
45
  parsing. Use `regExp({ flags })` for user-supplied sources, `biject()` for