-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathusage.go
112 lines (101 loc) · 2.6 KB
/
usage.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright (C) 2016 Space Monkey, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package flagfile
import (
"flag"
"fmt"
"os"
"strings"
)
func formatFlag(f *flag.Flag) string {
// Two spaces before -; see next two comments.
s := fmt.Sprintf(" -%s", f.Name)
name, usage := flag.UnquoteUsage(f)
if len(name) > 0 {
s += " " + name
}
// Boolean flags of one ASCII letter are so common we
// treat them specially, putting their usage on the same line.
if len(s) <= 4 { // space, space, '-', 'x'.
s += "\t"
} else {
// Four spaces before the tab triggers good alignment
// for both 4- and 8-space tab stops.
s += "\n \t"
}
s += usage
switch f.DefValue {
case "0", "false", "":
default:
s += fmt.Sprintf(" (default %#v)", f.DefValue)
}
return s
}
func header() { fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0]) }
func isWithheld(name string) bool {
switch name {
case "flagfile", "flagout":
return true
default:
return false
}
}
// ShortUsage only outputs to stderr flags without "." and withholds some
// system flags.
func ShortUsage() {
header()
flag.VisitAll(func(f *flag.Flag) {
if strings.Contains(f.Name, ".") || isWithheld(f.Name) {
return
}
fmt.Fprint(os.Stderr, formatFlag(f), "\n")
})
fmt.Fprintln(os.Stderr)
fmt.Fprintln(os.Stderr, " -help-all")
fmt.Fprintln(os.Stderr, " \tShow all possible flags.")
}
// FullUsage outputs full usage information to stderr. All flags.
func FullUsage() {
header()
var withheld []string
flag.VisitAll(func(f *flag.Flag) {
if strings.Contains(f.Name, ".") {
return
}
if isWithheld(f.Name) {
withheld = append(withheld, formatFlag(f))
return
}
fmt.Fprint(os.Stderr, formatFlag(f), "\n")
})
if len(withheld) > 0 {
fmt.Fprintln(os.Stderr)
}
for _, msg := range withheld {
fmt.Fprint(os.Stderr, msg, "\n")
}
current_section := ""
flag.VisitAll(func(f *flag.Flag) {
pos := strings.LastIndex(f.Name, ".")
if pos == -1 {
return
}
section := f.Name[:pos]
if section != current_section {
current_section = section
fmt.Fprintln(os.Stderr)
}
fmt.Fprint(os.Stderr, formatFlag(f), "\n")
})
}