argsbarg 3.5.0 → 3.6.1
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/CHANGELOG.md +25 -1
- package/README.md +24 -8
- package/docs/cli-program.md +75 -1
- package/docs/templates/cursor/rules/cli-program.mdc +15 -21
- package/index.d.ts +45 -0
- package/package.json +10 -1
- package/src/context.ts +91 -15
- package/src/docs/api-guide.ts +17 -3
- package/src/docs/mcp-guide.ts +3 -1
- package/src/formats.test.ts +35 -0
- package/src/formats.ts +135 -0
- package/src/index.test.ts +7 -7
- package/src/index.ts +13 -1
- package/src/mcp/tools.ts +67 -18
- package/src/parse.ts +33 -3
- package/src/types.ts +24 -0
- package/src/validate.ts +54 -0
- package/.cursor/plans/cliprogram_capabilities_refactor_081e1737.plan.md +0 -224
- package/.cursor/plans/mcp_v1.1_polish_e9656029.plan.md +0 -260
- package/.cursor/plans/mcp_v1.2_invocation_and_extensions_a4f82c1e.plan.md +0 -647
- package/.cursor/plans/v1.3_parser_ergonomics_b3e91f02.plan.md +0 -455
- package/.cursor/rules/code.mdc +0 -9
- package/.github/copilot-instructions.md.md +0 -8
- package/.github/pull_request_template.md +0 -13
- package/.private/scratch.md +0 -4
- package/CLAUDE.md +0 -8
- package/biome.json +0 -40
- package/bun.lock +0 -43
- package/justfile +0 -57
- package/logo.png +0 -0
- package/plan.md +0 -19
- package/scripts/release.ts +0 -266
- package/tsconfig.json +0 -12
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,28 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [3.6.1] - 2026-06-23
|
|
11
|
+
|
|
12
|
+
### Fixed
|
|
13
|
+
|
|
14
|
+
- **npm package** — `package.json` `files` whitelist so publish no longer ships `.cursor/`, `.private/`, `.github/`, or other dev-only paths (npm does not honor `.gitignore`).
|
|
15
|
+
|
|
16
|
+
## [3.6.0] - 2026-06-23
|
|
17
|
+
|
|
18
|
+
### Added
|
|
19
|
+
|
|
20
|
+
- **`CliValueFormat`** — optional `format` on string options: `duration`, `comma-list`, `date`, `date-time`; optional `default` and `pattern` (mutually exclusive with `format`).
|
|
21
|
+
- **`CliContext`** — `durationOpt`, `commaListOpt`, `dateOpt`, `dateTimeOpt`, and `readLeafInputs()` for schema-driven handler reads.
|
|
22
|
+
- **`formats` exports** — `parseDurationMs`, `parseCommaList`, `parseDate`, `parseDateTime` for reuse outside handlers.
|
|
23
|
+
|
|
24
|
+
### Changed
|
|
25
|
+
|
|
26
|
+
- **Post-parse validation** — applies option `default` values and validates `format` / `pattern` before handlers run.
|
|
27
|
+
- **MCP varargs** — `tools/call` positional arrays must be JSON arrays (comma-separated strings no longer accepted).
|
|
28
|
+
- **MCP comma-list options** — `format: comma-list` accepts string or array in `tools/call`.
|
|
29
|
+
- **`docs mcp`**, **`docs api`**, and **`docs/cli-program.md`** — document value formats and varargs policy.
|
|
30
|
+
- **Cursor rule template** (`docs/templates/cursor/rules/cli-program.mdc`) — thin tripwire that directs agents to read `node_modules/argsbarg/docs/cli-program.md` instead of duplicating authoring guidance.
|
|
31
|
+
|
|
10
32
|
## [3.5.0] - 2026-06-22
|
|
11
33
|
|
|
12
34
|
### Added
|
|
@@ -371,7 +393,9 @@ const cli = { ... } satisfies CliProgram; // or : CliProgram
|
|
|
371
393
|
- Migrate schemas: rename every `children` property to **`commands`**; move positional definitions to **`CliPositional`** objects on `positionals` and strip `positional` / `argMin` / `argMax` from flag definitions under `options` (flags only carry `name`, `description`, `kind`, and optional `shortName`).
|
|
372
394
|
- Imports: use `CliPositional` where needed; replace `CliOptionDef` with `CliOption` or `CliPositional` as appropriate.
|
|
373
395
|
|
|
374
|
-
[Unreleased]: https://github.com/bdombro/bun-argsbarg/compare/v3.
|
|
396
|
+
[Unreleased]: https://github.com/bdombro/bun-argsbarg/compare/v3.6.1...HEAD
|
|
397
|
+
[3.6.1]: https://github.com/bdombro/bun-argsbarg/releases/tag/v3.6.1
|
|
398
|
+
[3.6.0]: https://github.com/bdombro/bun-argsbarg/releases/tag/v3.6.0
|
|
375
399
|
[3.5.0]: https://github.com/bdombro/bun-argsbarg/releases/tag/v3.5.0
|
|
376
400
|
[3.4.2]: https://github.com/bdombro/bun-argsbarg/releases/tag/v3.4.2
|
|
377
401
|
[3.4.1]: https://github.com/bdombro/bun-argsbarg/releases/tag/v3.4.1
|
package/README.md
CHANGED
|
@@ -112,9 +112,9 @@ Opt in on the program root with `mcpServer: { enabled: true }`, then run `myapp
|
|
|
112
112
|
|
|
113
113
|
See **[docs/mcp.md](docs/mcp.md)** for configuration, env bootstrapping, custom resources, Cursor setup, and protocol details. See **[docs/cli-program.md](docs/cli-program.md)** for schema authoring (consumer apps: copy **`docs/templates/cursor/rules/cli-program.mdc`** to **`.cursor/rules/cli-program.mdc`**).
|
|
114
114
|
|
|
115
|
-
### Install
|
|
115
|
+
### Install CLI
|
|
116
116
|
|
|
117
|
-
After `bun build --compile` (or when running via `bun`), ship your CLI and let users run:
|
|
117
|
+
argsbarg includes CLI features to manage installation of your compiled bun app. After `bun build --compile` (or when running via `bun`), ship your CLI and let users run:
|
|
118
118
|
|
|
119
119
|
```bash
|
|
120
120
|
myapp install --all --yes
|
|
@@ -140,12 +140,23 @@ myapp completion fish > ~/.config/fish/completions/myapp.fish
|
|
|
140
140
|
|
|
141
141
|
|
|
142
142
|
|
|
143
|
-
##
|
|
143
|
+
## Quick Start
|
|
144
144
|
|
|
145
145
|
```bash
|
|
146
|
-
bun add
|
|
146
|
+
bun add argsbarg
|
|
147
147
|
```
|
|
148
148
|
|
|
149
|
+
### Cursor / AI agents
|
|
150
|
+
|
|
151
|
+
Argsbarg ships authoring docs in `node_modules/argsbarg/docs/`. Agents do not load them unless your repo points there — copy the thin Cursor rule after install (it tells agents to **read** `cli-program.md`, not duplicate it):
|
|
152
|
+
|
|
153
|
+
```bash
|
|
154
|
+
mkdir -p .cursor/rules
|
|
155
|
+
cp node_modules/argsbarg/docs/templates/cursor/rules/cli-program.mdc .cursor/rules/cli-program.mdc
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
Add app-specific conventions in a second rule if needed. Full guide: **[docs/cli-program.md](docs/cli-program.md)**.
|
|
159
|
+
|
|
149
160
|
|
|
150
161
|
## How it works
|
|
151
162
|
|
|
@@ -178,9 +189,13 @@ Add `CliPositional` entries to the command’s `positionals` list (separate from
|
|
|
178
189
|
|
|
179
190
|
### Reading values (`CliContext`)
|
|
180
191
|
|
|
181
|
-
- `ctx.flag("verbose")` — presence options (`boolean`).
|
|
192
|
+
- `ctx.flag("verbose")` / `ctx.hasFlag("verbose")` — presence options (`boolean`).
|
|
182
193
|
- `ctx.stringOpt("name")` / `ctx.numberOpt("count")` — `string | undefined` / `number | null`.
|
|
183
|
-
- `ctx.
|
|
194
|
+
- `ctx.durationOpt("timeout")` — duration options (`format: CliValueFormat.Duration`) as milliseconds.
|
|
195
|
+
- `ctx.commaListOpt("services")` — comma-list options as `string[] | undefined`.
|
|
196
|
+
- `ctx.dateOpt("on")` / `ctx.dateTimeOpt("since")` — ISO date / date-time options.
|
|
197
|
+
- `ctx.readLeafInputs()` — coerced option and positional values for the current leaf (schema-driven).
|
|
198
|
+
- `ctx.typedOpt<T>("custom", parseFn)` — custom parsing for type-safe option resolution.
|
|
184
199
|
- `ctx.args` — positional words in order as `string[]`.
|
|
185
200
|
- `ctx.positional("name")` — named positional lookup; varargs slots return `string[]`, single slots return `string | undefined`.
|
|
186
201
|
- `ctx.program` — program root (`CliProgram`) for contextual help.
|
|
@@ -221,12 +236,13 @@ The package root (`argsbarg` / `src/index.ts`) exports the types and runtime you
|
|
|
221
236
|
| Symbol | Role |
|
|
222
237
|
| --- | --- |
|
|
223
238
|
| `CliProgram`, `CliOption`, `CliPositional`, `CliHandler` | Schema and handler types. |
|
|
224
|
-
| `CliOptionKind`, `CliFallbackMode` | Option kinds (`
|
|
239
|
+
| `CliOptionKind`, `CliValueFormat`, `CliFallbackMode` | Option kinds, value formats (`duration`, `comma-list`, `date`, `date-time`), and root fallback behavior. |
|
|
225
240
|
| `CliSchemaValidationError` | Thrown when the static command tree violates schema rules. |
|
|
226
|
-
| `CliContext` | Handler context (`ctx.flag`, `ctx.stringOpt`, `ctx.
|
|
241
|
+
| `CliContext` | Handler context (`ctx.flag`, `ctx.stringOpt`, `ctx.durationOpt`, `ctx.readLeafInputs`, `ctx.invocation`, …). |
|
|
227
242
|
| `cliRun(root, [argv])` | Validate, parse argv, dispatch, exit. |
|
|
228
243
|
| `cliInvoke(root, argv)` | Parse and dispatch without exiting; returns captured stdout/stderr. |
|
|
229
244
|
| `cliErrWithHelp(ctx, msg)` | Print error + scoped help on stderr, exit 1. |
|
|
245
|
+
| `parseDurationMs`, `parseCommaList`, `parseDate`, `parseDateTime` | Optional format parsers for use outside handlers. |
|
|
230
246
|
|
|
231
247
|
Reserved identifiers (validated at startup): root commands **`completion`**, **`version`**, **`install`**, **`docs`** (when `docs.enabled` is `true`), and **`mcp`** (when `mcpServer.enabled` is `true`).
|
|
232
248
|
|
package/docs/cli-program.md
CHANGED
|
@@ -136,6 +136,65 @@ Do **not** use `mcpTool.description` to paper over missing `--yes`, non-standard
|
|
|
136
136
|
|
|
137
137
|
If help text and MCP behavior match after your fixes, **omit `mcpTool` entirely**.
|
|
138
138
|
|
|
139
|
+
## Value formats
|
|
140
|
+
|
|
141
|
+
On **string options**, optional metadata improves validation, MCP `inputSchema`, and handler reads:
|
|
142
|
+
|
|
143
|
+
| Field | Purpose |
|
|
144
|
+
| --- | --- |
|
|
145
|
+
| `format: CliValueFormat.Duration` | Values like `30s`, `20m`, `1h`; read with `ctx.durationOpt(name)` (milliseconds) |
|
|
146
|
+
| `format: CliValueFormat.CommaList` | Single-flag lists (`--services a,b`); MCP may pass string or array; read with `ctx.commaListOpt(name)` |
|
|
147
|
+
| `format: CliValueFormat.Date` | `YYYY-MM-DD`; read with `ctx.dateOpt(name)` |
|
|
148
|
+
| `format: CliValueFormat.DateTime` | RFC 3339 instant; read with `ctx.dateTimeOpt(name)` |
|
|
149
|
+
| `default: "..."` | Applied in post-parse when the option is omitted (not valid with `required: true`) |
|
|
150
|
+
| `pattern: "..."` | Regex validation (mutually exclusive with `format`) |
|
|
151
|
+
|
|
152
|
+
`format` applies to **string options only** — not positionals. Post-parse keeps raw strings in `ctx.opts`; typed readers return coerced values.
|
|
153
|
+
|
|
154
|
+
**Example** (duration with default, comma-list flag):
|
|
155
|
+
|
|
156
|
+
```typescript
|
|
157
|
+
import { CliOptionKind, CliValueFormat } from "argsbarg";
|
|
158
|
+
|
|
159
|
+
options: [
|
|
160
|
+
{
|
|
161
|
+
name: "timeout",
|
|
162
|
+
description: "Maximum wait time.",
|
|
163
|
+
kind: CliOptionKind.String,
|
|
164
|
+
format: CliValueFormat.Duration,
|
|
165
|
+
default: "20m",
|
|
166
|
+
},
|
|
167
|
+
{
|
|
168
|
+
name: "services",
|
|
169
|
+
description: "Service names to reset (single env only).",
|
|
170
|
+
kind: CliOptionKind.String,
|
|
171
|
+
format: CliValueFormat.CommaList,
|
|
172
|
+
},
|
|
173
|
+
],
|
|
174
|
+
handler: async (ctx) => {
|
|
175
|
+
const timeoutMs = ctx.durationOpt("timeout")!; // always set via default
|
|
176
|
+
const services = ctx.commaListOpt("services"); // string[] | undefined
|
|
177
|
+
},
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
**Varargs positionals** (`argMax: 0`):
|
|
181
|
+
|
|
182
|
+
| Surface | Multiple values |
|
|
183
|
+
| --- | --- |
|
|
184
|
+
| CLI | Space-separated words: `myapp uids uid-a uid-b` |
|
|
185
|
+
| MCP | JSON array on the positional key: `{ "uids": ["uid-a", "uid-b"] }` |
|
|
186
|
+
|
|
187
|
+
Read varargs with `ctx.positional("uids")` (returns `string[]`) or `ctx.args`. Do not comma-split argv tokens or use `format` on positionals.
|
|
188
|
+
|
|
189
|
+
**`readLeafInputs()`** — for leaves with several flags, one schema-driven read instead of hand-rolled `hasFlag` / `stringOpt` lines:
|
|
190
|
+
|
|
191
|
+
```typescript
|
|
192
|
+
const { limit, "skip-readiness": skipReadiness, timeout } = ctx.readLeafInputs();
|
|
193
|
+
// duration → number (ms); comma-list → string[]; presence → boolean; number → number
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
Cross-field rules (e.g. `--match-remote` requires `--branch`) stay in consumer `resolve*` layers — argsbarg does not validate those.
|
|
197
|
+
|
|
139
198
|
## Headless-capable handlers
|
|
140
199
|
|
|
141
200
|
Simple leaves (read args, print stdout) are already headless — no extra work. **Any handler that might mount Ink, prompt, or open a browser should also implement a scriptable fast path** for:
|
|
@@ -219,7 +278,22 @@ Do not declare user commands named `completion`, `install`, `mcp`, `version`, or
|
|
|
219
278
|
|
|
220
279
|
## Cursor rule for consumer repos
|
|
221
280
|
|
|
222
|
-
|
|
281
|
+
Argsbarg ships framework docs under `node_modules/argsbarg/docs/` (same files as this repo’s `docs/`). **This file is the authoritative guide** — the Cursor rule is a thin tripwire that tells agents to read it.
|
|
282
|
+
|
|
283
|
+
Agents do **not** discover package docs automatically. Wire them in after `bun add argsbarg`:
|
|
284
|
+
|
|
285
|
+
1. **Copy the Cursor rule** (recommended):
|
|
286
|
+
|
|
287
|
+
```bash
|
|
288
|
+
mkdir -p .cursor/rules
|
|
289
|
+
cp node_modules/argsbarg/docs/templates/cursor/rules/cli-program.mdc .cursor/rules/cli-program.mdc
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
The template is ~25 lines: when to read which doc, plus hard rules agents often get wrong. It does **not** duplicate this guide — re-copy after upgrading argsbarg when the template changes.
|
|
293
|
+
|
|
294
|
+
2. **Optional:** a second rule for app-only conventions (e.g. `src/cli/shared.ts` flag names, JSON-only handlers, Ink patterns).
|
|
295
|
+
|
|
296
|
+
3. **Optional:** `.cursor/argsbarg.mdc` or `AGENTS.md` pointing at `node_modules/argsbarg/docs/cli-program.md` for broader context.
|
|
223
297
|
|
|
224
298
|
## See also
|
|
225
299
|
|
|
@@ -1,29 +1,23 @@
|
|
|
1
1
|
---
|
|
2
|
-
description: Argsbarg
|
|
3
|
-
globs: "
|
|
2
|
+
description: Argsbarg schema — read framework docs before editing CLI commands
|
|
3
|
+
globs: "src/**/commands/**/*.{ts,tsx},src/index.{ts,tsx}"
|
|
4
4
|
alwaysApply: false
|
|
5
5
|
---
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
When adding or changing argsbarg schema, leaf handlers, or MCP exposure:
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
- Reserved root commands (do not declare): `completion`, `install`, `mcp`, `version`, `docs`, `update`
|
|
9
|
+
1. **Read** `node_modules/argsbarg/docs/cli-program.md` (required — authoritative guide).
|
|
10
|
+
2. MCP tools, varargs, `inputSchema` → also `node_modules/argsbarg/docs/mcp.md`.
|
|
11
|
+
3. `install`, completions, skills → `node_modules/argsbarg/docs/install.md`.
|
|
12
|
+
4. Bundled `docs` built-in → `node_modules/argsbarg/docs/bundled-docs.md`.
|
|
14
13
|
|
|
15
|
-
|
|
16
|
-
- **Inline** options, positionals, and handler schema; extract to `export const …Command = { … } satisfies CliLeaf` (or router type) when shared flags, `mcpTool` spreads, or large handlers justify a module — not zero-arg wrapper functions
|
|
17
|
-
- Leaf descriptions: action-oriented, not UI jargon
|
|
18
|
-
- Prefer option names `yes`, `dry-run`, `json` when semantics match; describe non-interactive use on the option
|
|
14
|
+
**Hard rules** (details and examples are in the docs above — do not contradict them):
|
|
19
15
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
16
|
+
- Reserved root commands: `completion`, `install`, `mcp`, `version`, `docs`, `update`.
|
|
17
|
+
- `satisfies CliProgram` / `CliLeaf`; action-oriented `description` on root, commands, options, and positionals.
|
|
18
|
+
- Omit `mcpTool` unless genuinely CLI-only (`enabled: false`), `requiresEnv`, or an irreducible wire limit — fix schema and headless handlers first.
|
|
19
|
+
- Interactive leaves: one headless path for MCP, non-TTY CLI, and `--yes` / `--dry-run` / `--json` (`shouldRunHeadless*`, `requireYesInNonTty`); not raw `isTTY`.
|
|
20
|
+
- String options: `format` / `default` / `pattern` per `cli-program.md`; `readLeafInputs()` for multi-flag leaves.
|
|
21
|
+
- Varargs (`argMax: 0`): CLI space-separated; MCP JSON array only — no comma-splitting positionals.
|
|
23
22
|
|
|
24
|
-
|
|
25
|
-
Interactive commands need one fast path for MCP, non-TTY CLI, and `--yes` / `--dry-run` / `--json`.
|
|
26
|
-
- Mutators: `requireYesInNonTty`, `shouldRunHeadlessWithYes`
|
|
27
|
-
- Queries: `shouldRunHeadless`, `wantsExplicitJson`
|
|
28
|
-
- Varargs/positionals: `shouldRunHeadlessWithPositionals`
|
|
29
|
-
- `ctx.invocation === "mcp"` only for subprocess/TTY wire behavior; use argsbarg headless helpers, not raw `isTTY`
|
|
23
|
+
**App-specific conventions:** add below or in a separate `.cursor/rules/` file (shared flags path, Ink vs JSON-only, etc.).
|
package/index.d.ts
CHANGED
|
@@ -17,6 +17,20 @@ export declare enum CliOptionKind {
|
|
|
17
17
|
/** Fixed set of allowed string values. Requires non-empty `choices` on the option. */
|
|
18
18
|
Enum = "enum"
|
|
19
19
|
}
|
|
20
|
+
/**
|
|
21
|
+
* Named validation/coercion for string options (`format` on `CliOption`).
|
|
22
|
+
* Positionals do not use `format`; varargs use space-separated CLI tokens and JSON arrays over MCP.
|
|
23
|
+
*/
|
|
24
|
+
export declare enum CliValueFormat {
|
|
25
|
+
/** Duration text such as `30s`, `20m`, `1h`, `2d` (default unit minutes when omitted). */
|
|
26
|
+
Duration = "duration",
|
|
27
|
+
/** Comma-separated list on a single option value (`--services a,b`). */
|
|
28
|
+
CommaList = "comma-list",
|
|
29
|
+
/** Calendar date `YYYY-MM-DD`. */
|
|
30
|
+
Date = "date",
|
|
31
|
+
/** RFC 3339 instant with `Z` or numeric offset. */
|
|
32
|
+
DateTime = "date-time"
|
|
33
|
+
}
|
|
20
34
|
/**
|
|
21
35
|
* When `fallbackCommand` is used for missing or unknown subcommand tokens at a routing node.
|
|
22
36
|
*/
|
|
@@ -56,6 +70,15 @@ export interface CliOption {
|
|
|
56
70
|
* Must be a non-empty array of distinct non-empty strings.
|
|
57
71
|
*/
|
|
58
72
|
choices?: string[];
|
|
73
|
+
/**
|
|
74
|
+
* Named string validation for `kind: String` options. Mutually exclusive with `pattern`.
|
|
75
|
+
* Not supported on positionals.
|
|
76
|
+
*/
|
|
77
|
+
format?: CliValueFormat;
|
|
78
|
+
/** Default value applied in post-parse when the option is omitted. */
|
|
79
|
+
default?: string;
|
|
80
|
+
/** Regex pattern for string options. Mutually exclusive with `format`. */
|
|
81
|
+
pattern?: string;
|
|
59
82
|
}
|
|
60
83
|
/**
|
|
61
84
|
* An ordered positional argument slot, listed on leaf `positionals`.
|
|
@@ -280,6 +303,8 @@ export declare class CliSchemaValidationError extends Error {
|
|
|
280
303
|
/** Creates a schema validation error with a human-readable rule violation. */
|
|
281
304
|
constructor(message: string);
|
|
282
305
|
}
|
|
306
|
+
/** Coerced leaf inputs keyed by option and positional names. */
|
|
307
|
+
export type CliLeafInputs = Record<string, boolean | number | string | string[] | undefined>;
|
|
283
308
|
/**
|
|
284
309
|
* Values passed to a leaf command handler after parsing: app name, routed path, args, and merged options.
|
|
285
310
|
*/
|
|
@@ -303,11 +328,31 @@ export declare class CliContext {
|
|
|
303
328
|
* This is the TypeScript-native advantage over the Swift version.
|
|
304
329
|
*/
|
|
305
330
|
typedOpt<T>(name: string, parse: (s: string) => T): T | null;
|
|
331
|
+
/** Duration option in milliseconds (post-parse validated). */
|
|
332
|
+
durationOpt(name: string): number | undefined;
|
|
333
|
+
/** Comma-list option as a string array (post-parse validated). */
|
|
334
|
+
commaListOpt(name: string): string[] | undefined;
|
|
335
|
+
/** Date option as canonical YYYY-MM-DD (post-parse validated). */
|
|
336
|
+
dateOpt(name: string): string | undefined;
|
|
337
|
+
/** Date-time option as normalized ISO 8601 UTC (post-parse validated). */
|
|
338
|
+
dateTimeOpt(name: string): string | undefined;
|
|
306
339
|
/** Returns the value(s) for a named positional slot. Varargs slots return string[]; single slots return string | undefined. */
|
|
307
340
|
positional(name: string): string | string[] | undefined;
|
|
341
|
+
/** Reads coerced option and positional values for the current leaf from schema metadata. */
|
|
342
|
+
readLeafInputs(): CliLeafInputs;
|
|
343
|
+
private _readOptionValue;
|
|
344
|
+
private _leafNode;
|
|
308
345
|
private _posMap;
|
|
309
346
|
private _positionalMap;
|
|
310
347
|
}
|
|
348
|
+
/** Parses a duration string (e.g. 20m, 1h, 30s) into milliseconds. */
|
|
349
|
+
export declare function parseDurationMs(durationStr: string): number;
|
|
350
|
+
/** Splits a comma-separated string into trimmed non-empty tokens. */
|
|
351
|
+
export declare function parseCommaList(s: string): string[];
|
|
352
|
+
/** Returns canonical YYYY-MM-DD after validation. */
|
|
353
|
+
export declare function parseDate(s: string): string;
|
|
354
|
+
/** Returns normalized ISO 8601 UTC after validation. */
|
|
355
|
+
export declare function parseDateTime(s: string): string;
|
|
311
356
|
/** Minimal context for headless routing helpers. */
|
|
312
357
|
export type HeadlessContext = Pick<CliContext, "invocation">;
|
|
313
358
|
/** True when `--json` was passed or the handler was invoked via MCP. */
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "argsbarg",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.6.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"engines": {
|
|
6
6
|
"bun": ">=1.3"
|
|
@@ -20,6 +20,15 @@
|
|
|
20
20
|
"default": "./src/index.ts"
|
|
21
21
|
}
|
|
22
22
|
},
|
|
23
|
+
"files": [
|
|
24
|
+
"src",
|
|
25
|
+
"index.d.ts",
|
|
26
|
+
"docs",
|
|
27
|
+
"examples",
|
|
28
|
+
"README.md",
|
|
29
|
+
"LICENSE",
|
|
30
|
+
"CHANGELOG.md"
|
|
31
|
+
],
|
|
23
32
|
"devDependencies": {
|
|
24
33
|
"@biomejs/biome": "^2.5.0",
|
|
25
34
|
"@types/bun": "^1.3.12",
|
package/src/context.ts
CHANGED
|
@@ -7,10 +7,15 @@ It keeps handlers small with a typed read API for flags, strings, numbers, and c
|
|
|
7
7
|
parsed values.
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
import
|
|
11
|
-
import {
|
|
10
|
+
import { parseCommaList, parseDate, parseDateTime, parseDurationMs } from "./formats.ts";
|
|
11
|
+
import { collectOptionDefs } from "./parse.ts";
|
|
12
|
+
import type { CliInvocation, CliLeaf, CliNode, CliOption, CliProgram } from "./types.ts";
|
|
13
|
+
import { CliOptionKind, CliValueFormat, isCliLeaf, isCliRouter } from "./types.ts";
|
|
12
14
|
import { strictParseDouble } from "./utils.ts";
|
|
13
15
|
|
|
16
|
+
/** Coerced leaf inputs keyed by option and positional names. */
|
|
17
|
+
export type CliLeafInputs = Record<string, boolean | number | string | string[] | undefined>;
|
|
18
|
+
|
|
14
19
|
/**
|
|
15
20
|
* Values passed to a leaf command handler after parsing: app name, routed path, args, and merged options.
|
|
16
21
|
*/
|
|
@@ -70,38 +75,109 @@ export class CliContext {
|
|
|
70
75
|
}
|
|
71
76
|
}
|
|
72
77
|
|
|
78
|
+
/** Duration option in milliseconds (post-parse validated). */
|
|
79
|
+
durationOpt(name: string): number | undefined {
|
|
80
|
+
const s = this.opts[name];
|
|
81
|
+
if (s === undefined) return undefined;
|
|
82
|
+
return parseDurationMs(s);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Comma-list option as a string array (post-parse validated). */
|
|
86
|
+
commaListOpt(name: string): string[] | undefined {
|
|
87
|
+
const s = this.opts[name];
|
|
88
|
+
if (s === undefined) return undefined;
|
|
89
|
+
return parseCommaList(s);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Date option as canonical YYYY-MM-DD (post-parse validated). */
|
|
93
|
+
dateOpt(name: string): string | undefined {
|
|
94
|
+
const s = this.opts[name];
|
|
95
|
+
if (s === undefined) return undefined;
|
|
96
|
+
return parseDate(s);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Date-time option as normalized ISO 8601 UTC (post-parse validated). */
|
|
100
|
+
dateTimeOpt(name: string): string | undefined {
|
|
101
|
+
const s = this.opts[name];
|
|
102
|
+
if (s === undefined) return undefined;
|
|
103
|
+
return parseDateTime(s);
|
|
104
|
+
}
|
|
105
|
+
|
|
73
106
|
/** Returns the value(s) for a named positional slot. Varargs slots return string[]; single slots return string | undefined. */
|
|
74
107
|
positional(name: string): string | string[] | undefined {
|
|
75
108
|
return this._positionalMap()[name];
|
|
76
109
|
}
|
|
77
110
|
|
|
78
|
-
|
|
111
|
+
/** Reads coerced option and positional values for the current leaf from schema metadata. */
|
|
112
|
+
readLeafInputs(): CliLeafInputs {
|
|
113
|
+
const leaf = this._leafNode();
|
|
114
|
+
if (!leaf) return {};
|
|
79
115
|
|
|
80
|
-
|
|
81
|
-
|
|
116
|
+
const out: CliLeafInputs = {};
|
|
117
|
+
for (const opt of collectOptionDefs(this.program, this.commandPath)) {
|
|
118
|
+
out[opt.name] = this._readOptionValue(opt);
|
|
119
|
+
}
|
|
120
|
+
for (const p of leaf.positionals ?? []) {
|
|
121
|
+
const val = this.positional(p.name);
|
|
122
|
+
if (val === undefined) {
|
|
123
|
+
out[p.name] = undefined;
|
|
124
|
+
} else if (Array.isArray(val)) {
|
|
125
|
+
out[p.name] = val;
|
|
126
|
+
} else {
|
|
127
|
+
out[p.name] = val;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return out;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
private _readOptionValue(opt: CliOption): boolean | number | string | string[] | undefined {
|
|
134
|
+
if (opt.kind === CliOptionKind.Presence) {
|
|
135
|
+
return this.hasFlag(opt.name);
|
|
136
|
+
}
|
|
137
|
+
if (opt.kind === CliOptionKind.Number) {
|
|
138
|
+
const n = this.numberOpt(opt.name);
|
|
139
|
+
return n === null ? undefined : n;
|
|
140
|
+
}
|
|
141
|
+
if (opt.format === CliValueFormat.Duration) {
|
|
142
|
+
return this.durationOpt(opt.name);
|
|
143
|
+
}
|
|
144
|
+
if (opt.format === CliValueFormat.CommaList) {
|
|
145
|
+
return this.commaListOpt(opt.name);
|
|
146
|
+
}
|
|
147
|
+
if (opt.format === CliValueFormat.Date) {
|
|
148
|
+
return this.dateOpt(opt.name);
|
|
149
|
+
}
|
|
150
|
+
if (opt.format === CliValueFormat.DateTime) {
|
|
151
|
+
return this.dateTimeOpt(opt.name);
|
|
152
|
+
}
|
|
153
|
+
return this.stringOpt(opt.name);
|
|
154
|
+
}
|
|
82
155
|
|
|
156
|
+
private _leafNode(): CliLeaf | undefined {
|
|
83
157
|
let node: CliNode = this.program;
|
|
84
158
|
for (const seg of this.commandPath) {
|
|
85
|
-
if (!isCliRouter(node))
|
|
86
|
-
this._posMap = {};
|
|
87
|
-
return {};
|
|
88
|
-
}
|
|
159
|
+
if (!isCliRouter(node)) return undefined;
|
|
89
160
|
const child = node.commands.find((c) => c.key === seg);
|
|
90
|
-
if (!child)
|
|
91
|
-
this._posMap = {};
|
|
92
|
-
return {};
|
|
93
|
-
}
|
|
161
|
+
if (!child) return undefined;
|
|
94
162
|
node = child;
|
|
95
163
|
}
|
|
164
|
+
return isCliLeaf(node) ? node : undefined;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
private _posMap: Record<string, string | string[]> | undefined;
|
|
168
|
+
|
|
169
|
+
private _positionalMap(): Record<string, string | string[]> {
|
|
170
|
+
if (this._posMap) return this._posMap;
|
|
96
171
|
|
|
97
|
-
|
|
172
|
+
const leaf = this._leafNode();
|
|
173
|
+
if (!leaf) {
|
|
98
174
|
this._posMap = {};
|
|
99
175
|
return {};
|
|
100
176
|
}
|
|
101
177
|
|
|
102
178
|
const map: Record<string, string | string[]> = {};
|
|
103
179
|
let argIdx = 0;
|
|
104
|
-
for (const p of
|
|
180
|
+
for (const p of leaf.positionals ?? []) {
|
|
105
181
|
const { argMax = 1 } = p;
|
|
106
182
|
if (argMax === 0) {
|
|
107
183
|
map[p.name] = this.args.slice(argIdx);
|
package/src/docs/api-guide.ts
CHANGED
|
@@ -23,6 +23,20 @@ function optionType(opt: CliOption): string {
|
|
|
23
23
|
return opt.kind;
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
+
function optionFormatDefault(opt: CliOption): string {
|
|
27
|
+
const parts: string[] = [];
|
|
28
|
+
if (opt.format !== undefined) {
|
|
29
|
+
parts.push(opt.format);
|
|
30
|
+
}
|
|
31
|
+
if (opt.default !== undefined) {
|
|
32
|
+
parts.push(`default \`${opt.default}\``);
|
|
33
|
+
}
|
|
34
|
+
if (opt.pattern !== undefined) {
|
|
35
|
+
parts.push(`pattern \`${opt.pattern}\``);
|
|
36
|
+
}
|
|
37
|
+
return parts.length > 0 ? parts.join("; ") : "—";
|
|
38
|
+
}
|
|
39
|
+
|
|
26
40
|
/** Markdown table cell for one option flag. */
|
|
27
41
|
function optionLabel(opt: CliOption): string {
|
|
28
42
|
const long = `\`--${opt.name}\``;
|
|
@@ -33,7 +47,7 @@ function optionLabel(opt: CliOption): string {
|
|
|
33
47
|
/** One options table row. */
|
|
34
48
|
function formatOptionRow(opt: CliOption): string {
|
|
35
49
|
const req = opt.required ? "required" : "optional";
|
|
36
|
-
return `| ${optionLabel(opt)} | ${optionType(opt)} | ${req} | ${opt.description} |`;
|
|
50
|
+
return `| ${optionLabel(opt)} | ${optionType(opt)} | ${req} | ${optionFormatDefault(opt)} | ${opt.description} |`;
|
|
37
51
|
}
|
|
38
52
|
|
|
39
53
|
/** One positionals table row. */
|
|
@@ -99,8 +113,8 @@ function renderCommandNode(
|
|
|
99
113
|
|
|
100
114
|
if ((node.options ?? []).length > 0) {
|
|
101
115
|
lines.push("#### Options", "");
|
|
102
|
-
lines.push("| Option | Type | Required | Description |");
|
|
103
|
-
lines.push("| --- | --- | --- | --- |");
|
|
116
|
+
lines.push("| Option | Type | Required | Format / default | Description |");
|
|
117
|
+
lines.push("| --- | --- | --- | --- | --- |");
|
|
104
118
|
for (const opt of node.options ?? []) {
|
|
105
119
|
lines.push(formatOptionRow(opt));
|
|
106
120
|
}
|
package/src/docs/mcp-guide.ts
CHANGED
|
@@ -206,7 +206,9 @@ export function generateMcpGuide(root: CliProgram): string {
|
|
|
206
206
|
"Arguments are a flat JSON object keyed by long option and positional names (hyphenated option names are valid keys).",
|
|
207
207
|
`See \`${root.key} docs schema\` or the schema resource for per-tool shapes.`,
|
|
208
208
|
"",
|
|
209
|
-
"Varargs positionals accept a JSON array
|
|
209
|
+
"Varargs positionals accept a JSON array of strings (not a comma-separated string).",
|
|
210
|
+
"Options with `format: comma-list` accept a comma-separated string or JSON array.",
|
|
211
|
+
"Options with a schema `default` are applied when omitted.",
|
|
210
212
|
"",
|
|
211
213
|
"## Protocol",
|
|
212
214
|
"",
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { expect, test } from "bun:test";
|
|
2
|
+
import {
|
|
3
|
+
parseCommaList,
|
|
4
|
+
parseDate,
|
|
5
|
+
parseDateTime,
|
|
6
|
+
parseDurationMs,
|
|
7
|
+
validateFormatValue,
|
|
8
|
+
} from "./formats.ts";
|
|
9
|
+
import { CliValueFormat } from "./types.ts";
|
|
10
|
+
|
|
11
|
+
test("parseDurationMs parses minutes and hours", () => {
|
|
12
|
+
expect(parseDurationMs("30s")).toBe(30_000);
|
|
13
|
+
expect(parseDurationMs("5m")).toBe(5 * 60 * 1000);
|
|
14
|
+
expect(parseDurationMs("2h")).toBe(2 * 60 * 60 * 1000);
|
|
15
|
+
expect(parseDurationMs("1d")).toBe(24 * 60 * 60 * 1000);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
test("parseCommaList splits and trims", () => {
|
|
19
|
+
expect(parseCommaList("a,b")).toEqual(["a", "b"]);
|
|
20
|
+
expect(parseCommaList(" a , b , ")).toEqual(["a", "b"]);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
test("parseDate validates calendar dates", () => {
|
|
24
|
+
expect(parseDate("2026-06-22")).toBe("2026-06-22");
|
|
25
|
+
expect(() => parseDate("2026-02-30")).toThrow();
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test("parseDateTime normalizes to UTC ISO", () => {
|
|
29
|
+
expect(parseDateTime("2026-06-22T15:00:00Z")).toBe("2026-06-22T15:00:00.000Z");
|
|
30
|
+
expect(() => parseDateTime("2026-06-22")).toThrow();
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
test("validateFormatValue rejects invalid duration", () => {
|
|
34
|
+
expect(() => validateFormatValue("nope", CliValueFormat.Duration)).toThrow();
|
|
35
|
+
});
|