-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompletion_test.toit
More file actions
778 lines (708 loc) · 26.6 KB
/
completion_test.toit
File metadata and controls
778 lines (708 loc) · 26.6 KB
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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
// Copyright (C) 2026 Toit contributors.
// Use of this source code is governed by a Zero-Clause BSD license that can
// be found in the tests/LICENSE file.
import cli
import cli.completion_ show *
import expect show *
main:
test-empty-input
test-subcommand-completion
test-option-name-completion
test-option-value-completion
test-enum-completion
test-flag-completion
test-after-dashdash
test-hidden-excluded
test-alias-completion
test-already-provided-excluded
test-multi-still-suggested
test-custom-completion-callback
test-completion-context
test-nested-subcommands
test-rest-completion
test-option-equals-value
test-completion-with-descriptions
test-help-only-at-root
test-flags-hidden-without-dash-prefix
test-option-path
test-rest-positional-index
test-rest-positional-index-after-dashdash
test-rest-multi-not-skipped
test-rest-dependent-completion
test-rest-multi-records-all
test-rest-after-dashdash-recorded
test-short-option-marks-seen
test-short-option-pending-value
test-packed-short-options
test-custom-completer-no-file-fallback
test-option-extensions
test-help-completion
test-help-gated-on-availability
test-empty-input:
root := cli.Command "app"
--subcommands=[
cli.Command "serve" --help="Start a server." --run=:: null,
cli.Command "build" --help="Build the project." --run=:: null,
]
result := complete_ root [""]
values := result.candidates.map: it.value
expect (values.contains "serve")
expect (values.contains "build")
expect (values.contains "help")
// Flags should NOT appear when prefix is empty (no "-" prefix).
expect (not (values.contains "--help"))
expect (not (values.contains "-h"))
test-subcommand-completion:
root := cli.Command "app"
--subcommands=[
cli.Command "serve" --help="Start a server." --run=:: null,
cli.Command "status" --help="Show status." --run=:: null,
cli.Command "build" --help="Build the project." --run=:: null,
]
// Complete with prefix "s".
result := complete_ root ["s"]
values := result.candidates.map: it.value
expect (values.contains "serve")
expect (values.contains "status")
expect (not (values.contains "build"))
test-option-name-completion:
root := cli.Command "app"
--options=[
cli.Option "output" --short-name="o" --help="Output path.",
cli.Flag "verbose" --short-name="v" --help="Be verbose.",
]
--run=:: null
// Complete "--".
result := complete_ root ["--"]
values := result.candidates.map: it.value
expect (values.contains "--output")
expect (values.contains "--verbose")
expect (values.contains "--no-verbose")
expect (values.contains "--help")
// Complete "-".
result = complete_ root ["-"]
values = result.candidates.map: it.value
expect (values.contains "-o")
expect (values.contains "-v")
expect (values.contains "-h")
// Complete "--ou".
result = complete_ root ["--ou"]
values = result.candidates.map: it.value
expect (values.contains "--output")
expect (not (values.contains "--verbose"))
test-option-value-completion:
root := cli.Command "app"
--options=[
cli.OptionEnum "format" ["json", "text", "csv"] --help="Output format.",
cli.Option "file" --help="Input file.",
]
--run=:: null
// After a non-flag option, complete its values.
result := complete_ root ["--format", ""]
values := result.candidates.map: it.value
expect-equals 3 values.size
expect (values.contains "json")
expect (values.contains "text")
expect (values.contains "csv")
expect-equals DIRECTIVE-NO-FILE-COMPLETION_ result.directive
// After a string option with no completions, expect file completion.
result = complete_ root ["--file", ""]
expect-equals 0 result.candidates.size
expect-equals DIRECTIVE-FILE-COMPLETION_ result.directive
test-enum-completion:
root := cli.Command "app"
--options=[
cli.OptionEnum "color" ["red", "green", "blue"] --help="Pick a color.",
]
--run=:: null
// Complete after --color.
result := complete_ root ["--color", "r"]
values := result.candidates.map: it.value
// All enum values are returned; shell filters by prefix.
expect (values.contains "red")
expect (values.contains "green")
expect (values.contains "blue")
test-flag-completion:
root := cli.Command "app"
--options=[
cli.Flag "verbose" --help="Be verbose.",
]
--run=:: null
// Flags don't need value completion in normal flow since the parser
// doesn't consume the next arg. But --verbose= should complete.
result := complete_ root ["--verbose="]
values := result.candidates.map: it.value
expect (values.contains "--verbose=true")
expect (values.contains "--verbose=false")
test-after-dashdash:
root := cli.Command "app"
--options=[
cli.Option "output" --help="Output path.",
]
--rest=[
cli.Option "files" --multi --help="Input files.",
]
--run=:: null
// After --, no option completion.
result := complete_ root ["--", ""]
values := result.candidates.map: it.value
expect (not (values.contains "--output"))
expect-equals DIRECTIVE-FILE-COMPLETION_ result.directive
test-hidden-excluded:
root := cli.Command "app"
--options=[
cli.Option "secret" --hidden --help="Secret option.",
cli.Option "visible" --help="Visible option.",
]
--subcommands=[
cli.Command "hidden-cmd" --hidden --run=:: null,
cli.Command "visible-cmd" --help="Visible command." --run=:: null,
]
// Hidden options and commands should not appear.
result := complete_ root [""]
values := result.candidates.map: it.value
expect (not (values.contains "--secret"))
expect (not (values.contains "hidden-cmd"))
expect (values.contains "visible-cmd")
result = complete_ root ["--"]
values = result.candidates.map: it.value
expect (not (values.contains "--secret"))
expect (values.contains "--visible")
test-alias-completion:
root := cli.Command "app"
--subcommands=[
cli.Command "device" --aliases=["dev", "d"] --help="Device commands." --run=:: null,
]
result := complete_ root [""]
values := result.candidates.map: it.value
expect (values.contains "device")
expect (values.contains "dev")
expect (values.contains "d")
result = complete_ root ["de"]
values = result.candidates.map: it.value
expect (values.contains "device")
expect (values.contains "dev")
test-already-provided-excluded:
root := cli.Command "app"
--options=[
cli.Option "output" --help="Output path.",
cli.Option "input" --help="Input path.",
]
--run=:: null
// After providing --output, it should not be suggested again.
result := complete_ root ["--output", "foo", "--"]
values := result.candidates.map: it.value
expect (not (values.contains "--output"))
expect (values.contains "--input")
test-multi-still-suggested:
root := cli.Command "app"
--options=[
cli.Option "tag" --multi --help="Tags.",
]
--run=:: null
// Multi options should still be suggested after being provided.
result := complete_ root ["--tag", "v1", "--"]
values := result.candidates.map: it.value
expect (values.contains "--tag")
test-custom-completion-callback:
root := cli.Command "app"
--options=[
cli.Option "host" --help="Target host."
--completion=:: | context/cli.CompletionContext |
hosts := ["localhost", "staging.example.com", "prod.example.com"]
(hosts.filter: it.starts-with context.prefix).map: cli.CompletionCandidate it,
]
--run=:: null
// Custom callback is called with the context.
result := complete_ root ["--host", "local"]
values := result.candidates.map: it.value
expect-equals 1 values.size
expect (values.contains "localhost")
result = complete_ root ["--host", ""]
values = result.candidates.map: it.value
expect-equals 3 values.size
test-completion-context:
// Verify that the completion context provides seen options.
seen/Map? := null
root := cli.Command "app"
--options=[
cli.Option "output" --help="Output path.",
cli.Option "target" --help="Target."
--completion=:: | context/cli.CompletionContext |
seen = context.seen-options
["a", "b"].map: cli.CompletionCandidate it,
]
--run=:: null
result := complete_ root ["--output", "foo", "--target", ""]
expect-not-null seen
expect (seen.contains "output")
expect-equals ["foo"] seen["output"]
test-completion-with-descriptions:
root := cli.Command "app"
--options=[
cli.Option "device" --help="Device to use."
--completion=:: | context/cli.CompletionContext |
[
cli.CompletionCandidate "abc-123" --description="My Phone",
cli.CompletionCandidate "def-456" --description="My Laptop",
],
]
--run=:: null
result := complete_ root ["--device", ""]
expect-equals 2 result.candidates.size
// Check that descriptions are preserved.
candidate := result.candidates.first
expect-equals "abc-123" candidate.value
expect-equals "My Phone" candidate.description
// Also verify --device=prefix preserves descriptions.
result = complete_ root ["--device=a"]
expect-equals 2 result.candidates.size
candidate = result.candidates.first
expect-equals "--device=abc-123" candidate.value
expect-equals "My Phone" candidate.description
test-nested-subcommands:
root := cli.Command "app"
--options=[
cli.Flag "verbose" --help="Be verbose.",
]
--subcommands=[
cli.Command "device"
--help="Device commands."
--options=[
cli.Option "name" --help="Device name.",
]
--subcommands=[
cli.Command "list" --help="List devices." --run=:: null,
cli.Command "show" --help="Show device." --run=:: null,
],
]
// After "device", complete its subcommands.
result := complete_ root ["device", ""]
values := result.candidates.map: it.value
expect (values.contains "list")
expect (values.contains "show")
// Flags should NOT appear without "-" prefix.
expect (not (values.contains "--verbose"))
expect (not (values.contains "--name"))
// But with "-" prefix, options should appear.
result = complete_ root ["device", "-"]
values = result.candidates.map: it.value
expect (values.contains "--verbose")
expect (values.contains "--name")
expect (values.contains "--help")
expect (values.contains "-h")
// Complete "device l".
result = complete_ root ["device", "l"]
values = result.candidates.map: it.value
expect (values.contains "list")
expect (not (values.contains "show"))
test-rest-completion:
root := cli.Command "app"
--rest=[
cli.OptionEnum "action" ["start", "stop", "restart"]
--help="Action to perform.",
]
--run=:: null
// Rest arguments with completion should work.
result := complete_ root [""]
values := result.candidates.map: it.value
expect (values.contains "start")
expect (values.contains "stop")
expect (values.contains "restart")
test-option-equals-value:
root := cli.Command "app"
--options=[
cli.OptionEnum "format" ["json", "text"] --help="Output format.",
]
--run=:: null
// Complete --format=j.
result := complete_ root ["--format=j"]
values := result.candidates.map: it.value
expect (values.contains "--format=json")
expect (values.contains "--format=text")
test-help-only-at-root:
root := cli.Command "app"
--subcommands=[
cli.Command "device"
--help="Device commands."
--subcommands=[
cli.Command "list" --help="List devices." --run=:: null,
],
]
// "help" should appear at root level.
result := complete_ root [""]
values := result.candidates.map: it.value
expect (values.contains "help")
// "help" should NOT appear after descending into a subcommand.
result = complete_ root ["device", ""]
values = result.candidates.map: it.value
expect (not (values.contains "help"))
expect (values.contains "list")
test-flags-hidden-without-dash-prefix:
root := cli.Command "app"
--options=[
cli.Option "output" --help="Output path.",
]
--subcommands=[
cli.Command "serve" --help="Start a server." --run=:: null,
]
// With empty prefix, flags should not appear.
result := complete_ root [""]
values := result.candidates.map: it.value
expect (values.contains "serve")
expect (not (values.contains "--output"))
expect (not (values.contains "--help"))
expect (not (values.contains "-h"))
// With "-" prefix, flags should appear.
result = complete_ root ["-"]
values = result.candidates.map: it.value
expect (values.contains "--output")
expect (values.contains "--help")
expect (values.contains "-h")
// Subcommands should not appear when prefix starts with "-".
expect (not (values.contains "serve"))
test-option-path:
// OptionPath for files should use file-completion directive.
root := cli.Command "app"
--options=[
cli.OptionPath "config" --help="Config file.",
]
--run=:: null
result := complete_ root ["--config", ""]
expect-equals 0 result.candidates.size
expect-equals DIRECTIVE-FILE-COMPLETION_ result.directive
// OptionPath for directories should use directory-completion directive.
root = cli.Command "app"
--options=[
cli.OptionPath "output-dir" --directory --help="Output directory.",
]
--run=:: null
result = complete_ root ["--output-dir", ""]
expect-equals 0 result.candidates.size
expect-equals DIRECTIVE-DIRECTORY-COMPLETION_ result.directive
// OptionPath with --option=prefix should also use the correct directive.
result = complete_ root ["--output-dir=foo"]
expect-equals DIRECTIVE-DIRECTORY-COMPLETION_ result.directive
// OptionPath type should reflect the directory flag.
file-opt := cli.OptionPath "file" --help="A file."
expect-equals "path" file-opt.type
dir-opt := cli.OptionPath "dir" --directory --help="A dir."
expect-equals "directory" dir-opt.type
test-rest-positional-index:
root := cli.Command "app"
--rest=[
cli.OptionEnum "action" ["start", "stop", "restart"]
--help="Action to perform.",
cli.OptionEnum "target" ["dev", "staging", "prod"]
--help="Target environment.",
]
--run=:: null
// With no prior positional args, should complete the first rest option.
result := complete_ root [""]
values := result.candidates.map: it.value
expect (values.contains "start")
expect (not (values.contains "dev"))
// After providing the first positional, should complete the second rest option.
result = complete_ root ["start", ""]
values = result.candidates.map: it.value
expect (not (values.contains "start"))
expect (values.contains "dev")
expect (values.contains "staging")
expect (values.contains "prod")
test-rest-positional-index-after-dashdash:
root := cli.Command "app"
--rest=[
cli.OptionEnum "action" ["start", "stop"]
--help="Action to perform.",
cli.OptionEnum "target" ["dev", "prod"]
--help="Target environment.",
]
--run=:: null
// After -- and one positional, should complete the second rest option.
result := complete_ root ["--", "start", ""]
values := result.candidates.map: it.value
expect (not (values.contains "start"))
expect (values.contains "dev")
expect (values.contains "prod")
test-rest-multi-not-skipped:
root := cli.Command "app"
--rest=[
cli.OptionEnum "files" ["a.txt", "b.txt"] --multi
--help="Input files.",
]
--run=:: null
// Multi rest options should still complete even after prior positionals.
result := complete_ root ["a.txt", ""]
values := result.candidates.map: it.value
expect (values.contains "a.txt")
expect (values.contains "b.txt")
test-rest-dependent-completion:
// A completion callback for a later rest argument can condition on an
// earlier rest argument by reading context.seen-options.
seen/Map? := null
root := cli.Command "app"
--rest=[
cli.OptionEnum "kind" ["user", "group"] --help="Resource kind.",
cli.Option "name" --help="Resource name."
--completion=:: | context/cli.CompletionContext |
seen = context.seen-options
kind := (context.seen-options.get "kind" --if-absent=: [""]).first
candidates := kind == "user"
? ["alice", "bob"]
: ["admins", "devs"]
candidates.map: cli.CompletionCandidate it,
]
--run=:: null
result := complete_ root ["user", ""]
values := result.candidates.map: it.value
expect-equals ["user"] seen["kind"]
expect (values.contains "alice")
expect (values.contains "bob")
expect (not (values.contains "admins"))
result = complete_ root ["group", ""]
values = result.candidates.map: it.value
expect-equals ["group"] seen["kind"]
expect (values.contains "admins")
expect (values.contains "devs")
expect (not (values.contains "alice"))
test-rest-multi-records-all:
// For a multi rest option, seen-options records every value in order.
seen/Map? := null
root := cli.Command "app"
--rest=[
cli.Option "files" --multi --help="Input files."
--completion=:: | context/cli.CompletionContext |
seen = context.seen-options
[],
]
--run=:: null
complete_ root ["a.txt", "b.txt", "c.txt", ""]
expect-equals ["a.txt", "b.txt", "c.txt"] seen["files"]
test-rest-after-dashdash-recorded:
// Positionals after -- are also recorded in seen-options under the
// owning rest option's name.
seen/Map? := null
root := cli.Command "app"
--rest=[
cli.OptionEnum "kind" ["user", "group"] --help="Kind.",
cli.Option "name" --help="Name."
--completion=:: | context/cli.CompletionContext |
seen = context.seen-options
[],
]
--run=:: null
complete_ root ["--", "user", ""]
expect-equals ["user"] seen["kind"]
test-short-option-marks-seen:
root := cli.Command "app"
--options=[
cli.Option "output" --short-name="o" --help="Output path.",
cli.Option "input" --short-name="i" --help="Input path.",
]
--run=:: null
// After providing -o with a value, --output should not be suggested again.
result := complete_ root ["-o", "foo", "-"]
values := result.candidates.map: it.value
expect (not (values.contains "--output"))
expect (not (values.contains "-o"))
expect (values.contains "--input")
expect (values.contains "-i")
test-short-option-pending-value:
root := cli.Command "app"
--options=[
cli.OptionEnum "format" ["json", "text"] --short-name="f" --help="Format.",
]
--run=:: null
// After -f, the next word should complete the option's values.
result := complete_ root ["-f", ""]
values := result.candidates.map: it.value
expect (values.contains "json")
expect (values.contains "text")
expect-equals DIRECTIVE-NO-FILE-COMPLETION_ result.directive
test-packed-short-options:
root := cli.Command "app"
--options=[
cli.Flag "verbose" --short-name="v" --help="Be verbose.",
cli.Option "output" --short-name="o" --help="Output path.",
cli.Flag "force" --short-name="F" --help="Force.",
]
--run=:: null
// Packed flags: -vF should mark both as seen.
result := complete_ root ["-vF", "--"]
values := result.candidates.map: it.value
expect (not (values.contains "--verbose"))
expect (not (values.contains "--force"))
expect (values.contains "--output")
// Packed flag + value option: -vo should set pending for output.
result = complete_ root ["-vo", "out.txt", "--"]
values = result.candidates.map: it.value
expect (not (values.contains "--verbose"))
expect (not (values.contains "--output"))
// Packed with inline value: -ofile.txt should consume the value.
result = complete_ root ["-ofile.txt", "--"]
values = result.candidates.map: it.value
expect (not (values.contains "--output"))
expect (values.contains "--verbose")
test-custom-completer-no-file-fallback:
root := cli.Command "app"
--options=[
cli.Option "host" --help="Target host."
--completion=:: | context/cli.CompletionContext |
hosts := ["localhost", "staging.example.com"]
(hosts.filter: it.starts-with context.prefix).map: cli.CompletionCandidate it,
]
--run=:: null
// When a custom completer returns no matches, should NOT fall back to file completion.
result := complete_ root ["--host", "xyz"]
expect-equals 0 result.candidates.size
expect-equals DIRECTIVE-NO-FILE-COMPLETION_ result.directive
// Same with --option=prefix form.
result = complete_ root ["--host=xyz"]
expect-equals 0 result.candidates.size
expect-equals DIRECTIVE-NO-FILE-COMPLETION_ result.directive
// A plain string option with no completer SHOULD fall back to file completion.
root2 := cli.Command "app"
--options=[
cli.Option "file" --help="Input file.",
]
--run=:: null
result = complete_ root2 ["--file", ""]
expect-equals 0 result.candidates.size
expect-equals DIRECTIVE-FILE-COMPLETION_ result.directive
test-option-extensions:
// OptionPath with extensions should report them in the result.
root := cli.Command "app"
--options=[
cli.OptionPath "config" --extensions=[".toml", ".yaml"] --help="Config file.",
]
--run=:: null
result := complete_ root ["--config", ""]
expect-equals DIRECTIVE-FILE-COMPLETION_ result.directive
expect-equals 2 result.extensions.size
expect (result.extensions.contains ".toml")
expect (result.extensions.contains ".yaml")
// With --option=prefix form.
result = complete_ root ["--config=foo"]
expect-equals DIRECTIVE-FILE-COMPLETION_ result.directive
expect-equals 2 result.extensions.size
// OptionInFile with extensions.
root = cli.Command "app"
--options=[
cli.OptionInFile "input" --extensions=[".csv"] --help="Input file.",
]
--run=:: null
result = complete_ root ["--input", ""]
expect-equals DIRECTIVE-FILE-COMPLETION_ result.directive
expect-equals 1 result.extensions.size
expect (result.extensions.contains ".csv")
// OptionOutFile with extensions.
root = cli.Command "app"
--options=[
cli.OptionOutFile "output" --extensions=[".log"] --help="Output file.",
]
--run=:: null
result = complete_ root ["--output", ""]
expect-equals DIRECTIVE-FILE-COMPLETION_ result.directive
expect-equals 1 result.extensions.size
expect (result.extensions.contains ".log")
// Without extensions, result.extensions should be null.
root = cli.Command "app"
--options=[
cli.OptionPath "file" --help="Any file.",
]
--run=:: null
result = complete_ root ["--file", ""]
expect-equals DIRECTIVE-FILE-COMPLETION_ result.directive
expect-equals null result.extensions
// OptionPath with --directory and --extensions should throw.
expect-throw "OptionPath can't have both --directory and --extensions.":
cli.OptionPath "dir" --directory --extensions=[".txt"] --help="Bad."
// Rest option with extensions.
root = cli.Command "app"
--rest=[
cli.OptionPath "config" --extensions=[".toml"] --help="Config file.",
]
--run=:: null
result = complete_ root [""]
expect-equals DIRECTIVE-FILE-COMPLETION_ result.directive
expect-equals 1 result.extensions.size
expect (result.extensions.contains ".toml")
test-help-completion:
root := cli.Command "app"
--options=[
cli.Flag "verbose" --help="Be verbose.",
]
--subcommands=[
cli.Command "serve" --help="Start a server."
--options=[
cli.Option "port" --help="Port number.",
]
--run=:: null,
cli.Command "build" --help="Build the project." --run=:: null,
cli.Command "device"
--help="Device commands."
--options=[
cli.Option "name" --help="Device name.",
]
--subcommands=[
cli.Command "list" --help="List devices." --run=:: null,
cli.Command "show" --help="Show device." --run=:: null,
],
]
// "app help " should complete subcommands but not "help" itself.
result := complete_ root ["help", ""]
values := result.candidates.map: it.value
expect (values.contains "serve")
expect (values.contains "build")
expect (values.contains "device")
expect (not (values.contains "help"))
// "app help s" should filter by prefix.
result = complete_ root ["help", "s"]
values = result.candidates.map: it.value
expect (values.contains "serve")
expect (not (values.contains "build"))
// "app help device " should complete device's subcommands.
result = complete_ root ["help", "device", ""]
values = result.candidates.map: it.value
expect (values.contains "list")
expect (values.contains "show")
expect (not (values.contains "help"))
// "app help -" should NOT complete options (help doesn't use them).
result = complete_ root ["help", "-"]
values = result.candidates.map: it.value
expect (values.is-empty)
// "app help device -" should NOT complete device's options.
result = complete_ root ["help", "device", "-"]
values = result.candidates.map: it.value
expect (values.is-empty)
// "app help serve " should complete nothing (leaf command in help mode).
result = complete_ root ["help", "serve", ""]
values = result.candidates.map: it.value
expect (values.is-empty)
expect-equals DIRECTIVE-NO-FILE-COMPLETION_ result.directive
test-help-gated-on-availability:
// When a command defines its own "help" option, --help should not be suggested.
root := cli.Command "app"
--options=[
cli.Option "help" --help="Custom help option.",
]
--run=:: null
result := complete_ root ["--"]
values := result.candidates.map: it.value
// The user's own --help is in all-named-options and will appear.
expect (values.contains "--help")
// But it should appear only once (from the user's option, not the synthetic one).
expect-equals 1 (values.filter: it == "--help").size
// When a command uses short-name "h", -h should not be suggested as help.
root2 := cli.Command "app"
--options=[
cli.Flag "hack" --short-name="h" --help="Hack mode.",
]
--run=:: null
result = complete_ root2 ["-"]
values = result.candidates.map: it.value
// -h should appear (for --hack), but only once.
expect (values.contains "-h")
expect-equals 1 (values.filter: it == "-h").size
// --help should still appear since "help" as a name is not taken.
expect (values.contains "--help")