rich-click CLI: --output json for machine-readable help - #335
Conversation
Extend the rich-click CLI's --output option with a `json` value so any
Click CLI can emit the --help-json schema without opting in itself:
- `rich-click --output json [cmd] --help` emits the JSON schema instead of
rendered help. Implemented via a RichCommand.get_help() override that
returns get_help_json() when the context's export_console_as is "json"
(set by --output json), so a plain --help yields JSON.
- --output html/svg/text now also applies to the --help-json flag: the
schema is rendered through the recording console and exported, so e.g.
`rich-click -c '{"help_json": true}' --output svg [cmd] --help-json`
produces an SVG of the JSON.
Uses --output json (a value of the existing -o option) rather than a
--json flag, so there is no risk of clashing with a wrapped command's own
options — the rich-click wrapper consumes -o before the script args.
getvalue() treats "json" as plain-text export defensively (it should never
reach there for help, since get_help short-circuits). Docs + tests added.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
FWIW, you could do |
|
I guess one benefit of rich-click -c '{"help_json": true}' --output=svg [cmd] --help-jsoninstead becoming this? rich-click --json --output=svg [cmd] --helpI'm not sure I have a strong preference either way. Honestly I can't see many people saving SVGs of JSON output, so combination of the two probably isn't a common scenario 😅 |
Stacked on top of #331 (base branch
help-json). Implements dwreeves's CLI suggestion from that review.What
Extends the rich-click CLI's
--outputoption with ajsonvalue, so machine-readable help works through the CLI wrapper for any Click CLI — even ones that haven't opted intohelp_json.Two behaviours:
rich-click --output=json [cmd] --helpemits the--help-jsonschema instead of rendered help. A plain--helpyields JSON — no need for the target to enablehelp_json. Implemented via aRichCommand.get_help()override that returnsget_help_json()when the context'sexport_console_asis"json".--outputapplies to the--help-jsonflag too. The schema is rendered through the recording console and exported, sorich-click -c '{"help_json": true}' --output=svg [cmd] --help-jsonproduces an SVG of the JSON (dwreeves's second case).Why
--output json, not--jsonIt's a value of the existing
-o/--outputoption, not a new flag. The wrapper consumes--outputbefore the script args (allow_interspersed_args=False), so the wrapped command never sees it — no risk of clashing with the target's own options.Notes
getvalue()treats"json"as plain-text export defensively; it should never reach there for help, sinceget_helpshort-circuits.rich_click_cli.md, cross-ref frommachine_readable_help.md); 3 new tests + regenerated--helpsnapshot.main.