refactor(flags): accept plural flag names through one normalize function

Every command accepts a flag's plural, such as --add-labels for
--add-label (D8), instead of per-command alias maps.

Signed-off-by: bircni <bircni@icloud.com>
This commit is contained in:
bircni committed 2026-10-12 18:44:29 +02:00
1 parent dc24231ebc
commit f2f1dd1e34
3 files changed
+19 -8

No files matched your search

+18 -3
View File
@@ -214,19 +214,34 @@ var rootGroups = map[string]string{
// Commands add other gh verbs, such as new, co, add and get, to their own Aliases.
var verbAliases = map[string]string{"list": "ls", "delete": "rm"}
// addVerbAliases gives every list and delete command its short name.
// addVerbAliases gives every list and delete command its short name, and lets its flags be named
// in the plural.
func addVerbAliases(cmd *cobra.Command) {
if alias, ok := verbAliases[cmd.Name()]; ok && cmd.HasParent() && !slices.Contains(cmd.Aliases, alias) {
cmd.Aliases = append(cmd.Aliases, alias)
}
acceptPlurals(cmd)
for _, child := range cmd.Commands() {
addVerbAliases(child)
}
}
// acceptPlurals lets the flags of cmd be named in the plural, such as --add-labels for the gh name
// --add-label (D8), without showing the plural in help.
func acceptPlurals(cmd *cobra.Command) {
names := map[string]bool{}
cmd.Flags().VisitAll(func(flag *pflag.Flag) { names[flag.Name] = true })
normalize := cmd.Flags().GetNormalizeFunc()
cmd.Flags().SetNormalizeFunc(func(flags *pflag.FlagSet, name string) pflag.NormalizedName {
if singular, ok := strings.CutSuffix(name, "s"); ok && !names[name] && names[singular] {
name = singular
}
return normalize(flags, name)
})
}
// flagAliases makes the flags of cmd accept each key of aliases as another name of the flag it maps
// to, without showing it in help. Renamed flags keep their old names working this way, such as the
// plural --add-labels after the singular gh name --add-label became primary (D8).
// to, without showing it in help. Renamed flags keep their old names working this way.
func flagAliases(cmd *cobra.Command, aliases map[string]string) {
normalize := cmd.Flags().GetNormalizeFunc()
cmd.Flags().SetNormalizeFunc(func(flags *pflag.FlagSet, name string) pflag.NormalizedName {
-1
View File
@@ -537,7 +537,6 @@ func (f *metadataFlags) register(cmd *cobra.Command) {
flags.StringSliceVar(&f.addAssignees, "add-assignee", nil, "Add assignees by `login`; @me for yourself")
flags.StringSliceVar(&f.removeAssignees, "remove-assignee", nil, "Remove assignees by `login`; @me for yourself")
flags.StringSliceVar(&f.setAssignees, "set-assignees", nil, "Replace all assignees; an empty value clears them")
flagAliases(cmd, map[string]string{"add-labels": "add-label", "remove-labels": "remove-label", "add-assignees": "add-assignee", "remove-assignees": "remove-assignee"})
}
// completeMetadataFlags completes the label and assignee flags of metadataFlags.
+1 -4
View File
@@ -143,10 +143,7 @@ pull request, or mark it as a draft or ready for review.`, Annotations: pullArgu
cmd.Flags().StringSliceVar(&reviewers.Remove.Users, "remove-reviewer", nil, "Withdraw review requests from users")
cmd.Flags().StringSliceVar(&reviewers.Add.Teams, "add-reviewer-team", nil, "Request reviews from teams of the repository's organization")
cmd.Flags().StringSliceVar(&reviewers.Remove.Teams, "remove-reviewer-team", nil, "Withdraw review requests from teams")
flagAliases(cmd, map[string]string{
"milestone-name": "milestone", "add-reviewers": "add-reviewer", "remove-reviewers": "remove-reviewer",
"add-reviewer-teams": "add-reviewer-team", "remove-reviewer-teams": "remove-reviewer-team",
})
flagAliases(cmd, map[string]string{"milestone-name": "milestone"})
for _, name := range []string{"add-reviewer", "remove-reviewer"} {
_ = cmd.RegisterFlagCompletionFunc(name, a.completeUsers())
}