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.
@@ -1,113 +1,73 @@
1
- import { Option, OptionParser, OptionUsage } from "./Option";
2
- import { Positional, PositionalParser, PositionalUsage } from "./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
- * Describes an operation the combination of options, positional arguments, and an
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
- * An `Operation` is created with {@link operation} and passed to
10
- * {@link command}, {@link commandWithSubcommands}, or {@link commandChained} to build
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 Input - The context value the handler receives at execution time (forwarded
14
- * from the parent command's context or from a preceding chained operation).
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<Input, Output> = {
15
+ export type Operation<Context, Result> = {
20
16
  /**
21
- * Returns usage metadata (options and positionals) without consuming any arguments.
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(): OperationUsage;
19
+ generateUsage(): UsageOperation;
25
20
  /**
26
- * Parses options and positionals from `readerArgs` and returns an
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
- createFactory(readerArgs: ReaderArgs): OperationFactory<Input, Output>;
23
+ consumeAndMakeDecoder(
24
+ readerArgs: ReaderArgs,
25
+ ): OperationDecoder<Context, Result>;
37
26
  };
38
27
 
39
28
  /**
40
- * Produced by {@link Operation.createFactory} after argument parsing.
41
- * Instantiating it finalises value extraction and produces an {@link OperationInstance}.
29
+ * Produced by {@link Operation.consumeAndMakeDecoder}.
42
30
  *
43
- * @typeParam Input - operation instance input type {@link Operation}.
44
- * @typeParam Output - operation instance output type {@link Operation}.
31
+ * @typeParam Context - See {@link Operation}.
32
+ * @typeParam Result - See {@link Operation}.
45
33
  */
46
- export type OperationFactory<Input, Output> = {
34
+ export type OperationDecoder<Context, Result> = {
47
35
  /**
48
- * Extracts the final parsed values for all options and returns an
49
- * {@link OperationInstance} ready for execution.
36
+ * Creates a ready-to-execute {@link OperationInterpreter}.
50
37
  *
51
- * @throws {@link TypoError} if any option or positional validation failed during
52
- * {@link Operation.createFactory}.
38
+ * @throws {@link TypoError} if parsing or decoding failed.
53
39
  */
54
- createInstance(): OperationInstance<Input, Output>;
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 Input - The value the caller must supply as context.
61
- * @typeParam Output - The value produced on successful execution.
46
+ * @typeParam Context - Caller-supplied context.
47
+ * @typeParam Result - Value produced on success.
62
48
  */
63
- export type OperationInstance<Input, Output> = {
49
+ export type OperationInterpreter<Context, Result> = {
64
50
  /**
65
- * Runs the operation handler with the provided input context and the parsed
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(input: Input): Promise<Output>;
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 a set of options, positionals, and an
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 - The context type accepted by the handler.
98
- * @typeParam Result - The return type of the handler.
99
- * @typeParam Options - Object type mapping option keys to their parsed value types.
100
- * @typeParam Positionals - Tuple type of parsed positional value types, in order.
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 - Declares the options and positionals this operation accepts.
103
- * @param inputs.options - A map from arbitrary keys to {@link Option} descriptors.
104
- * The same keys appear in `handler`'s `inputs.options` argument.
105
- * @param inputs.positionals - An ordered array of {@link Positional} descriptors.
106
- * Their parsed values appear in `handler`'s `inputs.positionals` argument, in the
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<OptionUsage>();
110
+ const optionsUsage = new Array<UsageOption>();
151
111
  for (const optionKey in inputs.options) {
152
112
  const optionInput = inputs.options[optionKey]!;
153
- if (optionInput) {
154
- optionsUsage.push(optionInput.generateUsage());
155
- }
113
+ optionsUsage.push(optionInput.generateUsage());
156
114
  }
157
- const positionalsUsage = new Array<PositionalUsage>();
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
- createFactory(readerArgs: ReaderArgs) {
164
- const optionsGetters: Record<string, OptionParser<any>> = {};
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
- optionsGetters[optionKey] = optionInput.createParser(readerArgs);
125
+ optionsDecoders[optionKey] =
126
+ optionInput.registerAndMakeDecoder(readerArgs);
168
127
  }
169
- const positionalsParsers: Array<PositionalParser<any>> = [];
128
+ const positionalsDecoders: Array<PositionalDecoder<any>> = [];
170
129
  for (const positionalInput of inputs.positionals) {
171
- positionalsParsers.push(positionalInput.createParser(readerArgs));
130
+ positionalsDecoders.push(
131
+ positionalInput.consumeAndMakeDecoder(readerArgs),
132
+ );
172
133
  }
173
134
  return {
174
- createInstance() {
135
+ decodeAndMakeInterpreter() {
175
136
  const optionsValues: any = {};
176
- for (const optionKey in optionsGetters) {
177
- optionsValues[optionKey] = optionsGetters[optionKey]!.parseValue();
137
+ for (const optionKey in optionsDecoders) {
138
+ optionsValues[optionKey] =
139
+ optionsDecoders[optionKey]!.getAndDecodeValue();
178
140
  }
179
141
  const positionalsValues: any = [];
180
- for (const positionalParser of positionalsParsers) {
181
- positionalsValues.push(positionalParser.parseValue());
142
+ for (const positionalDecoder of positionalsDecoders) {
143
+ positionalsValues.push(positionalDecoder.decodeValue());
182
144
  }
183
145
  return {
184
146
  executeWithContext(context: Context) {