clap-ts 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/types.d.ts CHANGED
@@ -4,8 +4,21 @@
4
4
  */
5
5
  /** Argument value type - matches clap's value_parser types. */
6
6
  export type ArgType = 'boolean' | 'string' | 'number' | 'enum' | 'positional';
7
- /** How an argument collects values. */
8
- export type ArgAction = 'set' | 'append' | 'count';
7
+ /**
8
+ * How an argument collects values.
9
+ * - set: replace (default)
10
+ * - append: collect into an array
11
+ * - count: number of occurrences
12
+ * - setTrue / setFalse: force a boolean regardless of the flag's polarity
13
+ * - help / helpShort / helpLong / version: trigger the built-in output
14
+ */
15
+ export type ArgAction = 'set' | 'append' | 'count' | 'setTrue' | 'setFalse' | 'help' | 'helpShort' | 'helpLong' | 'version';
16
+ /**
17
+ * Where a parsed value came from. Extends clap's ValueSource with 'config',
18
+ * which sits between an environment variable and a default, and 'prompt' for a
19
+ * value the user typed when asked.
20
+ */
21
+ export type ValueSource = 'cli' | 'env' | 'config' | 'prompt' | 'default';
9
22
  /** Min/max constraint for number of values an argument accepts. */
10
23
  export interface NumArgs {
11
24
  readonly min: number;
@@ -13,16 +26,35 @@ export interface NumArgs {
13
26
  }
14
27
  /** Custom value parser function. Receives raw string, returns parsed value or throws. */
15
28
  export type ValueParserFn = (value: string) => unknown;
29
+ /** One allowed value for an argument, with optional help text and aliases. */
30
+ export interface PossibleValue {
31
+ /** The canonical value as it appears in help and completions. */
32
+ readonly name: string;
33
+ /** Description shown next to the value in long help. */
34
+ readonly help?: string;
35
+ /** Additional accepted spellings, not shown in help. */
36
+ readonly aliases?: readonly string[];
37
+ /** Accept the value but keep it out of help and completions. */
38
+ readonly hidden?: boolean;
39
+ }
16
40
  /** Hint for shell completion behavior -- guides what kind of values to complete. */
17
- export type ValueHint = 'filePath' | 'dirPath' | 'anyPath' | 'executablePath' | 'commandName' | 'hostname' | 'username' | 'url' | 'emailAddress';
41
+ export type ValueHint = 'unknown' | 'other' | 'filePath' | 'dirPath' | 'anyPath' | 'executablePath' | 'commandName' | 'commandString' | 'commandWithArguments' | 'hostname' | 'username' | 'url' | 'emailAddress';
42
+ /** When to colourise help and error output. */
43
+ export type ColorChoice = 'auto' | 'always' | 'never';
18
44
  /** Supported shells for completion script generation. */
19
- export type Shell = 'bash' | 'zsh' | 'fish' | 'powershell';
45
+ export type Shell = 'bash' | 'zsh' | 'fish' | 'powershell' | 'elvish' | 'nushell';
20
46
  /** Full argument definition - matches clap::Arg. */
21
47
  export interface ArgDef {
22
48
  /** Value type for this argument. */
23
49
  readonly type: ArgType;
24
50
  /** Human-readable description shown in help. */
25
51
  readonly description?: string;
52
+ /** Extended description shown with --help but not with -h. */
53
+ readonly longDescription?: string;
54
+ /** Sort key within the arg's help section; lower values come first. */
55
+ readonly displayOrder?: number;
56
+ /** Render the description on its own line beneath the flag. */
57
+ readonly nextLineHelp?: boolean;
26
58
  /** Short flag character (e.g., 'v' for -v). */
27
59
  readonly short?: string;
28
60
  /** Long flag name (e.g., 'verbose' for --verbose). Defaults to the arg key. */
@@ -35,11 +67,19 @@ export interface ArgDef {
35
67
  readonly default?: string | number | boolean | readonly string[];
36
68
  /** Value to use when the flag is present but no value given (e.g., --port vs --port=8080). */
37
69
  readonly defaultMissingValue?: string | number | boolean;
70
+ /** Values to use for a multi-value arg present without values. */
71
+ readonly defaultMissingValues?: readonly string[];
38
72
  /**
39
73
  * Conditional default: [otherArgName, otherArgValue, defaultValue].
40
74
  * If the other arg equals the given value, this default is applied.
41
75
  */
42
76
  readonly defaultValueIf?: readonly [string, string, string | number | boolean];
77
+ /** Several conditional defaults; the first whose condition holds is applied. */
78
+ readonly defaultValueIfs?: readonly (readonly [
79
+ string,
80
+ string,
81
+ string | number | boolean
82
+ ])[];
43
83
  /** Whether this argument is required. */
44
84
  readonly required?: boolean;
45
85
  /**
@@ -52,6 +92,12 @@ export interface ArgDef {
52
92
  * Makes this arg required when the condition is met.
53
93
  */
54
94
  readonly requiredIfEq?: readonly [string, string];
95
+ /** Required when ANY of these [argName, argValue] conditions holds. */
96
+ readonly requiredIfEqAny?: readonly (readonly [string, string])[];
97
+ /** Required when ALL of these [argName, argValue] conditions hold. */
98
+ readonly requiredIfEqAll?: readonly (readonly [string, string])[];
99
+ /** Required unless ALL of the named args are present. */
100
+ readonly requiredUnlessPresentAll?: readonly string[];
55
101
  /** Cannot be used with ANY other argument. */
56
102
  readonly exclusive?: boolean;
57
103
  /** Global arg -- inherited by all subcommands. */
@@ -60,30 +106,69 @@ export interface ArgDef {
60
106
  readonly env?: string;
61
107
  /** Display name for the value in help (e.g., "PATH", "PORT"). */
62
108
  readonly valueName?: string;
109
+ /** Per-value display names for a multi-value arg (e.g., ['X', 'Y', 'Z']). */
110
+ readonly valueNames?: readonly string[];
63
111
  /**
64
112
  * Value validation/parsing. Either:
65
- * - A string array of allowed values (enum-like restriction), or
113
+ * - An array of allowed values, each a plain string or a PossibleValue, or
66
114
  * - A function that parses/validates the raw string value (throw to reject).
67
115
  */
68
- readonly valueParser?: readonly string[] | ValueParserFn;
116
+ readonly valueParser?: readonly (string | PossibleValue)[] | ValueParserFn;
117
+ /** Match allowed values case-insensitively. The input is kept as typed. */
118
+ readonly ignoreCase?: boolean;
69
119
  /** Character to split values on (e.g., ',' for --tags=a,b,c). */
70
120
  readonly valueDelimiter?: string;
121
+ /** Require `--flag=value` form; reject `--flag value`. */
122
+ readonly requireEquals?: boolean;
123
+ /** Token that ends value collection for a multi-value arg (e.g., ';'). */
124
+ readonly valueTerminator?: string;
71
125
  /** Min/max number of values this arg accepts. */
72
126
  readonly numArgs?: NumArgs;
73
127
  /** Names of args that conflict with this one (mutually exclusive). */
74
128
  readonly conflictsWith?: readonly string[];
75
129
  /** Names of args that must also be present when this one is used. */
76
130
  readonly requires?: readonly string[];
131
+ /** Names of args this one overrides. The argument given later wins. */
132
+ readonly overridesWith?: readonly string[];
133
+ /** Requires the named arg only when this one equals a value: [value, argName]. */
134
+ readonly requiresIf?: readonly [string, string];
135
+ /** Several conditional requirements, each [thisValue, requiredArgName]. */
136
+ readonly requiresIfs?: readonly (readonly [string, string])[];
137
+ /** Argument group name this arg belongs to. */
138
+ readonly group?: string;
139
+ /** Argument group names this arg belongs to. */
140
+ readonly groups?: readonly string[];
77
141
  /** How values are collected: set (replace), append (collect into array), count. */
78
142
  readonly action?: ArgAction;
79
143
  /** Hide this argument from all help output. */
80
144
  readonly hidden?: boolean;
145
+ /**
146
+ * Mark the argument deprecated. Using it warns on stderr and help labels it.
147
+ * A string is used as the warning's reason.
148
+ */
149
+ readonly deprecated?: boolean | string;
150
+ /**
151
+ * Name of the argument that supersedes this one. Named in the warning, and
152
+ * the value is forwarded there when that argument was not given itself.
153
+ */
154
+ readonly replacedBy?: string;
81
155
  /** Hide this argument from short help (-h) only. */
82
156
  readonly hideShortHelp?: boolean;
83
157
  /** Hide this argument from long help (--help) only. */
84
158
  readonly hideLongHelp?: boolean;
85
- /** Hide possible values list from help (when valueParser is string[]). */
159
+ /** Hide possible values list from help (when valueParser is an array). */
86
160
  readonly hidePossibleValues?: boolean;
161
+ /** Hide the [default: ...] note from help. */
162
+ readonly hideDefaultValue?: boolean;
163
+ /** Hide the [env: ...] note from help. */
164
+ readonly hideEnv?: boolean;
165
+ /**
166
+ * Treat the value as sensitive: masked when prompted for, and its
167
+ * environment variable's value kept out of help.
168
+ */
169
+ readonly secret?: boolean;
170
+ /** Show [env: VAR] in help without the variable's current value. */
171
+ readonly hideEnvValues?: boolean;
87
172
  /** Description for the --no-X variant of boolean flags. */
88
173
  readonly negativeDescription?: string;
89
174
  /** Accept values that start with a hyphen (e.g., --grep -pattern). */
@@ -94,6 +179,8 @@ export interface ArgDef {
94
179
  readonly trailingVarArg?: boolean;
95
180
  /** Positional that requires -- before it (like clap's last()). */
96
181
  readonly last?: boolean;
182
+ /** Explicit 1-based position for a positional arg. */
183
+ readonly index?: number;
97
184
  /** Custom section heading in help output (groups args under this heading). */
98
185
  readonly helpHeading?: string;
99
186
  /** Hint for shell completion -- guides what kind of values to suggest (files, dirs, hosts, etc.). */
@@ -126,8 +213,18 @@ export interface StylesDef {
126
213
  export interface CommandMeta {
127
214
  /** Command name (used in usage line). */
128
215
  readonly name: string;
216
+ /** Name shown in the usage line, when the binary differs from the command. */
217
+ readonly binName?: string;
218
+ /** Name shown in the help header and version output. */
219
+ readonly displayName?: string;
129
220
  /** Version string (shown with --version). */
130
221
  readonly version?: string;
222
+ /** Longer version text, shown with --version where -V shows `version`. */
223
+ readonly longVersion?: string;
224
+ /** Give subcommands this command's version, as clap's propagate_version does. */
225
+ readonly propagateVersion?: boolean;
226
+ /** Author line, available to help templates as {author}. */
227
+ readonly author?: string;
131
228
  /** Short description (one line, shown in parent's subcommand list). */
132
229
  readonly description?: string;
133
230
  /** Longer "about" text (shown at top of this command's help). */
@@ -138,10 +235,37 @@ export interface CommandMeta {
138
235
  readonly beforeHelp?: string;
139
236
  /** Text appended after the help output. */
140
237
  readonly afterHelp?: string;
238
+ /** Text prepended before long help only (--help, not -h). */
239
+ readonly beforeLongHelp?: string;
240
+ /** Text appended after long help only (--help, not -h). */
241
+ readonly afterLongHelp?: string;
141
242
  /** Hide this command from parent's help subcommand list. */
142
243
  readonly hidden?: boolean;
244
+ /**
245
+ * Mark the command deprecated. Running it warns on stderr and help labels it.
246
+ * A string is used as the warning's reason.
247
+ */
248
+ readonly deprecated?: boolean | string;
249
+ /** Name of the command that supersedes this one, named in the warning. */
250
+ readonly replacedBy?: string;
143
251
  /** Visible aliases shown next to the command name in help. */
144
252
  readonly aliases?: readonly string[];
253
+ /** Aliases that work but stay out of help. */
254
+ readonly hiddenAliases?: readonly string[];
255
+ /** Invoke this subcommand with a short flag, as in `pacman -S`. */
256
+ readonly shortFlag?: string;
257
+ /** Invoke this subcommand with a long flag, as in `pacman --sync`. */
258
+ readonly longFlag?: string;
259
+ /** Extra short flag forms for this subcommand, kept out of help. */
260
+ readonly shortFlagAliases?: readonly string[];
261
+ /** Extra long flag forms for this subcommand, kept out of help. */
262
+ readonly longFlagAliases?: readonly string[];
263
+ /** Extra short flag forms shown in help. */
264
+ readonly visibleShortFlagAliases?: readonly string[];
265
+ /** Extra long flag forms shown in help. */
266
+ readonly visibleLongFlagAliases?: readonly string[];
267
+ /** Sort key among sibling subcommands in help; lower comes first. */
268
+ readonly displayOrder?: number;
145
269
  /** Require a subcommand to be provided. */
146
270
  readonly subcommandRequired?: boolean;
147
271
  /** Accept partial subcommand names (e.g., 'ser' matches 'serve'). */
@@ -156,9 +280,57 @@ export interface CommandMeta {
156
280
  readonly subcommandNegatesReqs?: boolean;
157
281
  /** Show help if no arguments are provided (instead of running). */
158
282
  readonly argRequiredElseHelp?: boolean;
283
+ /** Do not add the built-in -h/--help flag. */
284
+ readonly disableHelpFlag?: boolean;
285
+ /** Do not add the built-in -V/--version flag. */
286
+ readonly disableVersionFlag?: boolean;
287
+ /** Do not add the built-in `help` subcommand. */
288
+ readonly disableHelpSubcommand?: boolean;
289
+ /** Render help without colour even on a capable terminal. */
290
+ readonly disableColoredHelp?: boolean;
291
+ /** When to colourise output. 'never' matches disableColoredHelp. */
292
+ readonly color?: ColorChoice;
293
+ /** Summarise each subcommand's own args inside this command's help. */
294
+ readonly flattenHelp?: boolean;
295
+ /** Collect parse errors on ParseResult instead of throwing. */
296
+ readonly ignoreErrors?: boolean;
297
+ /** Default help heading for args that set none. */
298
+ readonly nextHelpHeading?: string;
299
+ /** Starting display order for args that set none. */
300
+ readonly nextDisplayOrder?: number;
301
+ /** Do not split values after `--` on their valueDelimiter. */
302
+ readonly dontDelimitTrailingValues?: boolean;
303
+ /** Reject at build time any visible arg that has no description. */
304
+ readonly helpExpected?: boolean;
305
+ /** Fixed width for help output, overriding the terminal width. */
306
+ readonly termWidth?: number;
307
+ /** Upper bound on the terminal width used for help output. */
308
+ readonly maxTermWidth?: number;
309
+ /** Replace the generated usage line. */
310
+ readonly overrideUsage?: string;
311
+ /** Replace the whole help output. */
312
+ readonly overrideHelp?: string;
313
+ /** Heading for the subcommand list (default "Commands"). */
314
+ readonly subcommandHelpHeading?: string;
315
+ /** Placeholder for the subcommand in the usage line (default "COMMAND"). */
316
+ readonly subcommandValueName?: string;
317
+ /** Let every arg of this command accept values starting with a hyphen. */
318
+ readonly allowHyphenValues?: boolean;
319
+ /** Let every arg of this command accept negative numbers as values. */
320
+ readonly allowNegativeNumbers?: boolean;
321
+ /** Allow the first positional to be omitted when later ones are given. */
322
+ readonly allowMissingPositional?: boolean;
323
+ /** Repeating a single-value arg replaces it instead of being an error. */
324
+ readonly argsOverrideSelf?: boolean;
325
+ /** A subcommand name ends value collection for a multi-value arg. */
326
+ readonly subcommandPrecedenceOverArg?: boolean;
327
+ /** Dispatch on the invoked binary name, busybox style. */
328
+ readonly multicall?: boolean;
329
+ /** argv holds no binary name, so nothing is stripped from the front. */
330
+ readonly noBinaryName?: boolean;
159
331
  /**
160
332
  * Custom help template with placeholders:
161
- * {name}, {version}, {about}, {usage}, {all-args}, {arguments},
333
+ * {name}, {version}, {author}, {about}, {usage}, {all-args}, {arguments},
162
334
  * {options}, {commands}, {before-help}, {after-help}
163
335
  */
164
336
  readonly helpTemplate?: string;
@@ -173,6 +345,10 @@ export interface ArgGroup {
173
345
  readonly required?: boolean;
174
346
  /** Whether args in the group are mutually exclusive. */
175
347
  readonly multiple?: boolean;
348
+ /** Args that cannot be used when any member of this group is present. */
349
+ readonly conflictsWith?: readonly string[];
350
+ /** Args that must be present when any member of this group is present. */
351
+ readonly requires?: readonly string[];
176
352
  }
177
353
  /**
178
354
  * Infer the parsed type from an ArgDef.
@@ -204,6 +380,17 @@ export interface CommandContext<T extends ArgsDef = ArgsDef> {
204
380
  readonly cmd: CommandDef<T>;
205
381
  /** Name of the resolved subcommand, if any. */
206
382
  readonly subCommand?: string;
383
+ /**
384
+ * Where each argument's value came from, by arg key. Absent means the arg has
385
+ * no value at all.
386
+ */
387
+ readonly valueSources: ReadonlyMap<string, ValueSource>;
388
+ /**
389
+ * Where the handler should write. Defaults to process.stdout/stderr; the
390
+ * testing helpers swap them so a run can be asserted without spawning.
391
+ */
392
+ readonly stdout: OutputSink;
393
+ readonly stderr: OutputSink;
207
394
  /** Arbitrary user data (for passing state between setup/run/cleanup). */
208
395
  data: Record<string, unknown>;
209
396
  }
@@ -215,14 +402,44 @@ export interface CommandDef<T extends ArgsDef = ArgsDef> {
215
402
  readonly args?: T;
216
403
  /** Subcommand definitions (name -> command). */
217
404
  readonly subCommands?: Record<string, CommandDef<any>>;
405
+ /**
406
+ * Subcommands built on first use rather than at definition time, so a CLI
407
+ * with many heavy subcommands does not pay for all of them at startup.
408
+ * Merged with `subCommands`, which wins on a name collision.
409
+ */
410
+ readonly lazySubCommands?: () => Record<string, CommandDef<any>>;
411
+ /** Parse each argument of an external subcommand before handing it on. */
412
+ readonly externalSubcommandValueParser?: ValueParserFn;
218
413
  /** Argument groups for validation and help grouping. */
219
414
  readonly groups?: readonly ArgGroup[];
220
415
  /** Called before run. Return value is ignored; throw to abort. */
221
- readonly setup?: (ctx: CommandContext<T>) => void | Promise<void>;
416
+ setup?(ctx: CommandContext<T>): void | Promise<void>;
222
417
  /** Main command handler. */
223
- readonly run?: (ctx: CommandContext<T>) => void | Promise<void>;
418
+ run?(ctx: CommandContext<T>): void | Promise<void>;
224
419
  /** Called after run (even on error). */
225
- readonly cleanup?: (ctx: CommandContext<T>) => void | Promise<void>;
420
+ cleanup?(ctx: CommandContext<T>): void | Promise<void>;
421
+ }
422
+ /** Options for man page generation. */
423
+ export interface ManOptions {
424
+ /** Page name; defaults to the command's binName, displayName or name. */
425
+ readonly name?: string;
426
+ /** Man section number (default '1'). */
427
+ readonly section?: string;
428
+ /** Manual title shown in the page header. */
429
+ readonly manual?: string;
430
+ }
431
+ /** Options for markdown documentation output. */
432
+ export interface MarkdownOptions {
433
+ /** Command name used in headings and usage; defaults to binName or name. */
434
+ readonly name?: string;
435
+ /** Document title rendered above the command, as a level-1 heading. */
436
+ readonly title?: string;
437
+ /** Text appended after the last command section. */
438
+ readonly footer?: string;
439
+ }
440
+ /** Somewhere to write output. `process.stdout` satisfies this. */
441
+ export interface OutputSink {
442
+ write(chunk: string): void;
226
443
  }
227
444
  /** Options for runMain / runCommand. */
228
445
  export interface RunOptions {
@@ -234,6 +451,45 @@ export interface RunOptions {
234
451
  readonly showHelpOnEmpty?: boolean;
235
452
  /** Custom styles for help and error output. */
236
453
  readonly styles?: Partial<StylesDef>;
454
+ /** Where help and version output goes (default: process.stdout). */
455
+ readonly stdout?: OutputSink;
456
+ /** Where errors go (default: process.stderr). */
457
+ readonly stderr?: OutputSink;
458
+ /**
459
+ * Called with the exit code the run settled on, before `process.exit`. Set
460
+ * alongside `exit: false` to observe the code without ending the process.
461
+ */
462
+ readonly onExit?: (code: number) => void;
463
+ /**
464
+ * Values from a configuration file, filling args that the command line and
465
+ * environment left alone. A nested object keyed by a subcommand name scopes
466
+ * its contents to that command; scalar keys apply at every level.
467
+ *
468
+ * `loadConfig` from `clap-ts/config` produces this shape.
469
+ *
470
+ * Pass a thunk to defer the file search: it runs only if some argument is
471
+ * still sitting on its default, so a fully specified command line reads
472
+ * nothing from disk.
473
+ */
474
+ readonly config?: Record<string, unknown> | (() => Record<string, unknown> | undefined);
475
+ /**
476
+ * Supply values for required arguments still missing after argv, the
477
+ * environment and the config have had their turn. Returning a record fills
478
+ * them in with source 'prompt'; returning undefined leaves validation to
479
+ * fail as it would have.
480
+ *
481
+ * `promptMissing` from `clap-ts/prompt` is the intended implementation.
482
+ */
483
+ readonly fillMissing?: (missing: readonly MissingArg[], command: CommandDef<any>) => Promise<Record<string, unknown> | undefined>;
484
+ }
485
+ /** A required argument that nothing has supplied yet. */
486
+ export interface MissingArg {
487
+ /** Key in the command's `args`. */
488
+ readonly key: string;
489
+ /** The argument's definition. */
490
+ readonly def: ArgDef;
491
+ /** How it reads in a message: `--port` or `<FILE>`. */
492
+ readonly label: string;
237
493
  }
238
494
  /** Result of parsing arguments. */
239
495
  export interface ParseResult {
@@ -245,14 +501,26 @@ export interface ParseResult {
245
501
  readonly rest: readonly string[];
246
502
  /** The subcommand name if one was matched. */
247
503
  readonly subCommand?: string;
504
+ /** Whether the matched subcommand was accepted via allowExternalSubcommands. */
505
+ readonly subCommandIsExternal: boolean;
506
+ /** Tokens following the matched subcommand, to be parsed against it. */
507
+ readonly subCommandArgs: readonly string[];
248
508
  /** Whether --help / -h was requested. */
249
509
  readonly helpRequested: boolean;
250
510
  /** Whether -h (short) was used vs --help (long). */
251
511
  readonly helpIsShort: boolean;
252
512
  /** Whether --version / -V was requested. */
253
513
  readonly versionRequested: boolean;
514
+ /** Whether -V (short) was used rather than --version. */
515
+ readonly versionIsShort: boolean;
254
516
  /** Unknown flags that were passed. */
255
517
  readonly unknown: readonly string[];
518
+ /** Errors collected instead of thrown, when meta.ignoreErrors is set. */
519
+ readonly errors: readonly string[];
520
+ /** Notices to show the user without failing, such as deprecation warnings. */
521
+ readonly warnings: readonly string[];
256
522
  /** Set of arg keys that were explicitly provided (not defaults or env). */
257
523
  readonly explicitlySet: ReadonlySet<string>;
524
+ /** Where each parsed value came from, by arg key. */
525
+ readonly valueSources: ReadonlyMap<string, ValueSource>;
258
526
  }