func manPrintFlags(out *bytes.Buffer, flags *pflag.FlagSet) { flags.VisitAll(func(flag *pflag.Flag) { if len(flag.Deprecated) > 0 || flag.Hidden { return } format := "" if len(flag.Shorthand) > 0 { format = "**-%s**, **--%s**" } else { format = "%s**--%s**" } if len(flag.NoOptDefVal) > 0 { format = format + "[" } if flag.Value.Type() == "string" { // put quotes on the value format = format + "=%q" } else { format = format + "=%s" } if len(flag.NoOptDefVal) > 0 { format = format + "]" } format = format + "\n\t%s\n\n" fmt.Fprintf(out, format, flag.Shorthand, flag.Name, flag.DefValue, flag.Usage) }) }
// Test if the named flag is a boolean flag. func isBooleanFlag(name string, f *flag.FlagSet) bool { flag := f.Lookup(name) if flag == nil { return false } return flag.Value.Type() == "bool" }
// Test if the named flag is a boolean flag. func isBooleanShortFlag(name string, f *flag.FlagSet) bool { result := false f.VisitAll(func(f *flag.Flag) { if f.Shorthand == name && f.Value.Type() == "bool" { result = true } }) return result }
// BindBoolFlag binds the flag based on the provided info. If LongName == "", nothing is registered func (f FlagInfo) BindBoolFlag(flags *pflag.FlagSet, target *bool) { // you can't register a flag without a long name if len(f.LongName) > 0 { // try to parse Default as a bool. If it fails, assume false boolVal, err := strconv.ParseBool(f.Default) if err != nil { boolVal = false } flags.BoolVarP(target, f.LongName, f.ShortName, boolVal, f.Description) } }
// MarkFlagFilename adds the BashCompFilenameExt annotation to the named flag in the flag set, if it exists. // Generated bash autocompletion will select filenames for the flag, limiting to named extensions if provided. func MarkFlagFilename(flags *pflag.FlagSet, name string, extensions ...string) error { return flags.SetAnnotation(name, BashCompFilenameExt, extensions) }
// MarkFlagRequired adds the BashCompOneRequiredFlag annotation to the named flag in the flag set, if it exists. func MarkFlagRequired(flags *pflag.FlagSet, name string) error { return flags.SetAnnotation(name, BashCompOneRequiredFlag, []string{"true"}) }
// BindStringFlag binds the flag based on the provided info. If LongName == "", nothing is registered func (f FlagInfo) BindStringFlag(flags *pflag.FlagSet, target *string) { // you can't register a flag without a long name if len(f.LongName) > 0 { flags.StringVarP(target, f.LongName, f.ShortName, f.Default, f.Description) } }