@kjanat/dreamcli 2.5.0 → 3.0.0-rc.2

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 (160) hide show
  1. package/CHANGELOG.md +153 -1
  2. package/README.md +9 -2
  3. package/dist/core/cli/dispatch.mjs +214 -1
  4. package/dist/core/cli/help-links.mjs +41 -1
  5. package/dist/core/cli/index.d.mts +109 -16
  6. package/dist/core/cli/index.mjs +901 -1
  7. package/dist/core/cli/planner.d.mts +27 -5
  8. package/dist/core/cli/planner.mjs +300 -1
  9. package/dist/core/cli/plugin.mjs +41 -1
  10. package/dist/core/cli/propagate.mjs +57 -1
  11. package/dist/core/cli/root-help.d.mts +16 -2
  12. package/dist/core/cli/root-help.mjs +208 -6
  13. package/dist/core/cli/root-surface.d.mts +18 -3
  14. package/dist/core/cli/root-surface.mjs +27 -1
  15. package/dist/core/cli/runtime-preflight.d.mts +16 -1
  16. package/dist/core/cli/runtime-preflight.mjs +168 -1
  17. package/dist/core/completion/index.d.mts +32 -1
  18. package/dist/core/completion/index.mjs +90 -1
  19. package/dist/core/completion/shells/bash.mjs +304 -3
  20. package/dist/core/completion/shells/fish.mjs +159 -2
  21. package/dist/core/completion/shells/powershell.mjs +353 -2
  22. package/dist/core/completion/shells/shared.d.mts +10 -0
  23. package/dist/core/completion/shells/shared.mjs +154 -1
  24. package/dist/core/completion/shells/zsh.mjs +244 -2
  25. package/dist/core/config/index.mjs +246 -1
  26. package/dist/core/config/package-json.mjs +385 -3
  27. package/dist/core/errors/index.mjs +87 -1
  28. package/dist/core/execution/index.mjs +189 -1
  29. package/dist/core/help/ansi.mjs +122 -2
  30. package/dist/core/help/index.d.mts +45 -1
  31. package/dist/core/help/index.mjs +329 -7
  32. package/dist/core/json-schema/index.mjs +487 -9
  33. package/dist/core/json-schema/meta-descriptions.generated.d.mts +38 -38
  34. package/dist/core/json-schema/meta-descriptions.generated.mjs +93 -28
  35. package/dist/core/output/activity.mjs +452 -1
  36. package/dist/core/output/bind.mjs +56 -1
  37. package/dist/core/output/contracts.mjs +57 -1
  38. package/dist/core/output/display-value.mjs +32 -1
  39. package/dist/core/output/index.d.mts +21 -0
  40. package/dist/core/output/index.mjs +442 -2
  41. package/dist/core/output/renderers.mjs +46 -1
  42. package/dist/core/output/writer.mjs +12 -1
  43. package/dist/core/parse/index.d.mts +15 -1
  44. package/dist/core/parse/index.mjs +555 -1
  45. package/dist/core/prompt/index.d.mts +11 -4
  46. package/dist/core/prompt/index.mjs +348 -6
  47. package/dist/core/resolve/args.mjs +105 -1
  48. package/dist/core/resolve/coerce.mjs +279 -1
  49. package/dist/core/resolve/config.mjs +19 -1
  50. package/dist/core/resolve/errors.mjs +140 -2
  51. package/dist/core/resolve/flags.mjs +196 -1
  52. package/dist/core/resolve/index.mjs +85 -1
  53. package/dist/core/resolve/property.d.mts +2 -1
  54. package/dist/core/resolve/property.mjs +45 -1
  55. package/dist/core/schema/arg.d.mts +90 -10
  56. package/dist/core/schema/arg.mjs +463 -1
  57. package/dist/core/schema/command.d.mts +21 -1
  58. package/dist/core/schema/command.mjs +712 -1
  59. package/dist/core/schema/flag.d.mts +92 -2
  60. package/dist/core/schema/flag.mjs +453 -1
  61. package/dist/core/schema/index.d.mts +2 -1
  62. package/dist/core/schema/middleware.mjs +41 -1
  63. package/dist/core/schema/number-constraints.d.mts +96 -0
  64. package/dist/core/schema/number-constraints.mjs +60 -0
  65. package/dist/core/schema/prompt.d.mts +15 -0
  66. package/dist/core/schema/run.d.mts +4 -4
  67. package/dist/core/schema-dsl/runtime.mjs +271 -2
  68. package/dist/core/schema-dsl/to-json-schema.mjs +74 -1
  69. package/dist/core/testkit/index.d.mts +9 -5
  70. package/dist/core/testkit/index.mjs +83 -1
  71. package/dist/index.d.mts +3 -1
  72. package/dist/index.mjs +22 -1
  73. package/dist/runtime/adapter.mjs +82 -1
  74. package/dist/runtime/auto.mjs +46 -1
  75. package/dist/runtime/bun.mjs +28 -1
  76. package/dist/runtime/deno.mjs +217 -2
  77. package/dist/runtime/detect.mjs +45 -1
  78. package/dist/runtime/node.mjs +169 -2
  79. package/dist/runtime/paths.mjs +48 -1
  80. package/dist/runtime.mjs +7 -1
  81. package/dist/strings.mjs +22 -1
  82. package/dist/testkit.mjs +5 -1
  83. package/dist/version.mjs +7 -1
  84. package/examples/basic.ts +5 -1
  85. package/examples/gh/package.json +2 -7
  86. package/examples/pwsh-demo/package.json +1 -7
  87. package/examples/pwsh-demo/src/main.test.ts +1 -4
  88. package/examples/pwsh-demo/src/main.ts +14 -4
  89. package/examples/transport-launcher.ts +85 -0
  90. package/package.json +23 -41
  91. package/dist/runtime/support.d.mts +0 -42
  92. package/dist/runtime/support.mjs +0 -1
  93. package/src/core/cli/dispatch.ts +0 -349
  94. package/src/core/cli/help-links.ts +0 -82
  95. package/src/core/cli/index.ts +0 -1413
  96. package/src/core/cli/planner.ts +0 -487
  97. package/src/core/cli/plugin.ts +0 -117
  98. package/src/core/cli/propagate.ts +0 -97
  99. package/src/core/cli/root-help.ts +0 -252
  100. package/src/core/cli/root-surface.ts +0 -74
  101. package/src/core/cli/runtime-preflight.ts +0 -438
  102. package/src/core/completion/index.ts +0 -88
  103. package/src/core/completion/shells/bash.ts +0 -459
  104. package/src/core/completion/shells/fish.ts +0 -259
  105. package/src/core/completion/shells/powershell.ts +0 -439
  106. package/src/core/completion/shells/shared.ts +0 -283
  107. package/src/core/completion/shells/zsh.ts +0 -381
  108. package/src/core/config/index.ts +0 -417
  109. package/src/core/config/package-json.ts +0 -560
  110. package/src/core/errors/index.ts +0 -193
  111. package/src/core/execution/index.ts +0 -268
  112. package/src/core/help/ansi.ts +0 -144
  113. package/src/core/help/index.ts +0 -516
  114. package/src/core/json-schema/index.ts +0 -891
  115. package/src/core/json-schema/meta-descriptions.generated.ts +0 -185
  116. package/src/core/output/activity.ts +0 -575
  117. package/src/core/output/bind.ts +0 -59
  118. package/src/core/output/contracts.ts +0 -194
  119. package/src/core/output/display-value.ts +0 -48
  120. package/src/core/output/index.ts +0 -704
  121. package/src/core/output/renderers.ts +0 -125
  122. package/src/core/output/writer.ts +0 -46
  123. package/src/core/parse/index.ts +0 -681
  124. package/src/core/prompt/index.ts +0 -583
  125. package/src/core/resolve/args.ts +0 -144
  126. package/src/core/resolve/coerce.ts +0 -432
  127. package/src/core/resolve/config.ts +0 -26
  128. package/src/core/resolve/contracts.ts +0 -135
  129. package/src/core/resolve/errors.ts +0 -227
  130. package/src/core/resolve/flags.ts +0 -255
  131. package/src/core/resolve/index.ts +0 -108
  132. package/src/core/resolve/property.ts +0 -99
  133. package/src/core/schema/activity.ts +0 -240
  134. package/src/core/schema/arg.ts +0 -719
  135. package/src/core/schema/command.ts +0 -1498
  136. package/src/core/schema/flag.ts +0 -801
  137. package/src/core/schema/index.ts +0 -91
  138. package/src/core/schema/middleware.ts +0 -171
  139. package/src/core/schema/prompt.ts +0 -156
  140. package/src/core/schema/run.ts +0 -198
  141. package/src/core/schema-dsl/index.ts +0 -109
  142. package/src/core/schema-dsl/parse.ts +0 -262
  143. package/src/core/schema-dsl/runtime.ts +0 -609
  144. package/src/core/schema-dsl/to-json-schema.ts +0 -102
  145. package/src/core/testkit/index.ts +0 -123
  146. package/src/index.ts +0 -179
  147. package/src/runtime/adapter.ts +0 -303
  148. package/src/runtime/auto.ts +0 -110
  149. package/src/runtime/bun.ts +0 -55
  150. package/src/runtime/deno.ts +0 -369
  151. package/src/runtime/detect.ts +0 -105
  152. package/src/runtime/index.ts +0 -21
  153. package/src/runtime/node-builtins.d.ts +0 -27
  154. package/src/runtime/node.ts +0 -300
  155. package/src/runtime/paths.ts +0 -64
  156. package/src/runtime/support.ts +0 -135
  157. package/src/runtime.ts +0 -31
  158. package/src/strings.ts +0 -34
  159. package/src/testkit.ts +0 -25
  160. package/src/version.ts +0 -20
@@ -1,801 +0,0 @@
1
- /**
2
- * Flag schema builder with full type inference.
3
- *
4
- * Each factory (`flag.string()`, `flag.boolean()`, etc.) returns an immutable
5
- * {@linkcode FlagBuilder} whose generic parameter tracks the value type and presence
6
- * state. Chained modifiers (`.default()`, `.required()`, `.alias()`, …) return
7
- * new builders with updated type-level and runtime state.
8
- *
9
- * @module dreamcli/core/schema/flag
10
- */
11
-
12
- import type {
13
- ConfirmPromptConfig,
14
- InputPromptConfig,
15
- MultiselectPromptConfig,
16
- PromptConfig,
17
- SelectPromptConfig,
18
- } from './prompt.ts';
19
-
20
- // --- Type-level configuration (phantom state tracked through the chain)
21
-
22
- /** All flag presence states as a runtime array. */
23
- const FLAG_PRESENCES = ['optional', 'required', 'defaulted'] as const;
24
-
25
- /**
26
- * Presence describes whether a flag value is guaranteed to exist when the
27
- * action handler runs:
28
- *
29
- * - `'optional'` — not required; unresolved value follows the kind-specific
30
- * optional fallback (`undefined` for most flags, `[]` for arrays)
31
- * - `'required'` — must be supplied; error if missing
32
- * - `'defaulted'` — always present (falls back to default value)
33
- */
34
- type FlagPresence = (typeof FLAG_PRESENCES)[number];
35
-
36
- /**
37
- * Fallback behavior when an optional flag resolves no value from any source.
38
- *
39
- * Most optional flags resolve to `undefined`; array flags instead resolve to
40
- * an empty array `[]`.
41
- */
42
- type OptionalFallback = 'undefined' | 'empty-array';
43
-
44
- /**
45
- * Compile-time state carried through the builder chain.
46
- *
47
- * Adding new tracked properties only requires extending this interface — no
48
- * builder signature changes.
49
- */
50
- interface FlagConfig {
51
- /** The resolved value type (e.g. `string`, `number`, `'us' | 'eu'`). */
52
- readonly valueType: unknown;
53
- /** Whether the flag is optional, required, or has a default. */
54
- readonly presence: FlagPresence;
55
- /** What an unresolved optional flag becomes at the action boundary. */
56
- readonly optionalFallback: OptionalFallback;
57
- /** The runtime kind discriminator, mirroring {@link FlagKind}. */
58
- readonly flagKind: FlagKind;
59
- }
60
-
61
- // --- Type-level helpers
62
-
63
- /**
64
- * Advanced type helper used by {@linkcode FlagBuilder} modifiers to replace presence.
65
- * Most consumers rely on inference and never reference this directly.
66
- */
67
- type WithPresence<C extends FlagConfig, P extends FlagPresence> = {
68
- readonly valueType: C['valueType'];
69
- readonly presence: P;
70
- readonly optionalFallback: C['optionalFallback'];
71
- readonly flagKind: C['flagKind'];
72
- };
73
-
74
- /**
75
- * Compute the final value type from config — this is what handlers receive.
76
- *
77
- * Advanced type helper: this powers {@link InferFlag} and action-handler
78
- * inference. Most apps do not need to mention it explicitly.
79
- *
80
- * - `'optional'` + `'undefined'` fallback → `T | undefined`
81
- * - `'optional'` + `'empty-array'` fallback → `T`
82
- * - `'required'` → `T`
83
- * - `'defaulted'` → `T`
84
- */
85
- type ResolvedValue<C extends FlagConfig> = C['presence'] extends 'optional'
86
- ? C['optionalFallback'] extends 'empty-array'
87
- ? C['valueType']
88
- : C['valueType'] | undefined
89
- : C['valueType'];
90
-
91
- /** Extract the resolved value type from a {@linkcode FlagBuilder}. */
92
- type InferFlag<B> = B extends FlagBuilder<infer C extends FlagConfig> ? ResolvedValue<C> : never;
93
-
94
- /** Extract resolved value types from a record of builders. */
95
- type InferFlags<T extends Record<string, FlagBuilder<FlagConfig>>> = {
96
- [K in keyof T]: InferFlag<T[K]>;
97
- };
98
-
99
- /**
100
- * Maps a {@linkcode FlagConfig} to the prompt config types that are compatible
101
- * with the flag's kind. Prevents compile-time mismatches such as
102
- * `flag.enum([…]).prompt({ kind: 'multiselect' })`.
103
- *
104
- * - `'boolean'` → {@link ConfirmPromptConfig}
105
- * - `'string'` → {@link InputPromptConfig} | {@link SelectPromptConfig}
106
- * - `'number'` → {@link InputPromptConfig}
107
- * - `'enum'` → {@link SelectPromptConfig} | {@link InputPromptConfig}
108
- * - `'array'` → {@link MultiselectPromptConfig}
109
- * - `'custom'` → all prompt kinds ({@link PromptConfig})
110
- */
111
- type PromptConfigByFlagKind = {
112
- readonly string: InputPromptConfig | SelectPromptConfig;
113
- readonly number: InputPromptConfig;
114
- readonly boolean: ConfirmPromptConfig;
115
- readonly enum: SelectPromptConfig | InputPromptConfig;
116
- readonly array: MultiselectPromptConfig;
117
- readonly custom: PromptConfig;
118
- };
119
-
120
- type AllowedPromptConfig<C extends FlagConfig> = PromptConfigByFlagKind[C['flagKind']];
121
-
122
- // --- Runtime schema data
123
-
124
- /** All flag kind discriminators as a runtime array. */
125
- const FLAG_KINDS = ['string', 'number', 'boolean', 'enum', 'array', 'custom'] as const;
126
-
127
- /** Discriminator for the kind of value a flag accepts. */
128
- type FlagKind = (typeof FLAG_KINDS)[number];
129
-
130
- /**
131
- * Custom parse function for `flag.custom()`.
132
- *
133
- * Receives `string` from CLI argv and env vars, or any JSON-representable
134
- * value from config files. Narrow inside the function as needed.
135
- */
136
- type FlagParseFn<T> = (raw: unknown) => T;
137
-
138
- /** Runtime descriptor for a flag alias. */
139
- interface FlagAlias {
140
- /** Alias name without `-` / `--` prefix. */
141
- readonly name: string;
142
- /** Whether the alias is parser-only and hidden from user-facing surfaces. */
143
- readonly hidden: boolean;
144
- }
145
-
146
- /**
147
- * The runtime descriptor stored inside every {@linkcode FlagBuilder}. Consumers (parser,
148
- * help generator, resolution chain) read this to understand the flag's shape
149
- * without touching generics.
150
- */
151
- interface FlagSchema {
152
- /** What kind of value this flag accepts. */
153
- readonly kind: FlagKind;
154
- /** Current presence state. */
155
- readonly presence: FlagPresence;
156
- /** Runtime default value (if any). */
157
- readonly defaultValue: unknown;
158
- /** Short/long aliases (e.g. `[{ name: 'f', hidden: false }]` for `--force`). */
159
- readonly aliases: readonly FlagAlias[];
160
- /** Environment variable name for v0.2+ resolution. */
161
- readonly envVar: string | undefined;
162
- /** Dotted config path for v0.2+ resolution (e.g. `'deploy.region'`). */
163
- readonly configPath: string | undefined;
164
- /** Human-readable description for help text. */
165
- readonly description: string | undefined;
166
- /** Allowed literal values when `kind === 'enum'`. */
167
- readonly enumValues: readonly string[] | undefined;
168
- /** Element schema when `kind === 'array'`. */
169
- readonly elementSchema: FlagSchema | undefined;
170
- /** Interactive prompt configuration for v0.3+ resolution. */
171
- readonly prompt: PromptConfig | undefined;
172
- /** Custom parse function (only when `kind === 'custom'`). */
173
- readonly parseFn: FlagParseFn<unknown> | undefined;
174
- /**
175
- * Deprecation marker.
176
- *
177
- * - `undefined` — not deprecated (default)
178
- * - `true` — deprecated with no migration message
179
- * - `string` — deprecated with a reason/migration message
180
- *
181
- * When a deprecated flag is used, a warning is emitted to stderr.
182
- * Help text shows `[deprecated]` or `[deprecated: <reason>]`.
183
- */
184
- readonly deprecated: string | true | undefined;
185
- /**
186
- * Whether this flag propagates to subcommands in nested command trees.
187
- *
188
- * When `true`, the flag is automatically available to all descendant
189
- * commands. A child command that defines a flag with the same name
190
- * shadows the propagated parent flag.
191
- *
192
- * @defaultValue `false`
193
- */
194
- readonly propagate: boolean;
195
- }
196
-
197
- /**
198
- * Low-level overrides accepted by {@link createSchema}.
199
- *
200
- * `aliases` accepts both legacy string input and structured {@link FlagAlias}
201
- * objects so tests and internal fixtures can be migrated incrementally.
202
- */
203
- type FlagSchemaOverrides = Omit<Partial<FlagSchema>, 'aliases'> & {
204
- readonly aliases?: readonly (string | FlagAlias)[];
205
- };
206
-
207
- /**
208
- * Normalise an alias input into a full {@link FlagAlias} object.
209
- *
210
- * @param alias - Raw alias name or structured alias object.
211
- * @returns Normalised alias record.
212
- */
213
- function normalizeFlagAlias(alias: string | FlagAlias): FlagAlias {
214
- if (typeof alias === 'string') {
215
- return { name: alias, hidden: false };
216
- }
217
-
218
- return {
219
- name: alias.name,
220
- hidden: alias.hidden,
221
- };
222
- }
223
-
224
- /**
225
- * Normalise alias input into immutable alias records.
226
- *
227
- * @param aliases - Alias input values.
228
- * @returns Normalised alias objects.
229
- */
230
- function normalizeFlagAliases(aliases: readonly (string | FlagAlias)[]): readonly FlagAlias[] {
231
- return aliases.map(normalizeFlagAlias);
232
- }
233
-
234
- /**
235
- * List alias names for a flag schema.
236
- *
237
- * @param schema - Flag schema whose aliases should be listed.
238
- * @param options - Visibility and length filtering.
239
- * @returns Alias names in registration order.
240
- */
241
- function getFlagAliasNames(
242
- schema: FlagSchema,
243
- options?: {
244
- readonly includeHidden?: boolean;
245
- readonly kind?: 'all' | 'short' | 'long';
246
- },
247
- ): readonly string[] {
248
- const includeHidden = options?.includeHidden ?? false;
249
- const kind = options?.kind ?? 'all';
250
-
251
- return schema.aliases
252
- .filter((alias) => includeHidden || !alias.hidden)
253
- .filter((alias) => {
254
- if (kind === 'short') return alias.name.length === 1;
255
- if (kind === 'long') return alias.name.length > 1;
256
- return true;
257
- })
258
- .map((alias) => alias.name);
259
- }
260
-
261
- /**
262
- * Create a raw {@link FlagSchema} object with sensible defaults.
263
- *
264
- * Most consumers should prefer the higher-level {@link flag} factory,
265
- * which returns an immutable {@link FlagBuilder} with type inference and
266
- * safe modifier chaining. `createSchema()` is the low-level escape hatch
267
- * for advanced schema composition, tests, or custom factories that need to
268
- * work directly with the runtime descriptor.
269
- *
270
- * `overrides` are shallow-merged on top of the default shape, so callers are
271
- * responsible for keeping the resulting schema internally consistent.
272
- *
273
- * @param kind - Discriminator for the value type this flag accepts.
274
- * @param overrides - Partial {@link FlagSchema} fields merged onto defaults.
275
- * @returns A fully populated {@link FlagSchema}.
276
- *
277
- * @example
278
- * ```ts
279
- * const schema = createSchema('enum', {
280
- * enumValues: ['us', 'eu', 'ap'],
281
- * description: 'Deployment region',
282
- * });
283
- * ```
284
- */
285
- function createSchema(kind: FlagKind, overrides?: FlagSchemaOverrides): FlagSchema {
286
- const aliases =
287
- overrides?.aliases !== undefined ? normalizeFlagAliases(overrides.aliases) : ([] as const);
288
- const { aliases: _ignoredAliases, ...rest } = overrides ?? {};
289
-
290
- return {
291
- kind,
292
- presence: 'optional',
293
- defaultValue: undefined,
294
- envVar: undefined,
295
- configPath: undefined,
296
- description: undefined,
297
- enumValues: undefined,
298
- elementSchema: undefined,
299
- prompt: undefined,
300
- parseFn: undefined,
301
- deprecated: undefined,
302
- propagate: false,
303
- ...rest,
304
- aliases,
305
- };
306
- }
307
-
308
- // --- FlagBuilder — immutable builder with type-level tracking
309
-
310
- /**
311
- * Immutable flag schema builder.
312
- *
313
- * The type parameter `C` is a phantom that tracks the value type and presence
314
- * through the fluent chain. Each modifier returns a **new** builder — the
315
- * original is never mutated.
316
- *
317
- * @example
318
- * ```ts
319
- * const port = flag.number().default(8080);
320
- * type Port = InferFlag<typeof port>; // number
321
- *
322
- * const region = flag.enum(['us', 'eu', 'ap']);
323
- * type Region = InferFlag<typeof region>; // 'us' | 'eu' | 'ap' | undefined
324
- * ```
325
- */
326
- class FlagBuilder<C extends FlagConfig> {
327
- /** @internal Runtime schema descriptor. */
328
- readonly schema: FlagSchema;
329
-
330
- /**
331
- * @internal Type brand — exists only in the type system (`declare`
332
- * produces no runtime property). Used by {@linkcode InferFlag} / {@linkcode InferFlags}.
333
- */
334
- declare readonly _config: C;
335
-
336
- /**
337
- * Create a flag builder from a pre-built schema descriptor.
338
- *
339
- * @param schema - Runtime descriptor seeding this builder's state.
340
- */
341
- constructor(schema: FlagSchema) {
342
- this.schema = schema;
343
- }
344
-
345
- // -- Presence modifiers --------------------------------------------------
346
-
347
- /**
348
- * Provide a default value. The flag becomes "always present" — handlers
349
- * will never see `undefined`.
350
- *
351
- * The generic constraint `V extends C['valueType']` ensures the default
352
- * matches the flag's declared type.
353
- *
354
- * @param value - Fallback value used when no source provides one.
355
- * @returns The builder (for chaining).
356
- *
357
- * @example
358
- * ```ts
359
- * flag.number().default(8080).describe('Port to listen on')
360
- *
361
- * // $ mycli serve → port = 8080
362
- * // $ mycli serve --port 443 → port = 443
363
- * ```
364
- */
365
- default<V extends C['valueType']>(value: V): FlagBuilder<WithPresence<C, 'defaulted'>> {
366
- return new FlagBuilder({
367
- ...this.schema,
368
- presence: 'defaulted',
369
- defaultValue: value,
370
- });
371
- }
372
-
373
- /**
374
- * Mark the flag as required. If not resolved from any source the framework
375
- * will emit a `ValidationError` before the action handler runs.
376
- *
377
- * @returns The builder (for chaining).
378
- *
379
- * @example
380
- * ```ts
381
- * flag.string().required().describe('Deploy target')
382
- *
383
- * // $ mycli deploy
384
- * // # → Error: Missing required flag --target
385
- * // $ mycli deploy --target staging
386
- * // # → target = 'staging'
387
- * ```
388
- */
389
- required(): FlagBuilder<WithPresence<C, 'required'>> {
390
- return new FlagBuilder({
391
- ...this.schema,
392
- presence: 'required',
393
- });
394
- }
395
-
396
- // -- Metadata modifiers --------------------------------------------------
397
-
398
- /**
399
- * Add a short or long alias (e.g. `'f'` for `--force`, `'verbose'` as an
400
- * alternative long name).
401
- *
402
- * @param name - Single-char short alias or alternative long name.
403
- * @param options - Optional alias metadata. Hidden aliases remain parseable
404
- * but are omitted from help, completions, and suggestions.
405
- * @returns The builder (for chaining).
406
- *
407
- * @example
408
- * ```ts
409
- * flag.boolean().alias('v').describe('Enable verbose output')
410
- *
411
- * // $ mycli build -v → verbose = true
412
- * // $ mycli build --verbose → verbose = true
413
- * ```
414
- */
415
- alias(name: string, options?: { hidden?: boolean }): FlagBuilder<C> {
416
- return new FlagBuilder({
417
- ...this.schema,
418
- aliases: [
419
- ...this.schema.aliases,
420
- {
421
- name,
422
- hidden: options?.hidden ?? false,
423
- },
424
- ],
425
- });
426
- }
427
-
428
- /**
429
- * Bind to an environment variable (resolved in v0.2+).
430
- *
431
- * @param varName - Environment variable name (e.g. `'PORT'`).
432
- * @returns The builder (for chaining).
433
- *
434
- * @example
435
- * ```ts
436
- * flag.string().env('API_KEY').describe('Service API key')
437
- *
438
- * // $ API_KEY=sk-123 mycli request → apiKey = 'sk-123'
439
- * // $ mycli request --api-key sk-456 → apiKey = 'sk-456' (CLI wins)
440
- * ```
441
- */
442
- env(varName: string): FlagBuilder<C> {
443
- return new FlagBuilder({
444
- ...this.schema,
445
- envVar: varName,
446
- });
447
- }
448
-
449
- /**
450
- * Bind to a dotted config path (resolved in v0.2+).
451
- *
452
- * @param path - Dotted config key (e.g. `'deploy.region'`).
453
- * @returns The builder (for chaining).
454
- *
455
- * @example
456
- * ```ts
457
- * flag.string().config('deploy.region').default('us-east-1')
458
- * // Config file: { "deploy": { "region": "eu-west-1" } }
459
- * // $ mycli deploy
460
- * // # → region = 'eu-west-1' (from config)
461
- * // $ mycli deploy --region ap-south-1
462
- * // # → CLI flag wins
463
- * ```
464
- */
465
- config(path: string): FlagBuilder<C> {
466
- return new FlagBuilder({
467
- ...this.schema,
468
- configPath: path,
469
- });
470
- }
471
-
472
- /**
473
- * Human-readable description shown in help output.
474
- *
475
- * @param description - Text displayed next to the flag in `--help`.
476
- * @returns The builder (for chaining).
477
- */
478
- describe(description: string): FlagBuilder<C> {
479
- return new FlagBuilder({
480
- ...this.schema,
481
- description,
482
- });
483
- }
484
-
485
- /**
486
- * Attach interactive prompt configuration for v0.3+ resolution.
487
- *
488
- * When a flag value is not resolved from CLI, env, or config, the
489
- * prompt engine uses this config to interactively ask the user.
490
- * In non-interactive contexts (CI, piped stdin) prompts are skipped
491
- * and resolution falls through to default or required validation.
492
- *
493
- * @param config - {@link PromptConfig} describing the interactive prompt.
494
- * @returns The builder (for chaining).
495
- *
496
- * @example
497
- * ```ts
498
- * flag.string().prompt({ kind: 'input', message: 'Enter value:' })
499
- *
500
- * // $ mycli init → prompts "Enter value:" interactively
501
- * // $ mycli init --name foo → skips prompt, uses CLI value
502
- * ```
503
- */
504
- prompt(config: AllowedPromptConfig<C>): FlagBuilder<C> {
505
- return new FlagBuilder({
506
- ...this.schema,
507
- prompt: config,
508
- });
509
- }
510
-
511
- /**
512
- * Mark this flag as deprecated.
513
- *
514
- * When used, a warning is emitted to stderr. Help text shows
515
- * `[deprecated]` or `[deprecated: <reason>]`.
516
- *
517
- * Does not change the flag's type-level config — it's metadata only.
518
- *
519
- * @param message - Optional migration reason/guidance.
520
- * @returns The builder (for chaining).
521
- *
522
- * @example
523
- * ```ts
524
- * flag.string().deprecated('Use --target instead')
525
- *
526
- * // $ mycli deploy --dest staging
527
- * // ⚠ --dest is deprecated: Use --target instead
528
- * ```
529
- */
530
- deprecated(message?: string): FlagBuilder<C> {
531
- return new FlagBuilder({
532
- ...this.schema,
533
- deprecated: message ?? true,
534
- });
535
- }
536
-
537
- /**
538
- * Mark this flag as propagated to subcommands.
539
- *
540
- * Propagated flags are automatically available to all descendant
541
- * commands in a nested command tree. A child command that defines
542
- * a flag with the same name shadows the propagated parent flag.
543
- *
544
- * Does not change the flag's type-level config — it's metadata only.
545
- *
546
- * @returns The builder (for chaining).
547
- *
548
- * @example
549
- * ```ts
550
- * flag.boolean().alias('v').propagate().describe('Enable verbose output')
551
- *
552
- * // $ mycli --verbose deploy staging
553
- * // # → verbose = true in deploy handler
554
- * // $ mycli deploy --verbose staging
555
- * // # → same, inherited from parent
556
- * ```
557
- */
558
- propagate(): FlagBuilder<C> {
559
- return new FlagBuilder({
560
- ...this.schema,
561
- propagate: true,
562
- });
563
- }
564
- }
565
-
566
- // --- Factory namespace
567
-
568
- /**
569
- * Factory that creates {@link FlagBuilder} instances seeded with the correct
570
- * {@link FlagKind} and initial type-level config.
571
- */
572
- interface FlagFactory {
573
- /**
574
- * String-valued flag.
575
- *
576
- * @returns A {@link FlagBuilder} for `string` values.
577
- */
578
- string(): FlagBuilder<{
579
- readonly valueType: string;
580
- readonly presence: 'optional';
581
- readonly optionalFallback: 'undefined';
582
- readonly flagKind: 'string';
583
- }>;
584
-
585
- /**
586
- * Number-valued flag.
587
- *
588
- * @returns A {@link FlagBuilder} for `number` values.
589
- */
590
- number(): FlagBuilder<{
591
- readonly valueType: number;
592
- readonly presence: 'optional';
593
- readonly optionalFallback: 'undefined';
594
- readonly flagKind: 'number';
595
- }>;
596
-
597
- /**
598
- * Boolean flag. Implicitly defaults to `false` — the only flag kind where
599
- * the absence of a value is still meaningful (not `undefined`).
600
- *
601
- * @returns A {@link FlagBuilder} for `boolean` values (defaulted to `false`).
602
- */
603
- boolean(): FlagBuilder<{
604
- readonly valueType: boolean;
605
- readonly presence: 'defaulted';
606
- readonly optionalFallback: 'undefined';
607
- readonly flagKind: 'boolean';
608
- }>;
609
-
610
- /**
611
- * Enum flag with literal type inference.
612
- *
613
- * Requires a **non-empty** readonly tuple so that `T[number]` produces a
614
- * union of string literals rather than just `string`.
615
- *
616
- * @example
617
- * ```ts
618
- * flag.enum(['us', 'eu', 'ap'])
619
- * // inferred type: 'us' | 'eu' | 'ap'
620
- * ```
621
- *
622
- * @param values - Non-empty tuple of allowed string literals.
623
- * @returns A {@link FlagBuilder} whose value type is the union of `values`.
624
- */
625
- enum<const T extends readonly [string, ...string[]]>(
626
- values: T,
627
- ): FlagBuilder<{
628
- readonly valueType: T[number];
629
- readonly presence: 'optional';
630
- readonly optionalFallback: 'undefined';
631
- readonly flagKind: 'enum';
632
- }>;
633
-
634
- /**
635
- * Array flag — collects multiple values of the same element type.
636
- *
637
- * @example
638
- * ```ts
639
- * flag.array(flag.string())
640
- * // inferred type: string[]
641
- * ```
642
- *
643
- * @param element - {@link FlagBuilder} describing the element type.
644
- * @returns A {@link FlagBuilder} for arrays of the element type.
645
- */
646
- array<E extends FlagConfig>(
647
- element: FlagBuilder<E>,
648
- ): FlagBuilder<{
649
- readonly valueType: E['valueType'][];
650
- readonly presence: 'optional';
651
- readonly optionalFallback: 'empty-array';
652
- readonly flagKind: 'array';
653
- }>;
654
-
655
- /**
656
- * Custom-parsed flag. The parse function receives the raw value and must
657
- * return a value of type `T`. The return type is inferred from `parseFn`.
658
- *
659
- * The input is `string` from CLI argv and env vars, or any JSON value
660
- * from config files. Narrow inside the function as needed:
661
- *
662
- * ```ts
663
- * flag.custom((raw: unknown): string[] => {
664
- * if (Array.isArray(raw)) return raw.map(String);
665
- * if (typeof raw === 'string') return raw.split(',');
666
- * throw new Error(`Expected string or array, got ${typeof raw}`);
667
- * })
668
- * ```
669
- *
670
- * Throw an `Error` (or `ParseError`) to signal invalid input — it will
671
- * be wrapped with context and re-thrown as a `ParseError`.
672
- *
673
- * @see `coerceConfigValue` `'custom'` case in `core/resolve/index.ts`
674
- *
675
- * @example
676
- * ```ts
677
- * flag.custom((raw) => new URL(String(raw)))
678
- * // inferred type: URL | undefined
679
- * ```
680
- *
681
- * @param parseFn - Converts the raw input into a value of type `T`.
682
- * @returns A {@link FlagBuilder} whose value type is inferred from `parseFn`.
683
- */
684
- custom<T>(parseFn: FlagParseFn<T>): FlagBuilder<{
685
- readonly valueType: T;
686
- readonly presence: 'optional';
687
- readonly optionalFallback: 'undefined';
688
- readonly flagKind: 'custom';
689
- }>;
690
- }
691
-
692
- /**
693
- * Flag schema factory. Call `flag.<kind>()` to create an immutable
694
- * {@link FlagBuilder} with full type inference and safe modifier chaining.
695
- */
696
- const flag: FlagFactory = {
697
- string(): FlagBuilder<{
698
- readonly valueType: string;
699
- readonly presence: 'optional';
700
- readonly optionalFallback: 'undefined';
701
- readonly flagKind: 'string';
702
- }> {
703
- return new FlagBuilder(createSchema('string'));
704
- },
705
-
706
- number(): FlagBuilder<{
707
- readonly valueType: number;
708
- readonly presence: 'optional';
709
- readonly optionalFallback: 'undefined';
710
- readonly flagKind: 'number';
711
- }> {
712
- return new FlagBuilder(createSchema('number'));
713
- },
714
-
715
- boolean(): FlagBuilder<{
716
- readonly valueType: boolean;
717
- readonly presence: 'defaulted';
718
- readonly optionalFallback: 'undefined';
719
- readonly flagKind: 'boolean';
720
- }> {
721
- return new FlagBuilder(
722
- createSchema('boolean', {
723
- presence: 'defaulted',
724
- defaultValue: false,
725
- }),
726
- );
727
- },
728
-
729
- enum<const T extends readonly [string, ...string[]]>(
730
- values: T,
731
- ): FlagBuilder<{
732
- readonly valueType: T[number];
733
- readonly presence: 'optional';
734
- readonly optionalFallback: 'undefined';
735
- readonly flagKind: 'enum';
736
- }> {
737
- return new FlagBuilder(createSchema('enum', { enumValues: values }));
738
- },
739
-
740
- array<E extends FlagConfig>(
741
- element: FlagBuilder<E>,
742
- ): FlagBuilder<{
743
- readonly valueType: E['valueType'][];
744
- readonly presence: 'optional';
745
- readonly optionalFallback: 'empty-array';
746
- readonly flagKind: 'array';
747
- }> {
748
- return new FlagBuilder(createSchema('array', { elementSchema: element.schema }));
749
- },
750
-
751
- custom<T>(parseFn: FlagParseFn<T>): FlagBuilder<{
752
- readonly valueType: T;
753
- readonly presence: 'optional';
754
- readonly optionalFallback: 'undefined';
755
- readonly flagKind: 'custom';
756
- }> {
757
- return new FlagBuilder(createSchema('custom', { parseFn: parseFn as FlagParseFn<unknown> }));
758
- },
759
- };
760
-
761
- // --- Exports
762
-
763
- // Re-export prompt types for consumers
764
- export type {
765
- ConfirmPromptConfig,
766
- InputPromptConfig,
767
- MultiselectPromptConfig,
768
- PromptConfig,
769
- PromptConfigBase,
770
- PromptKind,
771
- PromptResult,
772
- SelectChoice,
773
- SelectPromptConfig,
774
- } from './prompt.ts';
775
- export { PROMPT_KINDS } from './prompt.ts';
776
- export type {
777
- AllowedPromptConfig,
778
- FlagAlias,
779
- FlagConfig,
780
- FlagFactory,
781
- FlagKind,
782
- FlagParseFn,
783
- FlagPresence,
784
- FlagSchema,
785
- FlagSchemaOverrides,
786
- InferFlag,
787
- InferFlags,
788
- OptionalFallback,
789
- ResolvedValue,
790
- WithPresence,
791
- };
792
- export {
793
- createSchema,
794
- FLAG_KINDS,
795
- FLAG_PRESENCES,
796
- FlagBuilder,
797
- flag,
798
- getFlagAliasNames,
799
- normalizeFlagAlias,
800
- normalizeFlagAliases,
801
- };