/
cli.go
163 lines (138 loc) · 3.83 KB
/
cli.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright (c) 2017 Qian Qiao
//
// 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 cli provides base constructs to build complex command line
// applications.
//
// By nesting components, one can build command line applications like: the
// go command.
//
package cli // import "github.com/qqiao/cli"
import (
"bytes"
"context"
"flag"
"io"
"strings"
"text/template"
)
// Component represents a command line component
type Component struct {
// Components are the sub-components of the current component
Components []*Component
// Run runs the component
// args are the arguments after the component name
Run func(ctx context.Context, comp *Component, args []string)
// UsageLine is the one-line usage message.
// The first word in the line is taken to be the component name
UsageLine string
// Short is the short description of the component
Short string
// Long is the longer more detailed description of the component
Long string
// flagSet is a set of flags specific to this component
flagSet *flag.FlagSet
}
// FlagSet returns the set of command line flags
func (c *Component) FlagSet() *flag.FlagSet {
if nil == c.flagSet {
c.flagSet = flag.NewFlagSet(c.Name(), flag.ExitOnError)
c.flagSet.Usage = c.Usage
}
return c.flagSet
}
// Name returns the name of the component: the first word in the UsageLine
func (c *Component) Name() string {
name := c.UsageLine
i := strings.Index(name, " ")
if i >= 0 {
name = name[:i]
}
return name
}
// Runnable returns whether this component is runnable or pure informational
func (c *Component) Runnable() bool {
return nil != c.Run
}
// SetOutput sets the destination for usage messages.
// If output is nil, stderr is used
func (c *Component) SetOutput(output io.Writer) {
c.FlagSet().SetOutput(output)
for _, c := range c.Components {
c.SetOutput(output)
}
}
var usageTemplate = `
{{- if .component.Runnable -}}
Usage: {{.component.UsageLine}}
{{end}}
{{- if ne (len .component.Long) 0 -}}
{{.component.Long | trim}}
{{end}}
{{- if ne (len .component.Components) 0}}
The components are:
{{- range .component.Components}}
{{- if .Runnable}}
{{.Name | printf "%-11s"}} {{.Short -}}
{{end -}}
{{end}}
{{end}}
{{- if ne (len .flags) 0}}
The flags are:
{{.flags -}}
{{end}}`
// Usage prints out the usage information
func (c *Component) Usage() {
flagSet := c.FlagSet()
output := flagSet.Output()
// Capture the output of the flagset so that it can be merged with the rest
// of the message
var buf bytes.Buffer
flagSet.SetOutput(&buf)
flagSet.PrintDefaults()
flagSet.SetOutput(output)
tmpl(output, usageTemplate, map[string]interface{}{
"component": c,
"flags": buf.String(),
})
}
// Passthrough is a implementation of the Run function that passes the
// execution through the sub commands
func Passthrough(ctx context.Context, comp *Component, args []string) {
flagSet := comp.FlagSet()
if flag.ErrHelp == flagSet.Parse(args) {
return
}
if flagSet.NArg() < 1 {
flagSet.Usage()
return
}
name := flagSet.Arg(0)
for _, c := range comp.Components {
if name == c.Name() {
if c.Runnable() {
c.Run(ctx, c, flagSet.Args()[1:])
return
}
}
}
flagSet.Usage()
}
func tmpl(w io.Writer, text string, data interface{}) {
t := template.New("top")
t.Funcs(template.FuncMap{
"trim": strings.TrimSpace,
})
template.Must(t.Parse(text))
t.Execute(w, data)
}