-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
66 lines (59 loc) · 1.06 KB
/
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
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
)
type Options struct {
Dir string
File string
Out string
Overwrite bool
Help bool
}
func (opt *Options) ValidateFile() {
ext := filepath.Ext(opt.File)
if ext != ".smc" {
fmt.Println("Error: Not a .smc file.")
os.Exit(0)
}
}
func (opt *Options) UpdateOut() {
if isFlagPassed("out") {
return
}
if isFlagPassed("file") {
opt.Out = filepath.Dir(opt.File)
} else {
opt.Out = opt.Dir
}
}
func (opt *Options) ValidateFlags() {
if opt.Help {
flag.Usage()
os.Exit(0)
}
if !isFlagPassed("dir") && !isFlagPassed("file") {
fmt.Println("Error: No target to clean. Specify a directory or file.")
flag.Usage()
os.Exit(0)
}
if isFlagPassed("dir") && isFlagPassed("file") {
fmt.Println("Error: Specify a dir OR a file. Can't do both.")
flag.Usage()
os.Exit(0)
}
}
func isFlagPassed(name string) bool {
found := false
flag.Visit(func(f *flag.Flag) {
if f.Name == name {
found = true
}
})
return found
}
func (opt *Options) Count() int {
return flag.NFlag()
}