From f1fdd55b38f30ad0b8cabf74ce032bc7ca24d3fe Mon Sep 17 00:00:00 2001 From: anqiansong Date: Mon, 23 May 2022 09:13:12 +0800 Subject: [PATCH] Support built-in shorthand flags (#1925) --- tools/goctl/cmd/root.go | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/tools/goctl/cmd/root.go b/tools/goctl/cmd/root.go index d2d8717c..3de981e7 100644 --- a/tools/goctl/cmd/root.go +++ b/tools/goctl/cmd/root.go @@ -69,15 +69,18 @@ func supportGoStdFlag(args []string) []string { flagValue = flagExpr[assignIndex:] } - f := parentCmd.Flag(flagName) - if f == nil { - continue - } - if f.Shorthand == flagName { - continue + if !isBuiltin(flagName) { + // The method Flag can only match the user custom flags. + f := parentCmd.Flag(flagName) + if f == nil { + continue + } + if f.Shorthand == flagName { + continue + } } - goStyleFlag := doubleDash + f.Name + goStyleFlag := doubleDash + flagName if assignIndex > 0 { goStyleFlag += flagValue } @@ -87,8 +90,13 @@ func supportGoStdFlag(args []string) []string { return copyArgs } +func isBuiltin(name string) bool { + return name == "version" || name == "help" +} + func init() { - rootCmd.Version = fmt.Sprintf("%s %s/%s", version.BuildVersion, + rootCmd.Version = fmt.Sprintf( + "%s %s/%s", version.BuildVersion, runtime.GOOS, runtime.GOARCH) rootCmd.AddCommand(api.Cmd) rootCmd.AddCommand(bug.Cmd)