forked from uchiiii/csv2md
-
Notifications
You must be signed in to change notification settings - Fork 0
/
args.go
61 lines (51 loc) · 1.02 KB
/
args.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
package main
import (
"fmt"
)
const (
ERROR_NO_FILENAME = "Error: at least one file name is required."
)
type Args struct {
Files []string
Delim string
Pad int
}
var Delims = []string{",", ";"}
func (args *Args) ValidateAll() error {
if err := args.validateFiles(); err != nil {
return err
}
if err := args.validateDelim(); err != nil {
return err
}
if err := args.validatePad(); err != nil {
return err
}
return nil
}
func (args *Args) validateFiles() error {
if len(args.Files) < 1 {
return fmt.Errorf(ERROR_NO_FILENAME)
}
return nil
}
func (args *Args) validateDelim() error {
if !contains(Delims, args.Delim) {
return fmt.Errorf("Error: delimiter should be one of %+q, but got %q", Delims, args.Delim)
}
return nil
}
func (args *Args) validatePad() error {
if args.Pad < 0 {
return fmt.Errorf("Error: padding should be positive integer, but got %d", args.Pad)
}
return nil
}
func contains(s []string, a string) bool {
for _, v := range s {
if v == a {
return true
}
}
return false
}