Skip to content

Commit

Permalink
define trueString constant
Browse files Browse the repository at this point in the history
  • Loading branch information
JunNishimura committed Jul 14, 2023
1 parent 46a2ba8 commit f3125ff
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ import (
flag "github.com/spf13/pflag"
)

const FlagSetByCobraAnnotation = "cobra_annotation_flag_set_by_cobra"
const (
FlagSetByCobraAnnotation = "cobra_annotation_flag_set_by_cobra"
trueString = "true"
)

// FParseErrWhitelist configures Flag parse errors to be ignored
type FParseErrWhitelist flag.ParseErrorsWhitelist
Expand Down Expand Up @@ -1109,7 +1112,7 @@ func (c *Command) ValidateRequiredFlags() error {
if !found {
return
}
if (requiredAnnotation[0] == "true") && !pflag.Changed {
if (requiredAnnotation[0] == trueString) && !pflag.Changed {
missingFlagNames = append(missingFlagNames, pflag.Name)
}
})
Expand Down Expand Up @@ -1146,7 +1149,7 @@ func (c *Command) InitDefaultHelpFlag() {
usage += c.Name()
}
c.Flags().BoolP("help", "h", false, usage)
_ = c.Flags().SetAnnotation("help", FlagSetByCobraAnnotation, []string{"true"})
_ = c.Flags().SetAnnotation("help", FlagSetByCobraAnnotation, []string{trueString})
}
}

Expand All @@ -1172,7 +1175,7 @@ func (c *Command) InitDefaultVersionFlag() {
} else {
c.Flags().Bool("version", false, usage)
}
_ = c.Flags().SetAnnotation("version", FlagSetByCobraAnnotation, []string{"true"})
_ = c.Flags().SetAnnotation("version", FlagSetByCobraAnnotation, []string{trueString})
}
}

Expand Down Expand Up @@ -1212,7 +1215,7 @@ Simply type ` + c.Name() + ` help [path to command] for full details.`,
PersistentPreRunE: func(cmd *Command, args []string) error {
cmd.Flags().VisitAll(func(pflag *flag.Flag) {
requiredAnnotation, found := pflag.Annotations[BashCompOneRequiredFlag]
if found && requiredAnnotation[0] == "true" {
if found && requiredAnnotation[0] == trueString {
// Disable any persistent required flags for the help command
pflag.Annotations[BashCompOneRequiredFlag] = []string{"false"}
}
Expand Down

0 comments on commit f3125ff

Please sign in to comment.