You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Sync README with post-merge state: Ctx, safe_json_string, full example layout
- Update Example section: list all files including config.rs, config
commands, contract.rs, tests/ directory, Ctx struct, --quiet flag
- Update Reusable Modules: replace Format-only API with Ctx-based API
matching actual example code, replace unsafe string interpolation
fallback with safe_json_string pattern using serde_json::json!
- Update Entry Point Runner: use Ctx::new instead of Format::detect
- Fix Philosophy #8: match const pattern used in example
- All 40 integration tests pass
Copy file name to clipboardExpand all lines: README.md
+61-30Lines changed: 61 additions & 30 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -82,7 +82,7 @@ If `inbox list` works, `account list` works. If `--json` forces JSON in one CLI,
82
82
83
83
### 8. Self-contained and portable
84
84
85
-
The binary carries its own skill file (`include_str!`). `skill install` deploys it. `update` replaces the binary from GitHub Releases. One artifact. The self-update mechanism is opt-in -- CLIs distributed via package managers or in managed environments should disable it.
85
+
The binary carries its own skill file as an embedded constant (via `const` or `include_str!`). `skill install` deploys it. `update` replaces the binary from GitHub Releases. One artifact. The self-update mechanism is opt-in -- CLIs distributed via package managers or in managed environments should disable it.
86
86
87
87
### 9. Speed is a feature
88
88
@@ -236,9 +236,9 @@ Self-update should be disableable via config (`update.enabled = false`) for mana
236
236
237
237
These are battle-tested patterns extracted from production CLIs. Each module is self-contained -- copy the pattern into your CLI and adapt.
238
238
239
-
### Output Format Detection
239
+
### Output Format Detection and Context
240
240
241
-
Every CLI needs this. Detect whether to output JSON or human-readable, based on `--json` flag or pipe detection.
241
+
Detect whether to output JSON or human-readable, based on `--json` flag or pipe detection. Bundle format + quiet into a `Ctx` that gets passed to all commands.
242
242
243
243
```rust
244
244
#[derive(Clone, Copy)]
@@ -255,39 +255,62 @@ impl Format {
255
255
Format::Human
256
256
}
257
257
}
258
+
}
258
259
259
-
pubfnis_json(self) ->bool {
260
-
matches!(self, Format::Json)
260
+
/// Output context: bundles format + quiet so commands take one parameter.
261
+
#[derive(Clone, Copy)]
262
+
pubstructCtx {
263
+
pubformat:Format,
264
+
pubquiet:bool,
265
+
}
266
+
267
+
implCtx {
268
+
pubfnnew(json_flag:bool, quiet:bool) ->Self {
269
+
Self { format:Format::detect(json_flag), quiet }
261
270
}
262
271
}
263
272
```
264
273
265
274
### JSON Envelope Helpers
266
275
267
-
`print_success_or` is the workhorse -- it handles JSON automatically and lets you provide a closure for human output. `print_error` sends errors to stderr in both formats.
276
+
`print_success_or` is the workhorse -- it handles JSON automatically and lets you provide a closure for human output. `--quiet` suppresses human output; JSON always emits. `print_error` sends errors to stderr in both formats (never suppressed by `--quiet`).
The `example/` directory contains a modular `greeter` CLI demonstrating the five core patterns (agent-info, JSON envelope, semantic exit codes, skill self-install, self-update) and the reusable entry point, error type, and output helpers. Config loading, secret handling, XDG paths, and HTTP retry are documented as code patterns in the Reusable Modules section above -- copy them into your CLI when you need them.
710
+
The `example/` directory contains a modular `greeter` CLI demonstrating all core patterns: agent-info with argument schemas, JSON envelope, semantic exit codes (0-4), `--json` pre-scan, `--quiet` flag, config loading via Figment, skill self-install, and self-update. It includes 40 integration tests that verify every contract.
688
711
689
712
```
690
713
example/
691
714
src/
692
-
main.rs # Entry point -- parse, detect format, dispatch, exit
0 commit comments