introduce AddJSONFlagsWithoutShorthand

This commit is contained in:
Benjamin Levesque 2025-08-31 17:56:26 +02:00
parent e1a1040f37
commit 45ecc5ece9
No known key found for this signature in database
GPG key ID: 765E3CB147AA4D4A
3 changed files with 38 additions and 28 deletions

View file

@ -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
}

View file

@ -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")

View file

@ -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)