From 0a02cb80c50ac9198c562ba894172f7ce432f18c Mon Sep 17 00:00:00 2001 From: bircni Date: Sun, 18 Oct 2026 17:20:24 +0200 Subject: [PATCH] fix: keep the command word after a leading --json Signed-off-by: bircni --- internal/app/app.go | 4 ++++ internal/app/app_test.go | 3 +++ 2 files changed, 7 insertions(+) diff --git a/internal/app/app.go b/internal/app/app.go index 92e3a630..d51741b3 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -424,6 +424,10 @@ func joinJSONFields(root *cobra.Command, args []string) []string { if args[i] != "--json" || i+1 >= len(args) || !jsonFieldList.MatchString(args[i+1]) { continue } + // A word that names a subcommand at this position is the command, as in `tea --json repo view`. + if parent, _, err := root.Find(args[:i]); err == nil && slices.ContainsFunc(parent.Commands(), func(c *cobra.Command) bool { return c.Name() == args[i+1] || c.HasAlias(args[i+1]) }) { + continue + } cmd, _, err := root.Find(slices.Concat(args[:i+1], args[i+2:])) if err == nil && terminal.ValidateOutput(args[i+1], "", commandOutputFields(cmd)) != nil && acceptsArgs(args) { continue diff --git a/internal/app/app_test.go b/internal/app/app_test.go index 778c419c..75261b28 100644 --- a/internal/app/app_test.go +++ b/internal/app/app_test.go @@ -98,6 +98,9 @@ func TestJoinJSONFields(t *testing.T) { {[]string{"search", "issue", "--json", "bug"}, []string{"search", "issue", "--json", "bug"}}, {[]string{"repo", "clone", "--json", "project"}, []string{"repo", "clone", "--json", "project"}}, {[]string{"search", "repo", "--json", "name,url", "tea"}, []string{"search", "repo", "--json=name,url", "tea"}}, + // A leading --json keeps the command word that follows it. + {[]string{"--json", "repo", "view", "alice/project"}, []string{"--json", "repo", "view", "alice/project"}}, + {[]string{"--json", "pr", "list"}, []string{"--json", "pr", "list"}}, {[]string{"--json", "number", "my-alias"}, []string{"--json=number", "my-alias"}}, // A word that cannot be an argument is a misspelled field, which then lists the available ones. {[]string{"issue", "list", "--json", "Number"}, []string{"issue", "list", "--json=Number"}},