forked from coaidev/coai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.go
71 lines (59 loc) · 1.48 KB
/
parser.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
package cli
import (
"fmt"
"log"
"os"
"strconv"
)
func GetArgs() []string {
return os.Args[1:]
}
func GetArg(args []string, idx int) string {
if len(args) <= idx {
log.Fatalln(fmt.Sprintf("not enough arguments: %d", idx))
}
return args[idx]
}
func GetArgInt(args []string, idx int) int {
i, err := strconv.Atoi(GetArg(args, idx))
if err != nil {
log.Fatalln(fmt.Sprintf("invalid argument: %s", err.Error()))
}
return i
}
func GetArgFloat(args []string, idx int, bitSize int) float64 {
f, err := strconv.ParseFloat(GetArg(args, idx), bitSize)
if err != nil {
log.Fatalln(fmt.Sprintf("invalid argument: %s", err.Error()))
}
return f
}
func GetArgFloat32(args []string, idx int) float32 {
return float32(GetArgFloat(args, idx, 32))
}
func GetArgFloat64(args []string, idx int) float64 {
return GetArgFloat(args, idx, 64)
}
func GetArgBool(args []string, idx int) bool {
b, err := strconv.ParseBool(GetArg(args, idx))
if err != nil {
log.Fatalln(fmt.Sprintf("invalid argument: %s", err.Error()))
}
return b
}
func GetArgInt64(args []string, idx int) int64 {
i, err := strconv.ParseInt(GetArg(args, idx), 10, 64)
if err != nil {
log.Fatalln(fmt.Sprintf("invalid argument: %s", err.Error()))
}
return i
}
func GetArgString(args []string, idx int) string {
return GetArg(args, idx)
}
func outputError(err error) {
fmt.Println(fmt.Sprintf("\033[31m[cli] error: %s\033[0m", err.Error()))
}
func outputInfo(t, msg string) {
fmt.Println(fmt.Sprintf("[cli] %s: %s", t, msg))
}