This repository has been archived by the owner on Oct 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
arguments.go
531 lines (494 loc) · 8.93 KB
/
arguments.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
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
package dgc
import (
"regexp"
"strconv"
"strings"
"time"
"github.com/karrick/tparse/v2"
)
var (
// RegexArguments defines the regex the argument string has to match
RegexArguments = regexp.MustCompile("(\"[^\"]+\"|[^\\s]+)")
// RegexUserMention defines the regex a user mention has to match
RegexUserMention = regexp.MustCompile("<@!?(\\d+)>")
// RegexRoleMention defines the regex a role mention has to match
RegexRoleMention = regexp.MustCompile("<@&(\\d+)>")
// RegexChannelMention defines the regex a channel mention has to match
RegexChannelMention = regexp.MustCompile("<#(\\d+)>")
// RegexBigCodeblock defines the regex a big codeblock has to match
RegexBigCodeblock = regexp.MustCompile("(?s)\\n*```(?:([\\w.\\-]*)\\n)?(.*)```")
// RegexSmallCodeblock defines the regex a small codeblock has to match
RegexSmallCodeblock = regexp.MustCompile("(?s)\\n*`(.*)`")
// CodeblockLanguages defines which languages are valid codeblock languages
CodeblockLanguages = []string{
"as",
"1c",
"abnf",
"accesslog",
"actionscript",
"ada",
"ado",
"adoc",
"apache",
"apacheconf",
"applescript",
"arduino",
"arm",
"armasm",
"asciidoc",
"aspectj",
"atom",
"autohotkey",
"autoit",
"avrasm",
"awk",
"axapta",
"bash",
"basic",
"bat",
"bf",
"bind",
"bnf",
"brainfuck",
"c",
"c++",
"cal",
"capnp",
"capnproto",
"cc",
"ceylon",
"clean",
"clj",
"clojure-repl",
"clojure",
"cls",
"cmake.in",
"cmake",
"cmd",
"coffee",
"coffeescript",
"console",
"coq",
"cos",
"cpp",
"cr",
"craftcms",
"crm",
"crmsh",
"crystal",
"cs",
"csharp",
"cson",
"csp",
"css",
"d",
"dart",
"dcl",
"delphi",
"dfm",
"diff",
"django",
"dns",
"do",
"docker",
"dockerfile",
"dos",
"dpr",
"dsconfig",
"dst",
"dts",
"dust",
"ebnf",
"elixir",
"elm",
"erb",
"erl",
"erlang-repl",
"erlang",
"excel",
"f90",
"f95",
"feature",
"fix",
"flix",
"fortran",
"freepascal",
"fs",
"fsharp",
"gams",
"gauss",
"gcode",
"gemspec",
"gherkin",
"glsl",
"gms",
"go",
"golang",
"golo",
"gradle",
"graph",
"groovy",
"gss",
"gyp",
"h",
"h++",
"haml",
"handlebars",
"haskell",
"haxe",
"hbs",
"hpp",
"hs",
"hsp",
"html.handlebars",
"html.hbs",
"html",
"htmlbars",
"http",
"https",
"hx",
"hy",
"hylang",
"i7",
"iced",
"icl",
"inform7",
"ini",
"instances",
"irb",
"irpf90",
"java",
"javascript",
"jboss-cli",
"jinja",
"js",
"json",
"jsp",
"jsx",
"julia",
"k",
"kdb",
"kotlin",
"lasso",
"lassoscript",
"lazarus",
"ldif",
"leaf",
"less",
"lfm",
"lisp",
"livecodeserver",
"livescript",
"llvm",
"lpr",
"ls",
"lsl",
"lua",
"m",
"mak",
"makefile",
"markdown",
"mathematica",
"matlab",
"maxima",
"md",
"mel",
"mercury",
"mips",
"mipsasm",
"mizar",
"mk",
"mkd",
"mkdown",
"ml",
"mm",
"mma",
"mojolicious",
"monkey",
"moo",
"moon",
"moonscript",
"n1ql",
"nc",
"nginx",
"nginxconf",
"nim",
"nimrod",
"nix",
"nixos",
"nsis",
"obj-c",
"objc",
"objectivec",
"ocaml",
"openscad",
"osascript",
"oxygene",
"p21",
"parser3",
"pas",
"pascal",
"patch",
"pb",
"pbi",
"pcmk",
"perl",
"pf.conf",
"pf",
"php",
"php3",
"php4",
"php5",
"php6",
"pl",
"plist",
"pm",
"podspec",
"pony",
"powershell",
"pp",
"processing",
"profile",
"prolog",
"protobuf",
"ps",
"puppet",
"purebasic",
"py",
"python",
"q",
"qml",
"qt",
"r",
"rb",
"rib",
"roboconf",
"rs",
"rsl",
"rss",
"ruby",
"ruleslanguage",
"rust",
"scad",
"scala",
"scheme",
"sci",
"scilab",
"scss",
"sh",
"shell",
"smali",
"smalltalk",
"sml",
"sqf",
"sql",
"st",
"stan",
"stata",
"step",
"step21",
"stp",
"styl",
"stylus",
"subunit",
"sv",
"svh",
"swift",
"taggerscript",
"tao",
"tap",
"tcl",
"tex",
"thor",
"thrift",
"tk",
"toml",
"tp",
"ts",
"twig",
"typescript",
"v",
"vala",
"vb",
"vbnet",
"vbs",
"vbscript-html",
"vbscript",
"verilog",
"vhdl",
"vim",
"wildfly-cli",
"x86asm",
"xhtml",
"xjb",
"xl",
"xls",
"xlsx",
"xml",
"xpath",
"xq",
"xquery",
"xsd",
"xsl",
"yaml",
"yml",
"zep",
"zephir",
"zone",
"zsh",
}
)
// Arguments represents the arguments that may be used in a command context
type Arguments struct {
raw string
arguments []*Argument
}
// Codeblock represents a Discord codeblock
type Codeblock struct {
Language string
Content string
}
// ParseArguments parses the raw string into several arguments
func ParseArguments(raw string) *Arguments {
// Define the raw arguments
rawArguments := RegexArguments.FindAllString(raw, -1)
arguments := make([]*Argument, len(rawArguments))
// Parse the raw arguments into correct ones
for index, rawArgument := range rawArguments {
rawArgument = stringTrimPreSuffix(rawArgument, "\"")
arguments[index] = &Argument{
raw: rawArgument,
}
}
// Return the arguments structure
return &Arguments{
raw: raw,
arguments: arguments,
}
}
// Raw returns the raw string value of the arguments
func (arguments *Arguments) Raw() string {
return arguments.raw
}
// AsSingle parses the given arguments as a single one
func (arguments *Arguments) AsSingle() *Argument {
return &Argument{
raw: arguments.raw,
}
}
// Amount returns the amount of given arguments
func (arguments *Arguments) Amount() int {
return len(arguments.arguments)
}
// Get returns the n'th argument
func (arguments *Arguments) Get(n int) *Argument {
if arguments.Amount() <= n {
return &Argument{
raw: "",
}
}
return arguments.arguments[n]
}
// Remove removes the n'th argument
func (arguments *Arguments) Remove(n int) {
// Check if the given index is valid
if arguments.Amount() <= n {
return
}
// Set the new argument slice
arguments.arguments = append(arguments.arguments[:n], arguments.arguments[n+1:]...)
// Set the new raw string
raw := ""
for _, argument := range arguments.arguments {
raw += argument.raw + " "
}
arguments.raw = strings.TrimSpace(raw)
}
// AsCodeblock parses the given arguments as a codeblock
func (arguments *Arguments) AsCodeblock() *Codeblock {
raw := arguments.Raw()
// Check if the raw string is a big codeblock
matches := RegexBigCodeblock.MatchString(raw)
if !matches {
// Check if the raw string is a small codeblock
matches = RegexSmallCodeblock.MatchString(raw)
if matches {
submatches := RegexSmallCodeblock.FindStringSubmatch(raw)
return &Codeblock{
Language: "",
Content: submatches[1],
}
}
return nil
}
// Define the content and the language
submatches := RegexBigCodeblock.FindStringSubmatch(raw)
language := ""
content := submatches[1] + submatches[2]
if submatches[1] != "" && !stringArrayContains(CodeblockLanguages, submatches[1], false) {
language = submatches[1]
content = submatches[2]
}
// Return the codeblock
return &Codeblock{
Language: language,
Content: content,
}
}
// Argument represents a single argument
type Argument struct {
raw string
}
// Raw returns the raw string value of the argument
func (argument *Argument) Raw() string {
return argument.raw
}
// AsBool parses the given argument into a boolean
func (argument *Argument) AsBool() (bool, error) {
return strconv.ParseBool(argument.raw)
}
// AsInt parses the given argument into an int32
func (argument *Argument) AsInt() (int, error) {
return strconv.Atoi(argument.raw)
}
// AsInt64 parses the given argument into an int64
func (argument *Argument) AsInt64() (int64, error) {
return strconv.ParseInt(argument.raw, 10, 64)
}
// AsUserMentionID returns the ID of the mentioned user or an empty string if it is no mention
func (argument *Argument) AsUserMentionID() string {
// Check if the argument is a user mention
matches := RegexUserMention.MatchString(argument.raw)
if !matches {
return ""
}
// Parse the user ID
userID := RegexUserMention.FindStringSubmatch(argument.raw)[1]
return userID
}
// AsRoleMentionID returns the ID of the mentioned role or an empty string if it is no mention
func (argument *Argument) AsRoleMentionID() string {
// Check if the argument is a role mention
matches := RegexRoleMention.MatchString(argument.raw)
if !matches {
return ""
}
// Parse the role ID
roleID := RegexRoleMention.FindStringSubmatch(argument.raw)[1]
return roleID
}
// AsChannelMentionID returns the ID of the mentioned channel or an empty string if it is no mention
func (argument *Argument) AsChannelMentionID() string {
// Check if the argument is a channel mention
matches := RegexChannelMention.MatchString(argument.raw)
if !matches {
return ""
}
// Parse the channel ID
channelID := RegexChannelMention.FindStringSubmatch(argument.raw)[1]
return channelID
}
// AsDuration parses the given argument into a duration
func (argument *Argument) AsDuration() (time.Duration, error) {
return tparse.AbsoluteDuration(time.Now(), argument.raw)
}