@kjanat/dreamcli 2.5.0 → 3.0.0-rc.10

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 (202) hide show
  1. package/CHANGELOG.md +344 -1
  2. package/README.md +9 -2
  3. package/dist/core/cli/dispatch.d.mts +6 -5
  4. package/dist/core/cli/dispatch.mjs +205 -1
  5. package/dist/core/cli/help-links.d.mts +0 -1
  6. package/dist/core/cli/help-links.mjs +41 -1
  7. package/dist/core/cli/index.d.mts +185 -19
  8. package/dist/core/cli/index.mjs +945 -1
  9. package/dist/core/cli/planner.d.mts +32 -6
  10. package/dist/core/cli/planner.mjs +300 -1
  11. package/dist/core/cli/plugin.d.mts +1 -1
  12. package/dist/core/cli/plugin.mjs +41 -1
  13. package/dist/core/cli/propagate.d.mts +1 -1
  14. package/dist/core/cli/propagate.mjs +57 -1
  15. package/dist/core/cli/root-help.d.mts +18 -3
  16. package/dist/core/cli/root-help.mjs +226 -6
  17. package/dist/core/cli/root-surface.d.mts +30 -4
  18. package/dist/core/cli/root-surface.mjs +28 -1
  19. package/dist/core/cli/runtime-preflight.d.mts +30 -3
  20. package/dist/core/cli/runtime-preflight.mjs +169 -1
  21. package/dist/core/completion/index.d.mts +32 -2
  22. package/dist/core/completion/index.mjs +90 -1
  23. package/dist/core/completion/shells/bash.d.mts +0 -1
  24. package/dist/core/completion/shells/bash.mjs +308 -3
  25. package/dist/core/completion/shells/fish.d.mts +0 -1
  26. package/dist/core/completion/shells/fish.mjs +162 -2
  27. package/dist/core/completion/shells/powershell.d.mts +0 -1
  28. package/dist/core/completion/shells/powershell.mjs +358 -2
  29. package/dist/core/completion/shells/shared.d.mts +23 -2
  30. package/dist/core/completion/shells/shared.mjs +176 -1
  31. package/dist/core/completion/shells/zsh.d.mts +0 -1
  32. package/dist/core/completion/shells/zsh.mjs +251 -2
  33. package/dist/core/config/index.d.mts +2 -3
  34. package/dist/core/config/index.mjs +246 -1
  35. package/dist/core/config/package-json.d.mts +27 -4
  36. package/dist/core/config/package-json.mjs +386 -3
  37. package/dist/core/errors/index.d.mts +3 -3
  38. package/dist/core/errors/index.mjs +87 -1
  39. package/dist/core/execution/index.d.mts +0 -1
  40. package/dist/core/execution/index.mjs +201 -1
  41. package/dist/core/help/ansi.d.mts +11 -5
  42. package/dist/core/help/ansi.mjs +121 -2
  43. package/dist/core/help/index.d.mts +65 -2
  44. package/dist/core/help/index.mjs +341 -7
  45. package/dist/core/help/theme.d.mts +68 -0
  46. package/dist/core/help/theme.mjs +46 -0
  47. package/dist/core/json-schema/index.d.mts +15 -2
  48. package/dist/core/json-schema/index.mjs +536 -12
  49. package/dist/core/json-schema/meta-descriptions.generated.d.mts +22 -5
  50. package/dist/core/json-schema/meta-descriptions.generated.mjs +102 -28
  51. package/dist/core/output/activity.d.mts +0 -1
  52. package/dist/core/output/activity.mjs +452 -1
  53. package/dist/core/output/bind.mjs +56 -1
  54. package/dist/core/output/contracts.d.mts +4 -5
  55. package/dist/core/output/contracts.mjs +57 -1
  56. package/dist/core/output/display-value.mjs +32 -1
  57. package/dist/core/output/index.d.mts +21 -1
  58. package/dist/core/output/index.mjs +442 -2
  59. package/dist/core/output/renderers.d.mts +0 -1
  60. package/dist/core/output/renderers.mjs +46 -1
  61. package/dist/core/output/writer.mjs +12 -1
  62. package/dist/core/parse/index.d.mts +54 -7
  63. package/dist/core/parse/index.mjs +727 -1
  64. package/dist/core/prompt/index.d.mts +12 -4
  65. package/dist/core/prompt/index.mjs +348 -6
  66. package/dist/core/resolve/args.d.mts +1 -1
  67. package/dist/core/resolve/args.mjs +105 -1
  68. package/dist/core/resolve/coerce.d.mts +1 -1
  69. package/dist/core/resolve/coerce.mjs +336 -1
  70. package/dist/core/resolve/config.mjs +19 -1
  71. package/dist/core/resolve/contracts.d.mts +9 -4
  72. package/dist/core/resolve/errors.d.mts +0 -1
  73. package/dist/core/resolve/errors.mjs +140 -2
  74. package/dist/core/resolve/flags.d.mts +7 -2
  75. package/dist/core/resolve/flags.mjs +251 -1
  76. package/dist/core/resolve/index.d.mts +2 -2
  77. package/dist/core/resolve/index.mjs +85 -1
  78. package/dist/core/resolve/property.d.mts +6 -2
  79. package/dist/core/resolve/property.mjs +53 -1
  80. package/dist/core/schema/activity.d.mts +31 -11
  81. package/dist/core/schema/arg.d.mts +92 -13
  82. package/dist/core/schema/arg.mjs +463 -1
  83. package/dist/core/schema/command.d.mts +29 -7
  84. package/dist/core/schema/command.mjs +726 -1
  85. package/dist/core/schema/flag.d.mts +577 -21
  86. package/dist/core/schema/flag.mjs +705 -1
  87. package/dist/core/schema/index.d.mts +5 -2
  88. package/dist/core/schema/middleware.d.mts +4 -3
  89. package/dist/core/schema/middleware.mjs +41 -1
  90. package/dist/core/schema/number-constraints.d.mts +96 -0
  91. package/dist/core/schema/number-constraints.mjs +60 -0
  92. package/dist/core/schema/prompt.d.mts +21 -3
  93. package/dist/core/schema/run.d.mts +24 -5
  94. package/dist/core/schema/string-constraints.d.mts +97 -0
  95. package/dist/core/schema/string-constraints.mjs +69 -0
  96. package/dist/core/schema/value-parsers.d.mts +78 -0
  97. package/dist/core/schema/value-parsers.mjs +142 -0
  98. package/dist/core/schema-dsl/runtime.mjs +271 -2
  99. package/dist/core/schema-dsl/to-json-schema.d.mts +0 -1
  100. package/dist/core/schema-dsl/to-json-schema.mjs +74 -1
  101. package/dist/core/testkit/index.d.mts +9 -36
  102. package/dist/core/testkit/index.mjs +83 -1
  103. package/dist/index.d.mts +12 -6
  104. package/dist/index.mjs +22 -1
  105. package/dist/runtime/adapter.d.mts +38 -2
  106. package/dist/runtime/adapter.mjs +87 -1
  107. package/dist/runtime/auto.d.mts +2 -3
  108. package/dist/runtime/auto.mjs +45 -1
  109. package/dist/runtime/deno.d.mts +24 -5
  110. package/dist/runtime/deno.mjs +262 -2
  111. package/dist/runtime/detect.mjs +45 -1
  112. package/dist/runtime/index.d.mts +3 -4
  113. package/dist/runtime/node.d.mts +8 -2
  114. package/dist/runtime/node.mjs +220 -2
  115. package/dist/runtime/paths.mjs +48 -1
  116. package/dist/runtime.d.mts +4 -4
  117. package/dist/runtime.mjs +6 -1
  118. package/dist/strings.mjs +22 -1
  119. package/dist/testkit.d.mts +1 -0
  120. package/dist/testkit.mjs +5 -1
  121. package/dist/version.d.mts +7 -3
  122. package/dist/version.mjs +7 -1
  123. package/dreamcli.schema.json +36 -7
  124. package/examples/basic.ts +5 -1
  125. package/examples/gh/package.json +2 -7
  126. package/examples/pwsh-demo/package.json +1 -7
  127. package/examples/pwsh-demo/src/main.test.ts +1 -4
  128. package/examples/pwsh-demo/src/main.ts +14 -4
  129. package/examples/transport-launcher.ts +85 -0
  130. package/package.json +43 -49
  131. package/dist/runtime/bun.d.mts +0 -28
  132. package/dist/runtime/bun.mjs +0 -1
  133. package/dist/runtime/support.d.mts +0 -42
  134. package/dist/runtime/support.mjs +0 -1
  135. package/src/core/cli/dispatch.ts +0 -349
  136. package/src/core/cli/help-links.ts +0 -82
  137. package/src/core/cli/index.ts +0 -1413
  138. package/src/core/cli/planner.ts +0 -487
  139. package/src/core/cli/plugin.ts +0 -117
  140. package/src/core/cli/propagate.ts +0 -97
  141. package/src/core/cli/root-help.ts +0 -252
  142. package/src/core/cli/root-surface.ts +0 -74
  143. package/src/core/cli/runtime-preflight.ts +0 -438
  144. package/src/core/completion/index.ts +0 -88
  145. package/src/core/completion/shells/bash.ts +0 -459
  146. package/src/core/completion/shells/fish.ts +0 -259
  147. package/src/core/completion/shells/powershell.ts +0 -439
  148. package/src/core/completion/shells/shared.ts +0 -283
  149. package/src/core/completion/shells/zsh.ts +0 -381
  150. package/src/core/config/index.ts +0 -417
  151. package/src/core/config/package-json.ts +0 -560
  152. package/src/core/errors/index.ts +0 -193
  153. package/src/core/execution/index.ts +0 -268
  154. package/src/core/help/ansi.ts +0 -144
  155. package/src/core/help/index.ts +0 -516
  156. package/src/core/json-schema/index.ts +0 -891
  157. package/src/core/json-schema/meta-descriptions.generated.ts +0 -185
  158. package/src/core/output/activity.ts +0 -575
  159. package/src/core/output/bind.ts +0 -59
  160. package/src/core/output/contracts.ts +0 -194
  161. package/src/core/output/display-value.ts +0 -48
  162. package/src/core/output/index.ts +0 -704
  163. package/src/core/output/renderers.ts +0 -125
  164. package/src/core/output/writer.ts +0 -46
  165. package/src/core/parse/index.ts +0 -681
  166. package/src/core/prompt/index.ts +0 -583
  167. package/src/core/resolve/args.ts +0 -144
  168. package/src/core/resolve/coerce.ts +0 -432
  169. package/src/core/resolve/config.ts +0 -26
  170. package/src/core/resolve/contracts.ts +0 -135
  171. package/src/core/resolve/errors.ts +0 -227
  172. package/src/core/resolve/flags.ts +0 -255
  173. package/src/core/resolve/index.ts +0 -108
  174. package/src/core/resolve/property.ts +0 -99
  175. package/src/core/schema/activity.ts +0 -240
  176. package/src/core/schema/arg.ts +0 -719
  177. package/src/core/schema/command.ts +0 -1498
  178. package/src/core/schema/flag.ts +0 -801
  179. package/src/core/schema/index.ts +0 -91
  180. package/src/core/schema/middleware.ts +0 -171
  181. package/src/core/schema/prompt.ts +0 -156
  182. package/src/core/schema/run.ts +0 -198
  183. package/src/core/schema-dsl/index.ts +0 -109
  184. package/src/core/schema-dsl/parse.ts +0 -262
  185. package/src/core/schema-dsl/runtime.ts +0 -609
  186. package/src/core/schema-dsl/to-json-schema.ts +0 -102
  187. package/src/core/testkit/index.ts +0 -123
  188. package/src/index.ts +0 -179
  189. package/src/runtime/adapter.ts +0 -303
  190. package/src/runtime/auto.ts +0 -110
  191. package/src/runtime/bun.ts +0 -55
  192. package/src/runtime/deno.ts +0 -369
  193. package/src/runtime/detect.ts +0 -105
  194. package/src/runtime/index.ts +0 -21
  195. package/src/runtime/node-builtins.d.ts +0 -27
  196. package/src/runtime/node.ts +0 -300
  197. package/src/runtime/paths.ts +0 -64
  198. package/src/runtime/support.ts +0 -135
  199. package/src/runtime.ts +0 -31
  200. package/src/strings.ts +0 -34
  201. package/src/testkit.ts +0 -25
  202. package/src/version.ts +0 -20
@@ -1,719 +0,0 @@
1
- /**
2
- * Positional argument schema builder with full type inference.
3
- *
4
- * Each factory (`arg.string()`, `arg.number()`, `arg.enum()`, `arg.custom()`) returns an
5
- * immutable {@linkcode ArgBuilder} whose generic parameter tracks the value type,
6
- * presence state, and variadic flag through the fluent chain.
7
- *
8
- * @module dreamcli/core/schema/arg
9
- */
10
-
11
- // --- Type-level configuration (phantom state tracked through the chain)
12
-
13
- /** All arg presence states as a runtime array. */
14
- const ARG_PRESENCES = ['required', 'optional', 'defaulted'] as const;
15
-
16
- /**
17
- * Presence describes whether a positional arg is guaranteed to exist when the
18
- * action handler runs:
19
- *
20
- * - `'required'` — must be supplied; error if missing (default)
21
- * - `'optional'` — may be `undefined` if not supplied
22
- * - `'defaulted'` — always present (falls back to default value)
23
- */
24
- type ArgPresence = (typeof ARG_PRESENCES)[number];
25
-
26
- /**
27
- * Compile-time state carried through the builder chain.
28
- */
29
- interface ArgConfig {
30
- /** The resolved value type (e.g. `string`, `number`, custom). */
31
- readonly valueType: unknown;
32
- /** Whether the arg is required, optional, or has a default. */
33
- readonly presence: ArgPresence;
34
- /** Whether this arg consumes remaining positionals. */
35
- readonly variadic: boolean;
36
- }
37
-
38
- // --- Type-level helpers
39
-
40
- /**
41
- * Advanced type helper used by {@linkcode ArgBuilder} modifiers to replace presence.
42
- * Most consumers rely on inference and never reference this directly.
43
- */
44
- type WithArgPresence<C extends ArgConfig, P extends ArgPresence> = {
45
- readonly valueType: C['valueType'];
46
- readonly presence: P;
47
- readonly variadic: C['variadic'];
48
- };
49
-
50
- /**
51
- * Advanced type helper used by {@linkcode ArgBuilder.variadic | ArgBuilder.variadic()}.
52
- * Most consumers rely on inference and never reference this directly.
53
- */
54
- type WithVariadic<C extends ArgConfig> = {
55
- readonly valueType: C['valueType'];
56
- readonly presence: C['presence'];
57
- readonly variadic: true;
58
- };
59
-
60
- /**
61
- * Compute the final value type from config — this is what handlers receive.
62
- *
63
- * Advanced type helper: this powers {@link InferArg} and action-handler
64
- * inference. Most apps do not need to mention it explicitly.
65
- *
66
- * Variadic args always produce an array. Non-variadic:
67
- * - `'optional'` → `T | undefined`
68
- * - `'required'` → `T`
69
- * - `'defaulted'` → `T`
70
- */
71
- type ResolvedArgValue<C extends ArgConfig> = C['variadic'] extends true
72
- ? C['valueType'][]
73
- : C['presence'] extends 'optional'
74
- ? C['valueType'] | undefined
75
- : C['valueType'];
76
-
77
- /** Extract the resolved value type from an {@linkcode ArgBuilder}. */
78
- type InferArg<B> = B extends ArgBuilder<infer C extends ArgConfig> ? ResolvedArgValue<C> : never;
79
-
80
- /** Extract resolved value types from a record of builders. */
81
- type InferArgs<T extends Record<string, ArgBuilder<ArgConfig>>> = {
82
- [K in keyof T]: InferArg<T[K]>;
83
- };
84
-
85
- // --- Runtime schema data
86
-
87
- /** All arg kind discriminators as a runtime array. */
88
- const ARG_KINDS = ['string', 'number', 'enum', 'custom'] as const;
89
-
90
- /** Discriminator for the kind of value an arg accepts. */
91
- type ArgKind = (typeof ARG_KINDS)[number];
92
-
93
- /** Custom parse function for `arg.custom()`. */
94
- type ArgParseFn<T> = (raw: string) => T;
95
-
96
- /**
97
- * The runtime descriptor stored inside every {@linkcode ArgBuilder}. Consumers (parser,
98
- * help generator) read this to understand the arg's shape without touching
99
- * generics.
100
- */
101
- interface ArgSchema {
102
- /** What kind of value this arg accepts. */
103
- readonly kind: ArgKind;
104
- /** Current presence state. */
105
- readonly presence: ArgPresence;
106
- /** Whether this arg consumes all remaining positionals. */
107
- readonly variadic: boolean;
108
- /**
109
- * Whether this arg may read from stdin during resolution.
110
- * @defaultValue `false`
111
- */
112
- readonly stdinMode: boolean;
113
- /** Runtime default value (if any). */
114
- readonly defaultValue: unknown;
115
- /** Human-readable description for help text. */
116
- readonly description: string | undefined;
117
- /**
118
- * Environment variable name for env resolution.
119
- *
120
- * When set and the CLI value is absent, the resolver reads this env var
121
- * and coerces the string to the arg's declared kind.
122
- *
123
- * @see {@link ArgBuilder.env} for the builder method.
124
- */
125
- readonly envVar: string | undefined;
126
- /** Allowed literal values when `kind === 'enum'`. */
127
- readonly enumValues: readonly string[] | undefined;
128
- /** Custom parse function (only when `kind === 'custom'`). */
129
- readonly parseFn: ArgParseFn<unknown> | undefined;
130
- /**
131
- * Deprecation marker.
132
- *
133
- * - `undefined` — not deprecated (default)
134
- * - `true` — deprecated with no migration message
135
- * - `string` — deprecated with a reason/migration message
136
- *
137
- * When a deprecated arg is used, a warning is emitted to stderr.
138
- * Help text shows `[deprecated]` or `[deprecated: <reason>]`.
139
- */
140
- readonly deprecated: string | true | undefined;
141
- }
142
-
143
- /**
144
- * Create a raw {@link ArgSchema} object with sensible defaults.
145
- *
146
- * Most consumers should prefer the higher-level {@link arg} factory, which
147
- * returns an immutable {@link ArgBuilder} with type inference and fluent
148
- * modifiers. `createArgSchema()` exists for advanced schema composition,
149
- * targeted tests, or custom builders that need the plain runtime descriptor.
150
- *
151
- * `overrides` are shallow-merged on top of the default shape, so callers are
152
- * responsible for preserving invariants such as variadic ordering and
153
- * compatible `parseFn` / `kind` combinations.
154
- *
155
- * @param kind - Discriminator for the value type this arg accepts.
156
- * @param overrides - Partial schema fields shallow-merged onto defaults.
157
- * @returns A fully populated {@link ArgSchema}.
158
- *
159
- * @example
160
- * ```ts
161
- * const schema = createArgSchema('custom', {
162
- * description: 'Hex color',
163
- * parseFn: (raw) => `#${raw}`,
164
- * });
165
- * ```
166
- */
167
- function createArgSchema(kind: ArgKind, overrides?: Partial<ArgSchema>): ArgSchema {
168
- return {
169
- kind,
170
- presence: 'required',
171
- variadic: false,
172
- stdinMode: false,
173
- defaultValue: undefined,
174
- description: undefined,
175
- envVar: undefined,
176
- enumValues: undefined,
177
- parseFn: undefined,
178
- deprecated: undefined,
179
- ...overrides,
180
- };
181
- }
182
-
183
- // --- ArgBuilder — immutable builder with type-level tracking
184
-
185
- /**
186
- * Immutable positional argument schema builder.
187
- *
188
- * The type parameter `C` is a phantom that tracks the value type, presence,
189
- * and variadic state through the fluent chain. Each modifier returns a **new**
190
- * builder — the original is never mutated.
191
- *
192
- * @example
193
- * ```ts
194
- * // Full command with multiple args and modifiers
195
- * import { command, arg } from '@kjanat/dreamcli';
196
- *
197
- * command('deploy')
198
- * .arg('target', arg.string()
199
- * .env('DEPLOY_TARGET')
200
- * .describe('Deploy target'))
201
- * .arg('port', arg.number()
202
- * .env('PORT')
203
- * .default(3000)
204
- * .describe('Port number'))
205
- * .arg('files', arg.string()
206
- * .variadic()
207
- * .optional()
208
- * .describe('Extra config files'))
209
- * .action(({ args }) => {
210
- * args.target; // string (required, from CLI or $DEPLOY_TARGET)
211
- * args.port; // number (defaulted, from CLI, $PORT, or 3000)
212
- * args.files; // string[] (optional variadic)
213
- * });
214
- * ```
215
- *
216
- * @example
217
- * ```ts
218
- * // Type inference
219
- * const target = arg.string();
220
- * type T = InferArg<typeof target>; // string
221
- *
222
- * const opt = arg.string().optional();
223
- * type O = InferArg<typeof opt>; // string | undefined
224
- *
225
- * const files = arg.string().variadic();
226
- * type F = InferArg<typeof files>; // string[]
227
- * ```
228
- */
229
- class ArgBuilder<C extends ArgConfig> {
230
- /** @internal Runtime schema descriptor. */
231
- readonly schema: ArgSchema;
232
-
233
- /**
234
- * @internal Type brand — exists only in the type system (`declare`
235
- * produces no runtime property). Used by {@linkcode InferArg} / {@linkcode InferArgs}.
236
- */
237
- declare readonly _config: C;
238
-
239
- /**
240
- * Create an arg builder from a pre-built schema descriptor.
241
- *
242
- * @param schema - Runtime descriptor for this positional argument.
243
- */
244
- constructor(schema: ArgSchema) {
245
- this.schema = schema;
246
- }
247
-
248
- // -- Presence modifiers --------------------------------------------------
249
-
250
- /**
251
- * Mark the arg as required (this is the default for positional args).
252
- * Produces an error if no value resolves from any configured source
253
- * (CLI → stdin → env → default).
254
- *
255
- * @example
256
- * ```ts
257
- * arg.string().required() // explicit, same as default
258
- *
259
- * // In a command — omitting <target> causes a ValidationError:
260
- * command('deploy')
261
- * .arg('target', arg.string().required().describe('Deploy target'))
262
- * // $ mycli deploy
263
- * // Error: Missing required argument <target>
264
- * ```
265
- *
266
- * @returns The builder (for chaining).
267
- */
268
- required(): ArgBuilder<WithArgPresence<C, 'required'>> {
269
- return new ArgBuilder({
270
- ...this.schema,
271
- presence: 'required',
272
- });
273
- }
274
-
275
- /**
276
- * Mark the arg as optional. Handlers receive `undefined` when absent.
277
- *
278
- * @example
279
- * ```ts
280
- * arg.string().optional()
281
- *
282
- * // In a command — handler receives `undefined` when omitted:
283
- * command('greet')
284
- * .arg('name', arg.string().optional().describe('Who to greet'))
285
- * .action(({ args }) => {
286
- * args.name; // string | undefined
287
- * });
288
- * // $ mycli greet → args.name is undefined
289
- * // $ mycli greet Alice → args.name is 'Alice'
290
- * ```
291
- *
292
- * @returns The builder (for chaining).
293
- */
294
- optional(): ArgBuilder<WithArgPresence<C, 'optional'>> {
295
- return new ArgBuilder({
296
- ...this.schema,
297
- presence: 'optional',
298
- });
299
- }
300
-
301
- /**
302
- * Provide a default value. The arg becomes "always present" — handlers
303
- * will never see `undefined`.
304
- *
305
- * The generic constraint `V extends C['valueType']` ensures the default
306
- * matches the arg's declared type.
307
- *
308
- * Resolution order when extra sources are configured: CLI → stdin → env → **default**.
309
- *
310
- * @param value - Fallback used when no CLI value or env var resolves.
311
- * @returns The builder (for chaining).
312
- *
313
- * @example
314
- * ```ts
315
- * arg.string().default('production')
316
- * arg.number().default(3000)
317
- *
318
- * // In a command — default kicks in when CLI and env are both absent:
319
- * command('deploy')
320
- * .arg('env', arg.string()
321
- * .env('DEPLOY_ENV')
322
- * .default('staging')
323
- * .describe('Target environment'))
324
- * .action(({ args }) => {
325
- * args.env; // string (never undefined)
326
- * });
327
- * // $ mycli deploy → 'staging' (default)
328
- * // $ DEPLOY_ENV=prod mycli deploy → 'prod' (env)
329
- * // $ mycli deploy production → 'production' (CLI)
330
- * ```
331
- */
332
- default<V extends C['valueType']>(value: V): ArgBuilder<WithArgPresence<C, 'defaulted'>> {
333
- return new ArgBuilder({
334
- ...this.schema,
335
- presence: 'defaulted',
336
- defaultValue: value,
337
- });
338
- }
339
-
340
- // -- Variadic modifier ---------------------------------------------------
341
-
342
- /**
343
- * Mark this arg as variadic — it consumes all remaining positional
344
- * arguments. The inferred type becomes `T[]`.
345
- *
346
- * A variadic arg must be the last positional in a command.
347
- *
348
- * @example
349
- * ```ts
350
- * arg.string().variadic()
351
- *
352
- * // In a command — collects all remaining positionals:
353
- * command('build')
354
- * .arg('entry', arg.string().describe('Main entry'))
355
- * .arg('extras', arg.string().variadic().optional().describe('Extra files'))
356
- * .action(({ args }) => {
357
- * args.entry; // string
358
- * args.extras; // string[]
359
- * });
360
- * // $ mycli build main.ts a.ts b.ts
361
- * // → entry = 'main.ts', extras = ['a.ts', 'b.ts']
362
- * ```
363
- *
364
- * @returns The builder (for chaining).
365
- */
366
- variadic(): ArgBuilder<WithVariadic<C>> {
367
- return new ArgBuilder({
368
- ...this.schema,
369
- variadic: true,
370
- });
371
- }
372
-
373
- /**
374
- * Allow this arg to resolve from stdin when CLI input is missing.
375
- *
376
- * Resolution order becomes: CLI value -> stdin -> env -> default.
377
- * Only one arg per command may enable stdin mode.
378
- *
379
- * @returns The builder (for chaining).
380
- *
381
- * @example
382
- * ```ts
383
- * arg.string().describe('Input text').stdin()
384
- * // $ echo "hello" | mycli transform → input = 'hello' (from stdin)
385
- * // $ mycli transform "hello" → input = 'hello' (from CLI)
386
- * ```
387
- */
388
- stdin(): ArgBuilder<C> {
389
- return new ArgBuilder({
390
- ...this.schema,
391
- stdinMode: true,
392
- });
393
- }
394
-
395
- // -- Resolution source modifiers ------------------------------------------
396
-
397
- /**
398
- * Bind to an environment variable.
399
- *
400
- * When the arg is not provided on the CLI, the resolver checks this
401
- * env var before falling back to the default value. The env string is
402
- * coerced to the arg's declared kind (passthrough for strings, parsed
403
- * for numbers, run through `parseFn` for custom args).
404
- *
405
- * Resolution order when extra sources are configured: **CLI → stdin → env → default**.
406
- *
407
- * Help output shows `[env: VAR]` next to the arg description.
408
- *
409
- * @param varName - Environment variable name (e.g. `'DEPLOY_TARGET'`).
410
- * @returns The builder (for chaining).
411
- *
412
- * @example
413
- * ```ts
414
- * command('deploy')
415
- * .arg('target', arg.string().env('DEPLOY_TARGET').describe('Deploy target'))
416
- * .arg('port', arg.number().env('PORT').default(3000))
417
- * .action(({ args }) => {
418
- * console.log(args.target); // from CLI, $DEPLOY_TARGET, or error
419
- * console.log(args.port); // from CLI, $PORT, or 3000
420
- * });
421
- * ```
422
- */
423
- env(varName: string): ArgBuilder<C> {
424
- return new ArgBuilder({
425
- ...this.schema,
426
- envVar: varName,
427
- });
428
- }
429
-
430
- // -- Metadata modifiers --------------------------------------------------
431
-
432
- /**
433
- * Human-readable description shown in help output.
434
- *
435
- * @param description - Text displayed next to the arg in `--help`.
436
- * @returns The builder (for chaining).
437
- *
438
- * @example
439
- * ```ts
440
- * arg.string().describe('Deploy target')
441
- *
442
- * // Help output:
443
- * // Arguments:
444
- * // <target> Deploy target
445
- * ```
446
- */
447
- describe(description: string): ArgBuilder<C> {
448
- return new ArgBuilder({
449
- ...this.schema,
450
- description,
451
- });
452
- }
453
-
454
- /**
455
- * Mark this arg as deprecated.
456
- *
457
- * When used, a warning is emitted to stderr. Help text shows
458
- * `[deprecated]` or `[deprecated: <reason>]`.
459
- *
460
- * Does not change the arg's type-level config — it's metadata only.
461
- *
462
- * @param message - Optional migration reason/guidance.
463
- * @returns The builder (for chaining).
464
- *
465
- * @example
466
- * ```ts
467
- * arg.string().deprecated() // generic
468
- * arg.string().deprecated('use --target flag instead') // with guidance
469
- *
470
- * // Help output:
471
- * // Arguments:
472
- * // <target> Deploy target [deprecated: use --target flag instead]
473
- * ```
474
- */
475
- deprecated(message?: string): ArgBuilder<C> {
476
- return new ArgBuilder({
477
- ...this.schema,
478
- deprecated: message ?? true,
479
- });
480
- }
481
- }
482
-
483
- // --- Factory namespace
484
-
485
- /**
486
- * Arg factory functions — the public API for creating positional arguments.
487
- *
488
- * Each method returns an {@linkcode ArgBuilder} seeded with the correct {@linkcode ArgKind}
489
- * and initial type-level config. Chain modifiers (`.optional()`, `.env()`,
490
- * `.default()`, `.variadic()`, `.stdin()`, `.describe()`, `.deprecated()`) to refine.
491
- *
492
- * All args are **required** by default. Resolution order when extra
493
- * sources are configured: **CLI → stdin → env → default**.
494
- *
495
- * @example Overview — all three kinds with common modifier patterns
496
- * ```ts
497
- * command('process')
498
- * // String arg with env fallback
499
- * .arg('input', arg.string()
500
- * .env('INPUT_FILE')
501
- * .describe('Input file path'))
502
- *
503
- * // Number arg with env + default
504
- * .arg('concurrency', arg.number()
505
- * .env('CONCURRENCY')
506
- * .default(4)
507
- * .describe('Worker threads'))
508
- *
509
- * // Custom arg — hex color parser with env
510
- * .arg('color', arg.custom((raw) => {
511
- * if (!/^#?[0-9a-f]{6}$/i.test(raw)) throw new Error('bad hex');
512
- * return raw.startsWith('#') ? raw : `#${raw}`;
513
- * })
514
- * .env('THEME_COLOR')
515
- * .optional()
516
- * .describe('Theme color (hex)'))
517
- *
518
- * .action(({ args }) => {
519
- * args.input; // string (required)
520
- * args.concurrency; // number (defaulted)
521
- * args.color; // string | undefined (optional custom)
522
- * });
523
- * ```
524
- */
525
- interface ArgFactory {
526
- /**
527
- * String-valued positional argument. Required by default.
528
- *
529
- * @example
530
- * ```ts
531
- * arg.string() // required string
532
- * arg.string().optional() // string | undefined
533
- * arg.string().env('TARGET') // falls back to $TARGET
534
- * arg.string().default('production') // always present
535
- *
536
- * // In a command:
537
- * command('deploy')
538
- * .arg('target', arg.string().env('DEPLOY_TARGET').describe('Deploy target'))
539
- * ```
540
- *
541
- * @returns A required string {@link ArgBuilder}.
542
- */
543
- string(): ArgBuilder<{
544
- readonly valueType: string;
545
- readonly presence: 'required';
546
- readonly variadic: false;
547
- }>;
548
-
549
- /**
550
- * Number-valued positional argument. Required by default.
551
- *
552
- * The parser coerces the raw CLI string to a number and emits a
553
- * `ParseError` if conversion fails. Env values are coerced the same way.
554
- *
555
- * @example
556
- * ```ts
557
- * arg.number() // required number
558
- * arg.number().default(8080) // defaults to 8080
559
- * arg.number().env('PORT') // falls back to $PORT (coerced)
560
- *
561
- * // In a command:
562
- * command('serve')
563
- * .arg('port', arg.number().env('PORT').default(3000).describe('Port'))
564
- * // $ mycli serve 8080 → 8080
565
- * // $ PORT=9090 mycli serve → 9090
566
- * // $ mycli serve → 3000
567
- * ```
568
- *
569
- * @returns A required number {@link ArgBuilder}.
570
- */
571
- number(): ArgBuilder<{
572
- readonly valueType: number;
573
- readonly presence: 'required';
574
- readonly variadic: false;
575
- }>;
576
-
577
- /**
578
- * Enum-valued positional argument. Required by default.
579
- *
580
- * Accepts only the listed string literals. The inferred type is the
581
- * union of those literals (e.g. `'us' | 'eu' | 'ap'`), not `string`.
582
- * Invalid values produce a `ParseError` listing allowed options.
583
- *
584
- * @param values - Non-empty tuple of allowed string values.
585
- * @returns A required enum {@link ArgBuilder} typed to the union of `values`.
586
- *
587
- * @example
588
- * ```ts
589
- * arg.enum(['us', 'eu', 'ap']) // required, 'us' | 'eu' | 'ap'
590
- * arg.enum(['dev', 'prod']).default('dev') // defaulted
591
- * arg.enum(['json', 'csv']).optional() // 'json' | 'csv' | undefined
592
- *
593
- * // In a command:
594
- * command('deploy')
595
- * .arg('region', arg.enum(['us', 'eu', 'ap']).env('REGION').describe('Target region'))
596
- * // $ mycli deploy us → 'us'
597
- * // $ mycli deploy invalid → ParseError: Allowed: us, eu, ap
598
- * ```
599
- */
600
- enum<const T extends readonly [string, ...string[]]>(
601
- values: T,
602
- ): ArgBuilder<{
603
- readonly valueType: T[number];
604
- readonly presence: 'required';
605
- readonly variadic: false;
606
- }>;
607
-
608
- /**
609
- * Custom-parsed positional argument. Required by default.
610
- *
611
- * The parse function receives the raw string and must return a value of
612
- * type `T`. Throw an `Error` (or `ParseError`) to signal invalid input.
613
- * The same parse function is used for both CLI and env values.
614
- *
615
- * @param parseFn - Converts the raw CLI string to `T`.
616
- * @returns A required custom {@link ArgBuilder} typed to the return of `parseFn`.
617
- *
618
- * @example
619
- * ```ts
620
- * arg.custom((raw) => Number.parseInt(raw, 16))
621
- * // inferred type: number
622
- *
623
- * // In a command — parse hex color from CLI or $COLOR:
624
- * command('theme')
625
- * .arg('color', arg.custom((raw) => {
626
- * if (!/^[0-9a-f]{6}$/i.test(raw)) throw new Error('Expected 6-digit hex');
627
- * return `#${raw}`;
628
- * }).env('COLOR').describe('Hex color code'))
629
- * // $ mycli theme ff6600 → '#ff6600'
630
- * // $ COLOR=aabbcc mycli theme → '#aabbcc'
631
- * ```
632
- */
633
- custom<T>(parseFn: ArgParseFn<T>): ArgBuilder<{
634
- readonly valueType: T;
635
- readonly presence: 'required';
636
- readonly variadic: false;
637
- }>;
638
- }
639
-
640
- /**
641
- * Positional argument schema factory.
642
- *
643
- * Entry point for defining args on a command. Use `arg.<kind>()` to create
644
- * an {@linkcode ArgBuilder}, then chain modifiers and pass the result to
645
- * `command().arg(name, builder)`.
646
- *
647
- * Four kinds are available:
648
- * - `arg.string()` — raw string (most common)
649
- * - `arg.number()` — parsed to number, errors on NaN
650
- * - `arg.enum(values)` — constrained to listed literals
651
- * - `arg.custom(fn)` — arbitrary parse function, infers return type
652
- *
653
- * @example
654
- * ```ts
655
- * import { command, arg } from '@kjanat/dreamcli';
656
- *
657
- * command('deploy')
658
- * .arg('target', arg.string().env('DEPLOY_TARGET').describe('Where to deploy'))
659
- * .arg('port', arg.number().env('PORT').default(3000))
660
- * .action(({ args }) => {
661
- * console.log(`Deploying to ${args.target} on port ${args.port}`);
662
- * });
663
- *
664
- * // $ mycli deploy production 8080 → target='production', port=8080
665
- * // $ DEPLOY_TARGET=staging mycli deploy → target='staging', port=3000
666
- * ```
667
- */
668
- const arg: ArgFactory = {
669
- string(): ArgBuilder<{
670
- readonly valueType: string;
671
- readonly presence: 'required';
672
- readonly variadic: false;
673
- }> {
674
- return new ArgBuilder(createArgSchema('string'));
675
- },
676
-
677
- number(): ArgBuilder<{
678
- readonly valueType: number;
679
- readonly presence: 'required';
680
- readonly variadic: false;
681
- }> {
682
- return new ArgBuilder(createArgSchema('number'));
683
- },
684
-
685
- enum<const T extends readonly [string, ...string[]]>(
686
- values: T,
687
- ): ArgBuilder<{
688
- readonly valueType: T[number];
689
- readonly presence: 'required';
690
- readonly variadic: false;
691
- }> {
692
- return new ArgBuilder(createArgSchema('enum', { enumValues: values }));
693
- },
694
-
695
- custom<T>(parseFn: ArgParseFn<T>): ArgBuilder<{
696
- readonly valueType: T;
697
- readonly presence: 'required';
698
- readonly variadic: false;
699
- }> {
700
- return new ArgBuilder(createArgSchema('custom', { parseFn: parseFn as ArgParseFn<unknown> }));
701
- },
702
- };
703
-
704
- // --- Exports
705
-
706
- export type {
707
- ArgConfig,
708
- ArgFactory,
709
- ArgKind,
710
- ArgParseFn,
711
- ArgPresence,
712
- ArgSchema,
713
- InferArg,
714
- InferArgs,
715
- ResolvedArgValue,
716
- WithArgPresence,
717
- WithVariadic,
718
- };
719
- export { ARG_KINDS, ARG_PRESENCES, ArgBuilder, arg, createArgSchema };