cleye 2.5.0 → 2.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -342,6 +342,36 @@ const argv = cli({
342
342
  argv.flags.size // => "large" ("small" | "medium" | "large")
343
343
  ```
344
344
 
345
+ #### Composable type helpers
346
+
347
+ `cleye/formats` is a tree-shakable subpath that ships ready-made type-function helpers for common flag shapes. Import only what you need.
348
+
349
+ ```ts
350
+ import {
351
+ oneOf, commaList, integer, float, range, url
352
+ } from 'cleye/formats'
353
+
354
+ cli({
355
+ flags: {
356
+ format: { type: oneOf('json', 'yaml', 'csv') }, // => 'json' | 'yaml' | 'csv'
357
+ tags: { type: commaList(String) }, // => string[]
358
+ port: { type: range(1024, 65_535) }, // => number, validated in range
359
+ count: { type: integer() }, // => number (integer only)
360
+ ratio: { type: float() }, // => number (finite float)
361
+ apiUrl: { type: url() } // => URL object
362
+ }
363
+ })
364
+ ```
365
+
366
+ | Helper | Return type | Description |
367
+ |--------|-------------|-------------|
368
+ | `oneOf(...values)` | Union of the given string literals | Throws if the value is not in the list. |
369
+ | `commaList(itemType)` | `T[]` | Splits on `,`, trims whitespace, maps each item through `itemType`. |
370
+ | `integer()` | `number` | Parses a base-10 integer. Throws on floats or non-numeric input. |
371
+ | `float()` | `number` | Parses a finite float. Throws on non-finite or non-numeric input. |
372
+ | `range(min, max)` | `(input: string) => number` | Returns a parser that validates the input parses to a number in `[min, max]`. |
373
+ | `url()` | `URL` | Parses with `new URL()`. Returns a `URL` object so callers get `.host`, `.pathname`, etc. |
374
+
345
375
  #### Default flags
346
376
  By default, _Cleye_ will try to handle the `--help, -h` and `--version` flags.
347
377
 
@@ -366,6 +396,18 @@ $ my-script --version
366
396
 
367
397
  The version is also shown in the help documentation. To opt out of handling `--version` while still showing the version in `--help`, pass the version into `help.version`.
368
398
 
399
+ > [!TIP]
400
+ > Import `name`, `version`, and `description` directly from `package.json` to avoid keeping them in sync manually:
401
+ > ```ts
402
+ > import { name, version, description } from './package.json' with { type: 'json' }
403
+ >
404
+ > cli({
405
+ > name,
406
+ > version,
407
+ > help: { description }
408
+ > })
409
+ > ```
410
+
369
411
  #### Strict flags
370
412
  To reject unknown flags with an error, enable `strictFlags`:
371
413
 
@@ -0,0 +1 @@
1
+ "use strict";var i=Object.defineProperty;var n=(r,e)=>i(r,"name",{value:e,configurable:!0});const s=n((...r)=>e=>{if(!r.includes(e))throw new Error(`Expected one of: ${r.join(", ")} (got: ${JSON.stringify(e)})`);return e},"oneOf"),c=n(r=>e=>e===""?[]:e.split(",").flatMap(o=>{const t=o.trim();return t===""?[]:[r(t)]}),"commaList"),f=n(()=>r=>{const e=Number(r);if(!Number.isInteger(e))throw new TypeError(`Expected an integer (got: ${JSON.stringify(r)})`);return e},"integer"),a=n(()=>r=>{const e=Number(r);if(!Number.isFinite(e))throw new TypeError(`Expected a finite number (got: ${JSON.stringify(r)})`);return e},"float"),g=n((r,e)=>o=>{const t=Number(o);if(!Number.isFinite(t))throw new TypeError(`Expected a number (got: ${JSON.stringify(o)})`);if(t<r||t>e)throw new Error(`Expected a number between ${r} and ${e} (got: ${t})`);return t},"range"),u=n(()=>r=>{try{return new URL(r)}catch{throw new Error(`Expected a valid URL (got: ${JSON.stringify(r)})`)}},"url");exports.commaList=c,exports.float=a,exports.integer=f,exports.oneOf=s,exports.range=g,exports.url=u;
@@ -0,0 +1,8 @@
1
+ declare const oneOf: <T extends string>(...values: T[]) => (input: string) => T;
2
+ declare const commaList: <T>(itemType: (value: string) => T) => (input: string) => T[];
3
+ declare const integer: () => (input: string) => number;
4
+ declare const float: () => (input: string) => number;
5
+ declare const range: (min: number, max: number) => (input: string) => number;
6
+ declare const url: () => (input: string) => URL;
7
+
8
+ export { commaList, float, integer, oneOf, range, url };
@@ -0,0 +1,8 @@
1
+ declare const oneOf: <T extends string>(...values: T[]) => (input: string) => T;
2
+ declare const commaList: <T>(itemType: (value: string) => T) => (input: string) => T[];
3
+ declare const integer: () => (input: string) => number;
4
+ declare const float: () => (input: string) => number;
5
+ declare const range: (min: number, max: number) => (input: string) => number;
6
+ declare const url: () => (input: string) => URL;
7
+
8
+ export { commaList, float, integer, oneOf, range, url };
@@ -0,0 +1 @@
1
+ var i=Object.defineProperty;var n=(r,e)=>i(r,"name",{value:e,configurable:!0});const c=n((...r)=>e=>{if(!r.includes(e))throw new Error(`Expected one of: ${r.join(", ")} (got: ${JSON.stringify(e)})`);return e},"oneOf"),s=n(r=>e=>e===""?[]:e.split(",").flatMap(o=>{const t=o.trim();return t===""?[]:[r(t)]}),"commaList"),f=n(()=>r=>{const e=Number(r);if(!Number.isInteger(e))throw new TypeError(`Expected an integer (got: ${JSON.stringify(r)})`);return e},"integer"),g=n(()=>r=>{const e=Number(r);if(!Number.isFinite(e))throw new TypeError(`Expected a finite number (got: ${JSON.stringify(r)})`);return e},"float"),a=n((r,e)=>o=>{const t=Number(o);if(!Number.isFinite(t))throw new TypeError(`Expected a number (got: ${JSON.stringify(o)})`);if(t<r||t>e)throw new Error(`Expected a number between ${r} and ${e} (got: ${t})`);return t},"range"),u=n(()=>r=>{try{return new URL(r)}catch{throw new Error(`Expected a valid URL (got: ${JSON.stringify(r)})`)}},"url");export{s as commaList,g as float,f as integer,c as oneOf,a as range,u as url};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cleye",
3
- "version": "2.5.0",
3
+ "version": "2.6.0",
4
4
  "description": "The intuitive CLI development tool",
5
5
  "keywords": [
6
6
  "cli",
@@ -23,17 +23,30 @@
23
23
  "skills"
24
24
  ],
25
25
  "type": "module",
26
+ "sideEffects": false,
26
27
  "main": "./dist/index.cjs",
27
28
  "module": "./dist/index.mjs",
28
29
  "types": "./dist/index.d.cts",
29
30
  "exports": {
30
- "require": {
31
- "types": "./dist/index.d.cts",
32
- "default": "./dist/index.cjs"
31
+ ".": {
32
+ "require": {
33
+ "types": "./dist/index.d.cts",
34
+ "default": "./dist/index.cjs"
35
+ },
36
+ "import": {
37
+ "types": "./dist/index.d.mts",
38
+ "default": "./dist/index.mjs"
39
+ }
33
40
  },
34
- "import": {
35
- "types": "./dist/index.d.mts",
36
- "default": "./dist/index.mjs"
41
+ "./formats": {
42
+ "require": {
43
+ "types": "./dist/formats.d.cts",
44
+ "default": "./dist/formats.cjs"
45
+ },
46
+ "import": {
47
+ "types": "./dist/formats.d.mts",
48
+ "default": "./dist/formats.mjs"
49
+ }
37
50
  }
38
51
  },
39
52
  "imports": {
@@ -41,6 +54,11 @@
41
54
  "types": "./src/index.ts",
42
55
  "development": "./src/index.ts",
43
56
  "default": "./dist/index.mjs"
57
+ },
58
+ "#cleye/formats": {
59
+ "types": "./src/formats.ts",
60
+ "development": "./src/formats.ts",
61
+ "default": "./dist/formats.mjs"
44
62
  }
45
63
  },
46
64
  "dependencies": {
@@ -172,6 +172,18 @@ cli({
172
172
  // help: false — disables --help handling; call argv.showHelp() manually
173
173
  ```
174
174
 
175
+ Tip: import `name`, `version`, and `description` from `package.json` to avoid keeping them in sync manually:
176
+
177
+ ```ts
178
+ import { name, version, description } from './package.json' with { type: 'json' }
179
+
180
+ cli({
181
+ name,
182
+ version,
183
+ help: { description }
184
+ })
185
+ ```
186
+
175
187
  ## Custom Flag Types
176
188
 
177
189
  Any function `(value: string) => T` works as a flag type — the return type is inferred.
@@ -210,6 +222,23 @@ cli({ flags: { env: [Env] } })
210
222
  // --env.TOKEN=abc --env.CI → merge to { TOKEN: 'abc', CI: true }
211
223
  ```
212
224
 
225
+ ## Type Helpers (`cleye/formats`)
226
+
227
+ `cleye/formats` ships composable, tree-shakable type-function helpers. Import only what you need.
228
+
229
+ ```ts
230
+ import {
231
+ oneOf, commaList, integer, float, range, url
232
+ } from 'cleye/formats'
233
+ ```
234
+
235
+ - `oneOf('a', 'b', 'c')` — infers `'a' | 'b' | 'c'`; throws if the value is not in the list.
236
+ - `commaList(itemType)` — splits `"a,b,c"` → `T[]`; trims whitespace; composes with other helpers (e.g. `commaList(integer())`).
237
+ - `integer()` — base-10 integer; throws on floats/non-numeric.
238
+ - `float()` — finite float; throws on `Infinity`/non-numeric.
239
+ - `range(min, max)` — returns `(input: string) => number`; validates input parses to a number in `[min, max]`.
240
+ - `url()` — parses with `new URL()`; returns a `URL` object (not a string).
241
+
213
242
  ## Help Customization
214
243
 
215
244
  ```ts