-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathgcli.go
49 lines (45 loc) · 1.21 KB
/
gcli.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
package gcli
import (
"github.com/urfave/cli/v2"
"github.com/urfave/sflags"
)
// GenerateTo takes a list of sflag.Flag,
// that are parsed from some config structure, and put it to dst.
func GenerateTo(src []*sflags.Flag, dst *[]cli.Flag) {
for _, srcFlag := range src {
name := srcFlag.Name
var aliases []string
if srcFlag.Short != "" {
aliases = append(aliases, srcFlag.Short)
}
*dst = append(*dst, &cli.GenericFlag{
Name: name,
EnvVars: srcFlag.EnvNames,
Aliases: aliases,
Hidden: srcFlag.Hidden,
Usage: srcFlag.Usage,
Value: srcFlag.Value,
Required: srcFlag.Required,
})
}
}
// ParseTo parses cfg, that is a pointer to some structure,
// and puts it to dst.
func ParseTo(cfg interface{}, dst *[]cli.Flag, optFuncs ...sflags.OptFunc) error {
flags, err := sflags.ParseStruct(cfg, optFuncs...)
if err != nil {
return err
}
GenerateTo(flags, dst)
return nil
}
// Parse parses cfg, that is a pointer to some structure,
// puts it to the new flag.FlagSet and returns it.
func Parse(cfg interface{}, optFuncs ...sflags.OptFunc) ([]cli.Flag, error) {
flags := make([]cli.Flag, 0)
err := ParseTo(cfg, &flags, optFuncs...)
if err != nil {
return nil, err
}
return flags, nil
}