summaryrefslogtreecommitdiff
path: root/compflag/compflag.go
blob: fb1e852537a536c5b042617eb53b23cb8fc16e95 (plain)
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// Package compflag provides a handful of standard library-compatible flags with bash complition capabilities.
//
// Usage
//
// 	import "github.com/posener/complete/v2/compflag"
//
// 	var (
// 		// Define flags...
// 		foo = compflag.String("foo", "", "")
// 	)
//
// 	func main() {
// 		compflag.Parse()
// 		// Main function.
// 	}
//
// Alternatively, the library can just be used with the standard library flag package:
//
// 	import (
// 		"flag"
// 		"github.com/posener/complete/v2/compflag"
// 	)
//
// 	var (
// 		// Define flags...
// 		foo = compflag.String("foo", "", "")
// 		bar = flag.String("bar", "", "")
// 	)
//
// 	func main() {
// 		complete.CommandLine()
// 		flag.Parse()
// 		// Main function.
// 	}
package compflag

import (
	"flag"
	"fmt"
	"os"
	"strconv"
	"time"

	"github.com/posener/complete/v2"
	"github.com/posener/complete/v2/predict"
)

// FlagSet is bash completion enabled flag.FlagSet.
type FlagSet flag.FlagSet

// Parse parses command line arguments.
func (fs *FlagSet) Parse(args []string) error {
	return (*flag.FlagSet)(fs).Parse(args)
}

func (fs *FlagSet) Visit(fn func(*flag.Flag))     { (*flag.FlagSet)(fs).Visit(fn) }
func (fs *FlagSet) VisitAll(fn func(*flag.Flag))  { (*flag.FlagSet)(fs).VisitAll(fn) }
func (fs *FlagSet) Arg(i int) string              { return (*flag.FlagSet)(fs).Arg(i) }
func (fs *FlagSet) Args() []string                { return (*flag.FlagSet)(fs).Args() }
func (fs *FlagSet) NArg() int                     { return (*flag.FlagSet)(fs).NArg() }
func (fs *FlagSet) NFlag() int                    { return (*flag.FlagSet)(fs).NFlag() }
func (fs *FlagSet) Name() string                  { return (*flag.FlagSet)(fs).Name() }
func (fs *FlagSet) PrintDefaults()                { (*flag.FlagSet)(fs).PrintDefaults() }
func (fs *FlagSet) Lookup(name string) *flag.Flag { return (*flag.FlagSet)(fs).Lookup(name) }
func (fs *FlagSet) Parsed() bool                  { return (*flag.FlagSet)(fs).Parsed() }

// Complete performs bash completion if needed.
func (fs *FlagSet) Complete() {
	complete.Complete(fs.Name(), complete.FlagSet((*flag.FlagSet)(CommandLine)))
}

func (fs *FlagSet) String(name string, value string, usage string, options ...predict.Option) *string {
	p := new(string)
	(*flag.FlagSet)(fs).Var(newStringValue(value, p, predict.Options(options...)), name, usage)
	return p
}

func (fs *FlagSet) Bool(name string, value bool, usage string, options ...predict.Option) *bool {
	p := new(bool)
	(*flag.FlagSet)(fs).Var(newBoolValue(value, p, predict.Options(options...)), name, usage)
	return p
}

func (fs *FlagSet) Int(name string, value int, usage string, options ...predict.Option) *int {
	p := new(int)
	(*flag.FlagSet)(fs).Var(newIntValue(value, p, predict.Options(options...)), name, usage)
	return p
}

func (fs *FlagSet) Duration(name string, value time.Duration, usage string, options ...predict.Option) *time.Duration {
	p := new(time.Duration)
	(*flag.FlagSet)(fs).Var(newDurationValue(value, p, predict.Options(options...)), name, usage)
	return p
}

var CommandLine = (*FlagSet)(flag.CommandLine)

// Parse parses command line arguments. It also performs bash completion when needed.
func Parse() {
	CommandLine.Complete()
	CommandLine.Parse(os.Args[1:])
}

func String(name string, value string, usage string, options ...predict.Option) *string {
	return CommandLine.String(name, value, usage, options...)
}

func Bool(name string, value bool, usage string, options ...predict.Option) *bool {
	return CommandLine.Bool(name, value, usage, options...)
}

func Int(name string, value int, usage string, options ...predict.Option) *int {
	return CommandLine.Int(name, value, usage, options...)
}

func Duration(name string, value time.Duration, usage string, options ...predict.Option) *time.Duration {
	return CommandLine.Duration(name, value, usage, options...)
}

type boolValue struct {
	v *bool
	predict.Config
}

func newBoolValue(val bool, p *bool, c predict.Config) *boolValue {
	*p = val
	return &boolValue{v: p, Config: c}
}

func (b *boolValue) Set(val string) error {
	v, err := strconv.ParseBool(val)
	*b.v = v
	if err != nil {
		return fmt.Errorf("bad value for bool flag")
	}
	return b.Check(val)
}

func (b *boolValue) Get() interface{} { return *b.v }

func (b *boolValue) String() string {
	if b == nil || b.v == nil {
		return strconv.FormatBool(false)
	}
	return strconv.FormatBool(*b.v)
}

func (b *boolValue) IsBoolFlag() bool { return true }

func (b *boolValue) Predict(prefix string) []string {
	if b.Predictor != nil {
		return b.Predictor.Predict(prefix)
	}
	// If false, typing the bool flag is expected to turn it on, so there is nothing to complete
	// after the flag.
	if !*b.v {
		return nil
	}
	// Otherwise, suggest only to turn it off.
	return []string{"false"}
}

type stringValue struct {
	v *string
	predict.Config
}

func newStringValue(val string, p *string, c predict.Config) *stringValue {
	*p = val
	return &stringValue{v: p, Config: c}
}

func (s *stringValue) Set(val string) error {
	*s.v = val
	return s.Check(val)
}

func (s *stringValue) Get() interface{} {
	return *s.v
}

func (s *stringValue) String() string {
	if s == nil || s.v == nil {
		return ""
	}
	return *s.v
}

func (s *stringValue) Predict(prefix string) []string {
	if s.Predictor != nil {
		return s.Predictor.Predict(prefix)
	}
	return []string{""}
}

type intValue struct {
	v *int
	predict.Config
}

func newIntValue(val int, p *int, c predict.Config) *intValue {
	*p = val
	return &intValue{v: p, Config: c}
}

func (i *intValue) Set(val string) error {
	v, err := strconv.ParseInt(val, 0, strconv.IntSize)
	*i.v = int(v)
	if err != nil {
		return fmt.Errorf("bad value for int flag")
	}
	return i.Check(val)
}

func (i *intValue) Get() interface{} { return *i.v }

func (i *intValue) String() string {
	if i == nil || i.v == nil {
		return strconv.Itoa(0)
	}
	return strconv.Itoa(*i.v)
}

func (s *intValue) Predict(prefix string) []string {
	if s.Predictor != nil {
		return s.Predictor.Predict(prefix)
	}
	return []string{""}
}

type durationValue struct {
	v *time.Duration
	predict.Config
}

func newDurationValue(val time.Duration, p *time.Duration, c predict.Config) *durationValue {
	*p = val
	return &durationValue{v: p, Config: c}
}

func (i *durationValue) Set(val string) error {
	v, err := time.ParseDuration(val)
	*i.v = v
	if err != nil {
		return fmt.Errorf("bad value for duration flag")
	}
	return i.Check(val)
}

func (i *durationValue) Get() interface{} { return *i.v }

func (i *durationValue) String() string {
	if i == nil || i.v == nil {
		return time.Duration(0).String()
	}
	return i.v.String()
}

func (s *durationValue) Predict(prefix string) []string {
	if s.Predictor != nil {
		return s.Predictor.Predict(prefix)
	}
	return []string{""}
}