-
Notifications
You must be signed in to change notification settings - Fork 15
/
newflag.go
45 lines (39 loc) · 869 Bytes
/
newflag.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
package main
import (
"flag"
"fmt"
"os"
"runtime"
"strings"
)
func newUsage() {
w := flag.CommandLine.Output()
fmt.Fprintf(w, "Expect-lua %s-windows-%s with %s\n",
version, runtime.GOARCH, runtime.Version())
fmt.Fprintf(w, "Usage of %s:\n", os.Args[0])
newPrintDefaults(flag.CommandLine)
}
func newPrintDefaults(fs *flag.FlagSet) {
fs.VisitAll(func(f *flag.Flag) {
if f.Usage == "" {
return // for obsolute flag
}
var b strings.Builder
fmt.Fprintf(&b, " -%s", f.Name)
var detail string
var usage string
var ok bool
if detail, usage, ok = strings.Cut(f.Usage, "\v"); ok {
fmt.Fprintf(&b, " %s", detail)
} else {
usage = f.Usage
}
if b.Len() <= 4 {
b.WriteByte('\t')
} else {
b.WriteString("\n \t")
}
b.WriteString(strings.ReplaceAll(usage, "\n", "\n \t"))
fmt.Fprintln(fs.Output(), b.String())
})
}