@optique/core 0.10.7 → 1.0.0-dev.1116

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.
Files changed (56) hide show
  1. package/README.md +4 -6
  2. package/dist/annotations.cjs +209 -1
  3. package/dist/annotations.d.cts +78 -1
  4. package/dist/annotations.d.ts +78 -1
  5. package/dist/annotations.js +201 -1
  6. package/dist/completion.cjs +194 -52
  7. package/dist/completion.js +194 -52
  8. package/dist/constructs.cjs +310 -78
  9. package/dist/constructs.d.cts +525 -644
  10. package/dist/constructs.d.ts +525 -644
  11. package/dist/constructs.js +311 -79
  12. package/dist/context.cjs +43 -3
  13. package/dist/context.d.cts +113 -5
  14. package/dist/context.d.ts +113 -5
  15. package/dist/context.js +41 -3
  16. package/dist/dependency.cjs +172 -66
  17. package/dist/dependency.d.cts +22 -2
  18. package/dist/dependency.d.ts +22 -2
  19. package/dist/dependency.js +172 -66
  20. package/dist/doc.cjs +46 -1
  21. package/dist/doc.d.cts +24 -0
  22. package/dist/doc.d.ts +24 -0
  23. package/dist/doc.js +46 -1
  24. package/dist/facade.cjs +702 -322
  25. package/dist/facade.d.cts +124 -190
  26. package/dist/facade.d.ts +124 -190
  27. package/dist/facade.js +703 -323
  28. package/dist/index.cjs +5 -0
  29. package/dist/index.d.cts +5 -5
  30. package/dist/index.d.ts +5 -5
  31. package/dist/index.js +3 -3
  32. package/dist/message.cjs +7 -4
  33. package/dist/message.js +7 -4
  34. package/dist/mode-dispatch.cjs +23 -1
  35. package/dist/mode-dispatch.d.cts +55 -0
  36. package/dist/mode-dispatch.d.ts +55 -0
  37. package/dist/mode-dispatch.js +21 -1
  38. package/dist/modifiers.cjs +210 -55
  39. package/dist/modifiers.js +211 -56
  40. package/dist/parser.cjs +80 -47
  41. package/dist/parser.d.cts +18 -3
  42. package/dist/parser.d.ts +18 -3
  43. package/dist/parser.js +82 -50
  44. package/dist/primitives.cjs +102 -37
  45. package/dist/primitives.d.cts +81 -24
  46. package/dist/primitives.d.ts +81 -24
  47. package/dist/primitives.js +103 -39
  48. package/dist/usage.cjs +88 -6
  49. package/dist/usage.d.cts +51 -13
  50. package/dist/usage.d.ts +51 -13
  51. package/dist/usage.js +85 -7
  52. package/dist/valueparser.cjs +391 -106
  53. package/dist/valueparser.d.cts +62 -10
  54. package/dist/valueparser.d.ts +62 -10
  55. package/dist/valueparser.js +391 -106
  56. package/package.json +10 -1
package/dist/facade.d.cts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { Message } from "./message.cjs";
2
- import { ShowChoicesOptions, ShowDefaultOptions } from "./doc.cjs";
2
+ import { HiddenVisibility, OptionName } from "./usage.cjs";
3
+ import { DocSection, ShowChoicesOptions, ShowDefaultOptions } from "./doc.cjs";
3
4
  import { InferMode, InferValue, Mode, ModeValue, Parser } from "./parser.cjs";
4
5
  import { ShellCompletion } from "./completion.cjs";
5
6
  import { ParserValuePlaceholder, SourceContext } from "./context.cjs";
@@ -8,101 +9,57 @@ import { Program } from "./program.cjs";
8
9
  //#region src/facade.d.ts
9
10
 
10
11
  /**
11
- * Naming style for shell completion command and option aliases.
12
+ * Sub-configuration for a meta command's command form.
12
13
  *
13
- * @since 0.10.0
14
- */
15
- type CompletionName = "singular" | "plural" | "both";
16
- /**
17
- * Visibility policy for completion entries in help and usage output.
18
- *
19
- * @since 0.10.0
14
+ * @since 1.0.0
20
15
  */
21
- type CompletionHelpVisibility = CompletionName | "none";
22
- type CompletionConfigBase<THelp> = {
16
+ interface CommandSubConfig {
23
17
  /**
24
- * Determines how completion is made available:
25
- *
26
- * - `"command"`: Only the `completion` subcommand is available
27
- * - `"both"`: Both `completion` subcommand and `--completion` option
28
- * are available
29
- *
30
- * @default `"both"`
18
+ * Command names. The first element is the display name shown in help
19
+ * output; additional elements are hidden aliases that are accepted but
20
+ * not shown.
31
21
  */
32
- readonly mode?: "command" | "both";
22
+ readonly names?: readonly [string, ...string[]];
33
23
  /**
34
- * Group label for the completion command in help output. When specified,
35
- * the completion command appears under a titled section with this name
36
- * instead of alongside user-defined commands.
37
- *
38
- * @since 0.10.0
24
+ * Group label for the command in help output. When specified, the command
25
+ * appears under a titled section with this name instead of alongside
26
+ * user-defined commands.
39
27
  */
40
28
  readonly group?: string;
41
29
  /**
42
- * Available shell completions. By default, includes `bash`, `fish`, `nu`,
43
- * `pwsh`, and `zsh`. You can provide additional custom shell completions
44
- * or override the defaults.
30
+ * Granular visibility control.
45
31
  *
46
- * @default `{ bash, fish, nu, pwsh, zsh }`
32
+ * - `true`: Hidden from usage, documentation, and suggestions.
33
+ * - `"usage"`: Hidden from usage lines only.
34
+ * - `"doc"`: Hidden from documentation only.
35
+ * - `"help"`: Hidden from usage and documentation only.
47
36
  */
48
- readonly shells?: Record<string, ShellCompletion>;
37
+ readonly hidden?: HiddenVisibility;
38
+ }
39
+ /**
40
+ * Sub-configuration for a meta command's option form.
41
+ *
42
+ * @since 1.0.0
43
+ */
44
+ interface OptionSubConfig {
49
45
  /**
50
- * Callback function invoked when completion is requested. The function
51
- * can optionally receive an exit code parameter.
52
- *
53
- * You usually want to pass `process.exit` on Node.js or Bun and
54
- * `Deno.exit` on Deno to this option.
55
- *
56
- * @default Returns `void` when completion is shown.
46
+ * Option names (all shown in help, e.g., `"-h"`, `"--help"`).
57
47
  */
58
- readonly onShow?: (() => THelp) | ((exitCode: number) => THelp);
59
- } | {
48
+ readonly names?: readonly [OptionName, ...OptionName[]];
60
49
  /**
61
- * Determines how completion is made available:
62
- *
63
- * - `"option"`: Only the `--completion` option is available
50
+ * Group label for the option in help output.
64
51
  */
65
- readonly mode: "option";
66
- /** @since 0.10.0 */
67
- readonly group?: never;
68
- /** @default `{ bash, fish, nu, pwsh, zsh }` */
69
- readonly shells?: Record<string, ShellCompletion>;
70
- /** @default Returns `void` when completion is shown. */
71
- readonly onShow?: (() => THelp) | ((exitCode: number) => THelp);
72
- };
73
- type CompletionConfigBoth<THelp> = CompletionConfigBase<THelp> & {
52
+ readonly group?: string;
74
53
  /**
75
- * Determines whether to use singular or plural naming for completion command/option.
54
+ * Granular visibility control.
76
55
  *
77
- * - `"singular"`: Use `completion` / `--completion`
78
- * - `"plural"`: Use `completions` / `--completions`
79
- * - `"both"`: Use both singular and plural forms
80
- *
81
- * @default `"both"`
56
+ * - `true`: Hidden from usage, documentation, and suggestions.
57
+ * - `"usage"`: Hidden from usage lines only.
58
+ * - `"doc"`: Hidden from documentation only.
59
+ * - `"help"`: Hidden from usage and documentation only.
82
60
  */
83
- readonly name?: "both";
84
- /**
85
- * Controls which completion entries appear in help and usage output.
86
- *
87
- * - `"both"`: Show both singular and plural entries
88
- * - `"singular"`: Show only singular entries
89
- * - `"plural"`: Show only plural entries
90
- * - `"none"`: Hide completion entries from help and usage
91
- *
92
- * @default Matches `name`
93
- * @since 0.10.0
94
- */
95
- readonly helpVisibility?: CompletionHelpVisibility;
96
- };
97
- type CompletionConfigSingular<THelp> = CompletionConfigBase<THelp> & {
98
- readonly name: "singular";
99
- readonly helpVisibility?: "singular" | "none";
100
- };
101
- type CompletionConfigPlural<THelp> = CompletionConfigBase<THelp> & {
102
- readonly name: "plural";
103
- readonly helpVisibility?: "plural" | "none";
104
- };
105
- type CompletionConfig<THelp> = CompletionConfigBoth<THelp> | CompletionConfigSingular<THelp> | CompletionConfigPlural<THelp>;
61
+ readonly hidden?: HiddenVisibility;
62
+ }
106
63
  /**
107
64
  * Configuration options for the {@link run} function.
108
65
  *
@@ -149,125 +106,76 @@ interface RunOptions<THelp, TError> {
149
106
  */
150
107
  readonly showChoices?: boolean | ShowChoicesOptions;
151
108
  /**
152
- * Help configuration. When provided, enables help functionality.
109
+ * A custom comparator function to control the order of sections in the
110
+ * help output. When provided, it is used instead of the default smart
111
+ * sort (command-only sections first, then mixed, then option/argument-only
112
+ * sections). Sections that compare equal (return `0`) preserve their
113
+ * original relative order.
114
+ *
115
+ * @param a The first section to compare.
116
+ * @param b The second section to compare.
117
+ * @returns A negative number if `a` should appear before `b`, a positive
118
+ * number if `a` should appear after `b`, or `0` if they are equal.
119
+ * @since 1.0.0
120
+ */
121
+ readonly sectionOrder?: (a: DocSection, b: DocSection) => number;
122
+ /**
123
+ * Help configuration. When provided, enables help functionality.
124
+ * At least one of `command` or `option` must be specified.
125
+ *
126
+ * @since 1.0.0
153
127
  */
154
128
  readonly help?: {
155
- /**
156
- * Determines how help is made available:
157
- *
158
- * - `"command"`: Only the `help` subcommand is available
159
- * - `"both"`: Both `help` subcommand and `--help` option are available
160
- *
161
- * @default `"option"`
162
- */
163
- readonly mode: "command" | "both";
164
- /**
165
- * Group label for the help command in help output. When specified,
166
- * the help command appears under a titled section with this name
167
- * instead of alongside user-defined commands.
168
- *
169
- * @since 0.10.0
170
- */
171
- readonly group?: string;
172
- /**
173
- * Callback function invoked when help is requested. The function can
174
- * optionally receive an exit code parameter.
175
- *
176
- * You usually want to pass `process.exit` on Node.js or Bun and
177
- * `Deno.exit` on Deno to this option.
178
- *
179
- * @default Returns `void` when help is shown.
180
- */
129
+ /** Callback invoked when help is requested. */
181
130
  readonly onShow?: (() => THelp) | ((exitCode: number) => THelp);
131
+ } & ({
132
+ readonly command: true | CommandSubConfig;
133
+ readonly option?: true | OptionSubConfig;
182
134
  } | {
183
- /**
184
- * Determines how help is made available:
185
- *
186
- * - `"option"`: Only the `--help` option is available
187
- *
188
- * @default `"option"`
189
- */
190
- readonly mode?: "option";
191
- /** @since 0.10.0 */
192
- readonly group?: never;
193
- /**
194
- * Callback function invoked when help is requested. The function can
195
- * optionally receive an exit code parameter.
196
- *
197
- * You usually want to pass `process.exit` on Node.js or Bun and
198
- * `Deno.exit` on Deno to this option.
199
- *
200
- * @default Returns `void` when help is shown.
201
- */
202
- readonly onShow?: (() => THelp) | ((exitCode: number) => THelp);
203
- };
135
+ readonly option: true | OptionSubConfig;
136
+ readonly command?: true | CommandSubConfig;
137
+ });
204
138
  /**
205
- * Version configuration. When provided, enables version functionality.
139
+ * Version configuration. When provided, enables version functionality.
140
+ * At least one of `command` or `option` must be specified.
141
+ *
142
+ * @since 1.0.0
206
143
  */
207
144
  readonly version?: {
208
- /**
209
- * Determines how version is made available:
210
- *
211
- * - `"command"`: Only the `version` subcommand is available
212
- * - `"both"`: Both `version` subcommand and `--version` option are
213
- * available
214
- *
215
- * @default `"option"`
216
- */
217
- readonly mode: "command" | "both";
218
- /**
219
- * The version string to display when version is requested.
220
- */
145
+ /** The version string to display when version is requested. */
221
146
  readonly value: string;
222
- /**
223
- * Group label for the version command in help output. When specified,
224
- * the version command appears under a titled section with this name
225
- * instead of alongside user-defined commands.
226
- *
227
- * @since 0.10.0
228
- */
229
- readonly group?: string;
230
- /**
231
- * Callback function invoked when version is requested. The function can
232
- * optionally receive an exit code parameter.
233
- *
234
- * You usually want to pass `process.exit` on Node.js or Bun and
235
- * `Deno.exit` on Deno to this option.
236
- *
237
- * @default Returns `void` when version is shown.
238
- */
147
+ /** Callback invoked when version is requested. */
239
148
  readonly onShow?: (() => THelp) | ((exitCode: number) => THelp);
149
+ } & ({
150
+ readonly command: true | CommandSubConfig;
151
+ readonly option?: true | OptionSubConfig;
240
152
  } | {
153
+ readonly option: true | OptionSubConfig;
154
+ readonly command?: true | CommandSubConfig;
155
+ });
156
+ /**
157
+ * Completion configuration. When provided, enables shell completion.
158
+ * At least one of `command` or `option` must be specified.
159
+ *
160
+ * @since 1.0.0
161
+ */
162
+ readonly completion?: {
241
163
  /**
242
- * Determines how version is made available:
243
- *
244
- * - `"option"`: Only the `--version` option is available
245
- *
246
- * @default `"option"`
247
- */
248
- readonly mode?: "option";
249
- /**
250
- * The version string to display when version is requested.
251
- */
252
- readonly value: string;
253
- /** @since 0.10.0 */
254
- readonly group?: never;
255
- /**
256
- * Callback function invoked when version is requested. The function can
257
- * optionally receive an exit code parameter.
164
+ * Available shell completions. By default, includes `bash`, `fish`,
165
+ * `nu`, `pwsh`, and `zsh`.
258
166
  *
259
- * You usually want to pass `process.exit` on Node.js or Bun and
260
- * `Deno.exit` on Deno to this option.
261
- *
262
- * @default Returns `void` when version is shown.
167
+ * @default `{ bash, fish, nu, pwsh, zsh }`
263
168
  */
169
+ readonly shells?: Record<string, ShellCompletion>;
170
+ /** Callback invoked when completion is requested. */
264
171
  readonly onShow?: (() => THelp) | ((exitCode: number) => THelp);
265
- };
266
- /**
267
- * Completion configuration. When provided, enables shell completion functionality.
268
- * @since 0.6.0
269
- */
270
- readonly completion?: CompletionConfig<THelp>;
172
+ } & ({
173
+ readonly command: true | CommandSubConfig;
174
+ readonly option?: true | OptionSubConfig;
175
+ } | {
176
+ readonly option: true | OptionSubConfig;
177
+ readonly command?: true | CommandSubConfig;
178
+ });
271
179
  /**
272
180
  * What to display above error messages:
273
181
  * - `"usage"`: Show usage information
@@ -444,7 +352,7 @@ type UnionToIntersection<U> = (U extends unknown ? (k: U) => void : never) exten
444
352
  * @template TValue The parser value type for placeholder substitution.
445
353
  * @since 0.10.0
446
354
  */
447
- type ExtractRequiredOptions<TContexts extends readonly SourceContext<unknown>[], TValue> = UnionToIntersection<TContexts[number] extends SourceContext<infer O> ? O extends void ? unknown : SubstituteParserValue<O, TValue> : unknown>;
355
+ type ExtractRequiredOptions<TContexts extends readonly SourceContext<unknown>[], TValue> = [TContexts[number] extends SourceContext<infer O> ? O extends void ? never : SubstituteParserValue<O, TValue> : never] extends [never] ? unknown : UnionToIntersection<TContexts[number] extends SourceContext<infer O> ? O extends void ? never : SubstituteParserValue<O, TValue> : never>;
448
356
  /**
449
357
  * Options for runWith functions.
450
358
  * Extends RunOptions with additional context-related settings.
@@ -459,7 +367,32 @@ interface RunWithOptions<THelp, TError> extends RunOptions<THelp, TError> {
459
367
  * `process.argv.slice(2)` on Node.js/Bun or `Deno.args` on Deno.
460
368
  */
461
369
  readonly args?: readonly string[];
370
+ /**
371
+ * Options to forward to source contexts. When contexts declare
372
+ * required options (via `$requiredOptions`), pass them here to
373
+ * avoid name collisions with runner-level options such as `args`,
374
+ * `help`, or `colors`.
375
+ *
376
+ * @since 1.0.0
377
+ */
378
+ readonly contextOptions?: Record<string, unknown>;
462
379
  }
380
+ /**
381
+ * When contexts require options, demands a `contextOptions` property
382
+ * typed to those requirements. When no context needs options
383
+ * (`void`, `unknown`, or `{}`), resolves to `unknown` (intersection no-op).
384
+ * When all context option keys are optional, `contextOptions` itself becomes
385
+ * optional so callers are not forced to pass an empty wrapper.
386
+ *
387
+ * @template TContexts The tuple/array type of source contexts.
388
+ * @template TValue The parser value type for placeholder substitution.
389
+ * @since 1.0.0
390
+ */
391
+ type ContextOptionsParam<TContexts extends readonly SourceContext<unknown>[], TValue> = keyof ExtractRequiredOptions<TContexts, TValue> extends never ? unknown : Record<string, never> extends ExtractRequiredOptions<TContexts, TValue> ? {
392
+ readonly contextOptions?: ExtractRequiredOptions<TContexts, TValue>;
393
+ } : {
394
+ readonly contextOptions: ExtractRequiredOptions<TContexts, TValue>;
395
+ };
463
396
  /**
464
397
  * Runs a parser with multiple source contexts.
465
398
  *
@@ -508,7 +441,7 @@ interface RunWithOptions<THelp, TError> extends RunOptions<THelp, TError> {
508
441
  * );
509
442
  * ```
510
443
  */
511
- declare function runWith<TParser extends Parser<Mode, unknown, unknown>, TContexts extends readonly SourceContext<unknown>[], THelp = void, TError = never>(parser: TParser, programName: string, contexts: TContexts, options: RunWithOptions<THelp, TError> & ExtractRequiredOptions<TContexts, InferValue<TParser>>): Promise<InferValue<TParser>>;
444
+ declare function runWith<TParser extends Parser<Mode, unknown, unknown>, TContexts extends readonly SourceContext<unknown>[], THelp = void, TError = never>(parser: TParser, programName: string, contexts: TContexts, options: RunWithOptions<THelp, TError> & ContextOptionsParam<TContexts, InferValue<TParser>>): Promise<InferValue<TParser>>;
512
445
  /**
513
446
  * Runs a synchronous parser with multiple source contexts.
514
447
  *
@@ -523,10 +456,11 @@ declare function runWith<TParser extends Parser<Mode, unknown, unknown>, TContex
523
456
  * @param contexts Source contexts to use (priority: earlier overrides later).
524
457
  * @param options Run options including args, help, version, etc.
525
458
  * @returns The parsed result.
526
- * @throws Error if any context returns a Promise.
459
+ * @throws Error if any context returns a Promise or if a context's
460
+ * `[Symbol.asyncDispose]` returns a Promise.
527
461
  * @since 0.10.0
528
462
  */
529
- declare function runWithSync<TParser extends Parser<"sync", unknown, unknown>, TContexts extends readonly SourceContext<unknown>[], THelp = void, TError = never>(parser: TParser, programName: string, contexts: TContexts, options: RunWithOptions<THelp, TError> & ExtractRequiredOptions<TContexts, InferValue<TParser>>): InferValue<TParser>;
463
+ declare function runWithSync<TParser extends Parser<"sync", unknown, unknown>, TContexts extends readonly SourceContext<unknown>[], THelp = void, TError = never>(parser: TParser, programName: string, contexts: TContexts, options: RunWithOptions<THelp, TError> & ContextOptionsParam<TContexts, InferValue<TParser>>): InferValue<TParser>;
530
464
  /**
531
465
  * Runs any parser asynchronously with multiple source contexts.
532
466
  *
@@ -543,6 +477,6 @@ declare function runWithSync<TParser extends Parser<"sync", unknown, unknown>, T
543
477
  * @returns Promise that resolves to the parsed result.
544
478
  * @since 0.10.0
545
479
  */
546
- declare function runWithAsync<TParser extends Parser<Mode, unknown, unknown>, TContexts extends readonly SourceContext<unknown>[], THelp = void, TError = never>(parser: TParser, programName: string, contexts: TContexts, options: RunWithOptions<THelp, TError> & ExtractRequiredOptions<TContexts, InferValue<TParser>>): Promise<InferValue<TParser>>;
480
+ declare function runWithAsync<TParser extends Parser<Mode, unknown, unknown>, TContexts extends readonly SourceContext<unknown>[], THelp = void, TError = never>(parser: TParser, programName: string, contexts: TContexts, options: RunWithOptions<THelp, TError> & ContextOptionsParam<TContexts, InferValue<TParser>>): Promise<InferValue<TParser>>;
547
481
  //#endregion
548
- export { CompletionHelpVisibility, CompletionName, ExtractRequiredOptions, type ParserValuePlaceholder, RunOptions, RunParserError, RunWithOptions, type SourceContext, SubstituteParserValue, runParser, runParserAsync, runParserSync, runWith, runWithAsync, runWithSync };
482
+ export { CommandSubConfig, ContextOptionsParam, ExtractRequiredOptions, OptionSubConfig, type ParserValuePlaceholder, RunOptions, RunParserError, RunWithOptions, type SourceContext, SubstituteParserValue, runParser, runParserAsync, runParserSync, runWith, runWithAsync, runWithSync };