| 1 |
--- |
| 2 |
name: cleye |
| 3 |
description: cleye CLI development tool — argv parsing with typed parameters, flags, commands, and auto-generated help. Use when building Node.js CLI scripts with cleye or when the user imports `cli` or `command` from `cleye`. |
| 4 |
--- |
| 5 |
|
| 6 |
# cleye |
| 7 |
|
| 8 |
## Quick Patterns |
| 9 |
|
| 10 |
| Task | Pattern | |
| 11 |
|------|---------| |
| 12 |
| Basic CLI | `cli({ name, parameters, flags })` | |
| 13 |
| Named command | `command({ name, parameters, flags }, callback)` | |
| 14 |
| Register commands | `cli({ commands: [cmd1, cmd2] })` | |
| 15 |
| Async callback | `await cli({ ... }, async (argv) => { ... })` | |
| 16 |
| Raw argv | `cli({ ... }, callback, process.argv.slice(2))` | |
| 17 |
|
| 18 |
## Parameters |
| 19 |
|
| 20 |
Positional arguments mapped to named properties on `argv._`. |
| 21 |
|
| 22 |
| Format | Description | |
| 23 |
|--------|-------------| |
| 24 |
| `<name>` | Required | |
| 25 |
| `[name]` | Optional | |
| 26 |
| `<name...>` | Required spread (1+) | |
| 27 |
| `[name...]` | Optional spread (0+) | |
| 28 |
| `--` | End-of-flags separator | |
| 29 |
|
| 30 |
```ts |
| 31 |
const argv = cli({ |
| 32 |
parameters: ['<required>', '[optional]', '[rest...]'] |
| 33 |
}) |
| 34 |
argv._.required // string |
| 35 |
argv._.optional // string | undefined |
| 36 |
argv._.rest // string[] |
| 37 |
argv._['--'] // string[] — everything after -- |
| 38 |
``` |
| 39 |
|
| 40 |
Multi-word names use camelCase on `argv._`: `'<first name>'` → `argv._.firstName`. |
| 41 |
|
| 42 |
## Flags |
| 43 |
|
| 44 |
```ts |
| 45 |
const argv = cli({ |
| 46 |
flags: { |
| 47 |
verbose: Boolean, // shorthand |
| 48 |
output: { |
| 49 |
type: String, |
| 50 |
alias: 'o', |
| 51 |
default: 'dist', |
| 52 |
description: 'Output directory', |
| 53 |
placeholder: '<path>' |
| 54 |
}, |
| 55 |
count: { |
| 56 |
type: [Number], // array: -n 1 -n 2 |
| 57 |
alias: 'n' |
| 58 |
} |
| 59 |
} |
| 60 |
}) |
| 61 |
|
| 62 |
argv.flags.verbose // boolean | undefined |
| 63 |
argv.flags.output // string |
| 64 |
argv.flags.count // number[] |
| 65 |
``` |
| 66 |
|
| 67 |
Flag name is camelCase; parsed from kebab-case CLI input (`--output-dir` → `outputDir`). |
| 68 |
|
| 69 |
**Delimiters** — `=`, `:`, and `.` all work as value separators: |
| 70 |
|
| 71 |
```sh |
| 72 |
--flag=value --flag:value --flag.value |
| 73 |
``` |
| 74 |
|
| 75 |
Use `:` when the value itself contains `=` (e.g. `--define:KEY=VALUE`). |
| 76 |
|
| 77 |
**Counting flags** — use `[Boolean]` and check `.length`: |
| 78 |
|
| 79 |
```ts |
| 80 |
cli({ |
| 81 |
flags: { |
| 82 |
verbose: { |
| 83 |
type: [Boolean], |
| 84 |
alias: 'v' |
| 85 |
} |
| 86 |
} |
| 87 |
}) |
| 88 |
// -vvv → argv.flags.verbose.length === 3 |
| 89 |
``` |
| 90 |
|
| 91 |
**Optional value** — custom type returns `true` when no value is given: |
| 92 |
|
| 93 |
```ts |
| 94 |
const OptionalString = (value: string) => value || true |
| 95 |
cli({ flags: { tag: OptionalString } }) |
| 96 |
// --tag → true |
| 97 |
// --tag beta → 'beta' |
| 98 |
``` |
| 99 |
|
| 100 |
## Boolean Flag Negation |
| 101 |
|
| 102 |
By default, set a boolean flag to `false` using the `=` operator: |
| 103 |
|
| 104 |
```sh |
| 105 |
--verbose=false # → false |
| 106 |
--verbose false # → true (false treated as separate argument) |
| 107 |
``` |
| 108 |
|
| 109 |
To also support the `--no-<flag>` convention, enable `booleanFlagNegation` (additive — `=false` still works): |
| 110 |
|
| 111 |
```ts |
| 112 |
cli({ |
| 113 |
flags: { verbose: Boolean }, |
| 114 |
booleanFlagNegation: true |
| 115 |
}) |
| 116 |
// --no-verbose → false |
| 117 |
// --verbose=false → false (still works) |
| 118 |
// Last-wins: --verbose --no-verbose → false |
| 119 |
``` |
| 120 |
|
| 121 |
Commands inherit `booleanFlagNegation` from the parent `cli()` but can override it. |
| 122 |
|
| 123 |
## Strict Flags |
| 124 |
|
| 125 |
```ts |
| 126 |
cli({ |
| 127 |
flags: { foo: Boolean }, |
| 128 |
strictFlags: true |
| 129 |
}) |
| 130 |
// --baz → Error: Unknown flag: --baz. (Did you mean --foo?) |
| 131 |
``` |
| 132 |
|
| 133 |
Commands inherit `strictFlags` but can override it. |
| 134 |
|
| 135 |
## Commands |
| 136 |
|
| 137 |
```ts |
| 138 |
// install-command.ts |
| 139 |
export const installCommand = command({ |
| 140 |
name: 'install', |
| 141 |
alias: ['i', 'add'], |
| 142 |
parameters: ['<package name>'], |
| 143 |
flags: { saveDev: Boolean } |
| 144 |
}, (argv) => { |
| 145 |
argv._.packageName // string |
| 146 |
argv.flags.saveDev // boolean | undefined |
| 147 |
}) |
| 148 |
|
| 149 |
// main.ts |
| 150 |
cli({ |
| 151 |
name: 'npm', |
| 152 |
version: '1.0.0', |
| 153 |
commands: [installCommand] |
| 154 |
}) |
| 155 |
``` |
| 156 |
|
| 157 |
When a command is matched, `argv.command` narrows the type for type-safe branching. |
| 158 |
|
| 159 |
## Help & Version |
| 160 |
|
| 161 |
```ts |
| 162 |
cli({ |
| 163 |
name: 'my-script', |
| 164 |
version: '1.2.3', // enables --version; also shown in --help |
| 165 |
help: { |
| 166 |
description: 'Does things', |
| 167 |
usage: 'my-script [flags] <file>', |
| 168 |
examples: ['my-script foo.txt', 'my-script --output=dist foo.txt'], |
| 169 |
version: '1.2.3' // show version in --help without handling --version |
| 170 |
} |
| 171 |
}) |
| 172 |
// help: false — disables --help handling; call argv.showHelp() manually |
| 173 |
``` |
| 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 |
|
| 187 |
## Custom Flag Types |
| 188 |
|
| 189 |
Any function `(value: string) => T` works as a flag type — the return type is inferred. |
| 190 |
|
| 191 |
```ts |
| 192 |
// Validation + narrowing |
| 193 |
const Port = (value: string) => { |
| 194 |
const n = Number(value) |
| 195 |
if (!Number.isInteger(n) || n < 1 || n > 65_535) { throw new Error(`Invalid port: ${value}`) } |
| 196 |
return n |
| 197 |
} |
| 198 |
// argv.flags.port → number |
| 199 |
|
| 200 |
// Enum |
| 201 |
const sizes = ['small', 'medium', 'large'] as const |
| 202 |
const Size = (v: string) => { |
| 203 |
if (!(sizes as readonly string[]).includes(v)) { throw new Error(`Expected: ${sizes.join(', ')}`) } |
| 204 |
return v as typeof sizes[number] |
| 205 |
} |
| 206 |
// argv.flags.size → 'small' | 'medium' | 'large' |
| 207 |
|
| 208 |
// Comma-separated list |
| 209 |
const List = (v: string) => v.split(',') |
| 210 |
// --tags a,b,c → argv.flags.tags === ['a', 'b', 'c'] |
| 211 |
|
| 212 |
// Inline JSON |
| 213 |
cli({ flags: { data: JSON.parse } }) |
| 214 |
// --data '{"key":1}' → argv.flags.data === { key: 1 } |
| 215 |
|
| 216 |
// Dot-nested object (combine with . delimiter) |
| 217 |
const Env = (v: string): Record<string, string | true> => { |
| 218 |
const [k, value] = v.split('=') |
| 219 |
return { [k]: value ?? true } |
| 220 |
} |
| 221 |
cli({ flags: { env: [Env] } }) |
| 222 |
// --env.TOKEN=abc --env.CI → merge to { TOKEN: 'abc', CI: true } |
| 223 |
``` |
| 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 |
|
| 242 |
## Help Customization |
| 243 |
|
| 244 |
```ts |
| 245 |
cli({ |
| 246 |
help: { |
| 247 |
render(nodes, renderers) { |
| 248 |
nodes.push('\nDocs: https://example.com') |
| 249 |
renderers.flagOperator = () => '=' // --flag=<value> |
| 250 |
return renderers.render(nodes) |
| 251 |
} |
| 252 |
} |
| 253 |
}) |
| 254 |
``` |
| 255 |
|
| 256 |
Default renderers: `/src/render-help/renderers.ts`. |
| 257 |
|
| 258 |
## ignoreArgv |
| 259 |
|
| 260 |
```ts |
| 261 |
cli({ |
| 262 |
ignoreArgv(type, flagOrArgv, value) { |
| 263 |
if (type === 'unknown-flag') { return true } // silently skip unknown flags |
| 264 |
} |
| 265 |
}) |
| 266 |
``` |
| 267 |
|
| 268 |
`type`: `'known-flag' | 'unknown-flag' | 'argument'` |
| 269 |
|
| 270 |
## Return Type |
| 271 |
|
| 272 |
```ts |
| 273 |
type ParsedArgv = { |
| 274 |
_: string[] & Parameters |
| 275 |
flags: { [name: string]: InferredType } |
| 276 |
unknownFlags: { [name: string]: (string | boolean)[] } |
| 277 |
showVersion: () => void |
| 278 |
showHelp: (options?: HelpOptions) => void |
| 279 |
} |
| 280 |
``` |
| 281 |
|
| 282 |
## TypeScript Exports |
| 283 |
|
| 284 |
```ts |
| 285 |
import type { Flags, Renderers, TypeFlag } from 'cleye' |
| 286 |
``` |
| 287 |
|
| 288 |
| Type | Use | |
| 289 |
|------|-----| |
| 290 |
| `Flags` | Type for the `flags` option object | |
| 291 |
| `Renderers` | Help renderer class type for `help.render` customization | |
| 292 |
| `TypeFlag` | Re-export from `type-flag` for portable type declarations | |