-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
193 lines (176 loc) · 3.87 KB
/
main.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package main
import (
"errors"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
)
var usageText = `usage:
cf submit FILE[.EXT] [-lang=LANG] [-prob=PROBLEM]
cf login [HANDLE]
cf con [CONTEST]
cf conf default
cf conf lang=LANGID
Where FILE is file to be submitted
when file has EXTension, it helps determine default language
otherwise file and language will be guessed.
When LANG is specified file extension will be not guessed
ang given lang will be used.
PROBLEM may contain problem letter (A,b,..) and/or contest number (eg. 42)
if part is not specified it will be guessed from extension (letter)
or saved config (contest).
CONTEST will be saved in conf.
HANDLE will be used to login, when not specified last login will be used.
Language config could be set to default for all languages
or set to LANGID for specific one.
`
func main() {
err := run()
show(err)
if err != nil {
os.Exit(1)
}
}
func show(err error) {
if err != nil {
fmt.Fprintln(os.Stderr, "err:", err)
}
}
func run() error {
cmd, param, prob, lang, err := parseArgs(os.Args[1:])
if err != nil {
return err
}
cf, err := newCF()
if err != nil {
return err
}
switch cmd {
case "login":
if param == "" {
if u, ok := cf.config["user"]; ok {
param = u
} else {
param, err = cf.askUser()
if err != nil {
return err
}
}
}
cf.config["user"] = param
p, err := cf.password()
if err != nil {
return err
}
err = cf.login(param, p)
if err != nil {
return err
}
err = cf.save()
if err != nil {
return err
}
return cf.saveCookie()
case "submit":
if param == "" {
if prob.task != "" {
param = prob.task
} else {
return errors.New("submit: empty problem code")
}
}
if prob.contest == 0 {
prob.contest, err = cf.contest()
if err != nil {
return err
}
}
if prob.task == "" {
base := filepath.Base(param)
idx := strings.LastIndexByte(base, '.')
if idx != -1 {
base = base[:idx]
}
if len(base) == 1 && isletter(rune(base[0])) {
prob.task = base
} else if len(base) == 2 && isletter(rune(base[0])) && base[1] >= '0' && base[1] <= '9' {
prob.task = base
} else {
return errors.New("submit: please explicitly pass problem code (-prob=...)")
}
}
prob.task = strings.ToUpper(prob.task)
err = cf.submit(param, prob, lang)
if err != nil {
return err
}
return cf.saveCookie()
case "con":
cf.config["contest"] = param
return cf.save()
case "conf":
return cf.reconf(param)
case "help":
fallthrough
default: // help
return errors.New(usageText)
}
}
func parseArgs(args []string) (cmd, param string, prob probCode, lang int, err error) {
for _, a := range args {
if len(a) == 0 {
continue
}
if a[0] == '-' {
if strings.HasPrefix(a, "-prob=") {
p := a[6:]
// contest must be first or skipped
// then goes problem code
// eg: 'a', '555a', '670d1', 'd1'
// 'a670' isn't allowed ('d1670' would be ambigous)
switch {
case len(p) == 0:
err = errors.New("cmdline: empty problem code")
return
case isletter(rune(p[0])):
prob.task = p
default:
taskIdx := strings.IndexFunc(p, isletter)
if taskIdx == -1 {
prob.contest, err = strconv.Atoi(p)
} else {
prob.task = p[taskIdx:]
prob.contest, err = strconv.Atoi(p[:taskIdx])
}
if err != nil {
err = fmt.Errorf("cmdline: invalid problem code %q", p)
return
}
}
} else if strings.HasPrefix(a, "-lang=") {
lang, err = strconv.Atoi(a[6:])
if err != nil {
return
}
} else {
err = fmt.Errorf("cmdline: unknown parameter %q", a)
return
}
continue
}
if len(cmd) == 0 {
cmd = a
} else if len(param) == 0 {
param = a
} else {
err = fmt.Errorf("cmdline: too much positional args")
return
}
}
return
}
func isletter(ch rune) bool {
return (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')
}