-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
53 lines (43 loc) · 917 Bytes
/
options.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
package main
import (
"errors"
"log"
"strings"
)
type Options struct {
Package string
TemplatePath string
}
func ParseOptions(optionString *string) (Options, error) {
o := Options{}
if optionString == nil {
return o, nil
}
var err error
options := strings.Split(*optionString, ",")
for _, option := range options {
o, err = setOption(option, o)
if err != nil {
return o, err
}
}
if o.TemplatePath == "" {
return o, errors.New("path to template must be set")
}
return o, nil
}
func setOption(optionString string, o Options) (Options, error) {
parts := strings.Split(optionString, "=")
if len(parts) == 1 || parts[1] == "" {
return o, errors.New("Option value must be set")
}
switch parts[0] {
case "package":
o.Package = parts[1]
case "template":
o.TemplatePath = parts[1]
default:
log.Printf("Unknown option: %s=%s\n", parts[0], parts[1])
}
return o, nil
}