cli-kiss 0.2.2 → 0.2.4
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/README.md +1 -1
- package/dist/index.d.ts +711 -1046
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/docs/.vitepress/config.mts +1 -0
- package/docs/.vitepress/theme/index.ts +4 -0
- package/docs/.vitepress/theme/style.css +4 -0
- package/docs/guide/01_getting_started.md +4 -4
- package/docs/guide/02_commands.md +72 -41
- package/docs/guide/03_options.md +13 -14
- package/docs/guide/04_positionals.md +6 -8
- package/docs/guide/05_types.md +9 -11
- package/docs/guide/06_run.md +4 -5
- package/docs/index.md +8 -3
- package/docs/public/hero.png +0 -0
- package/package.json +1 -1
- package/src/lib/Command.ts +151 -275
- package/src/lib/Operation.ts +57 -95
- package/src/lib/Option.ts +194 -181
- package/src/lib/Positional.ts +54 -112
- package/src/lib/Reader.ts +155 -156
- package/src/lib/Run.ts +64 -69
- package/src/lib/Type.ts +89 -145
- package/src/lib/Typo.ts +131 -195
- package/src/lib/Usage.ts +203 -69
- package/tests/unit.Reader.aliases.ts +31 -15
- package/tests/unit.Reader.commons.ts +99 -43
- package/tests/unit.Reader.shortBig.ts +75 -31
- package/tests/unit.command.execute.ts +146 -91
- package/tests/unit.command.usage.ts +235 -114
- package/tests/unit.runner.cycle.ts +50 -20
- package/tests/unit.runner.errors.ts +19 -3
package/src/lib/Operation.ts
CHANGED
|
@@ -1,113 +1,73 @@
|
|
|
1
|
-
import { Option,
|
|
2
|
-
import { Positional,
|
|
1
|
+
import { Option, OptionDecoder } from "./Option";
|
|
2
|
+
import { Positional, PositionalDecoder } from "./Positional";
|
|
3
3
|
import { ReaderArgs } from "./Reader";
|
|
4
|
+
import { UsageOperation, UsageOption, UsagePositional } from "./Usage";
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
|
-
*
|
|
7
|
-
* async execution handler that together form the core logic of a CLI command.
|
|
7
|
+
* Options, positionals, and an async handler that together form the logic of a CLI command.
|
|
8
8
|
*
|
|
9
|
-
*
|
|
10
|
-
* {@link
|
|
11
|
-
* a full {@link Command}.
|
|
9
|
+
* Created with {@link operation} and passed to {@link command},
|
|
10
|
+
* {@link commandWithSubcommands}, or {@link commandChained}.
|
|
12
11
|
*
|
|
13
|
-
* @typeParam
|
|
14
|
-
*
|
|
15
|
-
* @typeParam Output - The value the handler produces. For leaf operations this is
|
|
16
|
-
* typically `void`; for intermediate stages it is the payload forwarded to the next
|
|
17
|
-
* command in a chain.
|
|
12
|
+
* @typeParam Context - Injected at execution; forwarded to handlers.
|
|
13
|
+
* @typeParam Result - Value produced on execution; typically `void`.
|
|
18
14
|
*/
|
|
19
|
-
export type Operation<
|
|
15
|
+
export type Operation<Context, Result> = {
|
|
20
16
|
/**
|
|
21
|
-
* Returns usage metadata
|
|
22
|
-
* Called by the parent command factory when building the help/usage output.
|
|
17
|
+
* Returns usage metadata without consuming any arguments.
|
|
23
18
|
*/
|
|
24
|
-
generateUsage():
|
|
19
|
+
generateUsage(): UsageOperation;
|
|
25
20
|
/**
|
|
26
|
-
*
|
|
27
|
-
* {@link OperationFactory} that can create a ready-to-execute
|
|
28
|
-
* {@link OperationInstance}.
|
|
29
|
-
*
|
|
30
|
-
* Any parse error (unknown option, type mismatch, etc.) is captured and re-thrown
|
|
31
|
-
* when {@link OperationFactory.createInstance} is called.
|
|
32
|
-
*
|
|
33
|
-
* @param readerArgs - The shared argument reader. Options are registered on it and
|
|
34
|
-
* positionals are consumed in declaration order.
|
|
21
|
+
* Consumes args from `readerArgs` and returns an {@link OperationDecoder}.
|
|
35
22
|
*/
|
|
36
|
-
|
|
23
|
+
consumeAndMakeDecoder(
|
|
24
|
+
readerArgs: ReaderArgs,
|
|
25
|
+
): OperationDecoder<Context, Result>;
|
|
37
26
|
};
|
|
38
27
|
|
|
39
28
|
/**
|
|
40
|
-
* Produced by {@link Operation.
|
|
41
|
-
* Instantiating it finalises value extraction and produces an {@link OperationInstance}.
|
|
29
|
+
* Produced by {@link Operation.consumeAndMakeDecoder}.
|
|
42
30
|
*
|
|
43
|
-
* @typeParam
|
|
44
|
-
* @typeParam
|
|
31
|
+
* @typeParam Context - See {@link Operation}.
|
|
32
|
+
* @typeParam Result - See {@link Operation}.
|
|
45
33
|
*/
|
|
46
|
-
export type
|
|
34
|
+
export type OperationDecoder<Context, Result> = {
|
|
47
35
|
/**
|
|
48
|
-
*
|
|
49
|
-
* {@link OperationInstance} ready for execution.
|
|
36
|
+
* Creates a ready-to-execute {@link OperationInterpreter}.
|
|
50
37
|
*
|
|
51
|
-
* @throws {@link TypoError} if
|
|
52
|
-
* {@link Operation.createFactory}.
|
|
38
|
+
* @throws {@link TypoError} if parsing or decoding failed.
|
|
53
39
|
*/
|
|
54
|
-
|
|
40
|
+
decodeAndMakeInterpreter(): OperationInterpreter<Context, Result>;
|
|
55
41
|
};
|
|
56
42
|
|
|
57
43
|
/**
|
|
58
|
-
* A fully parsed, ready-to-execute operation.
|
|
44
|
+
* A fully parsed, decoded and ready-to-execute operation.
|
|
59
45
|
*
|
|
60
|
-
* @typeParam
|
|
61
|
-
* @typeParam
|
|
46
|
+
* @typeParam Context - Caller-supplied context.
|
|
47
|
+
* @typeParam Result - Value produced on success.
|
|
62
48
|
*/
|
|
63
|
-
export type
|
|
49
|
+
export type OperationInterpreter<Context, Result> = {
|
|
64
50
|
/**
|
|
65
|
-
*
|
|
66
|
-
* option/positional values.
|
|
67
|
-
*
|
|
68
|
-
* @param input - Context from the parent command (or the root context supplied to
|
|
69
|
-
* {@link runAndExit}).
|
|
70
|
-
* @returns A promise resolving to the handler's return value.
|
|
51
|
+
* Executes with the provided context.
|
|
71
52
|
*/
|
|
72
|
-
executeWithContext(
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Collected usage metadata produced by {@link Operation.generateUsage}.
|
|
77
|
-
* Consumed by the parent command factory when building {@link CommandUsage}.
|
|
78
|
-
*/
|
|
79
|
-
export type OperationUsage = {
|
|
80
|
-
/** Usage descriptors for all options registered by this operation. */
|
|
81
|
-
options: Array<OptionUsage>;
|
|
82
|
-
/** Usage descriptors for all positionals declared by this operation, in order. */
|
|
83
|
-
positionals: Array<PositionalUsage>;
|
|
53
|
+
executeWithContext(context: Context): Promise<Result>;
|
|
84
54
|
};
|
|
85
55
|
|
|
86
56
|
/**
|
|
87
|
-
* Creates an {@link Operation} from
|
|
88
|
-
* async handler function.
|
|
57
|
+
* Creates an {@link Operation} from options, positionals, and an async handler.
|
|
89
58
|
*
|
|
90
|
-
* The `handler` receives
|
|
91
|
-
* - `context` — the value passed down from the parent command.
|
|
92
|
-
* - `inputs.options` — an object whose keys match those declared in `inputs.options` and whose values are
|
|
93
|
-
* the parsed option values.
|
|
94
|
-
* - `inputs.positionals` — a tuple whose elements match `inputs.positionals` and whose
|
|
95
|
-
* values are the parsed positional values, in declaration order.
|
|
59
|
+
* The `handler` receives `context` and `inputs` with decoded `options` and `positionals`.
|
|
96
60
|
*
|
|
97
|
-
* @typeParam Context -
|
|
98
|
-
* @typeParam Result -
|
|
99
|
-
* @typeParam Options -
|
|
100
|
-
* @typeParam Positionals - Tuple
|
|
61
|
+
* @typeParam Context - Context type accepted by the handler.
|
|
62
|
+
* @typeParam Result - Return type of the handler.
|
|
63
|
+
* @typeParam Options - Map of option keys to parsed value types.
|
|
64
|
+
* @typeParam Positionals - Tuple of parsed positional value types, in order.
|
|
101
65
|
*
|
|
102
|
-
* @param inputs -
|
|
103
|
-
* @param inputs.options -
|
|
104
|
-
*
|
|
105
|
-
* @param
|
|
106
|
-
*
|
|
107
|
-
* same order.
|
|
108
|
-
* @param handler - The async function that implements the command logic. Receives the
|
|
109
|
-
* execution context and all parsed inputs.
|
|
110
|
-
* @returns An {@link Operation} ready to be composed into a command.
|
|
66
|
+
* @param inputs - Options and positionals this operation accepts.
|
|
67
|
+
* @param inputs.options - Map of keys to {@link Option} descriptors.
|
|
68
|
+
* @param inputs.positionals - Ordered array of {@link Positional} descriptors.
|
|
69
|
+
* @param handler - Async function implementing the command logic.
|
|
70
|
+
* @returns An {@link Operation}.
|
|
111
71
|
*
|
|
112
72
|
* @example
|
|
113
73
|
* ```ts
|
|
@@ -120,7 +80,7 @@ export type OperationUsage = {
|
|
|
120
80
|
* positionalRequired({ type: typeString, label: "NAME", description: "Name to greet" }),
|
|
121
81
|
* ],
|
|
122
82
|
* },
|
|
123
|
-
* async (_ctx, { options: { loud }, positionals: [name] })
|
|
83
|
+
* async function (_ctx, { options: { loud }, positionals: [name] }) {
|
|
124
84
|
* const message = `Hello, ${name}!`;
|
|
125
85
|
* console.log(loud ? message.toUpperCase() : message);
|
|
126
86
|
* },
|
|
@@ -147,38 +107,40 @@ export function operation<
|
|
|
147
107
|
): Operation<Context, Result> {
|
|
148
108
|
return {
|
|
149
109
|
generateUsage() {
|
|
150
|
-
const optionsUsage = new Array<
|
|
110
|
+
const optionsUsage = new Array<UsageOption>();
|
|
151
111
|
for (const optionKey in inputs.options) {
|
|
152
112
|
const optionInput = inputs.options[optionKey]!;
|
|
153
|
-
|
|
154
|
-
optionsUsage.push(optionInput.generateUsage());
|
|
155
|
-
}
|
|
113
|
+
optionsUsage.push(optionInput.generateUsage());
|
|
156
114
|
}
|
|
157
|
-
const positionalsUsage = new Array<
|
|
115
|
+
const positionalsUsage = new Array<UsagePositional>();
|
|
158
116
|
for (const positionalInput of inputs.positionals) {
|
|
159
117
|
positionalsUsage.push(positionalInput.generateUsage());
|
|
160
118
|
}
|
|
161
119
|
return { options: optionsUsage, positionals: positionalsUsage };
|
|
162
120
|
},
|
|
163
|
-
|
|
164
|
-
const
|
|
121
|
+
consumeAndMakeDecoder(readerArgs: ReaderArgs) {
|
|
122
|
+
const optionsDecoders: Record<string, OptionDecoder<any>> = {};
|
|
165
123
|
for (const optionKey in inputs.options) {
|
|
166
124
|
const optionInput = inputs.options[optionKey]!;
|
|
167
|
-
|
|
125
|
+
optionsDecoders[optionKey] =
|
|
126
|
+
optionInput.registerAndMakeDecoder(readerArgs);
|
|
168
127
|
}
|
|
169
|
-
const
|
|
128
|
+
const positionalsDecoders: Array<PositionalDecoder<any>> = [];
|
|
170
129
|
for (const positionalInput of inputs.positionals) {
|
|
171
|
-
|
|
130
|
+
positionalsDecoders.push(
|
|
131
|
+
positionalInput.consumeAndMakeDecoder(readerArgs),
|
|
132
|
+
);
|
|
172
133
|
}
|
|
173
134
|
return {
|
|
174
|
-
|
|
135
|
+
decodeAndMakeInterpreter() {
|
|
175
136
|
const optionsValues: any = {};
|
|
176
|
-
for (const optionKey in
|
|
177
|
-
optionsValues[optionKey] =
|
|
137
|
+
for (const optionKey in optionsDecoders) {
|
|
138
|
+
optionsValues[optionKey] =
|
|
139
|
+
optionsDecoders[optionKey]!.getAndDecodeValue();
|
|
178
140
|
}
|
|
179
141
|
const positionalsValues: any = [];
|
|
180
|
-
for (const
|
|
181
|
-
positionalsValues.push(
|
|
142
|
+
for (const positionalDecoder of positionalsDecoders) {
|
|
143
|
+
positionalsValues.push(positionalDecoder.decodeValue());
|
|
182
144
|
}
|
|
183
145
|
return {
|
|
184
146
|
executeWithContext(context: Context) {
|