From 45ecc5ece97e4bbd790270cc46661a81ec0bcaf1 Mon Sep 17 00:00:00 2001 From: Benjamin Levesque <14175665+benjlevesque@users.noreply.github.com> Date: Sun, 31 Aug 2025 17:56:26 +0200 Subject: [PATCH] introduce AddJSONFlagsWithoutShorthand --- pkg/cmd/auth/status/status.go | 3 ++- pkg/cmdutil/json_flags.go | 43 ++++++++++++++++++++++++---------- pkg/cmdutil/json_flags_test.go | 20 ++++------------ 3 files changed, 38 insertions(+), 28 deletions(-) diff --git a/pkg/cmd/auth/status/status.go b/pkg/cmd/auth/status/status.go index 417a0773c..bed9299b8 100644 --- a/pkg/cmd/auth/status/status.go +++ b/pkg/cmd/auth/status/status.go @@ -171,7 +171,8 @@ func NewCmdStatus(f *cmdutil.Factory, runF func(*StatusOptions) error) *cobra.Co cmd.Flags().BoolVarP(&opts.ShowToken, "show-token", "t", false, "Display the auth token") cmd.Flags().BoolVarP(&opts.Active, "active", "a", false, "Display the active account only") - cmdutil.AddJSONFlags(cmd, &opts.Exporter, authFields) + // the json flags are intentionally not given a shorthand to avoid conflict with -t/--show-token + cmdutil.AddJSONFlagsWithoutShorthand(cmd, &opts.Exporter, authFields) return cmd } diff --git a/pkg/cmdutil/json_flags.go b/pkg/cmdutil/json_flags.go index 29bb8c002..95e990585 100644 --- a/pkg/cmdutil/json_flags.go +++ b/pkg/cmdutil/json_flags.go @@ -24,10 +24,38 @@ type JSONFlagError struct { } func AddJSONFlags(cmd *cobra.Command, exportTarget *Exporter, fields []string) { - f := cmd.Flags() + f := createFlags() + + f.VisitAll(func(flag *pflag.Flag) { + if flag.Name == "jq" { + flag.Shorthand = "q" + } + if flag.Name == "template" { + flag.Shorthand = "t" + } + cmd.Flags().AddFlag(flag) + }) + + setupJsonFlags(cmd, exportTarget, fields) +} + +func AddJSONFlagsWithoutShorthand(cmd *cobra.Command, exportTarget *Exporter, fields []string) { + f := createFlags() + f.VisitAll(func(flag *pflag.Flag) { + cmd.Flags().AddFlag(flag) + }) + setupJsonFlags(cmd, exportTarget, fields) +} + +func createFlags() *pflag.FlagSet { + f := pflag.NewFlagSet("", pflag.ContinueOnError) f.StringSlice("json", nil, "Output JSON with the specified `fields`") - addStringFlagWithSafeShorthand(f, "jq", "q", "", "Filter JSON output using a jq `expression`") - addStringFlagWithSafeShorthand(f, "template", "t", "", "Format JSON output using a Go template; see \"gh help formatting\"") + f.String("jq", "", "Filter JSON output using a jq `expression`") + f.String("template", "", "Format JSON output using a Go template; see \"gh help formatting\"") + return f +} + +func setupJsonFlags(cmd *cobra.Command, exportTarget *Exporter, fields []string) { _ = cmd.RegisterFlagCompletionFunc("json", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { var results []string @@ -94,15 +122,6 @@ func AddJSONFlags(cmd *cobra.Command, exportTarget *Exporter, fields []string) { cmd.Annotations["help:json-fields"] = strings.Join(fields, ",") } -// addStringFlagWithSafeShorthand only adds the flag with shorthand if the shorthand is not already used by another flag. -func addStringFlagWithSafeShorthand(f *pflag.FlagSet, name, shorthand, value, usage string) { - if f.ShorthandLookup(shorthand) != nil { - f.String(name, value, usage) - } else { - f.StringP(name, shorthand, value, usage) - } -} - func checkJSONFlags(cmd *cobra.Command) (*jsonExporter, error) { f := cmd.Flags() jsonFlag := f.Lookup("json") diff --git a/pkg/cmdutil/json_flags_test.go b/pkg/cmdutil/json_flags_test.go index 15f0b7642..ee089960b 100644 --- a/pkg/cmdutil/json_flags_test.go +++ b/pkg/cmdutil/json_flags_test.go @@ -119,7 +119,7 @@ func TestAddJSONFlags(t *testing.T) { } } -func TestAddJSONFlagsNoDuplicateShorthand(t *testing.T) { +func TestAddJSONFlagsWithoutShorthand(t *testing.T) { tests := []struct { name string setFlags func(cmd *cobra.Command) @@ -129,23 +129,13 @@ func TestAddJSONFlagsNoDuplicateShorthand(t *testing.T) { name: "no conflicting flags", setFlags: func(cmd *cobra.Command) { cmd.Flags().StringP("web", "w", "", "") - }, - wantFlags: map[string]string{ - "web": "w", - "jq": "q", - "template": "t", - "json": "", - }, - }, - { - name: "conflicting flags", - setFlags: func(cmd *cobra.Command) { cmd.Flags().StringP("token", "t", "", "") }, wantFlags: map[string]string{ + "web": "w", "token": "t", - "jq": "q", - "template": "", // no shorthand + "jq": "", + "template": "", "json": "", }, }, @@ -156,7 +146,7 @@ func TestAddJSONFlagsNoDuplicateShorthand(t *testing.T) { cmd := &cobra.Command{Run: func(*cobra.Command, []string) {}} tt.setFlags(cmd) - AddJSONFlags(cmd, nil, []string{}) + AddJSONFlagsWithoutShorthand(cmd, nil, []string{}) for f, shorthand := range tt.wantFlags { flag := cmd.Flags().Lookup(f)