@kjanat/dreamcli 2.4.1 → 3.0.0-rc.1
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/CHANGELOG.md +169 -1
- package/README.md +14 -7
- package/dist/core/cli/index.d.mts +283 -81
- package/dist/core/cli/index.mjs +1 -1
- package/dist/core/cli/planner.d.mts +27 -5
- package/dist/core/cli/planner.mjs +1 -1
- package/dist/core/cli/root-help.d.mts +16 -2
- package/dist/core/cli/root-help.mjs +3 -5
- package/dist/core/cli/root-surface.d.mts +18 -3
- package/dist/core/cli/root-surface.mjs +1 -1
- package/dist/core/cli/runtime-preflight.d.mts +31 -5
- package/dist/core/cli/runtime-preflight.mjs +1 -1
- package/dist/core/completion/index.d.mts +32 -1
- package/dist/core/completion/index.mjs +1 -1
- package/dist/core/completion/shells/shared.d.mts +10 -0
- package/dist/core/completion/shells/shared.mjs +1 -1
- package/dist/core/config/index.d.mts +2 -2
- package/dist/core/config/package-json.d.mts +93 -20
- package/dist/core/config/package-json.mjs +3 -1
- package/dist/core/help/index.d.mts +45 -1
- package/dist/core/help/index.mjs +6 -6
- package/dist/core/json-schema/index.mjs +1 -1
- package/dist/core/json-schema/meta-descriptions.generated.d.mts +38 -38
- package/dist/core/parse/index.d.mts +15 -1
- package/dist/core/parse/index.mjs +1 -1
- package/dist/core/prompt/index.d.mts +11 -3
- package/dist/core/prompt/index.mjs +3 -3
- package/dist/core/resolve/coerce.mjs +1 -1
- package/dist/core/resolve/flags.mjs +1 -1
- package/dist/core/resolve/property.d.mts +2 -0
- package/dist/core/resolve/property.mjs +1 -1
- package/dist/core/schema/arg.d.mts +90 -10
- package/dist/core/schema/arg.mjs +1 -1
- package/dist/core/schema/command.d.mts +3 -1
- package/dist/core/schema/flag.d.mts +92 -2
- package/dist/core/schema/flag.mjs +1 -1
- package/dist/core/schema/index.d.mts +2 -1
- package/dist/core/schema/number-constraints.d.mts +96 -0
- package/dist/core/schema/number-constraints.mjs +1 -0
- package/dist/core/schema/prompt.d.mts +15 -0
- package/dist/core/schema/run.d.mts +4 -4
- package/dist/core/testkit/index.d.mts +9 -5
- package/dist/core/testkit/index.mjs +1 -1
- package/dist/index.d.mts +4 -3
- package/dist/index.mjs +1 -1
- package/dist/runtime/auto.mjs +1 -1
- package/dist/runtime/bun.mjs +1 -1
- package/dist/runtime/deno.mjs +2 -2
- package/dist/runtime/node.mjs +2 -2
- package/dist/version.mjs +1 -1
- package/examples/basic.ts +5 -1
- package/examples/gh/package.json +2 -7
- package/examples/pwsh-demo/package.json +1 -7
- package/examples/pwsh-demo/src/main.test.ts +1 -4
- package/examples/pwsh-demo/src/main.ts +14 -4
- package/examples/transport-launcher.ts +85 -0
- package/package.json +15 -24
- package/src/core/cli/dispatch.ts +7 -1
- package/src/core/cli/index.ts +521 -122
- package/src/core/cli/planner.ts +192 -23
- package/src/core/cli/root-help.ts +195 -105
- package/src/core/cli/root-surface.ts +25 -12
- package/src/core/cli/runtime-preflight.ts +43 -8
- package/src/core/completion/index.ts +50 -0
- package/src/core/completion/shells/powershell.ts +2 -2
- package/src/core/completion/shells/shared.ts +30 -3
- package/src/core/config/index.ts +12 -2
- package/src/core/config/package-json.ts +250 -41
- package/src/core/execution/index.ts +1 -4
- package/src/core/help/index.ts +57 -10
- package/src/core/json-schema/index.ts +23 -4
- package/src/core/json-schema/meta-descriptions.generated.ts +115 -98
- package/src/core/parse/index.ts +62 -1
- package/src/core/prompt/index.ts +54 -14
- package/src/core/resolve/coerce.ts +73 -5
- package/src/core/resolve/flags.ts +14 -0
- package/src/core/resolve/property.ts +4 -4
- package/src/core/schema/arg.ts +125 -3
- package/src/core/schema/command.ts +3 -1
- package/src/core/schema/flag.ts +120 -4
- package/src/core/schema/index.ts +5 -0
- package/src/core/schema/number-constraints.ts +153 -0
- package/src/core/schema/prompt.ts +15 -0
- package/src/core/schema/run.ts +4 -4
- package/src/core/testkit/index.ts +32 -8
- package/src/index.ts +8 -0
- package/src/runtime/auto.ts +0 -24
- package/src/runtime/bun.ts +0 -7
- package/src/runtime/deno.ts +0 -2
- package/src/runtime/node.ts +0 -11
- package/src/runtime/support.ts +1 -59
- package/src/schema.ts +16 -0
- package/dist/runtime/support.d.mts +0 -42
- package/dist/runtime/support.mjs +0 -1
|
@@ -6,22 +6,22 @@
|
|
|
6
6
|
*/
|
|
7
7
|
declare const definitionMetaSchemaDescriptions: {
|
|
8
8
|
readonly root: {
|
|
9
|
-
readonly description:
|
|
9
|
+
readonly description: 'Runtime descriptor for the CLI program.\n\nStores the program name, version, description, and registered commands.\\\nBuilt incrementally by CLIBuilder.';
|
|
10
10
|
readonly properties: {
|
|
11
11
|
readonly name: {
|
|
12
|
-
readonly description:
|
|
12
|
+
readonly description: 'Program name (used in help text, usage lines, and completion scripts).';
|
|
13
13
|
};
|
|
14
14
|
readonly version: {
|
|
15
|
-
readonly description:
|
|
15
|
+
readonly description: 'Program version (shown by `--version`).';
|
|
16
16
|
};
|
|
17
17
|
readonly description: {
|
|
18
|
-
readonly description:
|
|
18
|
+
readonly description: 'Program description (shown in root help).';
|
|
19
19
|
};
|
|
20
20
|
readonly defaultCommand: {
|
|
21
|
-
readonly description:
|
|
21
|
+
readonly description: 'Default command dispatched when no subcommand matches.\n\nWhen set, the CLI root behaves like a hybrid command group: subcommands\ndispatch by name as usual, but empty argv or flags-only argv falls\nthrough to this command instead of showing root help.\n\nSet via the .default() builder method.';
|
|
22
22
|
};
|
|
23
23
|
readonly commands: {
|
|
24
|
-
readonly description:
|
|
24
|
+
readonly description: 'Registered commands (type-erased for heterogeneous storage).';
|
|
25
25
|
};
|
|
26
26
|
};
|
|
27
27
|
};
|
|
@@ -33,25 +33,25 @@ declare const definitionMetaSchemaDescriptions: {
|
|
|
33
33
|
readonly description: "The command name (used for dispatch, e.g. `'deploy'`).";
|
|
34
34
|
};
|
|
35
35
|
readonly description: {
|
|
36
|
-
readonly description:
|
|
36
|
+
readonly description: 'Human-readable description for help text.';
|
|
37
37
|
};
|
|
38
38
|
readonly aliases: {
|
|
39
|
-
readonly description:
|
|
39
|
+
readonly description: 'Alternative names for this command.';
|
|
40
40
|
};
|
|
41
41
|
readonly hidden: {
|
|
42
|
-
readonly description:
|
|
42
|
+
readonly description: 'Whether this command is hidden from help listings.';
|
|
43
43
|
};
|
|
44
44
|
readonly examples: {
|
|
45
|
-
readonly description:
|
|
45
|
+
readonly description: 'Usage examples for help text.';
|
|
46
46
|
};
|
|
47
47
|
readonly flags: {
|
|
48
|
-
readonly description:
|
|
48
|
+
readonly description: 'Named flag schemas, keyed by flag name.';
|
|
49
49
|
};
|
|
50
50
|
readonly args: {
|
|
51
|
-
readonly description:
|
|
51
|
+
readonly description: 'Ordered positional arg entries (name + schema).';
|
|
52
52
|
};
|
|
53
53
|
readonly commands: {
|
|
54
|
-
readonly description:
|
|
54
|
+
readonly description: 'Nested subcommand schemas (for help rendering and completion).\n\nPure data — no execution closures. Populated by `.command()` on\n`CommandBuilder`. Empty for leaf commands.';
|
|
55
55
|
};
|
|
56
56
|
};
|
|
57
57
|
};
|
|
@@ -59,25 +59,25 @@ declare const definitionMetaSchemaDescriptions: {
|
|
|
59
59
|
readonly description: "The runtime descriptor stored inside every FlagBuilder. Consumers (parser,\nhelp generator, resolution chain) read this to understand the flag's shape\nwithout touching generics.";
|
|
60
60
|
readonly properties: {
|
|
61
61
|
readonly kind: {
|
|
62
|
-
readonly description:
|
|
62
|
+
readonly description: 'What kind of value this flag accepts.';
|
|
63
63
|
};
|
|
64
64
|
readonly presence: {
|
|
65
65
|
readonly description: "Presence describes whether a flag value is guaranteed to exist when the\naction handler runs:\n\n- `'optional'` — not required; unresolved value follows the kind-specific\n optional fallback (`undefined` for most flags, `[]` for arrays)\n- `'required'` — must be supplied; error if missing\n- `'defaulted'` — always present (falls back to default value)";
|
|
66
66
|
};
|
|
67
67
|
readonly defaultValue: {
|
|
68
|
-
readonly description:
|
|
68
|
+
readonly description: 'Runtime default value (if any).';
|
|
69
69
|
};
|
|
70
70
|
readonly aliases: {
|
|
71
71
|
readonly description: "Short/long aliases (e.g. `[{ name: 'f', hidden: false }]` for `--force`).";
|
|
72
72
|
};
|
|
73
73
|
readonly envVar: {
|
|
74
|
-
readonly description:
|
|
74
|
+
readonly description: 'Environment variable name for v0.2+ resolution.';
|
|
75
75
|
};
|
|
76
76
|
readonly configPath: {
|
|
77
77
|
readonly description: "Dotted config path for v0.2+ resolution (e.g. `'deploy.region'`).";
|
|
78
78
|
};
|
|
79
79
|
readonly description: {
|
|
80
|
-
readonly description:
|
|
80
|
+
readonly description: 'Human-readable description for help text.';
|
|
81
81
|
};
|
|
82
82
|
readonly enumValues: {
|
|
83
83
|
readonly description: "Allowed literal values when `kind === 'enum'`.";
|
|
@@ -86,13 +86,13 @@ declare const definitionMetaSchemaDescriptions: {
|
|
|
86
86
|
readonly description: "Element schema when `kind === 'array'`.";
|
|
87
87
|
};
|
|
88
88
|
readonly prompt: {
|
|
89
|
-
readonly description:
|
|
89
|
+
readonly description: 'Interactive prompt configuration for v0.3+ resolution.';
|
|
90
90
|
};
|
|
91
91
|
readonly deprecated: {
|
|
92
|
-
readonly description:
|
|
92
|
+
readonly description: 'Deprecation marker.\n\n- `undefined` — not deprecated (default)\n- `true` — deprecated with no migration message\n- `string` — deprecated with a reason/migration message\n\nWhen a deprecated flag is used, a warning is emitted to stderr.\nHelp text shows `[deprecated]` or `[deprecated: <reason>]`.';
|
|
93
93
|
};
|
|
94
94
|
readonly propagate: {
|
|
95
|
-
readonly description:
|
|
95
|
+
readonly description: 'Whether this flag propagates to subcommands in nested command trees.\n\nWhen `true`, the flag is automatically available to all descendant\ncommands. A child command that defines a flag with the same name\nshadows the propagated parent flag.';
|
|
96
96
|
};
|
|
97
97
|
};
|
|
98
98
|
};
|
|
@@ -100,25 +100,25 @@ declare const definitionMetaSchemaDescriptions: {
|
|
|
100
100
|
readonly description: "The runtime descriptor stored inside every ArgBuilder. Consumers (parser,\nhelp generator) read this to understand the arg's shape without touching\ngenerics.";
|
|
101
101
|
readonly properties: {
|
|
102
102
|
readonly name: {
|
|
103
|
-
readonly description:
|
|
103
|
+
readonly description: 'A named positional argument entry in the command schema.\n\nPairs a user-facing arg name with its ArgSchema descriptor.\nThe array ordering in CommandSchema.args determines CLI position.';
|
|
104
104
|
};
|
|
105
105
|
readonly kind: {
|
|
106
|
-
readonly description:
|
|
106
|
+
readonly description: 'What kind of value this arg accepts.';
|
|
107
107
|
};
|
|
108
108
|
readonly presence: {
|
|
109
109
|
readonly description: "Presence describes whether a positional arg is guaranteed to exist when the\naction handler runs:\n\n- `'required'` — must be supplied; error if missing (default)\n- `'optional'` — may be `undefined` if not supplied\n- `'defaulted'` — always present (falls back to default value)";
|
|
110
110
|
};
|
|
111
111
|
readonly variadic: {
|
|
112
|
-
readonly description:
|
|
112
|
+
readonly description: 'Whether this arg consumes all remaining positionals.';
|
|
113
113
|
};
|
|
114
114
|
readonly stdinMode: {
|
|
115
|
-
readonly description:
|
|
115
|
+
readonly description: 'Whether this arg may read from stdin during resolution.';
|
|
116
116
|
};
|
|
117
117
|
readonly defaultValue: {
|
|
118
|
-
readonly description:
|
|
118
|
+
readonly description: 'Runtime default value (if any).';
|
|
119
119
|
};
|
|
120
120
|
readonly description: {
|
|
121
|
-
readonly description:
|
|
121
|
+
readonly description: 'Human-readable description for help text.';
|
|
122
122
|
};
|
|
123
123
|
readonly envVar: {
|
|
124
124
|
readonly description: "Environment variable name for env resolution.\n\nWhen set and the CLI value is absent, the resolver reads this env var\nand coerces the string to the arg's declared kind.";
|
|
@@ -127,7 +127,7 @@ declare const definitionMetaSchemaDescriptions: {
|
|
|
127
127
|
readonly description: "Allowed literal values when `kind === 'enum'`.";
|
|
128
128
|
};
|
|
129
129
|
readonly deprecated: {
|
|
130
|
-
readonly description:
|
|
130
|
+
readonly description: 'Deprecation marker.\n\n- `undefined` — not deprecated (default)\n- `true` — deprecated with no migration message\n- `string` — deprecated with a reason/migration message\n\nWhen a deprecated arg is used, a warning is emitted to stderr.\nHelp text shows `[deprecated]` or `[deprecated: <reason>]`.';
|
|
131
131
|
};
|
|
132
132
|
};
|
|
133
133
|
};
|
|
@@ -138,44 +138,44 @@ declare const definitionMetaSchemaDescriptions: {
|
|
|
138
138
|
readonly description: "The kind of interactive prompt to present.\n\n- `'confirm'` — yes/no boolean question\n- `'input'` — free-text string input\n- `'select'` — single selection from a list\n- `'multiselect'` — multiple selections from a list";
|
|
139
139
|
};
|
|
140
140
|
readonly message: {
|
|
141
|
-
readonly description:
|
|
141
|
+
readonly description: 'The question displayed to the user.';
|
|
142
142
|
};
|
|
143
143
|
readonly placeholder: {
|
|
144
|
-
readonly description:
|
|
144
|
+
readonly description: 'Placeholder text shown before user types (informational only).';
|
|
145
145
|
};
|
|
146
146
|
readonly choices: {
|
|
147
|
-
readonly description:
|
|
147
|
+
readonly description: 'Available choices. When omitted for `enum` flags, the enum values\nfrom the flag schema are used automatically.';
|
|
148
148
|
};
|
|
149
149
|
readonly min: {
|
|
150
|
-
readonly description:
|
|
150
|
+
readonly description: 'Minimum number of selections required.';
|
|
151
151
|
};
|
|
152
152
|
readonly max: {
|
|
153
|
-
readonly description:
|
|
153
|
+
readonly description: 'Maximum number of selections allowed.';
|
|
154
154
|
};
|
|
155
155
|
};
|
|
156
156
|
};
|
|
157
157
|
readonly choice: {
|
|
158
|
-
readonly description:
|
|
158
|
+
readonly description: 'A selectable option for SelectPromptConfig and MultiselectPromptConfig prompts.';
|
|
159
159
|
readonly properties: {
|
|
160
160
|
readonly value: {
|
|
161
|
-
readonly description:
|
|
161
|
+
readonly description: 'The value returned when this choice is selected.';
|
|
162
162
|
};
|
|
163
163
|
readonly label: {
|
|
164
|
-
readonly description:
|
|
164
|
+
readonly description: 'Display label shown to the user.';
|
|
165
165
|
};
|
|
166
166
|
readonly description: {
|
|
167
|
-
readonly description:
|
|
167
|
+
readonly description: 'Optional description shown alongside the choice.';
|
|
168
168
|
};
|
|
169
169
|
};
|
|
170
170
|
};
|
|
171
171
|
readonly example: {
|
|
172
|
-
readonly description:
|
|
172
|
+
readonly description: 'A single usage example shown in help text.';
|
|
173
173
|
readonly properties: {
|
|
174
174
|
readonly command: {
|
|
175
175
|
readonly description: "The command invocation (e.g. `'deploy production --force'`).";
|
|
176
176
|
};
|
|
177
177
|
readonly description: {
|
|
178
|
-
readonly description:
|
|
178
|
+
readonly description: 'Optional description of what this example does.';
|
|
179
179
|
};
|
|
180
180
|
};
|
|
181
181
|
};
|
|
@@ -58,6 +58,20 @@ declare function tokenize(argv: readonly string[]): readonly Token[];
|
|
|
58
58
|
* @returns `true` if `token` occurs before any `--` separator.
|
|
59
59
|
*/
|
|
60
60
|
declare function includesBeforeSeparator(argv: readonly string[], token: string): boolean;
|
|
61
|
+
/**
|
|
62
|
+
* Remove every occurrence of `token` that appears before the `--` end-of-options
|
|
63
|
+
* separator, leaving post-separator literals untouched.
|
|
64
|
+
*
|
|
65
|
+
* The strip counterpart to {@linkcode includesBeforeSeparator}: root-level flags
|
|
66
|
+
* (`--json`) are stripped before dispatch/parse so the command schema never sees
|
|
67
|
+
* them, but a literal after `--` (`-- --json`) must reach the command unchanged.
|
|
68
|
+
*
|
|
69
|
+
* @param argv - Raw argument strings.
|
|
70
|
+
* @param token - Exact flag token to strip (e.g. `--json`).
|
|
71
|
+
* @returns A new argv with pre-separator occurrences of `token` removed, or the
|
|
72
|
+
* original reference when `token` does not occur before the separator.
|
|
73
|
+
*/
|
|
74
|
+
declare function stripBeforeSeparator(argv: readonly string[], token: string): readonly string[];
|
|
61
75
|
/**
|
|
62
76
|
* Raw parsed values before resolution (defaults, env, config, etc.).
|
|
63
77
|
*
|
|
@@ -106,4 +120,4 @@ declare function flagExpectsValue(schema: FlagSchema): boolean;
|
|
|
106
120
|
*/
|
|
107
121
|
declare function parse(schema: CommandSchema, argv: readonly string[]): ParseResult;
|
|
108
122
|
//#endregion
|
|
109
|
-
export { type ParseResult, type Token, buildFlagLookup, flagExpectsValue, includesBeforeSeparator, parse, tokenize };
|
|
123
|
+
export { type ParseResult, type Token, buildFlagLookup, flagExpectsValue, includesBeforeSeparator, parse, stripBeforeSeparator, tokenize };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{describeNumberConstraintViolation as e,validateNumberConstraints as t}from"../schema/number-constraints.mjs";import{getFlagAliasNames as n}from"../schema/flag.mjs";import{ParseError as r}from"../errors/index.mjs";function i(e){let t=[],n=!1;for(let r of e){if(n){t.push({kind:`positional`,value:r});continue}if(r===`--`){t.push({kind:`separator`}),n=!0;continue}if(r.startsWith(`--`)){let e=r.indexOf(`=`);e===-1?t.push({kind:`long-flag`,name:r.slice(2),value:void 0}):t.push({kind:`long-flag`,name:r.slice(2,e),value:r.slice(e+1)});continue}if(r.startsWith(`-`)&&r.length>1){t.push({kind:`short-flags`,chars:r.slice(1)});continue}t.push({kind:`positional`,value:r})}return t}function a(e,t){for(let n of e){if(n===`--`)return!1;if(n===t)return!0}return!1}function o(e,t){let n=e.indexOf(`--`),r=n===-1?e:e.slice(0,n);if(!r.includes(t))return e;let i=r.filter(e=>e!==t);return n===-1?i:[...i,...e.slice(n)]}function s(e){let t=new Map;for(let[r,i]of Object.entries(e)){t.set(r,[r,i]);for(let e of n(i,{includeHidden:!0}))t.set(e,[r,i])}return t}function c(e){return e.kind!==`boolean`}function l(n,i,a,o=`--${n}`){switch(a.kind){case`string`:return i;case`number`:{let s=Number(i);if(Number.isNaN(s))throw new r(`Invalid number value '${i}' for flag ${o}`,{code:`INVALID_VALUE`,details:{flag:n,input:o,value:i,expected:`number`}});let c=t(s,a.numberConstraints);if(c!==void 0)throw new r(`Invalid number value '${i}' for flag ${o}: ${e(c)}`,{code:`INVALID_VALUE`,details:{flag:n,input:o,value:i,expected:`number`,constraint:c.kind,...`bound`in c?{bound:c.bound}:{}}});return s}case`boolean`:if(i===`true`||i===`1`)return!0;if(i===`false`||i===`0`)return!1;throw new r(`Invalid boolean value '${i}' for flag ${o}. Use true/false or 1/0`,{code:`INVALID_VALUE`,details:{flag:n,input:o,value:i,expected:`boolean`}});case`enum`:{let e=a.enumValues;if(e===void 0)throw new r(`Enum flag --${n} is misconfigured: no allowed values declared`,{code:`INVALID_SCHEMA`,details:{flag:n,kind:`enum`,missing:`enumValues`}});if(!e.includes(i))throw new r(`Invalid value '${i}' for flag ${o}. Allowed: ${e.join(`, `)}`,{code:`INVALID_VALUE`,details:{flag:n,input:o,value:i,allowed:e}});return i}case`array`:return a.elementSchema?l(n,i,a.elementSchema):i;case`custom`:if(!a.parseFn)return i;try{return a.parseFn(i)}catch(e){throw e instanceof r?e:new r(`Failed to parse flag ${o}: ${e instanceof Error?e.message:String(e)}`,{code:`INVALID_VALUE`,details:{flag:n,input:o,value:i},cause:e})}}}function u(n,i,a){switch(a.kind){case`string`:return i;case`number`:{let o=Number(i);if(Number.isNaN(o))throw new r(`Invalid number value '${i}' for argument <${n}>`,{code:`INVALID_VALUE`,details:{arg:n,value:i,expected:`number`}});let s=t(o,a.numberConstraints);if(s!==void 0)throw new r(`Invalid number value '${i}' for argument <${n}>: ${e(s)}`,{code:`INVALID_VALUE`,details:{arg:n,value:i,expected:`number`,constraint:s.kind,...`bound`in s?{bound:s.bound}:{}}});return o}case`enum`:{let e=a.enumValues;if(e===void 0)throw new r(`Enum argument <${n}> is misconfigured: no allowed values declared`,{code:`INVALID_SCHEMA`,details:{arg:n,kind:`enum`,missing:`enumValues`}});if(!e.includes(i))throw new r(`Invalid value '${i}' for argument <${n}>. Allowed: ${e.join(`, `)}`,{code:`INVALID_VALUE`,details:{arg:n,value:i,allowed:e}});return i}case`custom`:if(!a.parseFn)return i;try{return a.parseFn(i)}catch(e){throw e instanceof r?e:new r(`Failed to parse argument <${n}>: ${e instanceof Error?e.message:String(e)}`,{code:`INVALID_VALUE`,details:{arg:n,value:i},cause:e})}}}function d(e,t){let n=i(t),r=s(e.flags),a={},o=[],c=0;for(;c<n.length;){let e=n[c];if(e===void 0)break;if(e.kind===`separator`){c++;continue}if(e.kind===`positional`){o.push(e.value),c++;continue}if(e.kind===`long-flag`){c=f(e,n,c,r,a);continue}c=p(e,n,c,r,a)}return{flags:a,args:h(e.args,o)}}function f(e,t,n,i,a){let o=i.get(e.name);if(!o){let t=g(e.name,i);throw new r(`Unknown flag --${e.name}${t?`. Did you mean --${t}?`:``}`,{code:`UNKNOWN_FLAG`,details:{flag:e.name,...t?{suggestion:t}:{}},...t?{suggest:`--${t}`}:{}})}let[s,u]=o;if(!c(u))return e.value===void 0?a[s]=!0:a[s]=l(s,e.value,u,`--${e.name}`),n+1;if(e.value!==void 0)return m(a,s,u,e.value),n+1;let d=t[n+1];if(d?.kind!==`positional`)throw new r(`Flag --${e.name} requires a value`,{code:`MISSING_VALUE`,details:{flag:s,input:e.name,kind:u.kind}});return m(a,s,u,d.value,`--${e.name}`),n+2}function p(e,t,n,i,a){let{chars:o}=e,s=n+1;for(let e=0;e<o.length;e++){let n=o.charAt(e),l=i.get(n);if(!l)throw new r(`Unknown flag -${n}`,{code:`UNKNOWN_FLAG`,details:{flag:n}});let[u,d]=l;if(!c(d)){a[u]=!0;continue}if(e<o.length-1){m(a,u,d,o.slice(e+1),`-${n}`);break}let f=t[s];if(f?.kind!==`positional`)throw new r(`Flag -${n} requires a value`,{code:`MISSING_VALUE`,details:{flag:u,input:n,kind:d.kind}});m(a,u,d,f.value,`-${n}`),s++}return s}function m(e,t,n,r,i=`--${t}`){let a=l(t,r,n,i);if(n.kind===`array`){let n=e[t];Array.isArray(n)?n.push(a):e[t]=[a]}else e[t]=a}function h(e,t){let n={},i=0;for(let r of e){if(r.schema.variadic){let e=t.slice(i);n[r.name]=e.map(e=>u(r.name,e,r.schema)),i=t.length;break}let e=t[i];e!==void 0&&(n[r.name]=u(r.name,e,r.schema),i++)}if(i<t.length){let n=t.slice(i);throw new r(`Unexpected positional argument${n.length>1?`s`:``}: ${n.join(`, `)}`,{code:`UNEXPECTED_POSITIONAL`,details:{excess:n,expected:e.length}})}return n}function g(e,t){let n,r=1/0,i=Math.max(2,Math.floor(e.length/2));for(let a of t.keys()){let o=t.get(a);if(o===void 0)continue;let[s,c]=o,l=c.aliases.find(e=>e.name===a);if(a.length<2||a!==s&&l?.hidden)continue;let u=_(e,a);u<r&&u<=i&&(r=u,n=a)}return n}function _(e,t){if(e===t)return 0;if(e.length===0)return t.length;if(t.length===0)return e.length;let n=e.length<=t.length?e:t,r=e.length<=t.length?t:e,i=n.length,a=r.length,o=Array.from({length:i+1},(e,t)=>t);for(let e=1;e<=a;e++){let t=o[0]??0;o[0]=e;for(let a=1;a<=i;a++){let i=n.charAt(a-1)===r.charAt(e-1)?0:1,s=Math.min((o[a]??0)+1,(o[a-1]??0)+1,t+i);t=o[a]??0,o[a]=s}}return o[i]??0}export{s as buildFlagLookup,c as flagExpectsValue,a as includesBeforeSeparator,d as parse,o as stripBeforeSeparator,i as tokenize};
|
|
@@ -105,9 +105,11 @@ declare const PROMPT_CANCEL: unique symbol;
|
|
|
105
105
|
* A queued answer consumed by {@link createTestPrompter}.
|
|
106
106
|
*
|
|
107
107
|
* The test prompter returns these values exactly as provided; it does not
|
|
108
|
-
* coerce
|
|
109
|
-
*
|
|
110
|
-
*
|
|
108
|
+
* coerce them. The one exception mirrors the terminal prompter: a string answer
|
|
109
|
+
* to an `input` prompt is run through the prompt's `validate` function, and a
|
|
110
|
+
* failing answer is rejected as a cancellation (see {@link createTestPrompter}).
|
|
111
|
+
* The normal resolution pipeline performs any later type coercion, so tests can
|
|
112
|
+
* supply values in the same shapes real prompts would yield:
|
|
111
113
|
*
|
|
112
114
|
* - `string` for `input` and `select`
|
|
113
115
|
* - `boolean` for `confirm`
|
|
@@ -139,6 +141,12 @@ interface TestPrompterOptions {
|
|
|
139
141
|
* Pass `PROMPT_CANCEL` as an answer to simulate the user cancelling
|
|
140
142
|
* that prompt.
|
|
141
143
|
*
|
|
144
|
+
* For `input` prompts that declare a `validate` function, a string answer is
|
|
145
|
+
* validated just as the terminal prompter does. An answer that fails validation
|
|
146
|
+
* is rejected as a cancellation (`{ answered: false }`) — mirroring the terminal
|
|
147
|
+
* prompter exhausting its retries — so prompt validation is integration-testable
|
|
148
|
+
* via {@linkcode runCommand} rather than silently accepted as the resolved value.
|
|
149
|
+
*
|
|
142
150
|
* @param answers - Ordered queue of answers. Use `PROMPT_CANCEL` for
|
|
143
151
|
* cancellation.
|
|
144
152
|
* @param options - Controls behavior when the queue is exhausted.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
const e=Symbol.for(`dreamcli.prompt.cancel`);function t(t,n){let r=0;return{promptOne(){if(r>=t.length)return n?.onExhausted===`cancel`?Promise.resolve({answered:!1}):Promise.reject(Error(`Test prompter exhausted: expected at most ${t.length} prompts, got prompt #${r+1}`));let
|
|
2
|
-
`),
|
|
3
|
-
`),{answered:!1}}async function i(e,t,n){let r=0;
|
|
1
|
+
const e=Symbol.for(`dreamcli.prompt.cancel`);function t(t,n){let r=0;return{promptOne(i){if(r>=t.length)return n?.onExhausted===`cancel`?Promise.resolve({answered:!1}):Promise.reject(Error(`Test prompter exhausted: expected at most ${t.length} prompts, got prompt #${r+1}`));let a=t[r];return r+=1,a===e||i.kind===`input`&&i.validate!==void 0&&typeof a==`string`&&i.validate(a)!==!0?Promise.resolve({answered:!1}):Promise.resolve({answered:!0,value:a})}}}function n(e,t){return{promptOne(n){switch(n.kind){case`confirm`:return r(n,e,t);case`input`:return i(n,e,t);case`select`:return a(n,e,t);case`multiselect`:return o(n,e,t)}}}}async function r(e,t,n){let r=e.default??!0,i=0;for(;i<10;){let a=r?`(Y/n)`:`(y/N)`;n(`${e.message} ${a} `);let o=await t();if(o===null)return{answered:!1};let s=o.trim().toLowerCase();if(s===``)return{answered:!0,value:r};if(s===`y`||s===`yes`)return{answered:!0,value:!0};if(s===`n`||s===`no`)return{answered:!0,value:!1};n(`Please answer y or n.
|
|
2
|
+
`),i+=1}return n(`Too many invalid attempts.
|
|
3
|
+
`),{answered:!1}}async function i(e,t,n){let r=0,i=[];e.placeholder!==void 0&&i.push(e.placeholder),e.default!==void 0&&i.push(`default: ${e.default}`);let a=i.length>0?` (${i.join(`, `)})`:``;for(;r<10;){n(`${e.message}${a}: `);let i=await t();if(i===null)return{answered:!1};let o=i.trim();if(o===``)return e.default===void 0?{answered:!0,value:``}:{answered:!0,value:e.default};if(e.validate!==void 0){let t=e.validate(o);if(t!==!0){n(`${t}\n`),r+=1;continue}}return{answered:!0,value:o}}return n(`Too many invalid attempts.
|
|
4
4
|
`),{answered:!1}}async function a(e,t,n){let r=0;for(;r<10;){n(`${e.message}\n`);for(let t=0;t<e.choices.length;t+=1){let r=e.choices[t];if(r===void 0)continue;let i=r.label??r.value,a=r.description===void 0?``:` - ${r.description}`;n(` ${t+1}) ${i}${a}\n`)}n(`Enter number: `);let i=await t();if(i===null)return{answered:!1};let a=Number(i.trim());if(Number.isNaN(a)||!Number.isInteger(a)||a<1||a>e.choices.length){n(`Please enter a number between 1 and ${e.choices.length}.\n`),r+=1;continue}let o=e.choices[a-1];if(o===void 0){r+=1;continue}return{answered:!0,value:o.value}}return n(`Too many invalid attempts.
|
|
5
5
|
`),{answered:!1}}async function o(e,t,n){let r=0;for(;r<10;){n(`${e.message}\n`);for(let t=0;t<e.choices.length;t+=1){let r=e.choices[t];if(r===void 0)continue;let i=r.label??r.value,a=r.description===void 0?``:` - ${r.description}`;n(` ${t+1}) ${i}${a}\n`)}n(`Enter numbers separated by commas (e.g. 1,3${e.min===void 0?``:`, min: ${e.min}`}${e.max===void 0?``:`, max: ${e.max}`}): `);let i=await t();if(i===null)return{answered:!1};let a=i.trim();if(a===``){if(e.min!==void 0&&e.min>0){n(`Please select at least ${e.min}.\n`),r+=1;continue}return{answered:!0,value:[]}}let o=a.split(`,`).map(e=>e.trim()),s=[],c=!0;for(let t of o){let r=Number(t);if(Number.isNaN(r)||!Number.isInteger(r)||r<1||r>e.choices.length){n(`Invalid selection '${t}'. Use numbers between 1 and ${e.choices.length}.\n`),c=!1;break}s.push(r)}if(!c){r+=1;continue}let l=[...new Set(s)];if(e.min!==void 0&&l.length<e.min){n(`Please select at least ${e.min}.\n`),r+=1;continue}if(e.max!==void 0&&l.length>e.max){n(`Please select at most ${e.max}.\n`),r+=1;continue}return{answered:!0,value:l.map(t=>e.choices[t-1]?.value??``)}}return n(`Too many invalid attempts.
|
|
6
6
|
`),{answered:!1}}function s(e,t){if(e.kind===`confirm`||e.kind===`input`)return e;let n=e.choices??t?.map(e=>({value:e}))??[];if(n.length===0)throw Error(`Prompt config (${e.kind}) requires choices but none were provided and no enum values are available`);let r=n;return e.kind===`select`?{kind:`select`,message:e.message,choices:r}:{kind:`multiselect`,message:e.message,choices:r,...e.min===void 0?{}:{min:e.min},...e.max===void 0?{}:{max:e.max}}}export{e as PROMPT_CANCEL,n as createTerminalPrompter,t as createTestPrompter,s as resolvePromptConfig};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{describeNumberConstraintViolation as e,validateNumberConstraints as t}from"../schema/number-constraints.mjs";import{ValidationError as n}from"../errors/index.mjs";import{toSharedArgPropertySchema as r,toSharedFlagPropertySchema as i}from"./property.mjs";function a(e){switch(e.kind){case`env`:return`from env ${e.envVar}`;case`config`:return`from config ${e.configPath}`;case`prompt`:return`from prompt`}}function o(e){switch(e.kind){case`env`:return{envVar:e.envVar};case`config`:return{configPath:e.configPath};case`prompt`:return{source:`prompt`}}}function s(e,t,r,i,s,c,l,u){return{ok:!1,error:new n(`${c} ${a(t)} for flag --${e}`,{code:r,details:{flag:e,...o(t),value:s,expected:i,...u},suggest:l})}}function c(r,i,s,c,l){let u=t(c,l);if(u===void 0)return{ok:!0,value:c};let d=e(u);return{ok:!1,error:new n(`Invalid number value '${String(s)}' ${a(i)} for flag --${r}: ${d}`,{code:`CONSTRAINT_VIOLATED`,details:{flag:r,...o(i),value:s,expected:`number`,constraint:u.kind,...`bound`in u?{bound:u.bound}:{}},suggest:i.kind===`env`?`Set ${i.envVar} to a valid number`:i.kind===`config`?`Set ${i.configPath} to a valid number in your config`:`Enter a valid number for --${r}`})}}function l(e,t,n,r){let a=i(r);if(a!==void 0)return u(e,t,n,a);switch(r.kind){case`boolean`:if(typeof n==`boolean`)return{ok:!0,value:n};if(typeof n==`string`){let e=n.toLowerCase();if(e===`true`||e===`1`||e===`yes`||t.kind===`prompt`&&e===`y`)return{ok:!0,value:!0};if(e===`false`||e===`0`||e===`no`||e===``||t.kind===`prompt`&&e===`n`)return{ok:!0,value:!1}}return s(e,t,`TYPE_MISMATCH`,`boolean`,n,typeof n==`string`?`Invalid boolean value '${n}'`:`Invalid boolean value`,t.kind===`env`?`Set ${t.envVar} to true/false, 1/0, or yes/no`:t.kind===`config`?`Set ${t.configPath} to true or false in your config`:`Answer yes or no for --${e}`);case`array`:if(Array.isArray(n)){if(r.elementSchema){let i=[];for(let a of n){let n=l(e,t,a,r.elementSchema);if(!n.ok)return n;i.push(n.value)}return{ok:!0,value:i}}return{ok:!0,value:n}}if(typeof n==`string`){if(n===``)return{ok:!0,value:[]};let i=n.split(`,`);if(r.elementSchema){let n=[];for(let a of i){let i=l(e,t,t.kind===`prompt`?a.trim():a,r.elementSchema);if(!i.ok)return i;n.push(i.value)}return{ok:!0,value:n}}return{ok:!0,value:t.kind===`prompt`?i.map(e=>e.trim()):i}}return s(e,t,`TYPE_MISMATCH`,`array`,n,`Invalid array value`,t.kind===`env`?`Set ${t.envVar} to comma-separated values`:t.kind===`config`?`Set ${t.configPath} to an array in your config`:`Provide valid values for --${e}`)}throw Error(`Unreachable flag coercion kind: ${r.kind}`)}function u(e,t,r,i){switch(i.kind){case`string`:return typeof r==`string`?{ok:!0,value:r}:t.kind===`prompt`||t.kind===`config`&&(typeof r==`number`||typeof r==`boolean`)?{ok:!0,value:String(r)}:s(e,t,`TYPE_MISMATCH`,`string`,r,`Invalid string value`,t.kind===`config`?`Set ${t.configPath} to a string in your config`:`Enter a valid string for --${e}`);case`number`:if(typeof r==`number`)return Number.isNaN(r)?s(e,t,`TYPE_MISMATCH`,`number`,r,`Invalid number value NaN`,t.kind===`env`?`Set ${t.envVar} to a valid number`:t.kind===`config`?`Set ${t.configPath} to a valid number in your config`:`Enter a valid number for --${e}`):c(e,t,r,r,i.numberConstraints);if(typeof r==`string`){let n=Number(r);if(!Number.isNaN(n))return c(e,t,r,n,i.numberConstraints)}return s(e,t,`TYPE_MISMATCH`,`number`,r,typeof r==`string`?`Invalid number value '${r}'`:`Invalid number value`,t.kind===`env`?`Set ${t.envVar} to a valid number`:t.kind===`config`?`Set ${t.configPath} to a valid number in your config`:`Enter a valid number for --${e}`);case`enum`:{let s=i.enumValues??[];return typeof r==`string`&&s.includes(r)?{ok:!0,value:r}:{ok:!1,error:new n(`Invalid value '${String(r)}' ${a(t)} for flag --${e}. Allowed: ${s.join(`, `)}`,{code:`INVALID_ENUM`,details:{flag:e,...o(t),value:r,allowed:s},suggest:t.kind===`env`?`Set ${t.envVar} to one of: ${s.join(`, `)}`:t.kind===`config`?`Set ${t.configPath} to one of: ${s.join(`, `)}`:`Select one of: ${s.join(`, `)}`})}}case`custom`:if(i.parseFn===void 0)return{ok:!0,value:r};try{return{ok:!0,value:i.parseFn(r)}}catch(i){let a=i instanceof Error?i.message:String(i);return{ok:!1,error:new n(`Failed to parse ${t.kind===`env`?`env ${t.envVar}`:t.kind===`config`?`config ${t.configPath}`:`prompt value`} for flag --${e}: ${a}`,{code:`TYPE_MISMATCH`,details:{flag:e,...o(t),value:r,expected:`custom`},suggest:t.kind===`env`?`Set ${t.envVar} to a valid value for --${e}`:t.kind===`config`?`Set ${t.configPath} to a valid value for --${e} in your config`:`Enter a valid value for --${e}`})}}}}function d(e){return e.kind===`env`?`from env ${e.envVar}`:`from stdin`}function f(e){return e.kind===`env`?{envVar:e.envVar}:{source:`stdin`}}function p(e,t,n){return t.kind===`env`?n===`number`?`Set ${t.envVar} to a valid number`:`Set ${t.envVar} to a valid value for <${e}>`:n===`number`?`Pipe a valid number to stdin for <${e}>`:`Pipe a valid value to stdin for <${e}>`}function m(e){return e.kind===`env`?{kind:`env`,envVar:e.envVar}:{kind:`prompt`}}function h(e,t,n,r){switch(n.kind){case`number`:{let n=`Invalid number value '<redacted>' ${d(t)} for argument <${e}>`;if(r.code!==`CONSTRAINT_VIOLATED`)return n;let i=/: ([^:]+)$/.exec(r.message)?.[1];return i===void 0?n:`${n}: ${i}`}case`enum`:{let n=Array.isArray(r.details?.allowed)?r.details.allowed.filter(e=>typeof e==`string`):[];return`Invalid value '<redacted>' ${d(t)} for argument <${e}>. Allowed: ${n.join(`, `)}`}case`custom`:{let n=/: ([^:]+)$/.exec(r.message)?.[1];return n===void 0?`Invalid value '<redacted>' ${d(t)} for argument <${e}>`:`Invalid value '<redacted>' ${d(t)} for argument <${e}>: ${n}`}case`string`:return`Invalid value '<redacted>' ${d(t)} for argument <${e}>`;default:return n.kind}}function g(e,t,n,r){return{arg:e,...f(t),...n.kind===`number`||n.kind===`custom`?{expected:n.kind}:{},...n.kind===`number`&&typeof r.details?.constraint==`string`?{constraint:r.details.constraint}:{},...n.kind===`number`&&typeof r.details?.bound==`number`?{bound:r.details.bound}:{},...n.kind===`enum`&&Array.isArray(r.details?.allowed)?{allowed:r.details.allowed.filter(e=>typeof e==`string`)}:{}}}function _(e,t,n,r){if(n.kind===`number`||n.kind===`custom`)return p(e,t,n.kind);if(n.kind===`enum`){let e=(Array.isArray(r.details?.allowed)?r.details.allowed.filter(e=>typeof e==`string`):[]).join(`, `);return t.kind===`env`?`Set ${t.envVar} to one of: ${e}`:`Provide one of: ${e}`}}function v(e,t,i,a){let o=u(e,m(t),i,r(a));if(o.ok)return o;let s=_(e,t,a,o.error),c=s===void 0?{code:o.error.code,details:g(e,t,a,o.error)}:{code:o.error.code,details:g(e,t,a,o.error),suggest:s};return{ok:!1,error:new n(h(e,t,a,o.error),c)}}export{v as coerceArgStringValue,l as coerceValue};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{ValidationError as e}from"../errors/index.mjs";import{resolvePromptConfig as t}from"../prompt/index.mjs";import{coerceValue as n}from"./coerce.mjs";import{isNonEmpty as r,throwAggregatedErrors as i}from"./errors.mjs";import{resolveConfigPath as a}from"./config.mjs";async function o(t,o,s,c,d,f,p){let m={},h=[],g=new Set;for(let[e,r]of Object.entries(t)){let t=Object.hasOwn(o,e),i=o[e];if(t&&i!==void 0){r.deprecated!==void 0&&p.push({kind:`flag`,name:e,message:r.deprecated}),m[e]=i;continue}if(r.envVar!==void 0){let t=s[r.envVar];if(t!==void 0){let i=n(e,{kind:`env`,envVar:r.envVar},t,r);if(i.ok){r.deprecated!==void 0&&p.push({kind:`flag`,name:e,message:r.deprecated}),m[e]=i.value;continue}h.push(i.error),g.add(e);continue}}if(r.configPath!==void 0){let t=a(c,r.configPath);if(t!==void 0){let i=n(e,{kind:`config`,configPath:r.configPath},t,r);if(i.ok){r.deprecated!==void 0&&p.push({kind:`flag`,name:e,message:r.deprecated}),m[e]=i.value;continue}h.push(i.error),g.add(e)}}}let _=f===void 0?void 0:f({flags:m});for(let[n,r]of Object.entries(t)){if(Object.hasOwn(m,n)||g.has(n))continue;let t=_?.[n],i;if(i=t!=null&&t!==!1&&t!==0&&t!==``?t:t===!1?void 0:r.prompt,i!==void 0&&d!==void 0){let e=await l(n,r,i,d);if(e.ok){r.deprecated!==void 0&&p.push({kind:`flag`,name:n,message:r.deprecated}),m[n]=e.value;continue}if(e.error!==void 0){h.push(e.error);continue}}if(r.defaultValue!==void 0){m[n]=r.defaultValue;continue}if(r.kind===`array`&&r.presence!==`required`){m[n]=[];continue}if(r.presence===`required`){let t={flag:n,kind:r.kind};r.envVar!==void 0&&(t.envVar=r.envVar),r.configPath!==void 0&&(t.configPath=r.configPath),h.push(new e(`Missing required flag --${n}`,{code:`REQUIRED_FLAG`,details:t,suggest:u(n,r)}));continue}m[n]=void 0}return r(h)&&i(h),m}const s={boolean:[`confirm`],string:[`input`,`select`],number:[`input`],enum:[`select`,`input`],array:[`multiselect`],custom:[`input`,`select`,`confirm`,`multiselect`]};function c(t,n,r){let i=s[n];if(!i.includes(r))return new e(`Prompt kind '${r}' is not compatible with ${n} flag --${t}. Use '${i[0]}' instead`,{code:`CONSTRAINT_VIOLATED`,details:{flag:t,flagKind:n,promptKind:r,allowed:i},suggest:`Change the prompt to { kind: '${i[0]}' } for --${t}`})}async function l(e,r,i,a){let o=c(e,r.kind,i.kind);if(o!==void 0)return{ok:!1,error:o};let s=t(i,r.enumValues),l=await a.promptOne(s);return l.
|
|
1
|
+
import{ValidationError as e}from"../errors/index.mjs";import{resolvePromptConfig as t}from"../prompt/index.mjs";import{coerceValue as n}from"./coerce.mjs";import{isNonEmpty as r,throwAggregatedErrors as i}from"./errors.mjs";import{resolveConfigPath as a}from"./config.mjs";async function o(t,o,s,c,d,f,p){let m={},h=[],g=new Set;for(let[e,r]of Object.entries(t)){let t=Object.hasOwn(o,e),i=o[e];if(t&&i!==void 0){r.deprecated!==void 0&&p.push({kind:`flag`,name:e,message:r.deprecated}),m[e]=i;continue}if(r.envVar!==void 0){let t=s[r.envVar];if(t!==void 0){let i=n(e,{kind:`env`,envVar:r.envVar},t,r);if(i.ok){r.deprecated!==void 0&&p.push({kind:`flag`,name:e,message:r.deprecated}),m[e]=i.value;continue}h.push(i.error),g.add(e);continue}}if(r.configPath!==void 0){let t=a(c,r.configPath);if(t!==void 0){let i=n(e,{kind:`config`,configPath:r.configPath},t,r);if(i.ok){r.deprecated!==void 0&&p.push({kind:`flag`,name:e,message:r.deprecated}),m[e]=i.value;continue}h.push(i.error),g.add(e)}}}let _=f===void 0?void 0:f({flags:m});for(let[n,r]of Object.entries(t)){if(Object.hasOwn(m,n)||g.has(n))continue;let t=_?.[n],i;if(i=t!=null&&t!==!1&&t!==0&&t!==``?t:t===!1?void 0:r.prompt,i!==void 0&&d!==void 0){let e=await l(n,r,i,d);if(e.ok){r.deprecated!==void 0&&p.push({kind:`flag`,name:n,message:r.deprecated}),m[n]=e.value;continue}if(e.error!==void 0){h.push(e.error);continue}}if(r.defaultValue!==void 0){m[n]=r.defaultValue;continue}if(r.kind===`array`&&r.presence!==`required`){m[n]=[];continue}if(r.presence===`required`){let t={flag:n,kind:r.kind};r.envVar!==void 0&&(t.envVar=r.envVar),r.configPath!==void 0&&(t.configPath=r.configPath),h.push(new e(`Missing required flag --${n}`,{code:`REQUIRED_FLAG`,details:t,suggest:u(n,r)}));continue}m[n]=void 0}return r(h)&&i(h),m}const s={boolean:[`confirm`],string:[`input`,`select`],number:[`input`],enum:[`select`,`input`],array:[`multiselect`],custom:[`input`,`select`,`confirm`,`multiselect`]};function c(t,n,r){let i=s[n];if(!i.includes(r))return new e(`Prompt kind '${r}' is not compatible with ${n} flag --${t}. Use '${i[0]}' instead`,{code:`CONSTRAINT_VIOLATED`,details:{flag:t,flagKind:n,promptKind:r,allowed:i},suggest:`Change the prompt to { kind: '${i[0]}' } for --${t}`})}async function l(e,r,i,a){let o=c(e,r.kind,i.kind);if(o!==void 0)return{ok:!1,error:o};let s=t(i,r.enumValues),l=await a.promptOne(s);return!l.answered||i.kind===`input`&&i.default===void 0&&typeof l.value==`string`&&l.value.trim()===``?{ok:!1,error:void 0}:n(e,{kind:`prompt`},l.value,r)}function u(e,t){let n=[];if(n.push(`Provide --${e}${t.kind===`boolean`?``:` <value>`}`),t.envVar!==void 0&&n.push(`set ${t.envVar}`),t.configPath!==void 0&&n.push(`add ${t.configPath} to config`),n.length<=1)return n.join(``);let r=n.slice(0,-1),i=n.slice(-1).join(``);return n.length===2?`${r.join(``)} or ${i}`:`${r.join(`, `)}, or ${i}`}export{o as resolveFlags};
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { NumberConstraints } from "../schema/number-constraints.mjs";
|
|
1
2
|
import { ArgSchema } from "../schema/arg.mjs";
|
|
2
3
|
import { FlagSchema } from "../schema/flag.mjs";
|
|
3
4
|
|
|
@@ -11,6 +12,7 @@ type SharedPropertySchema = {
|
|
|
11
12
|
readonly kind: 'string';
|
|
12
13
|
} | {
|
|
13
14
|
readonly kind: 'number';
|
|
15
|
+
readonly numberConstraints: NumberConstraints | undefined;
|
|
14
16
|
} | {
|
|
15
17
|
readonly kind: 'enum';
|
|
16
18
|
readonly enumValues: readonly string[] | undefined;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
function e(e){switch(e.kind){case`string`:return{kind:`string`};case`number`:return{kind:`number
|
|
1
|
+
function e(e){switch(e.kind){case`string`:return{kind:`string`};case`number`:return{kind:`number`,numberConstraints:e.numberConstraints};case`enum`:return{kind:`enum`,enumValues:e.enumValues};case`custom`:return e.parseFn===void 0?{kind:`custom`}:{kind:`custom`,parseFn:e.parseFn};case`boolean`:case`array`:return}}function t(e){switch(e.kind){case`string`:return{kind:`string`};case`number`:return{kind:`number`,numberConstraints:e.numberConstraints};case`enum`:return{kind:`enum`,enumValues:e.enumValues};case`custom`:{if(e.parseFn===void 0)return{kind:`custom`};let t=e.parseFn;return{kind:`custom`,parseFn:e=>t(typeof e==`string`?e:String(e))}}}}export{t as toSharedArgPropertySchema,e as toSharedFlagPropertySchema};
|
|
@@ -1,13 +1,6 @@
|
|
|
1
|
+
import { NumberConstraints } from "./number-constraints.mjs";
|
|
2
|
+
|
|
1
3
|
//#region src/core/schema/arg.d.ts
|
|
2
|
-
/**
|
|
3
|
-
* Positional argument schema builder with full type inference.
|
|
4
|
-
*
|
|
5
|
-
* Each factory (`arg.string()`, `arg.number()`, `arg.enum()`, `arg.custom()`) returns an
|
|
6
|
-
* immutable {@linkcode ArgBuilder} whose generic parameter tracks the value type,
|
|
7
|
-
* presence state, and variadic flag through the fluent chain.
|
|
8
|
-
*
|
|
9
|
-
* @module dreamcli/core/schema/arg
|
|
10
|
-
*/
|
|
11
4
|
/** All arg presence states as a runtime array. */
|
|
12
5
|
declare const ARG_PRESENCES: readonly ['required', 'optional', 'defaulted'];
|
|
13
6
|
/**
|
|
@@ -29,6 +22,8 @@ interface ArgConfig {
|
|
|
29
22
|
readonly presence: ArgPresence;
|
|
30
23
|
/** Whether this arg consumes remaining positionals. */
|
|
31
24
|
readonly variadic: boolean;
|
|
25
|
+
/** The runtime kind discriminator, mirroring {@link ArgKind}. */
|
|
26
|
+
readonly argKind: ArgKind;
|
|
32
27
|
}
|
|
33
28
|
/**
|
|
34
29
|
* Advanced type helper used by {@linkcode ArgBuilder} modifiers to replace presence.
|
|
@@ -38,6 +33,7 @@ type WithArgPresence<C extends ArgConfig, P extends ArgPresence> = {
|
|
|
38
33
|
readonly valueType: C['valueType'];
|
|
39
34
|
readonly presence: P;
|
|
40
35
|
readonly variadic: C['variadic'];
|
|
36
|
+
readonly argKind: C['argKind'];
|
|
41
37
|
};
|
|
42
38
|
/**
|
|
43
39
|
* Advanced type helper used by {@linkcode ArgBuilder.variadic | ArgBuilder.variadic()}.
|
|
@@ -47,6 +43,7 @@ type WithVariadic<C extends ArgConfig> = {
|
|
|
47
43
|
readonly valueType: C['valueType'];
|
|
48
44
|
readonly presence: C['presence'];
|
|
49
45
|
readonly variadic: true;
|
|
46
|
+
readonly argKind: C['argKind'];
|
|
50
47
|
};
|
|
51
48
|
/**
|
|
52
49
|
* Compute the final value type from config — this is what handlers receive.
|
|
@@ -102,6 +99,13 @@ interface ArgSchema {
|
|
|
102
99
|
readonly envVar: string | undefined;
|
|
103
100
|
/** Allowed literal values when `kind === 'enum'`. */
|
|
104
101
|
readonly enumValues: readonly string[] | undefined;
|
|
102
|
+
/**
|
|
103
|
+
* Numeric constraints when `kind === 'number'` (`undefined` otherwise).
|
|
104
|
+
*
|
|
105
|
+
* Enforced at the parse and resolution boundaries. `finite` defaults to
|
|
106
|
+
* `true`, so `Infinity` is rejected even when no constraints object is set.
|
|
107
|
+
*/
|
|
108
|
+
readonly numberConstraints: NumberConstraints | undefined;
|
|
105
109
|
/** Custom parse function (only when `kind === 'custom'`). */
|
|
106
110
|
readonly parseFn: ArgParseFn<unknown> | undefined;
|
|
107
111
|
/**
|
|
@@ -376,6 +380,71 @@ declare class ArgBuilder<C extends ArgConfig> {
|
|
|
376
380
|
* ```
|
|
377
381
|
*/
|
|
378
382
|
deprecated(message?: string): ArgBuilder<C>;
|
|
383
|
+
/**
|
|
384
|
+
* Require an integer value. Composes with other numeric constraints.
|
|
385
|
+
*
|
|
386
|
+
* @param value - Whether to require an integer.
|
|
387
|
+
* @defaultValue `true`
|
|
388
|
+
* @returns The builder (for chaining).
|
|
389
|
+
*
|
|
390
|
+
* @example
|
|
391
|
+
* ```ts
|
|
392
|
+
* arg.number().int() // rejects 3.7, accepts 3
|
|
393
|
+
* arg.number({ int: true }).int(false) // re-allows non-integers
|
|
394
|
+
* ```
|
|
395
|
+
*/
|
|
396
|
+
int(this: ArgBuilder<C & {
|
|
397
|
+
readonly argKind: 'number';
|
|
398
|
+
}>, value?: boolean): ArgBuilder<C>;
|
|
399
|
+
/**
|
|
400
|
+
* Set an inclusive lower bound. Composes with other numeric constraints;
|
|
401
|
+
* a later call overrides an earlier `min` (including one from the options
|
|
402
|
+
* object).
|
|
403
|
+
*
|
|
404
|
+
* @param value - Inclusive minimum.
|
|
405
|
+
* @returns The builder (for chaining).
|
|
406
|
+
*
|
|
407
|
+
* @example
|
|
408
|
+
* ```ts
|
|
409
|
+
* arg.number().min(0) // rejects -1, accepts 0
|
|
410
|
+
* arg.number({ min: 0 }).min(5) // effective min is 5
|
|
411
|
+
* ```
|
|
412
|
+
*/
|
|
413
|
+
min(this: ArgBuilder<C & {
|
|
414
|
+
readonly argKind: 'number';
|
|
415
|
+
}>, value: number): ArgBuilder<C>;
|
|
416
|
+
/**
|
|
417
|
+
* Set an inclusive upper bound. Composes with other numeric constraints;
|
|
418
|
+
* a later call overrides an earlier `max`.
|
|
419
|
+
*
|
|
420
|
+
* @param value - Inclusive maximum.
|
|
421
|
+
* @returns The builder (for chaining).
|
|
422
|
+
*
|
|
423
|
+
* @example
|
|
424
|
+
* ```ts
|
|
425
|
+
* arg.number().max(100) // rejects 101, accepts 100
|
|
426
|
+
* ```
|
|
427
|
+
*/
|
|
428
|
+
max(this: ArgBuilder<C & {
|
|
429
|
+
readonly argKind: 'number';
|
|
430
|
+
}>, value: number): ArgBuilder<C>;
|
|
431
|
+
/**
|
|
432
|
+
* Require (or, with `false`, allow) a finite value. Finiteness is enforced
|
|
433
|
+
* by default, so this is mainly used as `.finite(false)` to re-allow
|
|
434
|
+
* `Infinity` / `-Infinity`.
|
|
435
|
+
*
|
|
436
|
+
* @param allow - Whether to require a finite value.
|
|
437
|
+
* @defaultValue `true`
|
|
438
|
+
* @returns The builder (for chaining).
|
|
439
|
+
*
|
|
440
|
+
* @example
|
|
441
|
+
* ```ts
|
|
442
|
+
* arg.number().finite(false) // accepts Infinity
|
|
443
|
+
* ```
|
|
444
|
+
*/
|
|
445
|
+
finite(this: ArgBuilder<C & {
|
|
446
|
+
readonly argKind: 'number';
|
|
447
|
+
}>, allow?: boolean): ArgBuilder<C>;
|
|
379
448
|
}
|
|
380
449
|
/**
|
|
381
450
|
* Arg factory functions — the public API for creating positional arguments.
|
|
@@ -439,6 +508,7 @@ interface ArgFactory {
|
|
|
439
508
|
readonly valueType: string;
|
|
440
509
|
readonly presence: 'required';
|
|
441
510
|
readonly variadic: false;
|
|
511
|
+
readonly argKind: 'string';
|
|
442
512
|
}>;
|
|
443
513
|
/**
|
|
444
514
|
* Number-valued positional argument. Required by default.
|
|
@@ -460,12 +530,20 @@ interface ArgFactory {
|
|
|
460
530
|
* // $ mycli serve → 3000
|
|
461
531
|
* ```
|
|
462
532
|
*
|
|
533
|
+
* Constraints are enforced at the parse and resolution boundaries and also
|
|
534
|
+
* compose via chained methods (`.int()`, `.min()`, `.max()`, `.finite()`),
|
|
535
|
+
* which override values set here. The resolved value type stays `number`.
|
|
536
|
+
*
|
|
537
|
+
* @param constraints - Optional numeric constraints. `finite` defaults to
|
|
538
|
+
* `true`, so `Infinity` / `-Infinity` are rejected unless `finite: false`.
|
|
539
|
+
* @defaultValue `undefined` (finite-only, no bounds, non-integer allowed)
|
|
463
540
|
* @returns A required number {@link ArgBuilder}.
|
|
464
541
|
*/
|
|
465
|
-
number(): ArgBuilder<{
|
|
542
|
+
number(constraints?: NumberConstraints): ArgBuilder<{
|
|
466
543
|
readonly valueType: number;
|
|
467
544
|
readonly presence: 'required';
|
|
468
545
|
readonly variadic: false;
|
|
546
|
+
readonly argKind: 'number';
|
|
469
547
|
}>;
|
|
470
548
|
/**
|
|
471
549
|
* Enum-valued positional argument. Required by default.
|
|
@@ -494,6 +572,7 @@ interface ArgFactory {
|
|
|
494
572
|
readonly valueType: T[number];
|
|
495
573
|
readonly presence: 'required';
|
|
496
574
|
readonly variadic: false;
|
|
575
|
+
readonly argKind: 'enum';
|
|
497
576
|
}>;
|
|
498
577
|
/**
|
|
499
578
|
* Custom-parsed positional argument. Required by default.
|
|
@@ -524,6 +603,7 @@ interface ArgFactory {
|
|
|
524
603
|
readonly valueType: T;
|
|
525
604
|
readonly presence: 'required';
|
|
526
605
|
readonly variadic: false;
|
|
606
|
+
readonly argKind: 'custom';
|
|
527
607
|
}>;
|
|
528
608
|
}
|
|
529
609
|
/**
|
package/dist/core/schema/arg.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
function
|
|
1
|
+
import{assertNumberConstraints as e}from"./number-constraints.mjs";function t(e,t){return{kind:e,presence:`required`,variadic:!1,stdinMode:!1,defaultValue:void 0,description:void 0,envVar:void 0,enumValues:void 0,numberConstraints:void 0,parseFn:void 0,deprecated:void 0,...t}}var n=class t{schema;constructor(e){this.schema=e}required(){return new t({...this.schema,presence:`required`})}optional(){return new t({...this.schema,presence:`optional`})}default(e){return new t({...this.schema,presence:`defaulted`,defaultValue:e})}variadic(){return new t({...this.schema,variadic:!0})}stdin(){return new t({...this.schema,stdinMode:!0})}env(e){return new t({...this.schema,envVar:e})}describe(e){return new t({...this.schema,description:e})}deprecated(e){return new t({...this.schema,deprecated:e??!0})}int(e=!0){return new t({...this.schema,numberConstraints:{...this.schema.numberConstraints,int:e}})}min(n){let r={...this.schema.numberConstraints,min:n};return e(r),new t({...this.schema,numberConstraints:r})}max(n){let r={...this.schema.numberConstraints,max:n};return e(r),new t({...this.schema,numberConstraints:r})}finite(e=!0){return new t({...this.schema,numberConstraints:{...this.schema.numberConstraints,finite:e}})}};const r={string(){return new n(t(`string`))},number(r){return r!==void 0&&e(r),new n(t(`number`,r===void 0?{}:{numberConstraints:r}))},enum(e){return new n(t(`enum`,{enumValues:e}))},custom(e){return new n(t(`custom`,{parseFn:e}))}};export{n as ArgBuilder,r as arg,t as createArgSchema};
|
|
@@ -775,7 +775,9 @@ declare class CommandBuilder<F extends Record<string, FlagBuilder<FlagConfig>> =
|
|
|
775
775
|
* The builder controls the flag's type, presence, aliases, env/config
|
|
776
776
|
* bindings, and description. See {@link FlagBuilder} for available modifiers.
|
|
777
777
|
*
|
|
778
|
-
* @param name - Flag name
|
|
778
|
+
* @param name - Flag name: the `--name` CLI token and the matching key on `flags`
|
|
779
|
+
* in the handler. The name is preserved as-is (no camelCasing); read non-identifier
|
|
780
|
+
* names such as `'dry-run'` with bracket access (`flags['dry-run']`).
|
|
779
781
|
* @param builder - Configured {@linkcode FlagBuilder} from {@linkcode flag | flag.string()},
|
|
780
782
|
* `flag.boolean()`, `flag.number()`, `flag.enum()`, `flag.array()`, or `flag.custom()`.
|
|
781
783
|
*
|