MCPcopy
hub / github.com/spf13/pflag / FlagUsagesWrapped

Method FlagUsagesWrapped

flag.go:707–776  ·  view source on GitHub ↗

FlagUsagesWrapped returns a string containing the usage information for all flags in the FlagSet. Wrapped to `cols` columns (0 for no wrapping)

(cols int)

Source from the content-addressed store, hash-verified

705// for all flags in the FlagSet. Wrapped to `cols` columns (0 for no
706// wrapping)
707func (f *FlagSet) FlagUsagesWrapped(cols int) string {
708 buf := new(bytes.Buffer)
709
710 lines := make([]string, 0, len(f.formal))
711
712 maxlen := 0
713 f.VisitAll(func(flag *Flag) {
714 if flag.Hidden {
715 return
716 }
717
718 line := ""
719 if flag.Shorthand != "" && flag.ShorthandDeprecated == "" {
720 line = fmt.Sprintf(" -%s, --%s", flag.Shorthand, flag.Name)
721 } else {
722 line = fmt.Sprintf(" --%s", flag.Name)
723 }
724
725 varname, usage := UnquoteUsage(flag)
726 if varname != "" {
727 line += " " + varname
728 }
729 if flag.NoOptDefVal != "" {
730 switch flag.Value.Type() {
731 case "string":
732 line += fmt.Sprintf("[=\"%s\"]", flag.NoOptDefVal)
733 case "bool", "boolfunc":
734 if flag.NoOptDefVal != "true" {
735 line += fmt.Sprintf("[=%s]", flag.NoOptDefVal)
736 }
737 case "count":
738 if flag.NoOptDefVal != "+1" {
739 line += fmt.Sprintf("[=%s]", flag.NoOptDefVal)
740 }
741 default:
742 line += fmt.Sprintf("[=%s]", flag.NoOptDefVal)
743 }
744 }
745
746 // This special character will be replaced with spacing once the
747 // correct alignment is calculated
748 line += "\x00"
749 if len(line) > maxlen {
750 maxlen = len(line)
751 }
752
753 line += usage
754 if !flag.defaultIsZeroValue() {
755 if flag.Value.Type() == "string" {
756 line += fmt.Sprintf(" (default %q)", flag.DefValue)
757 } else {
758 line += fmt.Sprintf(" (default %s)", flag.DefValue)
759 }
760 }
761 if len(flag.Deprecated) != 0 {
762 line += fmt.Sprintf(" (DEPRECATED: %s)", flag.Deprecated)
763 }
764

Callers 4

TestFuncUsageFunction · 0.95
TestBoolFuncUsageFunction · 0.95
FlagUsagesMethod · 0.95
TestPrintUsage_2Function · 0.80

Calls 6

VisitAllMethod · 0.95
UnquoteUsageFunction · 0.85
wrapFunction · 0.85
defaultIsZeroValueMethod · 0.80
TypeMethod · 0.65
StringMethod · 0.65

Tested by 3

TestFuncUsageFunction · 0.76
TestBoolFuncUsageFunction · 0.76
TestPrintUsage_2Function · 0.64