-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathflags.go
109 lines (98 loc) · 2.1 KB
/
flags.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
package main
import (
"bytes"
"flag"
"fmt"
"go/doc"
"io"
"io/ioutil"
"os"
"strings"
)
func IsHelp(err error) bool {
return err == flag.ErrHelp
}
type CmdFlags struct {
*flag.FlagSet
docopt DocOpt
w io.Writer
name string
about []string
docs []string
args []string
argdocs [][]string
argsets [][]string
}
func Flags(name string, args []string) *CmdFlags {
set := flag.NewFlagSet(name, flag.ContinueOnError)
f := new(CmdFlags)
f.FlagSet = set
f.name = name
f.args = args
f.docopt.Width = 76
f.docopt.Indent = " "
f.docopt.PreIndent = "\t"
f.SetOutput(os.Stderr)
set.Usage = f.help
return f
}
// About provides a high level summary of the command.
func (f *CmdFlags) About(docs ...string) {
f.about = docs
}
// Docs provides detailed documentation of the command's behavior.
func (f *CmdFlags) Docs(docs ...string) {
f.docs = docs
}
func (f *CmdFlags) ArgSet(args ...string) {
f.argsets = append(f.argsets, args)
}
func (f *CmdFlags) ArgDoc(arg, help string) {
f.argdocs = append(f.argdocs, []string{arg, help})
}
func (f *CmdFlags) help() {
w := f.w
if w == nil {
w = ioutil.Discard
}
var buf bytes.Buffer
f.FlagSet.SetOutput(&buf)
if len(f.about) > 0 {
fmt.Fprintln(&buf, strings.Join(f.about, "\n"))
fmt.Fprintln(&buf)
}
fmt.Fprintln(&buf, "usage:")
if len(f.argsets) == 0 {
fmt.Fprintln(&buf, f.name)
} else {
sets := f.argsets
for _, set := range sets {
fmt.Fprintln(&buf, " "+f.name+" "+strings.Join(set, " "))
}
}
if len(f.argdocs) > 0 {
fmt.Fprintln(&buf, "arguments and flags:")
}
for _, argdoc := range f.argdocs {
if len(argdoc) == 0 {
panic("empty argdoc")
}
fmt.Fprintf(&buf, " %s: %s\n", argdoc[0], strings.Join(argdoc[1:], " "))
}
f.PrintDefaults()
if len(f.docs) > 0 {
fmt.Fprintln(&buf)
fmt.Fprintln(&buf, strings.Join(f.docs, "\n"))
}
doc.ToText(w, buf.String(), f.docopt.Indent, f.docopt.PreIndent, f.docopt.Width)
}
func (f *CmdFlags) SetOutput(w io.Writer) {
f.w = w
//f.FlagSet.SetOutput(w)
}
func (f *CmdFlags) Parse(args *[]string) (err error) {
if args == nil {
args = &f.args
}
return f.FlagSet.Parse(*args)
}