-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.cr
311 lines (233 loc) · 8.16 KB
/
parser.cr
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
require "easyclip"
require "option_parser"
require "./options"
require "./cli/version"
module DeepL
class Parser < OptionParser
getter opt : Options
property help_message : String
macro _on_debug_
on("-d", "--debug", "Show backtrace on error") do
CLI.debug = true
end
end
macro _on_help_
on("-h", "--help", "Show this help") do
opt.action = Action::Help
end
# Crystal's OptionParser returns to its initial state after parsing
# by `with_preserved_state`. This also initialises @flags.
# @help_message is needed to store subcommand messages.
@help_message = self.to_s
end
macro _set_action_(action, banner)
opt.action = {{action}}
@handlers.clear
@flags.clear
self.banner = {{banner}}
end
def initialize
super()
@opt = Options.new
@help_message = ""
self.banner = <<-BANNER
Program: DeepL CLI (Simple command line tool for DeepL)
Version: #{DeepL::CLI::VERSION}
Source: https://github.com/kojix2/deepl-cli
Usage: deepl [options] <file>
BANNER
on("doc", "Translate document") do
_set_action_(Action::TranslateDocument, "Usage: deepl doc [options] <file>")
on("-f", "--from [LANG]", "Source language [AUTO]") do |from|
opt.source_lang = from.upcase
end
on("-t", "--to [LANG]", "Target language [#{opt.target_lang}]") do |to|
opt.target_lang = to.upcase
end
on("-g", "--glossary NAME", "Glossary name") do |name|
opt.glossary_name = name
end
on("-F", "--formality OPT", "Formality (default more less)") do |v|
opt.formality = v
end
on("-o", "--output FILE", "Output file") do |file|
opt.output_file = Path[file]
end
on("-O", "--output-format FORMAT", "Output file format") do |format|
opt.output_format = format
end
on("-s", "--interval SEC", "Interval between requests") do |sec|
opt.interval = sec.to_f32
end
_on_debug_
_on_help_
end
on("glossary", "Manage glossaries") do
_set_action_(Action::Help, "Usage: deepl glossary [options] <subcommand>")
on("list", "List glossaries") do
_set_action_(Action::ListGlossariesLong, "Usage: deepl glossary list [options]")
_on_debug_
_on_help_
end
on("create", "Create a glossary") do |name|
_set_action_(Action::CreateGlossary, "Usage: deepl glossary create [options] <tsv|csv>")
on("-n", "--name NAME", "Glossary name") do |name|
opt.glossary_name = name
end
on("-f", "--from [LANG]", "Source language [AUTO]") do |from|
opt.source_lang = from.upcase
end
on("-t", "--to [LANG]", "Target language [#{opt.target_lang}]") do |to|
opt.target_lang = to.upcase
end
_on_debug_
_on_help_
end
on("delete", "Delete glossaries") do
_set_action_(Action::DeleteGlossaryByName, "Usage: deepl glossary delete <names>")
on("-i", "--id", "Delete glossary by ids instead of names") do
opt.action = Action::DeleteGlossaryById
end
_on_debug_
_on_help_
end
on("edit", "Edit glossaries") do
_set_action_(Action::EditGlossaryByName, "Usage: deepl glossary edit <names>")
on("-i", "--id", "Delete glossary by ids instead of names") do
opt.action = Action::DeleteGlossaryById
end
on("-e", "--editor EDITOR", "Editor command [ENV[\"EDITOR\"]]") do |editor|
ENV["EDITOR"] = editor
end
_on_debug_
_on_help_
end
on("view", "View glossaries") do
_set_action_(Action::OutputGlossaryEntriesByName, "Usage: deepl glossary view <names>")
on("-i", "--id ID", "Delete glossary by ids instead of names") do
opt.action = Action::OutputGlossaryEntriesById
end
on("-o", "--output FILE", "Output file") do |file|
opt.output_file = Path[file]
end
_on_debug_
_on_help_
end
on("-l", "--list", "List glossaries (short form)") do
opt.action = Action::ListGlossaries
# FIXME: short form
end
on("-p", "--language-pairs", "List language pairs") do
opt.action = Action::ListGlossaryLanguagePairs
end
_on_debug_
_on_help_
end
on("text", "Translate text (default)") do
# Reuse the options of the main command.
opt.action = Action::TranslateText
self.banner = "Usage: deepl text [options]"
@handlers.reject! %w(-h --help -d --debug -v --version -u --usage)
@flags.pop(4)
on("-s", "--split-sentences OPT", "Split sentences") do |v|
opt.split_sentences = v
end
on("-P", "--preserve-formatting", "Preserve formatting") do
opt.preserve_formatting = true
end
on("-T", "--tag-handling OPT", "Tag handling") do |v|
opt.tag_handling = v
end
on("-O", "--outline-detection", "Outline detection") do
opt.outline_detection = true
end
on("-N", "--non-splitting-tags TAGS", "Non-splitting tags") do |tags|
opt.non_splitting_tags = tags.split(",")
end
on("-S", "--splitting-tags TAGS", "Splitting tags") do |tags|
opt.splitting_tags = tags.split(",")
end
on("-I", "--ignore-tags TAGS", "Ignore tags") do |tags|
opt.ignore_tags = tags.split(",")
end
_on_debug_
_on_help_
end
on("-i", "--input TEXT", "Input text") do |text|
opt.input_text = text
end
on("-f", "--from [LANG]", "Source language [AUTO]") do |from|
if from.empty?
opt.action = Action::ListFromLanguages
else
opt.source_lang = from.upcase
end
end
on("-t", "--to [LANG]", "Target language [#{opt.target_lang}]") do |to|
if to.empty?
opt.action = Action::ListTargetLanguages
else
opt.target_lang = to.upcase
end
end
on("-p", "--paste", "Input text from clipboard") do
text = EasyClip.paste
if text.empty?
STDERR.puts "[deepl-cli] ERROR: Clipboard is empty."
exit(1)
end
STDERR.puts "[deepl-cli] Input text from clipboard: \n#{text}\n\n"
opt.input_text = text
end
# FIXME: This option is experimental.
# The name of the option may change in the future.
# on("-c", "--copy", "Copy translated text to clipboard (experimental)") do
# # TODO?
# end
on("-g", "--glossary NAME", "Glossary name") do |name|
opt.glossary_name = name
end
on("-D", "--detect-language", "Output detected source language") do
opt.detect_source_language = true
end
on("-F", "--formality OPT", "Formality (default more less)") do |v|
opt.formality = v
end
on("-C", "--context TEXT", "Context (experimental)") do |text|
opt.context = text
end
on("-A", "--ansi", "Do not remove ANSI escape codes") do
opt.no_ansi = false
end
# Why is --output option is needed?
# Because utf-8 is not fully supported in Windows console.
on("-o", "--output FILE", "Output file") do |file|
opt.output_file = Path[file]
end
on("-u", "--usage", "Check Usage and Limits") do
opt.action = Action::RetrieveUsage
# @handlers.clear
# @flags.clear
end
_on_debug_
on("-v", "--version", "Show version") do
opt.action = Action::Version
end
_on_help_
invalid_option do |flag|
STDERR.puts "[deepl-cli] ERROR: #{flag} is not a valid option."
STDERR.puts self
exit(1)
end
missing_option do |flag|
STDERR.puts "[deepl-cli] ERROR: #{flag} option expects an argument."
STDERR.puts self
exit(1)
end
end
def parse(args)
super
opt
end
end
end