-
Notifications
You must be signed in to change notification settings - Fork 16
/
option.go
89 lines (73 loc) · 1.51 KB
/
option.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
package exec_tool
import (
"fmt"
"strings"
"github.com/xhd2015/xgo/support/flag"
)
type options struct {
// by default all compiler's extra function
// are disabled, unless explicitly called with enable
enable bool
logCompile bool
debug string
debugWithDlv bool
testCompile bool // --test-compile
remainArgs []string
}
func parseOptions(args []string, stopAfterFirstArg bool) (*options, error) {
var enable bool
var verbose bool
var debug string
var testCompile bool
var debugWithDlv bool
var remainArgs []string
nArg := len(args)
for i := 0; i < nArg; i++ {
arg := args[i]
if !strings.HasPrefix(arg, "-") {
if stopAfterFirstArg {
remainArgs = append(remainArgs, args[i+1:]...)
break
} else {
remainArgs = append(remainArgs, arg)
continue
}
}
if arg == "--" {
remainArgs = append(remainArgs, args[i+1:]...)
break
}
if arg == "--enable" {
enable = true
continue
}
if arg == "--log-compile" {
verbose = true
continue
}
if arg == "--test-compile" {
testCompile = true
continue
}
if arg == "--debug-with-dlv" {
debugWithDlv = true
continue
}
ok, err := flag.TryParseFlagsValue([]string{"--debug"}, &debug, nil, &i, args)
if err != nil {
return nil, err
}
if ok {
continue
}
return nil, fmt.Errorf("unrecognized flag:%s", arg)
}
return &options{
enable: enable,
logCompile: verbose,
debug: debug,
testCompile: testCompile,
debugWithDlv: debugWithDlv,
remainArgs: remainArgs,
}, nil
}