forked from cornfeedhobo/pflag
-
Notifications
You must be signed in to change notification settings - Fork 1
/
func.go
45 lines (35 loc) · 1.39 KB
/
func.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
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package zflag
// -- int Value
type funcValue func(string) error
var _ Value = (*funcValue)(nil)
var _ Typed = (*funcValue)(nil)
func newFuncValue(fn func(string) error) *funcValue {
funcVal := funcValue(fn)
return &funcVal
}
func (i *funcValue) Set(val string) error {
return (*i)(val)
}
func (i *funcValue) Type() string {
return "string"
}
func (i *funcValue) String() string { return "" }
// Func defines a flag with specified name, and usage string.
// Each time the flag is seen, fn is called with the value of the flag.
// If fn returns a non-nil error, it will be treated as a flag value parsing error.
func (fs *FlagSet) Func(name string, usage string, fn func(string) error, opts ...Opt) {
fs.Var(newFuncValue(fn), name, usage, opts...)
}
// Func defines a flag with specified name, and usage string.
// Each time the flag is seen, fn is called with the value of the flag.
// If fn returns a non-nil error, it will be treated as a flag value parsing error.
func Func(name string, usage string, fn func(string) error, opts ...Opt) {
CommandLine.Func(name, usage, fn, opts...)
}
// These are not needed for this specific type, and they are added here to stop validate_funcs.sh from fail.
// func (f *FlagSet) GetFunc(
// func (f *FlagSet) MustGetFunc(
// func (f *FlagSet) FuncVar(
// func FuncVar(