-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.go
77 lines (63 loc) · 1.59 KB
/
cli.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
package main
import (
"flag"
"fmt"
"os"
)
type CmdArgs struct {
Level string
Style string
Args []string
}
var (
styles = map[string]DrawStyle{
"simple": DrawStyleSimple,
"big": DrawStyleBig,
}
)
func ParseArgs() (*CmdArgs, error) {
opts := CmdArgs{}
flag.Usage = flagHelpMessage
flag.StringVar(&opts.Level, "level", "normal", "slot difficulty. [easy|normal|hard]")
flag.StringVar(&opts.Style, "style", "simple", "style. [simple|big]")
flag.Parse()
opts.Args = flag.Args()
if err := opts.Validate(); err != nil {
return nil, err
}
return &opts, nil
}
func flagHelpMessage() {
cmd := os.Args[0]
fmt.Fprintln(os.Stderr, fmt.Sprintf("%s changes file permissions with a slot", cmd))
fmt.Fprintln(os.Stderr, "")
fmt.Fprintln(os.Stderr, "Usage:")
fmt.Fprintln(os.Stderr, fmt.Sprintf(" %s [OPTIONS] [files...]", cmd))
fmt.Fprintln(os.Stderr, "")
fmt.Fprintln(os.Stderr, "Examples:")
fmt.Fprintln(os.Stderr, fmt.Sprintf(" %s sample.txt", cmd))
fmt.Fprintln(os.Stderr, "")
fmt.Fprintln(os.Stderr, "Options:")
flag.PrintDefaults()
}
func (c *CmdArgs) Validate() error {
if len(c.Args) < 1 {
return fmt.Errorf("Must need files")
}
if _, ok := slotIntervalTime[c.Level]; !ok {
return fmt.Errorf("-level must be 'easy' or 'normal' or 'hard'.")
}
if _, ok := styles[c.Style]; !ok {
return fmt.Errorf("-style must be 'simple' or 'big'.")
}
for _, file := range c.Args {
_, err := os.Stat(file)
if os.IsNotExist(err) {
return fmt.Errorf("%s file doesn't exist.", file)
}
if os.IsPermission(err) {
return fmt.Errorf("%s permission denied.", file)
}
}
return nil
}