From a5169e94739e5a19e1936032b271c705c7d7aa84 Mon Sep 17 00:00:00 2001 From: Markus Unterwaditzer Date: Sat, 30 May 2026 08:10:56 +0200 Subject: [PATCH 1/4] Add custom helptext when --json is used Fix https://github.com/git-pkgs/forge/issues/89 This doesn't actually implement any features (such as, selecting specific JSON fields). All it does is steer the user to the right option. The only reason I wanted this flag to begin with is so that forge can become more of a drop-in replacement. --- internal/cli/pr.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/internal/cli/pr.go b/internal/cli/pr.go index 31c7da7..01906c1 100644 --- a/internal/cli/pr.go +++ b/internal/cli/pr.go @@ -45,6 +45,7 @@ func prViewCmd() *cobra.Command { var ( flagComments bool flagWeb bool + flagJSON string ) cmd := &cobra.Command{ @@ -52,6 +53,10 @@ func prViewCmd() *cobra.Command { Short: "View a pull request", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { + if cmd.Flags().Changed("json") { + return fmt.Errorf("--json is not supported; use --output json instead (field selection is not supported)\n\nTry: forge --output json pr view %s", strings.Join(args, " ")) + } + number, err := strconv.Atoi(args[0]) if err != nil { return fmt.Errorf("invalid PR number: %s", args[0]) @@ -96,6 +101,8 @@ func prViewCmd() *cobra.Command { cmd.Flags().BoolVarP(&flagComments, "comments", "c", false, "Show comments") cmd.Flags().BoolVarP(&flagWeb, "web", "w", false, "Open in browser") + cmd.Flags().StringVar(&flagJSON, "json", "", "Not supported; use --output json") + _ = cmd.Flags().MarkHidden("json") return cmd } From 37dcb60a2963862b2534061aebc892175af538e5 Mon Sep 17 00:00:00 2001 From: Markus Unterwaditzer Date: Sun, 31 May 2026 12:10:56 +0200 Subject: [PATCH 2/4] address review feedback --- internal/cli/pr.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/cli/pr.go b/internal/cli/pr.go index 01906c1..25f264b 100644 --- a/internal/cli/pr.go +++ b/internal/cli/pr.go @@ -102,6 +102,7 @@ func prViewCmd() *cobra.Command { cmd.Flags().BoolVarP(&flagComments, "comments", "c", false, "Show comments") cmd.Flags().BoolVarP(&flagWeb, "web", "w", false, "Open in browser") cmd.Flags().StringVar(&flagJSON, "json", "", "Not supported; use --output json") + cmd.Flags().Lookup("json").NoOptDefVal = " " _ = cmd.Flags().MarkHidden("json") return cmd } From 4ad91e7818065716ad26b4867e9f0ba6aeb449ee Mon Sep 17 00:00:00 2001 From: Markus Unterwaditzer Date: Sun, 31 May 2026 12:29:06 +0200 Subject: [PATCH 3/4] Work around bad error output, add testcase --- internal/cli/pr.go | 2 +- internal/cli/pr_test.go | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/internal/cli/pr.go b/internal/cli/pr.go index 25f264b..4f3f584 100644 --- a/internal/cli/pr.go +++ b/internal/cli/pr.go @@ -54,7 +54,7 @@ func prViewCmd() *cobra.Command { Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { if cmd.Flags().Changed("json") { - return fmt.Errorf("--json is not supported; use --output json instead (field selection is not supported)\n\nTry: forge --output json pr view %s", strings.Join(args, " ")) + return fmt.Errorf("--json is not supported; use --output json instead (field selection is not supported)\n\nTry: forge --output json pr view ") } number, err := strconv.Atoi(args[0]) diff --git a/internal/cli/pr_test.go b/internal/cli/pr_test.go index 4852128..799d7d5 100644 --- a/internal/cli/pr_test.go +++ b/internal/cli/pr_test.go @@ -142,3 +142,19 @@ func TestPRDiffRequiresArg(t *testing.T) { t.Fatal("expected error for missing argument") } } + +func TestPRViewJSONFlagNotSupported(t *testing.T) { + var buf bytes.Buffer + rootCmd.SetOut(&buf) + rootCmd.SetErr(&buf) + rootCmd.SetArgs([]string{"pr", "view", "--json", "title"}) + + err := rootCmd.Execute() + if err == nil { + t.Fatal("expected error for --json flag") + } + want := "--json is not supported; use --output json instead (field selection is not supported)\n\nTry: forge --output json pr view " + if err.Error() != want { + t.Errorf("unexpected error:\ngot: %s\nwant: %s", err.Error(), want) + } +} From 6868734680e67093cc7880f380248524ac4d1036 Mon Sep 17 00:00:00 2001 From: Markus Unterwaditzer Date: Sun, 31 May 2026 12:48:01 +0200 Subject: [PATCH 4/4] Add extra helptext for comments and reviews --- internal/cli/pr.go | 10 +++++++- internal/cli/pr_test.go | 54 ++++++++++++++++++++++++++++++++--------- 2 files changed, 51 insertions(+), 13 deletions(-) diff --git a/internal/cli/pr.go b/internal/cli/pr.go index 4f3f584..d991441 100644 --- a/internal/cli/pr.go +++ b/internal/cli/pr.go @@ -54,7 +54,15 @@ func prViewCmd() *cobra.Command { Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { if cmd.Flags().Changed("json") { - return fmt.Errorf("--json is not supported; use --output json instead (field selection is not supported)\n\nTry: forge --output json pr view ") + var hints []string + hints = append(hints, "forge --output json pr view ") + if strings.Contains(flagJSON, "comments") { + hints = append(hints, "forge pr view --comments ") + } + if strings.Contains(flagJSON, "reviews") { + hints = append(hints, "forge pr review list ") + } + return fmt.Errorf("--json is not supported; use --output json instead (field selection is not supported)\n\nTry: %s", strings.Join(hints, "\n ")) } number, err := strconv.Atoi(args[0]) diff --git a/internal/cli/pr_test.go b/internal/cli/pr_test.go index 799d7d5..4ad8902 100644 --- a/internal/cli/pr_test.go +++ b/internal/cli/pr_test.go @@ -144,17 +144,47 @@ func TestPRDiffRequiresArg(t *testing.T) { } func TestPRViewJSONFlagNotSupported(t *testing.T) { - var buf bytes.Buffer - rootCmd.SetOut(&buf) - rootCmd.SetErr(&buf) - rootCmd.SetArgs([]string{"pr", "view", "--json", "title"}) - - err := rootCmd.Execute() - if err == nil { - t.Fatal("expected error for --json flag") - } - want := "--json is not supported; use --output json instead (field selection is not supported)\n\nTry: forge --output json pr view " - if err.Error() != want { - t.Errorf("unexpected error:\ngot: %s\nwant: %s", err.Error(), want) + tests := []struct { + name string + args []string + want string + }{ + { + name: "generic field", + args: []string{"pr", "view", "--json=title", "1"}, + want: "--json is not supported; use --output json instead (field selection is not supported)\n\nTry: forge --output json pr view ", + }, + { + name: "comments field", + args: []string{"pr", "view", "--json=comments", "1"}, + want: "--json is not supported; use --output json instead (field selection is not supported)\n\nTry: forge --output json pr view \n forge pr view --comments ", + }, + { + name: "reviews field", + args: []string{"pr", "view", "--json=reviews", "1"}, + want: "--json is not supported; use --output json instead (field selection is not supported)\n\nTry: forge --output json pr view \n forge pr review list ", + }, + { + name: "both fields", + args: []string{"pr", "view", "--json=reviews,comments", "1"}, + want: "--json is not supported; use --output json instead (field selection is not supported)\n\nTry: forge --output json pr view \n forge pr view --comments \n forge pr review list ", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + var buf bytes.Buffer + rootCmd.SetOut(&buf) + rootCmd.SetErr(&buf) + rootCmd.SetArgs(tt.args) + + err := rootCmd.Execute() + if err == nil { + t.Fatal("expected error for --json flag") + } + if err.Error() != tt.want { + t.Errorf("unexpected error:\ngot: %s\nwant: %s", err.Error(), tt.want) + } + }) } }