-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
198 lines (174 loc) · 4.75 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
194
195
196
197
198
package main
import (
"errors"
"fmt"
"os"
"strconv"
)
const program_name string = "gordle"
const version_major int = 0
const version_minor int = 1
const version_patch int = 0
const default_dict_path string = "/usr/share/dict:/usr/dict"
const default_dict string = "words"
const help_msg string = program_name + " v%d.%d.%d" +
"\nUSAGE:" +
"\n %s [FLAGS] [OPTIONS]" +
"\n" +
"\nFLAGS:" +
"\n -h, --help Show this help message and exit" +
"\n -v, --version Show program name and version and exit" +
"\n --debug Show debug messages, including solutions" +
"\n" +
"\nOPTIONS:" +
"\n -l, --list Name of the dictionary file" +
"\n [default: \"" + default_dict + "\"]" +
"\n -d, --dicts Colon-separated list of directories in which" +
"\n to search for dictionary files" +
"\n [default: \"" + default_dict_path + "\"]" +
"\n -w, --words Number of words to guess" +
"\n [default: 1]" +
"\n -g, --guesses Maximum number of guesses" +
"\n [default: words + 5]" +
"\n -c, --chars The length (in utf-8 code points) of words" +
"\n [default: 5]" +
"\n -a, --force-ascii Only accept dictionary words that" +
"\n consist exclusively of letters in a-z and A-Z" +
"\n [default: true]" +
"\n"
type options struct {
dict_path string
language string
force_ascii bool
debug bool
max_guesses int
words int
chars int
}
func main() {
options, err := parse_args(os.Args[1:])
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n\n", err)
fmt.Fprintf(os.Stderr, help_msg, version_major, version_minor,
version_patch, os.Args[0])
os.Exit(1)
}
file, err := find_dict(options)
if err != nil {
fmt.Fprintf(os.Stderr, "%s: %v\n", os.Args[0], err)
os.Exit(1)
}
defer file.Close()
run(parse_dict(options, file), options)
}
func parse_args(args []string) (options options, err error) {
options.language = default_dict
options.dict_path = default_dict_path
options.force_ascii = true
options.debug = false
options.chars = 5
options.words = 1
n, err := parse_flags(args, &options)
if err == nil {
err = parse_options(args[n:], &options)
}
return
}
func parse_flags(args []string, options *options) (consumed int, err error) {
for i := 0; i < len(args); i++ {
arg := args[i]
if len(arg) < 2 || arg[0] != '-' {
return 0, fmt.Errorf("error at \"%s\": expected flag or option", arg)
}
var flag string
if arg[1] == '-' { // long flag
flag = arg[2:]
} else {
flag = arg[1:]
}
switch flag {
case "h", "help":
fmt.Printf(help_msg, version_major, version_minor, version_patch,
os.Args[0])
os.Exit(0)
case "v", "version":
fmt.Printf("%s v%d.%d.%d\n", program_name, version_major,
version_minor, version_patch)
os.Exit(0)
case "debug":
options.debug = true
default:
return i, nil
}
}
return len(args), nil
}
func parse_options(args []string, options *options) (err error) {
set_guesses := false
for i := 0; i < len(args); i++ {
arg := args[i]
// make sure the current argument could be a valid option
if len(arg) < 2 || arg[0] != '-' {
return fmt.Errorf(
"error at \"%s\": expected option", arg)
}
var opt string
var value string
if arg[1] != '-' && len(arg) > 2 { // short option, value in same token
opt = arg[1:2]
value = arg[2:]
} else { // value is in the next token
if i+1 >= len(args) {
return fmt.Errorf(
"error at \"%s\": missing value to option", arg)
}
if arg[1] == '-' { // long option
opt = arg[2:]
value = args[i+1]
} else { // short option
opt = arg[1:]
value = args[i+1]
}
i++
}
switch opt {
case "l", "list":
options.language = value
case "d", "dicts":
options.dict_path = value
case "w", "words":
var n uint64
n, err = strconv.ParseUint(value, 10, 32)
options.words = int(n)
case "g", "guesses":
var n uint64
n, err = strconv.ParseUint(value, 10, 32)
options.max_guesses = int(n)
set_guesses = true
case "c", "chars":
var n uint64
n, err = strconv.ParseUint(value, 10, 32)
options.chars = int(n)
case "a", "force-ascii":
switch value {
case "y", "yes", "true":
options.force_ascii = true
case "n", "no", "false":
options.force_ascii = false
default:
err = errors.New("unrecognized value, use one of: " +
"y, yes, true, n, no, false")
}
default:
err = errors.New("unrecognized option")
}
if err != nil {
err = fmt.Errorf("error at \"%s\": %v", arg, err)
return
}
}
if !set_guesses {
options.max_guesses = 3 + options.words + options.chars/2
}
return nil
}