forked from cornfeedhobo/pflag
-
Notifications
You must be signed in to change notification settings - Fork 1
/
formatter.go
56 lines (48 loc) · 1.5 KB
/
formatter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package zflag
import (
"fmt"
)
// FlagUsageFormatter is a function type that prints the usage for a single Flag.
// This should be returning two strings, one that is considered the "left" hand side,
// and one that is considered the "right" hand side.
// Once the left and right are determined for all flags, the length of the text is
// determined, and each is appropriated cut based the terminal's width, and some space
// is added between left and right.
type FlagUsageFormatter func(*Flag) (string, string)
func defaultUsageFormatter(flag *Flag) (string, string) {
left := " "
if flag.Shorthand != 0 && flag.ShorthandDeprecated == "" {
left += fmt.Sprintf("-%c", flag.Shorthand)
if !flag.ShorthandOnly {
left += ", "
}
} else {
left += " "
}
left += "--"
if _, isBoolFlag := flag.Value.(BoolFlag); isBoolFlag && flag.AddNegative {
left += "[no-]"
}
left += flag.Name
varname, usage := UnquoteUsage(flag)
if varname != "" {
left += " " + varname
}
right := usage
if flag.Required {
right += " (required)"
}
if !flag.DisablePrintDefault && !flag.DefaultIsZeroValue() {
if v, ok := flag.Value.(Typed); ok && v.Type() == "string" {
right += fmt.Sprintf(" (default %q)", flag.DefValue)
} else {
right += fmt.Sprintf(" (default %s)", flag.DefValue)
}
}
if len(flag.Deprecated) != 0 {
right += fmt.Sprintf(" (DEPRECATED: %s)", flag.Deprecated)
}
return left, right
}