-
-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathcliv2.go
42 lines (36 loc) · 1002 Bytes
/
cliv2.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
package ecspresso
import (
"fmt"
"strings"
"github.com/alecthomas/kong"
"github.com/fatih/color"
)
func ParseCLIv2(args []string) (string, *CLIOptions, func(), error) {
// compatible with v1
if len(args) == 0 || len(args) > 0 && args[0] == "help" {
args = []string{"--help"}
}
var opts CLIOptions
parser, err := kong.New(&opts, kong.Vars{"version": Version})
if err != nil {
return "", nil, nil, fmt.Errorf("failed to new kong: %w", err)
}
c, err := parser.Parse(args)
if err != nil {
return "", nil, nil, fmt.Errorf("failed to parse args: %w", err)
}
sub := strings.Fields(c.Command())[0]
for _, envFile := range opts.Envfile {
if err := ExportEnvFile(envFile); err != nil {
return sub, &opts, nil, fmt.Errorf("failed to load envfile: %w", err)
}
}
if opts.ExtStr == nil {
opts.ExtStr = map[string]string{}
}
if opts.ExtCode == nil {
opts.ExtCode = map[string]string{}
}
color.NoColor = !opts.Color
return sub, &opts, func() { c.PrintUsage(true) }, nil
}