cli-kiss 0.2.8 → 0.2.9

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.
@@ -20,7 +20,7 @@ import {
20
20
  optionFlag,
21
21
  positionalRequired,
22
22
  runAndExit,
23
- type,
23
+ typeString,
24
24
  } from "cli-kiss";
25
25
 
26
26
  const greetCommand = command(
@@ -32,7 +32,7 @@ const greetCommand = command(
32
32
  },
33
33
  positionals: [
34
34
  positionalRequired({
35
- type: type("name"),
35
+ type: typeString("name"),
36
36
  description: "The name of the person to greet",
37
37
  }),
38
38
  ],
@@ -12,7 +12,7 @@ const greet = command(
12
12
  operation(
13
13
  {
14
14
  options: {},
15
- positionals: [positionalRequired({ type: type("name") })],
15
+ positionals: [positionalRequired({ type: typeString("name") })],
16
16
  },
17
17
  async function (_ctx, { positionals: [name] }) {
18
18
  console.log(`Hello, ${name}!`);
@@ -85,7 +85,7 @@ const authenticatedDeploy = commandChained(
85
85
  options: {
86
86
  token: optionSingleValue({
87
87
  long: "token",
88
- type: type("secret"),
88
+ type: typeString("secret"),
89
89
  description: "API token",
90
90
  fallbackValueIfAbsent: function () {
91
91
  const t = process.env.API_TOKEN;
@@ -9,7 +9,7 @@ Fails if missing.
9
9
 
10
10
  ```ts
11
11
  const name = positionalRequired({
12
- type: type("person"),
12
+ type: typeString("person"),
13
13
  description: "The name of the person to greet",
14
14
  });
15
15
  // Usage:
@@ -29,7 +29,7 @@ Falls back to a default when absent.
29
29
 
30
30
  ```ts
31
31
  const greeting = positionalOptional({
32
- type: type("greeting"),
32
+ type: typeString("greeting"),
33
33
  description: "Custom greeting",
34
34
  hint: "default to 'Hello'",
35
35
  default: () => "Hello",
@@ -66,7 +66,7 @@ Optionally stop collecting at a specific sentinel token:
66
66
 
67
67
  ```ts
68
68
  const args = positionalVariadics({
69
- type: type("argument"),
69
+ type: typeString("argument"),
70
70
  endDelimiter: "STOP",
71
71
  description: "Arguments (end with STOP)",
72
72
  });
@@ -90,10 +90,10 @@ operation(
90
90
  {
91
91
  options: {},
92
92
  positionals: [
93
- positionalRequired({ type: type("src") }),
94
- positionalRequired({ type: type("dst") }),
95
- positionalOptional({ type: type("tag"), default: () => "latest" }),
96
- positionalVariadics({ type: type("extra") }),
93
+ positionalRequired({ type: typeString("src") }),
94
+ positionalRequired({ type: typeString("dst") }),
95
+ positionalOptional({ type: typeString("tag"), default: () => "latest" }),
96
+ positionalVariadics({ type: typeString("extra") }),
97
97
  ],
98
98
  },
99
99
  async function (_ctx, { positionals: [src, dst, tag, extras] }) {
@@ -101,6 +101,6 @@ operation(
101
101
  },
102
102
  );
103
103
  // Usage:
104
- // my-cli in out → src="in", src="out", tag="latest", extras=[]
105
- // my-cli in out v2 a b c → src="in", src="out", tag="v2", extras=["a","b","c"]
104
+ // my-cli in out → src="in", dst="out", tag="latest", extras=[]
105
+ // my-cli in out v2 a b c → src="in", dst="out", tag="v2", extras=["a","b","c"]
106
106
  ```
@@ -14,7 +14,7 @@ shown in help/errors.
14
14
 
15
15
  | Type factory | Content type | Accepts |
16
16
  | -------------- | ------------ | ------------------------------------------------------------------- |
17
- | `type` | `string` | Any string |
17
+ | `typeString` | `string` | Any string |
18
18
  | `typeBoolean` | `boolean` | `true/yes/on/y` → true, `false/no/off/n` → false (case-insensitive) |
19
19
  | `typeNumber` | `number` | Integers, floats, scientific notation |
20
20
  | `typeInteger` | `bigint` | Integer strings only |
@@ -23,7 +23,7 @@ shown in help/errors.
23
23
  | `typePath` | `string` | Non-empty path strings; optional sync existence check |
24
24
 
25
25
  ```ts
26
- type("greeting").decoder("hello"); // → "hello"
26
+ typeString("greeting").decoder("hello"); // → "hello"
27
27
  typeBoolean("flag").decoder("yes"); // → true
28
28
  typeNumber("pi").decoder("3.14"); // → 3.14
29
29
  typeInteger("id").decoder("9007199254740993"); // → 9007199254740993n
@@ -65,7 +65,7 @@ typePoint.decoder("x,2"); // → Error: at 0: a: Unable to parse: "x"
65
65
  The default separator is `","`. Pass a second argument to change it:
66
66
 
67
67
  ```ts
68
- typeTuple([type("name"), typeNumber()], ":");
68
+ typeTuple([typeString("name"), typeNumber()], ":");
69
69
  // "foo:42" → ["foo", 42]
70
70
  ```
71
71
 
@@ -93,13 +93,15 @@ over `typeList` when users should pass multiple values as separate flags
93
93
 
94
94
  :::
95
95
 
96
- ## `typeConverted` — transform decoded value
96
+ ## `typeMapped` — transformed decoded value
97
97
 
98
98
  Chains a base type with a transformation function:
99
99
 
100
100
  ```ts
101
- const typePort = typeConverted("port", typeNumber(), (n) => {
102
- if (n < 1 || n > 65535) throw new Error("Out of range");
101
+ const typePort = typeMapped("port", typeNumber(), (n) => {
102
+ if (n < 1 || n > 65535) {
103
+ throw new Error("Out of range");
104
+ }
103
105
  return n;
104
106
  });
105
107
  typePort.decoder("8080"); // → 8080
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cli-kiss",
3
- "version": "0.2.8",
3
+ "version": "0.2.9",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "devDependencies": {
@@ -17,7 +17,7 @@ import { UsageCommand } from "./Usage";
17
17
  * @typeParam Context - Injected at execution; forwarded to handlers.
18
18
  * @typeParam Result - Produced on execution; typically `void`.
19
19
  */
20
- export type Command<Context, Result> = {
20
+ export type Command<Context, Result = void> = {
21
21
  /**
22
22
  * Returns static metadata.
23
23
  */
@@ -197,11 +197,15 @@ export function commandWithSubcommands<Context, Payload, Result = void>(
197
197
  operation: Operation<Context, Payload>,
198
198
  subcommands: { [subcommand: string]: Command<Payload, Result> },
199
199
  ): Command<Context, Result> {
200
- // TODO - forbid subcommands that start with a "-" ?
201
200
  const subcommandNames = Object.keys(subcommands);
202
201
  if (subcommandNames.length === 0) {
203
202
  throw new Error("At least one subcommand is required");
204
203
  }
204
+ for (const name of subcommandNames) {
205
+ if (name.startsWith("-")) {
206
+ throw new Error(`Subcommand name "${name}" cannot start with "-".`);
207
+ }
208
+ }
205
209
  return {
206
210
  getInformation() {
207
211
  return information;
@@ -212,8 +216,9 @@ export function commandWithSubcommands<Context, Payload, Result = void>(
212
216
  const subcommandName = readerArgs.consumePositional();
213
217
  if (subcommandName === undefined) {
214
218
  const errorText = new TypoText();
219
+ errorText.push(new TypoString(`Missing argument: `));
215
220
  errorText.push(new TypoString(`<subcommand>`, typoStyleUserInput));
216
- errorText.push(new TypoString(`: Missing argument.`));
221
+ errorText.push(new TypoString(`.`));
217
222
  suggestSubcommandNames(errorText, "", subcommandNames);
218
223
  throw new TypoError(errorText);
219
224
  }
@@ -12,7 +12,7 @@ import { UsageOption, UsagePositional } from "./Usage";
12
12
  * @typeParam Context - Injected at execution; forwarded to handlers.
13
13
  * @typeParam Result - Value produced on execution; typically `void`.
14
14
  */
15
- export type Operation<Context, Result> = {
15
+ export type Operation<Context, Result = void> = {
16
16
  /**
17
17
  * Returns usage metadata without consuming any arguments.
18
18
  */
@@ -132,7 +132,9 @@ export function operation<
132
132
  return { options: optionsUsage, positionals: positionalsUsage };
133
133
  },
134
134
  consumeAndMakeDecoder(readerArgs: ReaderArgs) {
135
- const optionsDecoders: Record<string, OptionDecoder<any>> = {};
135
+ const optionsDecoders = {} as {
136
+ [K in keyof Options]: OptionDecoder<Options[K]>;
137
+ };
136
138
  if (inputs.options !== undefined) {
137
139
  for (const optionKey in inputs.options) {
138
140
  const optionInput = inputs.options[optionKey]!;
@@ -140,7 +142,9 @@ export function operation<
140
142
  optionInput.registerAndMakeDecoder(readerArgs);
141
143
  }
142
144
  }
143
- const positionalsDecoders: Array<PositionalDecoder<any>> = [];
145
+ const positionalsDecoders = [] as {
146
+ [K in keyof Positionals]: PositionalDecoder<Positionals[K]>;
147
+ };
144
148
  if (inputs.positionals !== undefined) {
145
149
  for (const positionalInput of inputs.positionals) {
146
150
  positionalsDecoders.push(
@@ -150,12 +154,16 @@ export function operation<
150
154
  }
151
155
  return {
152
156
  decodeAndMakeInterpreter() {
153
- const optionsValues: any = {};
157
+ const optionsValues = {} as {
158
+ [K in keyof Options]: Options[K];
159
+ };
154
160
  for (const optionKey in optionsDecoders) {
155
161
  optionsValues[optionKey] =
156
162
  optionsDecoders[optionKey]!.getAndDecodeValue();
157
163
  }
158
- const positionalsValues: any = [];
164
+ const positionalsValues = [] as {
165
+ [K in keyof Positionals]: Positionals[K];
166
+ };
159
167
  for (const positionalDecoder of positionalsDecoders) {
160
168
  positionalsValues.push(positionalDecoder.decodeValue());
161
169
  }
package/src/lib/Option.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  import {
2
2
  ReaderOptionGetter,
3
3
  ReaderOptionNextGuard,
4
- ReaderOptionRestGuard,
5
4
  ReaderOptions,
6
5
  ReaderOptionValue,
7
6
  } from "./Reader";
@@ -94,8 +93,8 @@ export function optionFlag(definition: {
94
93
  shortKey: short,
95
94
  aliasLongKeys: aliases?.longs,
96
95
  aliasShortKeys: aliases?.shorts,
97
- restGuard: () => false,
98
96
  nextGuard: () => false,
97
+ consumeGroupRestAsValue: false,
99
98
  });
100
99
  return {
101
100
  getAndDecodeValue() {
@@ -180,12 +179,6 @@ export function optionSingleValue<Value>(definition: {
180
179
  shortKey: short,
181
180
  aliasLongKeys: aliases?.longs,
182
181
  aliasShortKeys: aliases?.shorts,
183
- restGuard: () => {
184
- if (definition.impliedValueIfNotInlined !== undefined) {
185
- return false;
186
- }
187
- return true;
188
- },
189
182
  nextGuard: (value) => {
190
183
  if (definition.impliedValueIfNotInlined !== undefined) {
191
184
  return false;
@@ -198,6 +191,8 @@ export function optionSingleValue<Value>(definition: {
198
191
  }
199
192
  return true;
200
193
  },
194
+ consumeGroupRestAsValue:
195
+ definition.impliedValueIfNotInlined === undefined,
201
196
  });
202
197
  return {
203
198
  getAndDecodeValue() {
@@ -292,7 +287,6 @@ export function optionRepeatable<Value>(definition: {
292
287
  shortKey: short,
293
288
  aliasLongKeys: aliases?.longs,
294
289
  aliasShortKeys: aliases?.shorts,
295
- restGuard: () => true,
296
290
  nextGuard: (value) => {
297
291
  if (value.inlined !== null) {
298
292
  return false;
@@ -302,6 +296,7 @@ export function optionRepeatable<Value>(definition: {
302
296
  }
303
297
  return true;
304
298
  },
299
+ consumeGroupRestAsValue: true,
305
300
  });
306
301
  return {
307
302
  getAndDecodeValue() {
@@ -353,8 +348,8 @@ function setupOptionAliased(
353
348
  shortKey: string | undefined;
354
349
  aliasLongKeys: Array<string> | undefined;
355
350
  aliasShortKeys: Array<string> | undefined;
356
- restGuard: ReaderOptionRestGuard;
357
351
  nextGuard: ReaderOptionNextGuard;
352
+ consumeGroupRestAsValue: boolean;
358
353
  },
359
354
  ): () => Array<{ identifier: string; value: ReaderOptionValue }> {
360
355
  const { longKey, shortKey, aliasLongKeys, aliasShortKeys } = params;
@@ -369,8 +364,8 @@ function setupOptionAliased(
369
364
  return setupOptionMany(readerOptions, {
370
365
  longKeys,
371
366
  shortKeys,
372
- restGuard: params.restGuard,
373
367
  nextGuard: params.nextGuard,
368
+ consumeGroupRestAsValue: params.consumeGroupRestAsValue,
374
369
  });
375
370
  }
376
371
 
@@ -379,25 +374,32 @@ function setupOptionMany(
379
374
  params: {
380
375
  longKeys: Array<string>;
381
376
  shortKeys: Array<string>;
382
- restGuard: ReaderOptionRestGuard;
383
377
  nextGuard: ReaderOptionNextGuard;
378
+ consumeGroupRestAsValue: boolean;
384
379
  },
385
380
  ): () => Array<{ identifier: string; value: ReaderOptionValue }> {
386
- const { longKeys, shortKeys, restGuard, nextGuard } = params;
387
- const getters = new Array<ReaderOptionGetter>();
381
+ const { longKeys, shortKeys, nextGuard, consumeGroupRestAsValue } = params;
382
+ const getters = new Map<string, ReaderOptionGetter>();
388
383
  for (const key of longKeys) {
389
- getters.push(readerOptions.registerOptionLong({ key, nextGuard }));
384
+ getters.set(
385
+ `--${key}`,
386
+ readerOptions.registerOptionLong({ key, nextGuard }),
387
+ );
390
388
  }
391
389
  for (const key of shortKeys) {
392
- getters.push(
393
- readerOptions.registerOptionShort({ key, restGuard, nextGuard }),
390
+ getters.set(
391
+ `-${key}`,
392
+ readerOptions.registerOptionShort({
393
+ key,
394
+ nextGuard,
395
+ consumeGroupRestAsValue,
396
+ }),
394
397
  );
395
398
  }
396
399
  return () => {
397
400
  const results = new Array();
398
- for (const getter of getters) {
399
- const { identifier, values } = getter();
400
- for (const value of values) {
401
+ for (const [identifier, getter] of getters.entries()) {
402
+ for (const value of getter()) {
401
403
  results.push({ identifier, value });
402
404
  }
403
405
  }
@@ -81,12 +81,17 @@ export function positionalRequired<Value>(definition: {
81
81
  consumeAndMakeDecoder(readerPositionals: ReaderPositionals) {
82
82
  const positional = readerPositionals.consumePositional();
83
83
  if (positional === undefined) {
84
- const errorText = makeErrorText(label);
85
- errorText.push(new TypoString(`: Is required, but was not provided.`));
84
+ const errorText = makeErrorTextLabel("Missing argument", label);
86
85
  if (description !== undefined) {
87
- // TODO - should there be a dedicated hint here ?
88
86
  errorText.push(
89
- new TypoString(` (${description})`, typoStyleRegularWeaker),
87
+ new TypoString(`: `),
88
+ new TypoString(`${description}`),
89
+ );
90
+ }
91
+ if (hint !== undefined) {
92
+ errorText.push(
93
+ new TypoString(` `),
94
+ new TypoString(`(${hint})`, typoStyleRegularWeaker),
90
95
  );
91
96
  }
92
97
  throw new TypoError(errorText);
@@ -146,7 +151,12 @@ export function positionalOptional<Value>(definition: {
146
151
  try {
147
152
  return definition.default();
148
153
  } catch (error) {
149
- throwsWhenFailedToGetDefault(label);
154
+ const errorText = makeErrorTextLabel(
155
+ "Failed to get default value",
156
+ label,
157
+ );
158
+ errorText.push(new TypoString("."));
159
+ throw new TypoError(errorText, error);
150
160
  }
151
161
  }
152
162
  return decodeValue(label, definition.type, positional);
@@ -231,14 +241,9 @@ function decodeValue<Value>(
231
241
  );
232
242
  }
233
243
 
234
- function makeErrorText(label: string): TypoText {
244
+ function makeErrorTextLabel(message: string, label: string): TypoText {
235
245
  const errorText = new TypoText();
246
+ errorText.push(new TypoString(`${message}: `));
236
247
  errorText.push(new TypoString(label, typoStyleUserInput));
237
248
  return errorText;
238
249
  }
239
-
240
- function throwsWhenFailedToGetDefault(label: string): never {
241
- const errorText = makeErrorText(label);
242
- errorText.push(new TypoString(`: Failed to get default value.`));
243
- throw new TypoError(errorText);
244
- }
package/src/lib/Reader.ts CHANGED
@@ -8,6 +8,7 @@ import {
8
8
  } from "./Typo";
9
9
 
10
10
  /**
11
+ * Represents the parsing specification for a long option
11
12
  */
12
13
  export type ReaderOptionLongSpec = {
13
14
  key: string;
@@ -15,18 +16,14 @@ export type ReaderOptionLongSpec = {
15
16
  };
16
17
 
17
18
  /**
19
+ * Represents the parsing specification for a short option
18
20
  */
19
- export type ReaderOptionShortSpec = {
20
- key: string;
21
- restGuard: ReaderOptionRestGuard;
22
- nextGuard: ReaderOptionNextGuard;
21
+ export type ReaderOptionShortSpec = ReaderOptionLongSpec & {
22
+ consumeGroupRestAsValue: boolean;
23
23
  };
24
24
 
25
25
  /**
26
- */
27
- export type ReaderOptionRestGuard = (rest: string) => boolean;
28
-
29
- /**
26
+ * Determines whether the next token is valid for a given option.
30
27
  */
31
28
  export type ReaderOptionNextGuard = (
32
29
  value: ReaderOptionValue,
@@ -34,13 +31,7 @@ export type ReaderOptionNextGuard = (
34
31
  ) => boolean;
35
32
 
36
33
  /**
37
- */
38
- export type ReaderOptionResult = {
39
- identifier: string;
40
- values: ReadonlyArray<ReaderOptionValue>;
41
- };
42
-
43
- /**
34
+ * Represents the values parsed for an option.
44
35
  */
45
36
  export type ReaderOptionValue = {
46
37
  inlined: string | null;
@@ -48,8 +39,9 @@ export type ReaderOptionValue = {
48
39
  };
49
40
 
50
41
  /**
42
+ * Returns the parsed values for a registered option.
51
43
  */
52
- export type ReaderOptionGetter = () => ReaderOptionResult;
44
+ export type ReaderOptionGetter = () => ReadonlyArray<ReaderOptionValue>;
53
45
 
54
46
  /**
55
47
  * Option registration/query interface. Subset of {@link ReaderArgs}.
@@ -100,9 +92,10 @@ export class ReaderArgs {
100
92
  }
101
93
 
102
94
  /**
95
+ * Registers a long option and returns a getter for its parsed values.
103
96
  */
104
97
  registerOptionLong(longSpec: ReaderOptionLongSpec): ReaderOptionGetter {
105
- const identifier = `--${longSpec.key}`;
98
+ const identifier = `--${longSpec.key}`; // TODO - is this necessary ?
106
99
  if (!isValidOptionKey(longSpec.key)) {
107
100
  throw new Error(`Option identifier is invalid: ${identifier}.`);
108
101
  }
@@ -115,10 +108,11 @@ export class ReaderArgs {
115
108
  spec: longSpec,
116
109
  values,
117
110
  });
118
- return () => ({ identifier, values });
111
+ return () => values;
119
112
  }
120
113
 
121
114
  /**
115
+ * Registers a short option and returns a getter for its parsed values.
122
116
  */
123
117
  registerOptionShort(shortSpec: ReaderOptionShortSpec): ReaderOptionGetter {
124
118
  const identifier = `-${shortSpec.key}`;
@@ -149,7 +143,7 @@ export class ReaderArgs {
149
143
  spec: shortSpec,
150
144
  values,
151
145
  });
152
- return () => ({ identifier, values });
146
+ return () => values;
153
147
  }
154
148
 
155
149
  /**
@@ -243,7 +237,7 @@ export class ReaderArgs {
243
237
  this.#consumeOptionValues(shortContext, null);
244
238
  return true;
245
239
  }
246
- if (shortContext.spec.restGuard(tokenRest)) {
240
+ if (shortContext.spec.consumeGroupRestAsValue) {
247
241
  this.#consumeOptionValues(shortContext, tokenRest);
248
242
  return true;
249
243
  }
package/src/lib/Run.ts CHANGED
@@ -116,9 +116,9 @@ export async function runAndExit<Context>(
116
116
  });
117
117
  }
118
118
  // TODO - the lifecycle of this function should be improved
119
- // TODO - how to pass the color information to the command logic ?
119
+ // TODO - how to pass the color TypoSupport to the command logic ?
120
+ // TODO - handle completions generators ?
120
121
  /*
121
- // TODO - handle completions ?
122
122
  readerArgs.registerFlag({
123
123
  key: "completion",
124
124
  shorts: [],