-
Notifications
You must be signed in to change notification settings - Fork 221
/
Copy pathkeyword_usage.go
613 lines (501 loc) · 19.9 KB
/
keyword_usage.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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
package cst
import (
"fmt"
"strings"
"github.com/johnkerl/miller/v6/pkg/colorizer"
"github.com/johnkerl/miller/v6/pkg/lib"
)
// ----------------------------------------------------------------
type tKeywordUsageFunc func()
type tKeywordUsageEntry struct {
name string
usageFunc tKeywordUsageFunc
}
var KEYWORD_USAGE_TABLE = []tKeywordUsageEntry{
{"all", allKeywordUsage},
{"begin", beginKeywordUsage},
{"bool", boolKeywordUsage},
{"break", breakKeywordUsage},
{"call", callKeywordUsage},
{"continue", continueKeywordUsage},
{"do", doKeywordUsage},
{"dump", dumpKeywordUsage},
{"edump", edumpKeywordUsage},
{"elif", elifKeywordUsage},
{"else", elseKeywordUsage},
{"emit1", emit1KeywordUsage},
{"emit", emitKeywordUsage},
{"emitf", emitfKeywordUsage},
{"emitp", emitpKeywordUsage},
{"end", endKeywordUsage},
{"eprint", eprintKeywordUsage},
{"eprintn", eprintnKeywordUsage},
{"false", falseKeywordUsage},
{"filter", filterKeywordUsage},
{"float", floatKeywordUsage},
{"for", forKeywordUsage},
{"func", funcKeywordUsage},
{"funct", functKeywordUsage},
{"if", ifKeywordUsage},
{"in", inKeywordUsage},
{"int", intKeywordUsage},
{"map", mapKeywordUsage},
{"num", numKeywordUsage},
{"print", printKeywordUsage},
{"printn", printnKeywordUsage},
{"return", returnKeywordUsage},
{"stderr", stderrKeywordUsage},
{"stdout", stdoutKeywordUsage},
{"str", strKeywordUsage},
{"subr", subrKeywordUsage},
{"tee", teeKeywordUsage},
{"true", trueKeywordUsage},
{"unset", unsetKeywordUsage},
{"var", varKeywordUsage},
{"while", whileKeywordUsage},
{"ENV", ENVKeywordUsage},
{"FILENAME", FILENAMEKeywordUsage},
{"FILENUM", FILENUMKeywordUsage},
{"FNR", FNRKeywordUsage},
{"IFS", IFSKeywordUsage},
{"IPS", IPSKeywordUsage},
{"IRS", IRSKeywordUsage},
{"M_E", M_EKeywordUsage},
{"M_PI", M_PIKeywordUsage},
{"NF", NFKeywordUsage},
{"NR", NRKeywordUsage},
{"OFS", OFSKeywordUsage},
{"OPS", OPSKeywordUsage},
{"ORS", ORSKeywordUsage},
}
// ----------------------------------------------------------------
// Pass function_name == NULL to get usage for all keywords.
func UsageKeywords() {
for i, entry := range KEYWORD_USAGE_TABLE {
if i > 0 {
fmt.Println()
}
fmt.Printf("%s: ", colorizer.MaybeColorizeHelp(entry.name, true))
entry.usageFunc()
}
}
func UsageForKeyword(name string) {
if !TryUsageForKeyword(name) {
fmt.Printf("mlr: unrecognized keyword \"%s\".\n", name)
}
}
func TryUsageForKeyword(name string) bool {
for _, entry := range KEYWORD_USAGE_TABLE {
if entry.name == name {
fmt.Printf("%s: ", colorizer.MaybeColorizeHelp(entry.name, true))
entry.usageFunc()
return true
}
}
return false
}
func TryUsageForKeywordApproximate(searchString string) bool {
foundAny := false
for _, entry := range KEYWORD_USAGE_TABLE {
if strings.Contains(entry.name, searchString) {
fmt.Printf("%s: ", colorizer.MaybeColorizeHelp(entry.name, true))
entry.usageFunc()
foundAny = true
}
}
return foundAny
}
// ----------------------------------------------------------------
func ListKeywordsVertically() {
for _, entry := range KEYWORD_USAGE_TABLE {
fmt.Println(entry.name)
}
}
// ----------------------------------------------------------------
func ListKeywordsAsParagraph() {
keywords := make([]string, len(KEYWORD_USAGE_TABLE))
for i, entry := range KEYWORD_USAGE_TABLE {
keywords[i] = entry.name
}
lib.PrintWordsAsParagraph(keywords)
}
// ----------------------------------------------------------------
func allKeywordUsage() {
fmt.Println(
`used in "emit1", "emit", "emitp", and "unset" as a synonym for @*`,
)
}
func beginKeywordUsage() {
fmt.Println(
`defines a block of statements to be executed before input records
are ingested. The body statements must be wrapped in curly braces.
Example: 'begin { @count = 0 }'`)
}
func boolKeywordUsage() {
fmt.Println(
`declares a boolean local variable in the current curly-braced scope.
Type-checking happens at assignment: 'bool b = 1' is an error.`)
}
func breakKeywordUsage() {
fmt.Println(
`causes execution to continue after the body of the current for/while/do-while loop.`)
}
func callKeywordUsage() {
fmt.Println(
`used for invoking a user-defined subroutine.
Example: 'subr s(k,v) { print k . " is " . v} call s("a", $a)'`)
}
func continueKeywordUsage() {
fmt.Println(
`causes execution to skip the remaining statements in the body of
the current for/while/do-while loop. For-loop increments are still applied.`)
}
func doKeywordUsage() {
fmt.Println(
`with "while", introduces a do-while loop. The body statements must be wrapped
in curly braces.`)
}
func dumpKeywordUsage() {
fmt.Println(
`prints all currently defined out-of-stream variables immediately
to stdout as JSON.
With >, >>, or |, the data do not go directly to stdout but are instead
redirected.
The > and >> are for write and append, as in the shell, but (as with awk) the
file-overwrite for > is on first write, not per record. The | is for piping to
a process which will process the data. There will be one open file for each
distinct file name (for > and >>) or one subordinate process for each distinct
value of the piped-to command (for |). Output-formatting flags are taken from
the main command line.
Example: mlr --from f.dat put -q '@v[NR]=$*; end { dump }'
Example: mlr --from f.dat put -q '@v[NR]=$*; end { dump > "mytap.dat"}'
Example: mlr --from f.dat put -q '@v[NR]=$*; end { dump >> "mytap.dat"}'
Example: mlr --from f.dat put -q '@v[NR]=$*; end { dump | "jq .[]"}'`)
}
func edumpKeywordUsage() {
fmt.Println(
`prints all currently defined out-of-stream variables immediately
to stderr as JSON.
Example: mlr --from f.dat put -q '@v[NR]=$*; end { edump }'`)
}
func elifKeywordUsage() {
fmt.Println(
`the way Miller spells "else if". The body statements must be wrapped
in curly braces.`)
}
func elseKeywordUsage() {
fmt.Println(
`terminates an if/elif/elif chain. The body statements must be wrapped
in curly braces.`)
}
func emit1KeywordUsage() {
fmt.Printf(
`inserts an out-of-stream variable into the output record stream. Unlike
the other map variants, side-by-sides, indexing, and redirection are not supported,
but you can emit any map-valued expression.
Example: mlr --from f.dat put 'emit1 $*'
Example: mlr --from f.dat put 'emit1 mapsum({"id": NR}, $*)'
Please see %s://johnkerl.org/miller/doc for more information.
`, lib.DOC_URL)
}
func emitKeywordUsage() {
fmt.Printf(
`inserts an out-of-stream variable into the output record stream. Hashmap
indices present in the data but not slotted by emit arguments are not output.
With >, >>, or |, the data do not become part of the output record stream but
are instead redirected.
The > and >> are for write and append, as in the shell, but (as with awk) the
file-overwrite for > is on first write, not per record. The | is for piping to
a process which will process the data. There will be one open file for each
distinct file name (for > and >>) or one subordinate process for each distinct
value of the piped-to command (for |). Output-formatting flags are taken from
the main command line.
You can use any of the output-format command-line flags, e.g. --ocsv, --ofs,
etc., to control the format of the output if the output is redirected. See also mlr -h.
Example: mlr --from f.dat put 'emit > "/tmp/data-".$a, $*'
Example: mlr --from f.dat put 'emit > "/tmp/data-".$a, mapexcept($*, "a")'
Example: mlr --from f.dat put '@sums[$a][$b]+=$x; emit @sums'
Example: mlr --from f.dat put --ojson '@sums[$a][$b]+=$x; emit > "tap-".$a.$b.".dat", @sums'
Example: mlr --from f.dat put '@sums[$a][$b]+=$x; emit @sums, "index1", "index2"'
Example: mlr --from f.dat put '@sums[$a][$b]+=$x; emit @*, "index1", "index2"'
Example: mlr --from f.dat put '@sums[$a][$b]+=$x; emit > "mytap.dat", @*, "index1", "index2"'
Example: mlr --from f.dat put '@sums[$a][$b]+=$x; emit >> "mytap.dat", @*, "index1", "index2"'
Example: mlr --from f.dat put '@sums[$a][$b]+=$x; emit | "gzip > mytap.dat.gz", @*, "index1", "index2"'
Example: mlr --from f.dat put '@sums[$a][$b]+=$x; emit > stderr, @*, "index1", "index2"'
Example: mlr --from f.dat put '@sums[$a][$b]+=$x; emit | "grep somepattern", @*, "index1", "index2"'
Please see %s://johnkerl.org/miller/doc for more information.
`, lib.DOC_URL)
}
func emitfKeywordUsage() {
fmt.Printf(
`inserts non-indexed out-of-stream variable(s) side-by-side into the
output record stream.
With >, >>, or |, the data do not become part of the output record stream but
are instead redirected.
The > and >> are for write and append, as in the shell, but (as with awk) the
file-overwrite for > is on first write, not per record. The | is for piping to
a process which will process the data. There will be one open file for each
distinct file name (for > and >>) or one subordinate process for each distinct
value of the piped-to command (for |). Output-formatting flags are taken from
the main command line.
You can use any of the output-format command-line flags, e.g. --ocsv, --ofs,
etc., to control the format of the output if the output is redirected. See also mlr -h.
Example: mlr --from f.dat put '@a=$i;@b+=$x;@c+=$y; emitf @a'
Example: mlr --from f.dat put --oxtab '@a=$i;@b+=$x;@c+=$y; emitf > "tap-".$i.".dat", @a'
Example: mlr --from f.dat put '@a=$i;@b+=$x;@c+=$y; emitf @a, @b, @c'
Example: mlr --from f.dat put '@a=$i;@b+=$x;@c+=$y; emitf > "mytap.dat", @a, @b, @c'
Example: mlr --from f.dat put '@a=$i;@b+=$x;@c+=$y; emitf >> "mytap.dat", @a, @b, @c'
Example: mlr --from f.dat put '@a=$i;@b+=$x;@c+=$y; emitf > stderr, @a, @b, @c'
Example: mlr --from f.dat put '@a=$i;@b+=$x;@c+=$y; emitf | "grep somepattern", @a, @b, @c'
Example: mlr --from f.dat put '@a=$i;@b+=$x;@c+=$y; emitf | "grep somepattern > mytap.dat", @a, @b, @c'
Please see %s://johnkerl.org/miller/doc for more information.
`, lib.DOC_URL)
}
func emitpKeywordUsage() {
fmt.Printf(
`inserts an out-of-stream variable into the output record stream.
Hashmap indices present in the data but not slotted by emitp arguments are
output concatenated with ":".
With >, >>, or |, the data do not become part of the output record stream but
are instead redirected.
The > and >> are for write and append, as in the shell, but (as with awk) the
file-overwrite for > is on first write, not per record. The | is for piping to
a process which will process the data. There will be one open file for each
distinct file name (for > and >>) or one subordinate process for each distinct
value of the piped-to command (for |). Output-formatting flags are taken from
the main command line.
You can use any of the output-format command-line flags, e.g. --ocsv, --ofs,
etc., to control the format of the output if the output is redirected. See also mlr -h.
Example: mlr --from f.dat put '@sums[$a][$b]+=$x; emitp @sums'
Example: mlr --from f.dat put --opprint '@sums[$a][$b]+=$x; emitp > "tap-".$a.$b.".dat", @sums'
Example: mlr --from f.dat put '@sums[$a][$b]+=$x; emitp @sums, "index1", "index2"'
Example: mlr --from f.dat put '@sums[$a][$b]+=$x; emitp @*, "index1", "index2"'
Example: mlr --from f.dat put '@sums[$a][$b]+=$x; emitp > "mytap.dat", @*, "index1", "index2"'
Example: mlr --from f.dat put '@sums[$a][$b]+=$x; emitp >> "mytap.dat", @*, "index1", "index2"'
Example: mlr --from f.dat put '@sums[$a][$b]+=$x; emitp | "gzip > mytap.dat.gz", @*, "index1", "index2"'
Example: mlr --from f.dat put '@sums[$a][$b]+=$x; emitp > stderr, @*, "index1", "index2"'
Example: mlr --from f.dat put '@sums[$a][$b]+=$x; emitp | "grep somepattern", @*, "index1", "index2"'
Please see %s://johnkerl.org/miller/doc for more information.
`, lib.DOC_URL)
}
func endKeywordUsage() {
fmt.Println(
`defines a block of statements to be executed after input records
are ingested. The body statements must be wrapped in curly braces.
Example: 'end { emit @count }'
Example: 'end { eprint "Final count is " . @count }'`)
}
func eprintKeywordUsage() {
fmt.Println(
`prints expression immediately to stderr.
Example: mlr --from f.dat put -q 'eprint "The sum of x and y is ".($x+$y)'
Example: mlr --from f.dat put -q 'for (k, v in $*) { eprint k . " => " . v }'
Example: mlr --from f.dat put '(NR % 1000 == 0) { eprint "Checkpoint ".NR}'`)
}
func eprintnKeywordUsage() {
fmt.Println(
`prints expression immediately to stderr, without trailing newline.
Example: mlr --from f.dat put -q 'eprintn "The sum of x and y is ".($x+$y); eprint ""'`)
}
func falseKeywordUsage() {
fmt.Println(`the boolean literal value.`)
}
func filterKeywordUsage() {
fmt.Println(
`includes/excludes the record in the output record stream.
Example: mlr --from f.dat put 'filter (NR == 2 || $x > 5.4)'
Instead of put with 'filter false' you can simply use put -q. The following
uses the input record to accumulate data but only prints the running sum
without printing the input record:
Example: mlr --from f.dat put -q '@running_sum += $x * $y; emit @running_sum'`)
}
func floatKeywordUsage() {
fmt.Println(
`declares a floating-point local variable in the current curly-braced scope.
Type-checking happens at assignment: 'float x = 0' is an error.`)
}
func forKeywordUsage() {
fmt.Println(
`defines a for-loop using one of three styles. The body statements must
be wrapped in curly braces.
For-loop over stream record:
Example: 'for (k, v in $*) { ... }'
For-loop over out-of-stream variables:
Example: 'for (k, v in @counts) { ... }'
Example: 'for ((k1, k2), v in @counts) { ... }'
Example: 'for ((k1, k2, k3), v in @*) { ... }'
C-style for-loop:
Example: 'for (var i = 0, var b = 1; i < 10; i += 1, b *= 2) { ... }'`)
}
func funcKeywordUsage() {
fmt.Println(
`used for defining a user-defined function.
Example: 'func f(a,b) { return sqrt(a**2+b**2)} $d = f($x, $y)'`)
}
func functKeywordUsage() {
fmt.Println(
`used for saying that a function argument is a user-defined function.
Example: 'func g(num a, num b, funct f) :num { return f(a**2+b**2) }'`)
}
func ifKeywordUsage() {
fmt.Println(
`starts an if/elif/elif chain. The body statements must be wrapped
in curly braces.`)
}
func inKeywordUsage() {
fmt.Println(`used in for-loops over stream records or out-of-stream variables.`)
}
func intKeywordUsage() {
fmt.Println(
`declares an integer local variable in the current curly-braced scope.
Type-checking happens at assignment: 'int x = 0.0' is an error.`)
}
func mapKeywordUsage() {
fmt.Println(
`declares a map-valued local variable in the current curly-braced scope.
Type-checking happens at assignment: 'map b = 0' is an error. map b = {} is
always OK. map b = a is OK or not depending on whether a is a map.`)
}
func numKeywordUsage() {
fmt.Println(
`declares an int/float local variable in the current curly-braced scope.
Type-checking happens at assignment: 'num b = true' is an error.`)
}
func printKeywordUsage() {
fmt.Println(
`prints expression immediately to stdout.
Example: mlr --from f.dat put -q 'print "The sum of x and y is ".($x+$y)'
Example: mlr --from f.dat put -q 'for (k, v in $*) { print k . " => " . v }'
Example: mlr --from f.dat put '(NR % 1000 == 0) { print > stderr, "Checkpoint ".NR}'`)
}
func printnKeywordUsage() {
fmt.Println(
`prints expression immediately to stdout, without trailing newline.
Example: mlr --from f.dat put -q 'printn "."; end { print "" }'`)
}
func returnKeywordUsage() {
fmt.Println(
`specifies the return value from a user-defined function.
Omitted return statements (including via if-branches) result in an absent-null
return value, which in turns results in a skipped assignment to an LHS.`)
}
func stderrKeywordUsage() {
fmt.Println(
`Used for tee, emit, emitf, emitp, print, and dump in place of filename
to print to standard error.`)
}
func stdoutKeywordUsage() {
fmt.Println(
`Used for tee, emit, emitf, emitp, print, and dump in place of filename
to print to standard output.`)
}
func strKeywordUsage() {
fmt.Println(
`declares a string local variable in the current curly-braced scope.
Type-checking happens at assignment.`)
}
func subrKeywordUsage() {
fmt.Println(
`used for defining a subroutine.
Example: 'subr s(k,v) { print k . " is " . v} call s("a", $a)'`)
}
func teeKeywordUsage() {
fmt.Println(
`prints the current record to specified file.
This is an immediate print to the specified file (except for pprint format
which of course waits until the end of the input stream to format all output).
The > and >> are for write and append, as in the shell, but (as with awk) the
file-overwrite for > is on first write, not per record. The | is for piping to
a process which will process the data. There will be one open file for each
distinct file name (for > and >>) or one subordinate process for each distinct
value of the piped-to command (for |). Output-formatting flags are taken from
the main command line.
You can use any of the output-format command-line flags, e.g. --ocsv, --ofs,
etc., to control the format of the output. See also mlr -h.
emit with redirect and tee with redirect are identical, except tee can only
output $*.
Example: mlr --from f.dat put 'tee > "/tmp/data-".$a, $*'
Example: mlr --from f.dat put 'tee >> "/tmp/data-".$a.$b, $*'
Example: mlr --from f.dat put 'tee > stderr, $*'
Example: mlr --from f.dat put -q 'tee | "tr \[a-z\\] \[A-Z\\]", $*'
Example: mlr --from f.dat put -q 'tee | "tr \[a-z\\] \[A-Z\\] > /tmp/data-".$a, $*'
Example: mlr --from f.dat put -q 'tee | "gzip > /tmp/data-".$a.".gz", $*'
Example: mlr --from f.dat put -q --ojson 'tee | "gzip > /tmp/data-".$a.".gz", $*'`)
}
func trueKeywordUsage() {
fmt.Println(`the boolean literal value.`)
}
func unsetKeywordUsage() {
fmt.Println(
`clears field(s) from the current record, or an out-of-stream or local variable.
Example: mlr --from f.dat put 'unset $x'
Example: mlr --from f.dat put 'unset $*'
Example: mlr --from f.dat put 'for (k, v in $*) { if (k =~ "a.*") { unset $[k] } }'
Example: mlr --from f.dat put '...; unset @sums'
Example: mlr --from f.dat put '...; unset @sums["green"]'
Example: mlr --from f.dat put '...; unset @*'`)
}
func varKeywordUsage() {
fmt.Println(
`declares an untyped local variable in the current curly-braced scope.
Examples: 'var a=1', 'var xyz=""'`)
}
func whileKeywordUsage() {
fmt.Println(
`introduces a while loop, or with "do", introduces a do-while loop.
The body statements must be wrapped in curly braces.`)
}
func ENVKeywordUsage() {
fmt.Println(`access to environment variables by name, e.g. '$home = ENV["HOME"]'`)
}
func FILENAMEKeywordUsage() {
fmt.Println(`evaluates to the name of the current file being processed.`)
}
func FILENUMKeywordUsage() {
fmt.Println(
`evaluates to the number of the current file being processed,
starting with 1.`)
}
func FNRKeywordUsage() {
fmt.Println(
`evaluates to the number of the current record within the current file
being processed, starting with 1. Resets at the start of each file.`)
}
func IFSKeywordUsage() {
fmt.Println(`evaluates to the input field separator from the command line.`)
}
func IPSKeywordUsage() {
fmt.Println(`evaluates to the input pair separator from the command line.`)
}
func IRSKeywordUsage() {
fmt.Println(
`evaluates to the input record separator from the command line,
or to LF or CRLF from the input data if in autodetect mode (which is
the default).`)
}
func M_EKeywordUsage() {
fmt.Println(`the mathematical constant e.`)
}
func M_PIKeywordUsage() {
fmt.Println(`the mathematical constant pi.`)
}
func NFKeywordUsage() {
fmt.Println(`evaluates to the number of fields in the current record.`)
}
func NRKeywordUsage() {
fmt.Println(
`evaluates to the number of the current record over all files
being processed, starting with 1. Does not reset at the start of each file.`)
}
func OFSKeywordUsage() {
fmt.Println(`evaluates to the output field separator from the command line.`)
}
func OPSKeywordUsage() {
fmt.Println(`evaluates to the output pair separator from the command line.`)
}
func ORSKeywordUsage() {
fmt.Println(
`evaluates to the output record separator from the command line,
or to LF or CRLF from the input data if in autodetect mode (which is
the default).`)
}