Skip to content

fix(spec): emit valid kdl choices from cobra - #1670

Merged
jdx merged 1 commit into
mainfrom
fix/cobra-kdl-choice-nodes
Nov 23, 2026
Merged

jdx merged 1 commit into
mainfrom
fix/cobra-kdl-choice-nodes

Conversation

@jdx

@jdx jdx commented Nov 22, 2026 •

Copy link
Copy Markdown
Owner

Cobra commands with ValidArgs now generate a Usage spec that passes usage lint, whether the choices belong to the root command or a subcommand. The generator writes choice "dev" instead of a bare "dev" inside the choices block. The same renderer handles choices on flag arguments when supplied.

Verified the generated root, subcommand, and flag-argument specs with usage lint v7.4.0 and ran the Cobra integration tests.

Fixes #1669.

AI-assisted — Tool: ChatGPT; model: OpenAI/unavailable; version: unavailable.


Note

Low Risk
Scoped to KDL formatting for Cobra integration and test coverage; no runtime auth or data-path changes.

Overview
Cobra-derived Usage specs now render choices blocks with explicit choice "..." entries instead of bare quoted values, so generated KDL matches what usage lint expects. A shared renderChoices helper centralizes this for positional args (including subcommands) and flag value args.

Tests were tightened to assert full KDL for root/subcommand ValidArgs and a direct renderKDL flag-choices case, and a new lintKDL helper pipes the output through the Usage CLI lint command (with USAGE_BIN / build skip when unavailable).

Reviewed by Cursor Bugbot for commit e1ec03d. Bugbot is set up for automated code reviews on this repo. Configure here.

Summary by CodeRabbit

  • Bug Fixes
    • Improved how generated usage definitions represent choices for command arguments and flags. Choice values are now listed as individual entries, including choices nested under flag arguments, and quoted values are preserved correctly.
  • Tests
    • Added checks for generated choice output and optional validation with the Usage CLI. Tests skip CLI validation when the binary is unavailable.

@coderabbitai

coderabbitai Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The Cobra KDL renderer now emits each choice as a choice node. Tests cover choices on root and subcommand arguments and within flag arguments. They also lint the generated KDL when the Usage CLI is available.

Changes: Cobra choice rendering

Layer / File(s) Summary
Render and validate choice nodes
integrations/cobra/kdl.go, integrations/cobra/cobra_usage_test.go
renderArg and renderFlag use a shared helper to emit one choice node per value. Tests assert exact KDL for positional and flag arguments, then run the optional Usage CLI lint check.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: thecodesmith, greptile-apps

Merge Risk: Minimal · up to e1ec0

The change makes Cobra-generated Usage specs emit valid choice nodes for ValidArgs. No actionable merge risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: Cobra integration now emits valid KDL choice nodes. The changeset replaces bare quoted values in choices blocks with a choice node per value in kdl.go, and adds tests …
Linked Issues check Passed Issue #1669 requires the Cobra integration to emit valid Usage choices for ValidArgs. The issue states that a choices block must hold choice nodes, not bare strings. At the reviewed head, `integ…
Out of Scope Changes check Passed The changes fall into two groups. The first is the kdl.go renderer change. It replaces the bare-value output in renderArg and renderFlag with the shared renderChoices helper. This directly imp…
Full details: Title check

Explanation

The title names the main change: Cobra integration now emits valid KDL choice nodes. The changeset replaces bare quoted values in choices blocks with a choice node per value in kdl.go, and adds tests that check the exact KDL output and lint it with the Usage CLI. The title is specific and matches the linked issue #1669 about valid choices from ValidArgs. The 'fix(spec)' prefix is a conventional-commit style that is acceptable.

Full details: Linked Issues check

Explanation

Issue #1669 requires the Cobra integration to emit valid Usage choices for ValidArgs. The issue states that a choices block must hold choice nodes, not bare strings. At the reviewed head, integrations/cobra/kdl.go adds renderChoices, which writes choices { and then one choice <value> line per value. renderArg calls it at depth+1, and renderFlag calls it at depth+2. Both call sites keep the same indentation as the previous output, with values one level deeper than choices. The root-command case in the issue is covered by the renderArg path. The test summary reports exact-KDL assertions for root and subcommand arguments, a flag-argument renderer test, and a usage lint check when the binary is available. I did not run the tests or usage lint. The lint result relies on the author's report and the test code, which I did not inspect in full.

Full details: Out of Scope Changes check

Explanation

The changes fall into two groups. The first is the kdl.go renderer change. It replaces the bare-value output in renderArg and renderFlag with the shared renderChoices helper. This directly implements #1669. The flag-argument path uses the same renderer. The issue does not name flags, but the PR description states that choices on flag arguments use this renderer. The change is therefore a direct consequence of the shared helper and does not add unrelated behavior. The second group is test changes in integrations/cobra/cobra_usage_test.go. These tests check the new output and add an optional usage lint helper. They support the objective and do not add unrelated behavior.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Central YAML (base), Organization UI (inherited)
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 6d5ec548-b222-4ac7-8c6f-a7e4a1f2cf3b
Commits

Reviewing files that changed from the base of the PR and between a5dc9df and e1ec03d.

Files selected for processing: 2

  • integrations/cobra/cobra_usage_test.go
  • integrations/cobra/kdl.go

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

Finishing Touches
Fix failing CI checks

  • Commit to this branch
  • Create a new PR

@greptile-apps

greptile-apps Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge.

Summary

Fixes Cobra-generated KDL by writing choice "value" inside each choices block.

  • Cobra-generated argument choices now appear as KDL choice nodes.

Reviews (1) · Last reviewed commit: "fix(spec): emit valid kdl choices from c..." · Reviewed by Greptile

@github-actions

Copy link
Copy Markdown
Contributor

Instruction counts

benchmark trend instructions Δ wall (min) Δ
markdown ▅▅▅▄▁▇▅▆█▆▅▇▄▁ 269,885,328 → 269,843,391 -0.02% 47.16 → 47.24ms +0.15%
startup ▅▅▅▃▃▅▅▅▇▇▇▇▁█ 1,014,021 → 1,020,843 +0.67% 1.36 → 1.38ms +0.99%

No instruction-count regression above 1%.

Only instruction counts gate. Wall clock is shown for context — on identical hardware it moves 4-20% run to run.

Measured by tak — instruction-counted CLI benchmarks, stored in this repository's git notes.

Shadow comparison

Parsing mise use -g node@20 against a shadow of mise's committed spec.
Reported, not gated: the shadow grows as the derive learns to express more, so
what to watch is the ratio rather than either column.

framework stripped binary, bytes
usage 1254888
bpaf 2493728
clap 3079624
framework instructions, cold parse vs usage
usage 8497 —
clap 6517879 767x
bpaf 21908898 2578x
                                              min       p01       p10    median
usage-rs: argv -> struct                      784       794       806       821  ns
clap: build tree + parse -> struct        1305013   1312464   1322014   1336410  ns
bpaf: build parser + parse -> struct      3406105   3406105   3459816   3500775  ns

usage: argv -> struct                             777 ns      0.78 µs
clap: build tree + parse -> struct            1317831 ns   1317.83 µs
clap: parse -> struct, tree reused              50973 ns     50.97 µs
clap: build tree only                          812674 ns    812.67 µs

e1ec03dedc53 vs a5dc9df6b77f · measured on the runner, not pushed to the history.

@jdx
jdx merged commit 3723b49 into main Nov 23, 2026
17 checks passed
@jdx
jdx deleted the fix/cobra-kdl-choice-nodes branch November 23, 2026 02:55
mise-en-dev added a commit that referenced this pull request Nov 23, 2026
### 🐛 Bug Fixes

- **(fish)** keep backslashes in run= commands of embedded completion
specs by [@jdx](https://github.com/jdx) in
[#1659](#1659)
- **(powershell)** offer subcommands instead of files after a trailing
space by [@jdx](https://github.com/jdx) in
[#1672](#1672)
- **(spec)** emit valid kdl choices from cobra by
[@jdx](https://github.com/jdx) in
[#1670](#1670)

### 🛡️ Security

- **(deps)** update mise tools by
[@renovate[bot]](https://github.com/renovate[bot]) in
[#1663](#1663)

### 🔍 Other Changes

- **(entire)** remove trail runners by [@jdx](https://github.com/jdx) in
[a5dc9df](a5dc9df)
- fix the clippy lints that fail lint on main by
[@jdx](https://github.com/jdx) in
[#1666](#1666)

### 📦️ Dependency Updates

- lock file maintenance by
[@renovate[bot]](https://github.com/renovate[bot]) in
[#1665](#1665)
- update dependency effect to v4.1.4 by
[@renovate[bot]](https://github.com/renovate[bot]) in
[#1661](#1661)
- lock file maintenance by
[@renovate[bot]](https://github.com/renovate[bot]) in
[#1667](#1667)
- update module github.com/spf13/pflag to v1.1.0 by
[@renovate[bot]](https://github.com/renovate[bot]) in
[#1664](#1664)
- update github actions by
[@renovate[bot]](https://github.com/renovate[bot]) in
[#1662](#1662)
- update jdx/mise-action action to v5.2.8 by
[@renovate[bot]](https://github.com/renovate[bot]) in
[#1668](#1668)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cobra integration produces invalid usage for ValidArgs

1 participant