File: guide.md

package info (click to toggle)
ruby-toys 0.19.1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,044 kB
  • sloc: ruby: 3,857; sh: 12; makefile: 4
file content (4178 lines) | stat: -rw-r--r-- 163,415 bytes parent folder | download | duplicates (2)
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
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
<!--
# @title Toys User Guide
-->

# Toys User Guide

Toys is a configurable command line tool. Write commands in Ruby using a simple
DSL, and Toys will provide the command line executable and take care of all the
details such as argument parsing, online help, and error reporting.

Toys is designed for software developers, IT professionals, and other power
users who want to write and organize scripts to automate their workflows. It
can also be used as a replacement for Rake, providing a more natural command
line interface for your project's build tasks.

Unlike most command line frameworks, Toys is *not primarily* designed for
building and shipping a custom command line executable written in Ruby. Rather,
it provides a single executable called `toys` whose functionality you can
define by writing configuration files. (You can, however, build your own custom
command line executable using the related **toys-core** library.)

If this is your first time using Toys, we recommend starting with the
[README](https://dazuma.github.io/toys/gems/toys/latest), which includes a
tutorial that introduces how to install Toys, write and execute tools, and even
use Toys to replace Rake. The tutorial will likely give you enough information
to start using Toys effectively.

This user's guide is also structured like an extended tutorial, but it is much
longer and covers all the features of Toys in much more depth. Read it when
you're ready to unlock all the capabilities of Toys to create sophisticated
command line tools.

## Conceptual overview

Toys is a command line *framework*. It provides an executable called `toys`
with basic functions such as argument parsing and online help. You provide the
actual behavior of the Toys executable by writing **Toys files**.

Toys is a *multi-command* executable. You can define any number of commands,
called **tools**, which can be invoked by passing the tool name as an argument
to the `toys` executable. Tools are arranged in a hierarchy; you can define
**namespaces** that have **subtools**.

Tools can recognize command line arguments in the form of **flags** and
**positional arguments**. Flags can optionally take **values**, while
positional arguments can be **required** or **optional**. Flags can be
organized into **flag groups** that support marking flags or flag combinations
as required.

The configuration of a tool can include **descriptions**, for the tool itself,
and for each command line argument. These descriptions are displayed in the
tool's **online help** screen. Descriptions come in **long** and **short**
forms, which appear in different styles of help.

Toys searches for tools in specifically-named **Toys files** and **Toys
directories**. It searches for these in the current directory, in parent
directories, and in the Toys **search path**. You can also distribute tools in
gems or via git.

Toys provides various features to help you write tools. This includes providing
a **logger** for each tool, **mixins** that provide common functions a tool can
call (such as to control subprocesses and style output), and **templates**
which are prefabricated tools that you can configure for your needs. It also
integrates with testing frameworks, making it easy to write and run **tests**.

Finally, Toys provides useful **built-in behavior**, including automatically
providing flags to display help screens and set verbosity. It also includes a
built-in set of **system tools** that let you inspect and configure the Toys
system itself.

## The Toys command line

In this section, you will learn how Toys parses its command line, identifies a
tool to run, and interprets flags and other command line arguments.

The general form of the `toys` command line is:

    toys [TOOL...] [FLAGS...] [ARGS...]

### Tools

The **tool name** consists of all the command line arguments until the first
argument that begins with a hyphen (which is interpreted as a **flag**), until
no tool with that name exists (in which case the argument is treated as the
first **positional argument**), or until there are no more arguments.

For example, in the following command:

           |----TOOL----|
    $ toys system version

The tool name is `system version`. Notice that the tool name can have multiple
words. Tools are arranged hierarchically. In this case, `system` is a
**namespace** for tools related to the Toys system, and `version` is one of its
**subtools**. It prints the current Toys version.

The words in a tool name can be delimited with spaces as shown above, or
alternately periods or colons. The following commands also invoke the tool
`system version`:

    $ toys system.version
    $ toys system:version

In the following command:

           |TOOL| |ARG|
    $ toys system frodo

There is no subtool `frodo` under the `system` namespace, so Toys works
backward until it finds an existing tool. In this case, the `system` namespace
itself does exist, so Toys runs *it* as the tool, and passes it `frodo` as an
argument.

Namespaces such as `system` are themselves tools and can be executed like any
other tool. In the above case, it takes the argument `frodo`, determines it has
no subtool of that name, and prints an error message. More commonly, though,
you might execute a namespace without arguments:

    $ toys system

This displays the **online help screen** for the `system` namespace, which
includes a list of all its subtools and what they do.

It is also legitimate for the tool name to be empty. This invokes the **root
tool**, the top level namespace:

    $ toys

Like any namespace, invoking the root tool displays its help screen, including
showing the list of all its subtools.

One last example:

    $ toys frodo

If there is no tool called `frodo` in the top level namespace, then once again,
`frodo` is interpreted as an argument to the root tool. The root tool responds
by printing an error message that the `frodo` tool does not exist.

### Flags

**Flags** are generally arguments that begin with a hyphen, and are used to set
options for a tool.

Each tool recognizes a specific set of flags. If you pass an unknown flag to a
tool, the tool will generally display an error message.

Toys follows the typical Unix conventions for flags, specifically those covered
by Ruby's OptionParser library. You can provide short (single-character) flags
with a single hyphen, or long flags with a double hyphen. Some flags can also
take **values**. Following are a few examples.

Here we pass a single short flag (for verbose output).

    $ toys system -v

Here we pass multiple long flags (for verbose output and recursive subtool
search).

    $ toys system --verbose --recursive

You can combine short flags. The following passes both the `-v` and `-r` flags
(i.e. it has the same effect as the previous example.)

    $ toys system -vr

Long flags can be abbreviated, as long as the abbreviation is not ambiguous.
For example, there is only one flag (`--recursive`) beginning with the string
`--rec`, so you can use the shortened form.

    $ toys --rec

However, there are two flags (`--version` and `--verbose`) beginning with
`--ver`, so it cannot be used as an abbreviation. This will cause an error:

    $ toys --ver

Some flags take values. The root tool supports the `--search` flag to search
for tools that have the given keyword.

    $ toys --search=build
    $ toys --search build

The short form of the search flag `-s` also takes a value.

    $ toys -s build
    $ toys -sbuild

If a double hyphen `--` appears by itself in the arguments, it disables flag
parsing from that point. Any further arguments are treated as positional
arguments, even if they begin with hyphens. For example:

           |--FLAG--|   |---ARG---|
    $ toys --verbose -- --recursive

That will cause `--recursive` to be treated as a positional argument. (In this
case, as we saw earlier, the root tool will respond by printing an error
message that no tool named `--recursive` exists.)

Note that a single hyphen by itself `-` is not considered a flag, nor does it
disable flag parsing. It is treated as a normal positional argument.

#### Standard flags

For the most part, each tool specifies which flags and arguments it recognizes.
However, Toys adds a few standard flags globally to every tool. (It is possible
for individual tools to override these flags, but most tools should support
them.) These standard flags include:

*   `--help` (also `-?`) which displays the full help screen for the tool.
*   `--usage` which displays a shorter usage screen for the tool.
*   `--verbose` (also `-v`) which increases the verbosity. This affects the
    tool's logging display, increasing the number of log levels shown. This
    flag can be issued multiple times.
*   `--quiet` (also `-q`) which decreases the verbosity. This affects the
    tool's logging display, decreasing the number of log levels shown. This
    flag can also be issued multiple times.

Namespace tools (tools that have subtools but no explicit functionality of
their own) always behave as though `--help` is invoked. (They do recognize the
flag, but it has no additional effect.) Namespaces also support the following
additional flags:

*   `--all` which displays all subtools, including
    [hidden subtools](#hidden-tools) and namespaces.
*   `--no-recursive` which displays only immediate subtools, instead of the
    default behavior of showing all subtools recursively.
*   `--search=TERM` which displays only subtools whose name or description
    contain the specified search term.
*   `--tools` which displays just the list of subtools rather than the entire
    help screen.

Finally, the root tool also supports:

*   `--version` which displays the current Toys version.

### Positional arguments

Any arguments not recognized as flags or flag arguments, are interpreted as
**positional arguments**. Positional arguments are recognized in order and can
be required or optional.

Each tool recognizes a specific set of positional arguments. If you do not pass
a value for a required argument, or you pass too many arguments, the tool will
generally display an error message.

For example, the built-in `do` tool runs multiple tools in sequence. It
recognizes any number of positional arguments. Those arguments specify which
tools to run and what arguments to pass to them. If, for example, you had a
`build` tool and a `test` tool, you could run them in sequence with:

              |---ARGS---|
    $ toys do build , test

The three arguments `build` and `,` and `test` are positional arguments to the
`do` tool. (The `do` tool uses `,` to delimit the tools that it should run.)

Most tools allow flags and positional arguments to be interspersed. A flag will
be recognized even if it appears after some of the positional arguments.

However, this approach would not work for the `do` tool because its common case
is to pass flags down to the steps it runs. (That is, `do` wants most arguments
to be treated as positional even if they look like flags.) So `do` stops
recognizing flags once it encounters its first positional argument. That is,
you could do this:

              |------------ARGS-----------|
    $ toys do build --staging , test --help

Each tool can choose which behavior it will support, whether or not to enforce
[flags before positional args](#enforcing-flags-before-args).

You can also, of course, stop recognizing flags on the command line by passing
`--` as an argument.

### Tab completion

If you are using the Bash shell, Toys provides custom tab completion. See
[this section](#installing-tab-completion-for-bash) for instructions on
installing tab completion.

Toys will complete tool and subtool names, flags, values passed to flags, and
positional argument values, and it will respect the current context. For
example, if you type:

    $ toys <TAB><TAB>

The tab completion will show you a list of reasonable things that could appear
next, including the defined tool names (such as `system` and `do`) as well as
all the flags supported by the root tool (such as `--help` and `-v`). And of
course, if you start typing something, tab completion will limit the display to
matching completions. The following displays only flags, i.e. completions that
begin with a hyphen:

    $ toys -<TAB><TAB>

And if you type the following:

    $ toys sys<TAB>

It is likely only one tool name starts with `sys`, so completion will
automatically type the rest of `system` for you.

The tab completion for Toys also supports values passed to flags and positional
args. As we shall see later, when you define a flag or a positional argument,
you can specify how completions are computed.

**Note:** Because of the highly dynamic nature of Toys in which tools, flags,
and arguments can be highly customized, the completion implementation actually
requires *executing Toys* so it can analyze your tool configurations. This
unfortunately means paying some upfront latency as the Ruby interpreter starts
up. So you can expect a slight pause when evaluating tab completion for Toys,
at least in comparison with most other tab completions.

## Defining tools

So far we've been experimenting only with the built-in tools provided by Toys.
In this section, you will learn how to define tools by writing a **Toys file**.
We will cover how to write tools, including specifying the functionality of the
tool, the flags and arguments it takes, and how its description appears in the
help screen.

### Basic Toys syntax

A file named `.toys.rb` (note the leading period) in the current working
directory is called a **Toys file**. It defines tools available in that
directory and its subdirectories.

The format of a Toys file is a Ruby DSL that includes directives, methods, and
nested blocks. The actual DSL is specified in the {Toys::DSL::Tool} class.

To create a tool, write a `tool` block, giving the tool a name. Within the
block, use directives to set the properties of the tool, including descriptions
and the flags and arguments recognized by the tool. The actual functionality of
the tool is set by defining a `run` method.

Let's start with an example:

    tool "greet" do
      desc "Print a friendly greeting."
      long_desc "Prints a friendly greeting. You can customize whom to" \
                  " greet, and how friendly it should be.",
                "",
                "Example:",
                ["    toys greet --shout ruby"]

      optional_arg :whom, default: "world", desc: "Whom to greet."
      flag :shout, "-s", "--shout", desc: "Greet loudly."

      def run
        greeting = "Hello, #{whom}!"
        greeting = greeting.upcase if shout
        puts greeting
      end
    end

Its results should be mostly self-evident. But let's unpack a few details.

### Tool descriptions

Each tool can have a **short description** and/or a **long description**. The
short description is a generally a single string that is displayed with the
tool name, at the top of its help page or in a subtool list. The long
description generally includes multiple strings, which are displayed in
multiple lines in the "description" section of the tool's help page. Long
descriptions can include blank lines to separate paragraphs visually.

By default, each description string/line is word-wrapped when displayed. In the
long description example above, the first line is a bit longer than 80
characters, and may be word-wrapped if displayed on an 80-character terminal.

If you need to control the wrapping behavior, pass an array of strings for that
line. Each array element will be considered a unit for wrapping purposes, and
will not be split. The example command in the long description above
illustrates how to prevent a line from being word-wrapped. This is also a
useful technique for preserving spaces and indentation.

For more details, see the reference documentation for {Toys::DSL::Tool#desc}
and {Toys::DSL::Tool#long_desc}.

### Positional arguments

Tools can recognize any number of **positional arguments**. Each argument must
have a name, which is a key that the tool can use to obtain the argument's
value at execution time. Arguments can also have various properties controlling
how values are validated and expressed.

The above example uses the directive {Toys::DSL::Tool#optional_arg} to declare
an **optional argument** named `:whom`. If the argument is provided on the
command line e.g.

    $ toys greet ruby
    Hello, ruby!

Then the option `:whom` is set to the string `"ruby"`. Otherwise, if the
argument is omitted, e.g.

    $ toys greet
    Hello, world!

Then the option `:whom` is set to the default value `"world"`.

If the option name is symbol representing a valid method name (that doesn't
collide with a method already present), Toys will provide a method that you can
use to retrieve the value. In the above example, we retrieve the value for the
option `:whom` by calling the method `whom`. If the option name cannot be made
into a method, you can retrieve the value by calling {Toys::Context#get}.

An argument can also be **required**, which means it must be provided on the
command line; otherwise the tool will report a usage error. You can declare a
required argument using the directive {Toys::DSL::Tool#required_arg}.

#### Parsing required and optional arguments

When command line arguments are parsed, the required arguments are matched
first, in order, followed by the optional arguments. For example:

    tool "args-demo" do
      optional_arg :arg2
      required_arg :arg1

      def run
        puts "Options data is #{options.inspect}"
      end
    end

If a user runs

    $ toys args-demo foo
    Options data is {arg1: "foo", arg2: nil}

Then the required argument `:arg1` will be set to `"foo"`, and the optional
argument `:arg2` will not be set (i.e. it will remain `nil`).

If the user runs:

    $ toys args-demo foo bar
    Options data is {arg1: "foo", arg2: "bar"}

Then `:arg1` is set to `"foo"`, and `:arg2` is set to `"bar"`.

Running the following:

    $ toys args-demo

Will produce a usage error, because no value is set for the required argument
`:arg1`. Similarly, running:

    $ toys args-demo foo bar baz

Will also produce an error, since the tool does not define an argument to
match `"baz"`.

Optional arguments can declare a default value to be used if the argument is
not provided on the command line. For example:

    tool "args-demo" do
      required_arg :arg1
      optional_arg :arg2, default: "the-default"

      def run
        puts "Options data is #{options.inspect}"
      end
    end

Now running the following:

    $ toys args-demo foo
    Options data is {arg1: "foo", arg2: "the-default"}

Will set the required argument to `"foo"` as usual, and the optional argument,
because it is not provided, will default to `"the-default"` instead of `nil`.

#### Remaining arguments

Normally, unmatched arguments will result in an error message. However, you can
provide an "argument" to match all **remaining** unmatched arguments at the
end, using the directive {Toys::DSL::Tool#remaining_args}. For example:

    tool "args-demo" do
      required_arg :arg1
      optional_arg :arg2
      remaining_args :arg3

      def run
        puts "Options data is #{options.inspect}"
      end
    end

Now, we can see how the remaining arguments (if any) are collected by `:arg3`:

    $ toys args-demo foo bar baz qux
    Options data is {arg1: "foo", arg2: "bar", arg3: ["baz", "qux"]}

    $ toys args-demo foo
    Options data is {arg1: "foo", arg2: nil, arg3: []}

Tools can include any number of `required_arg` and `optional_arg` directives,
declaring any number of required and optional arguments. But tools can have at
most only one `remaining_args` directive.

#### Descriptions and the args DSL

Positional arguments can also have short and long descriptions, which are
displayed in online help. Set descriptions via the `desc:` and `long_desc:`
arguments to the argument directive. The `desc:` argument takes a single string
description, while the `long_desc:` argument takes an array of strings. Here is
an example:

    required_arg :arg,
                 desc: "This is a short description for the arg",
                 long_desc: ["Long descriptions can have multiple lines.",
                             "This is the second line."]

See the [above section on Descriptions](#tool-descriptions) for more
information on how descriptions are rendered and word wrapped.

Because long descriptions may be unwieldly to write as a hash argument in this
way, Toys provides an alternate syntax for defining arguments using a block.

    required_arg :arg do
      desc "This is a short description for the arg"
      long_desc "Long desc can be set as multiple lines together,",
                "like this second line."
      long_desc "Or you can call long_desc again to add more lines."
    end

For detailed info on configuring an argument using a block, see the
{Toys::DSL::PositionalArg} class.

#### Argument acceptors

Finally, positional arguments can use **acceptors** to define how to validate
arguments and convert them to Ruby objects for your tool to consume. By
default, Toys will accept any argument string, and expose it to your tool as a
raw string. However, you can provide an acceptor to change this behavior.

Acceptors are part of the OptionParser interface, and are described under the
[type coercion](http://ruby-doc.org/stdlib/libdoc/optparse/rdoc/OptionParser.html#class-OptionParser-label-Type+Coercion)
section. For example, you can provide the `Integer` class as an acceptor, which
will validate that the argument is a well-formed integer, and convert it to an
integer during parsing:

    tool "acceptor-demo" do
      required_arg :age, accept: Integer
      def run
        puts "Next year I will be #{age + 1}"  # Age is an integer
      end
    end

If you pass a non-integer for this argument, Toys will report a usage error.

You can use any of the ready-to-use coercion types provided by OptionParser,
including the special types such as
[OptionParser::DecimalInteger](http://ruby-doc.org/stdlib/libdoc/optparse/rdoc/OptionParser.html#DecimalInteger)
and
[OptionParser::OctalInteger](http://ruby-doc.org/stdlib/libdoc/optparse/rdoc/OptionParser.html#OctalInteger).

You can also create **custom acceptors**. See the
[section below on Custom Acceptors](#custom-acceptors) for more information.

#### Argument completions

Shell tab completion supports positional arguments, and arguments can be
configured to present a set of completion candidates for themselves.

By default, an argument does not provide any completions for itself. To change
that, set the `completion` option. Currently there are three ways to set the
completion:

*   Provide a static set of possible values, as an array of strings.
*   Specify that values should be paths in the file system by setting the
    symbol `:file_system`.
*   Provide a `Proc` that returns possible values.

The following are two example arguments, one that supports a static set of
completions and the other that supports file paths.

    required_arg :language, complete: ["ruby", "elixir", "rust"]
    required_arg :path, complete: :file_system

Completions are somewhat related to acceptors, and it is a common pattern to
set both in concert. But they perform distinct functions. Acceptors affect
argument parsing, whereas completions affect tab completion in the shell.

### Flags

Tools can also recognize **flags** on the command line. In our "greet" example,
we declared a flag named `:shout`:

    flag :shout, "-s", "--shout", desc: "Greet loudly."

Like a positional argument, a flag sets an option based on the command line
arguments passed to the tool. In the case above, the `:shout` option is set to
`true` if either `-s` or `--shout` is provided on the command line; otherwise
it remains falsy. The two flags `-s` and `--shout` are effectively synonyms and
have the same effect. A flag declaration can include any number of synonyms.

As with arguments, Toys can provide a method that you can call to retrieve the
option value set by a flag. This method is provided if the flag name is a
symbol representing a valid method name that does not collide with a method
already present. In our example, Toys provides a method called `shout` that
returns either true or false. If the option name cannot be made into a method,
you can retrieve the value by calling {Toys::Context#get}.

#### Flag types

Toys recognizes the same syntax used by the standard OptionParser library. This
means you can also declare a flag that can be set either to true or false:

    flag :shout, "--[no-]shout"

You can declare that a short or long flag takes a value:

    flag :whom, "--whom=VALUE"
    flag :whom, "--whom VALUE"
    flag :whom, "-wVALUE"
    flag :whom, "-w VALUE"

You can also declare the value to be optional:

    flag :whom, "--whom[=VALUE]"
    flag :whom, "--whom [VALUE]"
    flag :whom, "-w[VALUE]"
    flag :whom, "-w [VALUE]"

Note that if you define multiple flags together, they will all be coerced to
the same "type". That is, if one takes a value, they all will implicitly take
a value. (This is the same behavior as OptionParser.) In this example:

    flag :whom, "-w", "--whom=VALUE"

The `-w` flag will also implicitly take a value, because it is defined as a
synonym of another flag that takes a value.

Note also that Toys will raise an error if those flags are incompatible. For
example:

    flag :whom, "-w[VALUE]", "--whom=VALUE"

Raises an error because one flag's value is optional while the other is
required. (Again, this is consistent with OptionParser's behavior.)

#### Inferred flags

If you do not provide any actual flags, Toys will attempt to infer one from the
name of the option. A one-character name will yield a short flag, and a longer
name a long flag. Hence, the following two definitions are equivalent:

    flag :shout
    flag :shout, "--shout"

And the following two are equivalent:

    flag :S
    flag :S, "-S"

Inferred flags will convert underscores to hyphens. So the following two
definitions are also equivalent:

    flag :call_out
    flag :call_out, "--call-out"

#### Handling optional values

There are some subtleties in how the Ruby OptionParser library treats flags
with optional values. Although Toys does not use OptionParser interally, it
does, for the most part, replicate OptionParser's behavior. It is thus
important to understand that behavior if you use optional values.

First, if a flag has an optional value that is not provided on the command
line, then the option is set to `true`, as if it were a normal boolean flag
that didn't take a value. Consider this example:

    tool "flags-demo" do
      flag :output, "--output [DIRECTORY]", default: "."
      def run
        puts "output is #{output.inspect}"
      end
    end

If a user executes this without passing the `--output` flag, the default will
be printed as we expect.

    $ toys flags-demo
    output is "."

If a user executes this and provides a value for `--output`, it will show up:

    $ toys flags-demo --output /etc
    output is "/etc"

If a user provides `--output` but omits the value, it displays `true`:

    $ toys flags-demo --output
    output is true

Second, if the following argument looks like a flag (i.e. it begins with a
hyphen), it is not treated as an optional value. In this example, the argument
`--verbose` is not treated as the value of `--output` but as a separate flag.
(If `--output` had a *required* value, then `--verbose` would have been treated
as the value.)

    $ toys flags-demo --output --verbose
    output is true

Finally, there is an important difference between the syntax
`"--output [DIRECTORY]"` and `"--output=[DIRECTORY]"`. In the former case, the
following argument (as long as it doesn't look like a flag) will be treated as
the value. In the latter case, however, the following argument is *never*
treated as the value. In that latter case, you *must* use the equals sign
syntax to provide a value.

To illustrate, consider two flags with optional values, one using space and the
other using equals.

    tool "flags-demo-space" do
      flag :output, "--output [DIRECTORY]", default: "."
      set_remaining_args :remaining
      def run
        puts "output is #{output.inspect}"
      end
    end
    tool "flags-demo-equals" do
      flag :output, "--output=[DIRECTORY]", default: "."
      set_remaining_args :remaining
      def run
        puts "output is #{output.inspect}"
      end
    end

Here is the behavior:

    $ toys flags-demo-space --output=/etc
    output is "/etc"
    $ toys flags-demo-space --output /etc
    output is "/etc"
    $ toys flags-demo-equals --output=/etc
    output is "/etc"
    $ toys flags-demo-equals --output /etc
    output is true

#### Flag acceptors

Flags can use **acceptors** to define how to validate values and convert them
to Ruby objects for your tool to consume. By default, Toys will accept a flag
value string in any form, and expose it to your tool as a raw string. However,
you can provide an acceptor to change this behavior.

Acceptors are part of the OptionParser interface, and are described under the
[type coercion](http://ruby-doc.org/stdlib/libdoc/optparse/rdoc/OptionParser.html#class-OptionParser-label-Type+Coercion)
section. For example, you can provide the `Integer` class as an acceptor, which
will validate that the argument is a well-formed integer, and convert it to an
integer during parsing:

    tool "flags-demo" do
      flag :age, accept: Integer
      def run
        puts "Next year I will be #{age + 1}"  # Age is an integer
      end
    end

If you pass a non-integer for this flag value, Toys will report a usage error.

You can use any of the ready-to-use coercion types provided by OptionParser,
including the special types such as
[OptionParser::DecimalInteger](http://ruby-doc.org/stdlib/libdoc/optparse/rdoc/OptionParser.html#DecimalInteger)
and
[OptionParser::OctalInteger](http://ruby-doc.org/stdlib/libdoc/optparse/rdoc/OptionParser.html#OctalInteger).

You can also create **custom acceptors**. See the
[section below on Custom Acceptors](#custom-acceptors) for more information.

#### Defaults and handlers

Flags are usually optional; a flag can appear in a command line zero, one, or
any number of times.

If a flag is not passed in the command line arguments for a tool, by default
its corresponding option value will be `nil`. You can change this by providing
a default value for a flag:

    flag :age, accept: Integer, default: 21

If you pass a flag multiple times on the command line, by default the *last*
appearance of the flag will take effect. That is, suppose you define this flag:

    flag :shout, "--[no-]shout"

Now if you pass `--shout --no-shout`, then the value of the `:shout` option
will be `false`, i.e. the last value set on the command line. This is because a
flag normally *sets* its option value, replacing any previously set value.

You can, however, change this behavior by providing a **handler**. A handler is
a Ruby Proc that defines what a flag does to its option value. It takes up to
three arguments:

1.  The new value given
2.  The previously set value (which might be the default value if this is the
    first appearance of the flag)
3.  A hash containing all the options set so far

The proc must then return the new value for the option.

If the proc takes three arguments, it can use the third argument to read and/or
modify any other option. This enables powerful abilities such as flags that
affect the behavior of other flags.

Effectively, the default behavior (setting the value and ignoring the previous
value) is equivalent to the following handler that takes only the first
argument:

    flag :shout, "--[no-]shout", handler: proc { | val| val }

Toys gives the default handler the special name `:set`. So the above is also
equivalent to:

    flag :shout, "--[no-]shout", handler: :set

The `--verbose` flag, provided automatically by Toys for most tools, shows an
example of an alternate handler. Verbosity is represented by an integer value,
defaulting to 0. The `--verbose` flag can appear any number of times, and
*each* appearance increases the verbosity. Its implementation is internal to
Toys, but looks something like this:

    flag Toys::Context::Key::VERBOSITY, "-v", "--verbose",
         default: 0,
         handler: proc { |_val, prev| prev + 1 }

Similarly, the "--quiet" flag, which decreases the verbosity, is implemented
like this:

    flag Toys::Context::Key::VERBOSITY, "-q", "--quiet",
         default: 0,
         handler: proc { |_val, prev| prev - 1 }

Note that both flags affect the same option name,
{Toys::Context::Key::VERBOSITY}. The first increments it each time it appears,
and the second decrements it. A tool can query this option and get an integer
telling the requested verbosity level, as you will see
[below](#logging-and-verbosity).

Toys provides a few built-in handlers that can be specified by name. We already
discussed the default handler that can be specified by its name `:set` or by
simply omitting the `handler:` option. Another named handler is `:push`. This
handler is intended for flags that take values and can be provided more than
once. The final value is then an array of values.

In the following example, an invocation can provide any number of `--include`
flags, and the `:include` option will be set to an array of the given paths.

    flag :include, "-I", "--include PATH", default: [], handler: :push

The `:push` handler is equivalent to
`proc { |val, array| array.nil? ? [val] : array << val }`.

#### Descriptions and the flags DSL

Flags can also have short and long descriptions, which are displayed in online
help. Set descriptions via the `desc:` and `long_desc:` arguments to the flag
directive. The `desc:` argument takes a single string description, while the
`long_desc:` argument takes an array of strings. Here is an example:

    flag :my_flag, "--my-flag",
         desc: "This is a short description for the arg",
         long_desc: ["Long descriptions can have multiple lines.",
                     "This is the second line."]

See the [above section on Descriptions](#tool-descriptions) for more information on
how descriptions are rendered and word wrapped.

Because long descriptions may be unwieldly to write as a hash argument in this
way, Toys provides an alternate syntax for defining flags using a block.

    flag :my_flag do
      flags "--my-flag"
      desc "This is a short description for the flag"
      long_desc "Long desc can be set as multiple lines together,",
                "like this second line."
      long_desc "Or you can call long_desc again to add more lines."
    end

For detailed info on configuring an flag using a block, see the
{Toys::DSL::Flag} class.

#### Flag completions

Shell tab completion supports flag values, and flags can be configured to
present a set of completion candidates for themselves.

By default, a flag does not provide any completions for itself. To change that,
set the `completion` option. Currently there are three ways to set the
completion:

*   Provide a static set of possible values, as an array of strings.
*   Specify that values should be paths in the file system by setting the
    symbol `:file_system`.
*   Provide a `Proc` that returns possible values.

The following are two example flags, one that supports a static set of
completions and the other that supports file paths.

    flag :language, "--lang=VAL", complete_values: ["ruby", "elixir", "rust"]
    flag :path, "--path=VAL", complete_values: :file_system

Completions are somewhat related to acceptors, and it is a common pattern to
set both in concert. But they perform distinct functions. Acceptors affect
option parsing, whereas completions affect tab completion in the shell.

#### Flag groups

Flags can be organized into groups. This serves two functions:

*   Grouping related flags in the help and usage screens
*   Implementing required flags and other constraints

To create a simple flag group, use the `flag_group` directive, and provide a
block that defines the group's flags. You can also provide a group description
that appears in the help screen.

    flag_group desc: "Debug flags" do
      flag :debug, "-D", desc: "Enable debugger"
      flag :warnings, "-W[VAL]", desc: "Enable warnings"
    end

Flag groups can have a "type" that specifies constraints on the flags contained
in the group. Flags in a simple group like the above are ordinary optional
flags. However, you can specify that flags in the group are required using the
`all_required` directive:

    all_required desc: "Login flags (all required)" do
      flag :username, "--username=VAL", desc: "Set the username (required)"
      flag :password, "--password=VAL", desc: "Set the password (required)"
    end

If the tool is invoked without providing each of these required flags, it will
display an option parsing error.

The `all_required` directive is actually just shorthand for passing
`type: :required` to the `flag_group` directive. So the above is the same as:

    flag_group type: :required, desc: "Login flags (all required)" do
      flag :username, "--username=VAL", desc: "Set the username (required)"
      flag :password, "--password=VAL", desc: "Set the password (required)"
    end

The following are the supported types of flag groups:

*   The `:required` type, which you can create using the directive
    `all_required`. All flags from the group are required and must be provided
    on the command line to avoid an error.
*   The `:exactly_one` type, which you can create using the directive
    `exactly_one_required`. Exactly one, and no more than one, flag from the
    group must be provided on the command line to avoid an error.
*   The `:at_most_one` type, which you can create using the directive
    `at_most_one_required`. At most one flag from the group must be provided
    on the command line to avoid an error.
*   The `:at_least_one` type, which you can create using the directive
    `at_least_one_required`. At least one flag from the group must be provided
    on the command line to avoid an error.
*   The `:optional` type is the default created using the directive
    `flag_group` when no type is specified. Flags in the group are ordinary
    optional flags.

Flag group types are useful for a variety of tools. For example, suppose you
are writing a tool that deploys an app to one of several different kinds of
targets---say, a server, a VM, or a container. You could provide this
configuration for your tool with a flag group:

    tool "deploy" do
      exactly_one_required desc: "Deployment targets" do
        flag :server, "--server=IP_ADDR"
        flag :vm, "--vm=VM_ID"
        flag :container, "--container=CONTAINER_ID"
      end

      def run
        # Now exactly one of server, vm, or container will be set. The other
        # two options will be their default value, nil.
      end
    end

### Tool execution basics

When you run a tool from the command line, Toys will build the tool based on
its definition in a Toys file, and then it will attempt to execute it by
calling the `run` method. Normally, you should define this method in each of
your tools.

Note: If you do not define the `run` method for a tool, Toys provides a default
implementation that displays the tool's help screen. This is typically used for
namespaces, as we shall see [below](#namespaces-and-subtools). Most tools,
however, should define `run`.

Let's revisit the "greet" example we covered earlier.

    tool "greet" do
      optional_arg :whom, default: "world"
      flag :shout, "-s", "--shout"

      def run
        greeting = "Hello, #{whom}!"
        greeting = greeting.upcase if shout
        puts greeting
      end
    end

Note that you can produce output or interact with the console using the normal
Ruby `$stdout`, `$stderr`, and `$stdin` streams.

Note also how the `run` method can access values that were assigned by flags or
positional arguments by just calling a method with that flag or argument name.
When you declare a flag or argument, if the option name is a symbol that is a
valid Ruby method name, Toys will provide a method that you can call to get the
value. In the above example, `whom` and `shout` are such methods.

If you create a flag or argument whose option name is not a symbol *or* is not
a valid method name, you can still get the value by calling
{Toys::Context#get}. For example:

    tool "greet" do
      # The name "whom-to-greet" is not a valid method name.
      optional_arg "whom-to-greet", default: "world"
      flag :shout, "-s", "--shout"

      def run
        # We can access the "whom-to-greet" option using the "get" method.
        greeting = "Hello, #{get('whom-to-greet')}!"
        greeting = greeting.upcase if shout
        puts greeting
      end
    end

If a tool's `run` method finishes normally, Toys will exit with a result code
of 0, indicating success. You can exit immediately and/or provide a nonzero
result by calling {Toys::Context#exit}:

    def run
      puts "Exiting with an error..."
      exit(1)
      puts "Will never get here."
    end

If your `run` method raises an exception, Toys will display the exception and
exit with a nonzero code.

Finally, you can also define additional methods within the tool. These are
available to be called by your `run` method, and can be used to decompose your
tool implementation. Indeed, a tool is actually a class under the hood, and
you can define methods as with any other class. Here's a contrived example:

    tool "greet-many" do
      # Support any number of arguments on the command line
      remaining_args :whom, default: ["world"]
      flag :shout, "-s", "--shout"

      # You can define helper methods like this.
      def greet(name)
        greeting = "Hello, #{name}!"
        greeting = greeting.upcase if shout
        puts greeting
      end

      def run
        whom.each do |name|
          greet(name)
        end
      end
    end

This should be enough to get you started implementing tools. A variety of
additional features are available for your tool implementation and will be
discussed further below. But first we will cover a few important topics.

### Namespaces and subtools

Like many command line frameworks, Toys supports **subtools**. You can, for
example create a tool called "test" that runs your tests for a particular
project, but you might also want "test unit" and "test integration" tools to
run specific subsets of the test suite. One way to do this, of course, is for
the "test" tool to parse "unit" or "integration" as arguments. However, it's
often easier to define them as separate tools, subtools of "test".

To define a subtool, create nested `tool` directives. Here's a simple example:

    tool "test" do
      tool "unit" do
        def run
          puts "run unit tests here..."
        end
      end

      tool "integration" do
        def run
          puts "run integration tests here..."
        end
      end
    end

You can now invoke them like this:

    $ toys test unit
    run unit tests here...
    $ toys test integration
    run integration tests here...

Notice in this case, the parent "test" tool itself has no `run` method. This is
a common pattern: "test" is just a "container" for tools, a way of organizing
your tools. In Toys terminology, it is called a **namespace**. But it is still
a tool, and it can still be run:

    $ toys test

As discussed earlier, Toys provides a default implementation that displays the
help screen, which includes a list of the subtools and their descriptions.

As another example, the "root" tool is also normally a namespace. If you just
run Toys with no arguments:

    $ toys

The root tool will display the overall help screen for Toys.

Although it is a less common pattern, it is possible for a tool to have its own
`run` method even if it has subtools:

    tool "test" do
      def run
        puts "run all tests here..."
      end

      tool "unit" do
        def run
          puts "run only unit tests here..."
        end
      end

      tool "integration" do
        def run
          puts "run only integration tests here..."
        end
      end
    end

Now running `toys test` will run its own implementation.

(Yes, it is even possible to write a `run` method for the root tool. I don't
recommend doing so, because you would lose the root tool's useful default
implementation that lists all your tools.)

Toys allows subtools to be nested arbitrarily deep. In practice, however, more
than two or three levels of hierarchy can be confusing to use.

### Defining subtools using classes

It is also possible to define subtools by subclassing `Toys::Tool`. The class
will support the same DSL as a `tool` block would. For example, this is what
our greet tool would look like as a class:

    class Greet < Toys::Tool
      optional_arg :whom, default: "world"
      flag :shout, "-s", "--shout"

      def run
        greeting = "Hello, #{whom}!"
        greeting = greeting.upcase if shout
        puts greeting
      end
    end

Defining tools as clases is useful if you want your Toys files to look a bit
more like normal Ruby or you want to avoid long blocks. Additionally, they can
be useful to scope constants, which otherwise would be visible throughout the
entire file, as noted in the [section on using constants](#using-constants).

When you define a tool as a class, Toys infers the tool name by converting the
class name to "kebab-case". For example, the `Greet` class above would define a
tool called `greet`. If the class were called `GreetMany`, the tool would be
called `greet-many`. If you need to override this behavior or set a tool name
that is not possible from legal class names, pass the desired tool name to the
`Toys.Tool` method like this:

    class Greet < Toys.Tool("_my_greet")
      optional_arg :whom, default: "world"
      flag :shout, "-s", "--shout"

      def run
        greeting = "Hello, #{whom}!"
        greeting = greeting.upcase if shout
        puts greeting
      end
    end

You can create subtools by nesting classes:

    class Test < Toys::Tool
      class Unit < Toys::Tool
        def run
          puts "run only unit tests here..."
        end
      end

      class Integration < Toys::Tool
        def run
          puts "run only integration tests here..."
        end
      end
    end

The above defines `test unit` and `test integration` tools as expected.

Finally, you may not nest a class-based tool inside a block-based tool. (This
is because Ruby's constant definition rules would put such a class in an
unexpected namespace, leading to potential clashes that would be difficult to
diagnose.) It is, however, permissible to nest a block-based tool inside a
class-based tool.

Hence, this is not allowed, and will result in an exception:

    tool "test" do
      # This will raise Toys::ToolDefinitionError...
      class Unit < Toys::Tool
        def run
          puts "run unit tests..."
        end
      end
    end

But this is legal:

    class Test < Toys::Tool
      tool "unit" do
        def run
          puts "run unit tests..."
        end
      end
    end

In general, though, as a best practice, I recommend against mixing the two
styles. Define tools either as classes or as blocks, not both.

## Understanding Toys files

Toys commands are defined in Toys files. We covered the basic syntax for these
files in the [above section on defining tools](#defining-tools). In this
section, we will take a deeper look at what you can do with Toys files.

### Toys directories

So far we have been defining tools by writing a Toys file named `.toys.rb`
located in the current working directory. This works great if you have a small
number of fairly simple tools, but if you are defining many tools or tools with
long or complex implementations, you may find it better to split your tools in
separate files. You can have Toys load tools from multiple files by creating a
**Toys directory**.

A Toys directory is a directory called `.toys` located in the current working
directory. (Again, note the leading period.) Ruby files inside a Toys directory
(or any of its subdirectories) are loaded when Toys looks for tool definitions.
Furthermore, the name of the Ruby file (and indeed its path relative to the
Toys directory) determines which tool it defines.

For example, one way to create a "greet" tool, as we saw before, is to write a
`.toys.rb` file in the current directory, and populate it like this:

    tool "greet" do
      optional_arg :whom, default: "world"
      def run
        puts "Hello, #{whom}"
      end
    end

You could also create the same tool by creating a `.toys` directory, and then
creating a file `greet.rb` inside that directory.

    (current directory)
    |
    +- .toys/
       |
       +- greet.rb

The contents of `greet.rb` would be:

    optional_arg :whom, default: "world"
    def run
      puts "Hello, #{whom}"
    end

Notice that we did not use a `tool "greet"` block here. That is because the
name of the file `greet.rb` already provides a naming context: Toys already
knows that we are defining a "greet" tool.

If you do include a `tool` block inside the `greet.rb` file, it will create a
*subtool* of `greet`. In other words, the path to the Ruby file defines a
"starting point" for the names of tools defined in that file.

If you create subdirectories inside a Toys directory, their names also
contribute to the namespace of created tools. For example, if you create a
directory `.toys/test` and a file `unit.rb` under that directory, it will
create the tool `test unit`.

    (current directory)
    |
    +- .toys/
       |
       +- greet.rb   <-- defines "greet" (and subtools)
       |
       +- test/
          |
          +- unit.rb   <-- defines "test unit" (and its subtools)

Once again, `test unit` is the "starting point" for tools defined in the
`.toys/test/unit.rb` file. Declarations and methods at the top level of that
file will define the `test unit` tool. Any `tool` blocks you add to that file
will define subtools of `test unit`.

#### Index files

The file name `.toys.rb` can also be used inside Toys directories and
subdirectories. Such files are called **index files**, and they create tools
with the *directory* as the "starting point" namespace. For example, if you
create an index file directly underneath a `.toys` directory, it will define
top level tools (just like a `.toys.rb` file in the current directory.) An
index file located inside `.toys/test` will define tools with `test` as the
"starting point" namespace.

    (current directory)
    |
    +- .toys/
       |
       +- .toys.rb   <-- index file, defines tools at the top level
       |
       +- greet.rb   <-- defines "greet" (and subtools)
       |
       +- test/
          |
          +- .toys.rb   <-- index file, defines "test" (and its subtools)
          |
          +- unit.rb   <-- defines "test unit" (and its subtools)

Index files give you some flexibility for organizing your tools. For example,
if you have a number of subtools of `test`, including a lot of small tools and
one big complex subtool called `unit`, you might define all the simple tools in
the index file `.toys/test/.toys.rb`, while defining the large `test unit` tool
in the separate file `.toys/test/unit.rb`.

Toys also loads the index file in a directory *before* any other files in that
directory. This means they are convenient places to define shared code that can
be used by all the subtools defined in that directory, as we shall see later in
the [section on sharing code](#sharing-code).

### The Toys search path

So far we have seen how to define tools by writing a `.toys.rb` file in the
current directory, or by writing files inside a `.toys` directory in the
current directory. These tools are "scoped" to the current directory. If you
move to a different directory, they may not be available.

When Toys runs, it looks for tools in a **search path**. Specifically:

1.  It looks for a `.toys.rb` file and/or a `.toys` directory in the *current
    working directory*.
2.  It does the same in the *parent directory* of the current directory, and
    then its parent, and so on until it hits either the root of the file system
    or one of the global directories described in (3).
3.  It looks in a list of *global directories*, specified in the environment
    variable `TOYS_PATH`. This variable can contain a colon-delimited list of
    directory paths. If the variable is not set, the current user's *home
    directory*, and the system configuration directory (`/etc` on Unix systems)
    are used by default. Toys does *not* search parents of global directories.

It uses the *first* implementation that it finds for the requested tool. For
example, if the tool `greet` is defined in the `.toys.rb` file in the current
working directory, and also in the `.toys/greet.rb` file of the parent
directory, it will use the version in the current directory.

This means you could write a default implementation for a tool in your home
directory, and override it in the current directory. For example, you could
define a tool `get-credentials` in your home directory that gets credentials
you need for *most* of your projects. But maybe on particular project requires
different credentials, so you could define a different `get-credentials` tool
in that project's directory.

While a tool can be overridden when it is defined at different points in the
search path, it is *not* allowed to provide multiple definitions of a tool at
the *same* point in the search path. For example, if you define the `greet`
tool twice in the same `.toys.rb` file, Toys will report an error. Perhaps less
obviously, if you define `greet` in the `.toys.rb` file in the current
directory, and you also define it in the `.toys/greet.rb` file in the same
current directory, Toys will also report an error, since both are defined at
the same point (the current directory) in the search path.

Note that in the search path above, steps (1) and (2) are *context-dependent*.
That is, they may be different depending on what directory you are in. However,
step (3) is *not* context-dependent, and is searched regardless of where you
are located. Tools defined here are *global*, available everywhere.

#### Stopping search

Though it is uncommon practice, it is possible to stop the search process and
prevent Toys from loading tools further down in the search path (e.g. prevent
tools from being defined from parent directories or global directories). To do
so, use the directive {Toys::DSL::Tool#truncate_load_path!}. This directive
removes all directories further down the search path. It can be used, for
example, to disable global tools when you run Toys from the current directory.
It can also be useful if you are using
[Bundler integration](#using-bundler-with-a-tool) to prevent bundle conflicts
with parent directories, by disabling tools from parent directories.

The `truncate_load_path!` directive works only if no tools from further down
the search path have been loaded yet. It will raise {Toys::ToolDefinitionError}
if it fails to truncate the load path. In most cases, Toys is very smart about
loading tools only when needed, but there are exceptions. To minimize the
chance of problems, if you need to use `truncate_load_path!`, locate it as
early as possible in your Toys files, typically at the top of the
[index file](#index-files).

### Sharing tool definitions

It is a common practice to "share" tools across projects, that is, to implement
tools in a central place and reference them from potentially multiple toys
files. You can do this by "loading" remote tools from a toys file. You can load
tools from a file path on your local file system, from a git repository, or
even from a Ruby gem.

#### Loading from the file system

To load a tool from the file system, use the `load` directive, providing a path
to a file or directory. This will behave as though the file or directory's
contents were inserted in the current location.

As an example, if you had a shared file `/var/share/mytools.rb` as follows:

    # /var/share/mytools.rb
    tool "foo" do
      def run
        puts "Running foo"
      end
    end
    tool "bar" do
      def run
        puts "Running bar"
      end
    end

You could instantiate it in your toys file as follows:

    # .toys.rb
    load "/var/share/mytools.rb"

This has the effect of copying the _contents_ of `mytools.rb` into where it is
being referenced. In this case, it defines the two tools "foo" and "bar" at the
top level of your toys file.

Note that when you load a file, the name of the file does not define a tool or
a tool namespace. In the above case, the tools "foo" and "bar" are defined,
_not_ "mytools foo" or "mytools bar". Avoid the common mistake of referencing a
file that defines at the top level and not loading it inside a tool. For
example, consider a shared file `/var/share/single-tool.rb`:

    # /var/share/single-tool.rb
    desc "This file defines a tool at the top level"
    def run
      puts "Running the top level of single-tool.rb"
    end

If you load it like follows:

    # .toys.rb
    load "/var/share/single-tool.rb"

You will define the run method of the _root tool_. Instead, what you probably
want is:

    # .toys.rb
    tool "my-single-tool" do
      load "/var/share/single-tool.rb"
    end

Equivalently, you can say:

    # .toys.rb
    load "/var/share/single-tool.rb", as: "my-single-tool"

You can also use the `load` directive to load an entire directory. Again, this
will be have as though the directory's contents are inserted at the load point.
The contents of any `.toys.rb` index file will be defined at the load point
itself, and other files within the directory and subdirectories will be
namespaced accordingly.

As an example, consider this shared directory:

    /var/share/mytools
    |
    +- .toys.rb
    |
    +- greet.rb

Here's the contents of `.toys.rb` above:

    # /var/share/mytools/.toys.rb
    tool "foo" do
      def run
        puts "Running foo"
      end
    end

Here's the contents of `greet.rb`:

    # /var/share/mytools/greet.rb
    optional_arg :whom, default: "world"
    def run
      puts "Hello, #{whom}"
    end

You can then define the tools "foo" and "greet" by loading this directory:

    # .toys.rb
    load "/var/share/mytools"

#### Loading from a git repository

You can also load tools over a network using git, by defining the tools in a
git repository (e.g. on GitHub) and referencing it via the `load_git`
directive. This is a simple way to distribute tools across an organization or
over the internet.

The `load_git` directive takes a repository URL, and optionally a path within
the repository and a commit. If you do not specify the path, the entire
repository is loaded as if you were loading a directory. (See the discussion
above on loading from the file system.) Otherwise, if you specify a path, it
should be a file or a directory within the repository. A commit can be a SHA,
a branch, or a tag. If it is omitted, the default HEAD of the repository is
used.

As an example, suppose you have a repository `https://github.com/dazuma/example`
(not a real repo...), with the file `greet.rb` at the top level. You can then
reference that tool as follows:

    # .toys.rb
    load_git remote: "https://github.com/dazuma/example",
             path: "greet.rb",
             as: "greet"

As discussed earlier, if you are loading a file that includes definitions at
the top level, make sure you load it inside a tool, or use the `as:` parameter.

The `load_git` directive uses the Git Cache to download and reference files in
git repositories. This means if you omit the commit or set it to a reference
that can float such as a branch or HEAD, it is possible you might get a stale
commit, if the repository was previously pulled into the cache, and then
additional commits were subsequently pushed to the remote repo. To ensure you
get the latest data from `load_git`, you can set the `update:` parameter. If
set to true, this forces the cache to refresh (i.e. git pull). You can also set
it to an integer, representing a cache lifetime in seconds; the cache will
refresh only if it has been at least that long since the last refresh.

    # .toys.rb
    load_git remote: "https://github.com/dazuma/example",
             path: "greet.rb",
             as: "greet",
             update: 3600  # Pull the latest HEAD if the cache is an hour old

#### Loading from a Ruby gem

Finally, you can define and distribute tools in a Ruby gem, and load them from
the gem. This can be more involved as it requires creating and publishing a
gem, but it is useful for versioning and public distribution of tools.

A Ruby gem is basically just a zip file with some metadata. By convention, it
contains Ruby classes (usually in a top level `lib` directory), and sometimes
executables (often in a top level `bin` directory), and sometimes a few
documentation files. However, it's just a zip file and you can put anything in
it, including toys files.

By convention, toys tools live in the top level `toys` directory in a gem. For
example, you could publish a gem called "my-tools" and include `toys/greet.rb`.
Then, to reference the tool:

    # .toys.rb
    load_gem "my-tools"

This will define the tool "greet". In the process, it will ensure the gem is
installed and loaded (prompting you to confirm the install from rubygems.org if
necessary).

You can specify gem version requirements by providing the `version:` parameter.
For example:

    # .toys.rb
    load_gem "my-tools", version: ["~> 1.5", ">= 1.5.2"]

It takes version requirements in the format used by Rubygems and Bundler. You
can pass a single requirement, multiple requirements as an array, or none.

You can also specify a subpath within the gem to load. The subpath is relative
to the "toys" root directory in the gem. For example:

    # .toys.rb
    load_gem "my-tools", path: "greet.rb", as: "greet"

As before, if you are loading a file that includes definitions at the top
level, make sure you load it inside a tool, or use the `as:` parameter.

By default, `load_gem` looks inside the `toys` top level directory of a gem.
Although this is not recommended, if you need to change this default, you can
set the gem's `"toys_dir"` metadata to a different directory name. If this
metadata is set, `load_gem` will use it as the toys top level directory for
that gem.

You can also specify a different toys top level directory in the `load_gem`
directive. This can be useful if a gem has multiple toys directories:

    # .toys.rb
    load_gem "my-tools", toys_dir: "uncommon-tools"

#### Publishing a tools Ruby gem

Publishing a Ruby gem that includes tools is as simple as publishing a gem with
the tool files in an extra `toys` directory.

Another benefit of distributing tools via Ruby gem is that the gem's library
files can be used in the tools' implementations. When `load_gem` is run, the
gem is activated so its library directory is made available in the require
path. This may be simpler than using `.lib` directories as described later.

By default, the `load_gem` mechanism expects toys files to live inside the
`toys` top level directory of the gem. Although this is not recommended, if you
need to change this default, you can set the gem's `"toys_dir"` metadata to a
different directory name. If this metadata is set, `load_gem` will use it as
the toys top level directory for that gem. In either case, the caller of the
`load_gem` directive can still override it by passing the `:toys_dir` argument.

## The execution environment

This section describes the context and resources available to your tool when it
is running; that is, what you can call from your tool's `run` method.

Each tool is defined as a class that subclasses {Toys::Context}. The base class
defines helper methods, and provides access to a variety of data and objects
relevant to your tool. We have already seen earlier how to use the
{Toys::Context#get} method to retrieve option values, and how to use the
{Toys::Context#exit} method to exit immediately and return an exit code. Now we
will cover other resources available to your tool.

### Built-in context

In addition to the options set by your tool's flags and command line arguments,
a variety of other data and objects are also accessible using
{Toys::Context#get}. For example, you can get the full name of the tool being
executed like this:

    def run
      puts "Current tool is #{get(TOOL_NAME)}"
    end

The `TOOL_NAME` constant above is a well-known key that corresponds to the full
name (as an array of strings) of the running tool. A variety of well-known keys
are defined in the {Toys::Context::Key} module. They include information about
the current execution, such as the tool name and the original command line
arguments passed to it (before they were parsed). They also include some
internal Toys objects, which can be used to do things like write to the logger
or look up and call other tools.

Most of the important context also can be accessed from convenience methods.
For example, the `TOOL_NAME` is also available from {Toys::Context#tool_name}:

    def run
      puts "Current tool is #{tool_name}"
    end

Let's take a look at a few things your tool can do with the objects you can
access from built-in context.

### Logging and verbosity

Toys provides a Logger (a simple instance of the Ruby standard library logger
that writes to standard error) for your tool to use to report status
information. You can access this logger via the `LOGGER` context key, or
{Toys::Context#logger}. For example:

    def run
      logger.warn "Danger Will Robinson!"
    end

The current logger level is controlled by the verbosity. Verbosity is an
integer context value that you can retrieve using the `VERBOSITY` context key
or {Toys::Context#verbosity}. The verbosity is set to 0 by default. This
corresponds to a logger level of `WARN`. That is, warnings, errors, and fatals
are displayed, while infos and debugs are not. However,
[as we saw earlier](#standard-flags), most tools automatically respond to the
`--verbose` and `--quiet` flags, (or `-v` and `-q`), which increment and
decrement the verbosity value, respectively. If you pass `-v` to a tool, the
verbosity is incremented to 1, and the logger level is set to `INFO`. If you
pass `-q`, the verbosity is decremented to -1, and the logger level is set to
`ERROR`. So by using the provided logger, a tool can easily provide command
line based control of the output verbosity.

### Running tools from tools

A common operation a tool might want to do is "call" another tool. This can be
done via the CLI object, which you can retrieve using the `CLI` key or
{Toys::Context#cli}. These return the current instance of {Toys::CLI}, which is
the "main" interface to Toys. In particular, it provides the {Toys::CLI#run}
method, which can be used to call another tool:

    def run
      status = cli.run("greet", "rubyists", "-v")
      exit(status) unless status.zero?
    end

Pass the tool name and arguments as arguments to the run method. It will
execute, and return a process status code (i.e. 0 for success, and nonzero for
error). Make sure you handle the exit status. For example, in most cases, you
should probably exit if the tool you are calling returns a nonzero code.

You can also use the `exec` mixin [described below](#executing-subprocesses) to
run a tool in a separate process. This is particularly useful if you need to
capture or manipulate that tool's input or output stream.

### Helper methods and mixins

The methods of {Toys::Context} are not the only methods available for your tool
to call. We [saw earlier](#tool-execution-basics) that a tool can define
additional methods that you can use as helpers.

You can also include **mixins**, which are modules that bring in a whole set of
helper methods. Include a mixin using the `include` directive:

    tool "greet" do
      include :terminal
      def run
        puts "This is a bold line.", :bold
      end
    end

A mixin can be specified by providing a module itself, or by providing a
**mixin name**. In the above example, we used `:terminal`, which is the name
of a built-in mixin that Toys provides. Among other things, it defines a
special `puts` method that lets you include style information such as `:bold`,
which affects the display on ANSI-capable terminals.

For details on the built-in mixins provided by Toys, see the modules under
{Toys::StandardMixins}. We will look at a few examples of the use of these
mixins below. Built-in mixins have names that are symbols.

You can also define your own mixins, as we will see in the
[upcoming section on defining mixins](#defining-mixins).

### Executing subprocesses

Another common operation you might do in a tool is to execute other binaries.
For example, you might write a tool that shells out to `scp` to copy files to
a remote server.

Ruby itself provides a few convenient methods for simple execution, such as the
[Kernel#system](http://ruby-doc.org/core/Kernel.html#method-i-system) method.
However, these typically provide limited ability to control or interact with
subprocess streams, and you also need to remember to handle the exit status
yourself. If you do want to exert more control over subprocesses, you can use
[Process.spawn](http://ruby-doc.org/core/Process.html#method-c-spawn), or a
higher-level wrapper such as the
[open3 library](http://ruby-doc.org/stdlib/libdoc/open3/rdoc/index.html).

Another alternative is to use the `:exec` built-in mixin. This mixin provides
convenient methods for the common cases of executing subprocesses and capturing
their output, and a powerful block-based interface for controlling streams. The
exec mixin also lets you set a useful default option that causes the tool to
exit automatically if one of its subprocesses exits abnormally.

The exec mixin provides methods for running several different kinds of
subprocesses:

*   Normal processes started by the operating system.
*   Another Ruby process.
*   A shell script.
*   Another tool run in a separate (forked) process.
*   A block run in a separate (forked) process.

For more information, see the {Toys::StandardMixins::Exec} mixin module and the
underyling library {Toys::Utils::Exec}.

### Formatting output

Interacting with the user is a very common function of a command line tool, and
many modern tools include intricately designed and styled output, and terminal
effects such as progress bars and spinners. Toys provides several mixins that
can help create nicer interfaces.

First, there is `:terminal`, which provides some basic terminal features such
as styled output and simple spinners. For information, see the
{Toys::StandardMixins::Terminal} mixin module and the underyling library
{Toys::Utils::Terminal}.

If you prefer the venerable Highline library interface, Toys provides a mixin
called `:highline` that automatically installs the highline gem (version 2.x)
if it is not already available, and makes a highline object available to the
tool. For more information, see the {Toys::StandardMixins::Highline} mixin
module.

The `:pager` mixin provides a convenient interface to pager utilities such as
`less`, which let your users interactively page through long output. For more
information, see the {Toys::StandardMixins::Pager} mixin module and the
underlying library {Toys::Utils::Pager}.

You can also use other third-party gems such as
[tty](https://github.com/piotrmurach/tty). The section below on
[useful gems](#useful-gems) provides some examples.

### Standard base directories

Command line tools often need access to common user-specific files such as
caches and state data. The
[XDG Base Directory Specification](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html)
specifies standard locations for these files and standard environment variables
that can be used to override them. Toys provides easy access to these paths via
the `:xdg` mixin.

Here's a simple example:

    tool "my-tool" do
      include :xdg

      def run
        # Get config file paths, in order from most to least inportant
        config_files = xdg.lookup_config("my-tool/my-config.toml")
        config_files.each do |absolute_path|
          # Read the file...
        end
      end
    end

The `lookup_config` method and many others are documented under the
{Toys::Utils::XDG} class.

## Sharing code

As you accumulate additional and more complex tools, you may find that some of
your tools need to share some common configuration, data, or logic. You might,
for example, have a set of admin scripts that need to do some common
authentication. This section describes several techniques for sharing code
between tools, and describes the scope of Ruby structures, such as methods,
classes, and constants, that you might define in your tools.

### Defining mixins

We [saw earlier](#helper-methods-and-mixins) that you can mix a module (with
all its methods) into your tool using the `include` directive. You can specify
a module itself, or the name of a built-in mixin such as `:exec` or
`:terminal`. But you can also define your own mixin using the `mixin`
directive. A mixin defined in a tool can be `include`d in that tool or any of
its subtools or their subtools, recursively, so it's a useful way to share
code. Here's how that works.

Define a mixin using the `mixin` directive, and give it a name and a block. The
mixin name must be a string. (Symbols are reserved for built-in mixins.) In the
block, you can define methods that will be made available to any tool that
includes the mixin, in the same way that you can include a Ruby module.

(Note that, unlike full modules, mixins allow only methods to be shared. Mixins
do not support constants. See the next section on
[using constants](#using-constants) to learn how Toys handles constants.)

Here's an example. Suppose you had common setup code that you wanted to share
among your testing tools.

    tool "test" do
      # Define a mixin, which is just a collection of methods.
      mixin "common_test_code" do
        def setup
          # Do setup here
        end
      end

      tool "unit" do
        # Include the mixin by name
        include "common_test_code"
        def run
          setup  # Mixin methods are made available
          puts "run only unit tests here..."
        end
      end

      tool "integration" do
        include "common_test_code"
        def run
          setup
          puts "run only integration tests here..."
        end
      end
    end

A mixin is available to the tool in which it is defined, and any subtools and
descendants defined at the same point in the Toys search path, but not from
tools defined in a different point in the search path. For example, if you
define a mixin in a file located in a `.toys` directory, it will be visible to
descendant tools defined in that same directory, but not in a different `.toys`
directory.

A common technique, for example, would be to define a mixin in the
[index file](#index-files) in a Toys directory. You can then include it from
any subtools defined in other files in that same directory.

#### Mixin initializers

Sometimes a mixin will want to initialize some state before the tool executes.
For example, the `:highline` mixin creates an instance of Highline during tool
initialization. To do so, provide an `on_initialize` block in the mixin block.
The initializer block is called within the context of the tool after arguments
are parsed, so it has access to the tool's built-in context and options.

If you provide extra arguments when you `include` a mixin, those are passed to
the initializer block.

For example, suppose the `"common_test_code"` mixin needs to behave differently
depending on the type of tests (unit vs integration). Let's have the subtools
pass a value to the mixin's initializer:

    tool "test" do
      mixin "common_test_code" do
        # Initialize the mixin, and receive the argument passed to the
        # include directive
        on_initialize do |type|
          # Initializers are called in the context of the tool, and so can
          # affect the tool's state.
          set(:test_type, type)
        end

        def setup
          puts "Setting up #{get(:test_type)}..."
        end
      end

      tool "unit" do
        # Pass an extra argument to include
        include "common_test_code", "unit"
        def run
          setup
          puts "run only unit tests here..."
        end
      end

      tool "integration" do
        include "common_test_code", "integration"
        def run
          setup
          puts "run only integration tests here..."
        end
      end
    end

#### Mixin inclusion hooks

A mixin can also optionally provide directives to run when the mixin is
included, by defining an `on_include` block. (This is functionally similar to
defining an `included` method on a Ruby module.) The `on_include` block is
called within the context of the tool DSL, so it can invoke any DSL directives.

If you provide extra arguments when you `include` a mixin, those are passed to
the inclusion block.

### Using constants

You can define and use Ruby constants, i.e. names beginning with a capital
letter, in a Toys file. However, they are subject to Ruby's rules regarding
constant scope and lookup, which can be confusing, especially in a DSL. Toys
tries to simplify those rules and make constant behavior somewhat tractable,
but if you do use constants (which includes modules and classes defined in a
Toys file), it is important to understand how they work.

Constants in Toys are visible only within the Toys file in which they are
defined. They normally behave as though they are defined at the "top level" of
the file. Even if you define a constant lexically "inside" a tool or a mixin,
the constant does *not* end up connected to that tool or mixin; it is defined
at the file level.

    tool "test" do
      tool "unit" do
        # This constant is now usable for the rest of the file
        API_KEY_FOR_TESTING = "12345"
        def run
          # It is visible here
          puts API_KEY_FOR_TESTING
        end
      end

      tool "integration" do
        def run
          # And it is still visible here
          puts API_KEY_FOR_TESTING
        end
      end
    end

(Note it is still possible to attach constants to a tool or mixin by defining
them with `self::`. However, this is uncommon Ruby practice and is mildly
discouraged.)

Because of this, it is highly recommended that you define constants only at the
top level of a Toys file, so it doesn't "look" like it is scoped to something
smaller. In particular, do not attempt to define constants in a mixin, unless
you scope them with `self::`.

Modules and classes defined using the `module` or `class` keyword, are also
constants, and thus follow the same rules. So you could, for example, define a
"mixin" module like this:

    module CommonTestCode
      include Toys::Mixin
      def setup
        # Do setup here
      end
    end

    tool "test" do
      tool "unit" do
        # Include the modules as a mixin
        include CommonTestCode
        def run
          setup  # Module methods are made available
          puts "run only unit tests here..."
        end
      end

      tool "integration" do
        include CommonTestCode
        def run
          setup
          puts "run only integration tests here..."
        end
      end
    end

The difference between this technique and using the `mixin` directive we saw
earlier, is the scope. The module here is accessed via a constant, and so, like
any constant, it is visible only in the same file it is defined in. The `mixin`
directive creates mixins that are visible from *all* files at the same point in
the search path.

Not also, when you define a mixin in this way, you should include `Toys::Mixin`
in the module, as illustrated above. This makes `on_initialize` available in
the module.

### Templates

Another way to share code is to expand a **template**.

A template is a class that inserts a bunch of lines into a Toys file. It is
often used to "instantiate" prefabricated tools. For instance, Toys comes with
a template called "minitest" that can generate a test tool for you. You
instantiate it using the `expand` directive in your Toys file, like this:

    expand :minitest

And it will generate a tool called "test" that runs your test suite.

Most templates generate one or more complete tools. However, it is possible for
a template to generate just part of a tool, such as one or more description
directives. In general, expanding a template simply adds directives to your
Toys file.

Many templates can be configured with options such as the name of the tool to
generate, or details of the tool's behavior. This is done by passing additional
arguments to the `expand` directive, such as:

    expand :minitest, name: "unit-test", warnings: true

Alternatively, you can provide a block to `expand`. It will yield the template
to your block, letting you modify its properties:

    expand :minitest do |t|
      t.name = "unit-test"
      t.warnings = true
    end

Toys provides several built-in templates that are useful for project and gem
development, including templates that generate build, test, and documentation
tools. The `:minitest` template illustrated above is one of these built-in
templates. Like built-in mixins, built-in template names are always symbols.
You can read more about them in the next section on using
[Toys as a Rake replacement](#toys-as-a-rake-replacement).

You can also write your own templates. Here's how...

#### Defining templates

One way to define a template is to use the `template` directive. Like the
`mixin` directive, this creates a named template that you can access inside the
current tool and any of its subtools. Also, like mixins, your template name
must be a string.

Following is a simple template example:

    template "greet" do
      def initialize(name: "greet", whom: "world")
        @name = name
        @whom = whom
      end
      attr_accessor :name
      attr_accessor :whom

      on_expand do |template|
        tool template.name do
          desc "A greeting tool generated from a template"

          static :whom, template.whom

          def run
            puts "Hello, #{whom}!"
          end
        end
      end
    end

    expand "greet"

    expand "greet", name: "greet-ruby", whom: "ruby"

Above we created a template called "greet". A template is simply a class. It
will typically have a constructor, and methods to access configuration
properties. When the template is expanded, the class gets instantiated, and you
can set those properties.

Next, a template has an `on_expand` block. This block contains the Toys file
directives that should be generated by the template. The template object is
passed to the block, so it can access the template configuration when
generating directives. The "greet" template in the above example generates a
tool whose name is set by the template's `name` property.

Notice in the above example, we used the {Toys::DSL::Tool#static} directive to
create a "static" option for `template.whom`. A "static" option is an option
whose value is set directly in code rather than via a flag or argument. We use
it here because we can't access the `template` local variable from within the
`run` method. Static options are a common technique for accessing template
configuration properties from inside tool methods.

By convention, it is a good idea for configuration options for your template to
be settable *both* as arguments to the constructor, *and* as `attr_accessor`
properties. In this way, when you expand the template, options can be provided
either as arguments to the `expand` directive, or in a block passed to the
directive by setting properties on the template object.

#### Template classes

Finally, templates are classes, and you can create a template directly as a
class by including the {Toys::Template} module in your class definition.

    class GreetTemplate
      include Toys::Template

      def initialize(name: "greet", whom: "world")
        @name = name
        @whom = whom
      end
      attr_accessor :name
      attr_accessor :whom

      on_expand do |template|
        tool template.name do
          desc "A greeting tool generated from a template"
          static :whom, template.whom
          def run
            puts "Hello, #{whom}!"
          end
        end
      end
    end

    expand GreetTemplate, name: "greet-ruby", whom: "ruby"

Remember that classes created this way are constants, and so the name
`GreetTemplate` is available only inside the Toys file where it was defined.

You must `include Toys::Template` if you define a template directly as a class,
but you can omit it if you use the `template` directive to define the template
in a block.

Defining templates as classes is also a useful way for third-party gems to
provide Toys integration. For example, suppose you are writing a code analysis
gem, and you want to make it easy for your users to create a Toys tool that
invokes your analysis. Just write a template class in your gem, maybe named
`MyAnalysis::ToysTemplate`. Now, just instruct your users to include the
following in their Toys file:

    require "my_analysis"
    expand MyAnalysis::ToysTemplate

### Loading from a lib directory

For more complicated tools, you might want to write normal Ruby modules and
classes as helpers. Toys provides a way to write Ruby code outside of its DSL
and `require` the code from your tool, using `.lib` directories.

Note: If you are writing tools for distribution in a Ruby gem as described
[earlier](#publishing-a-tools-ruby-gem), consider simply defining your Ruby
modules and classes in the gem's lib directory instead of using this mechanism.

To use `.lib` directories, you must define your tools inside a
[Toys directory](#toys-directories). When a tool is executed, it looks for
directories called `.lib` in the Toys directory, and adds them to the Ruby load
path. Your tool can thus call `require` to load helpers from any Ruby files in
a `.lib` directory.

For example, take the following directory structure:

    (current directory)
    |
    +- .toys/
       |
       +- .lib/   <-- available when a tool is executed
       |  |
       |  +- greeting_helper.rb
       |
       +- greet.rb

The `greeting_helper.rb` file can contain any Ruby code.

    # .toys/.lib/greeting_helper.rb

    module GreetingHelper
      def self.make_greeting(whom)
        "Hello, #{whom}!"
      end
    end

Now you can `require "greeting_helper"` in your `greet` tool.

    # .toys/greet.rb

    tool "greet" do
      optional_arg :whom, default: "world", desc: "Whom to greet."
      def run
        require "greeting_helper"
        puts GreetingHelper.make_greeting(whom)
      end
    end

Note that `.lib` directories are available only when your tool is being *run*,
not when it is being defined. So any `require` statements should be located
*inside* your `run` method.

    tool "greet" do
      # Do not try to require the file here. Toys will not find it because
      # the tool is not yet being run.
      # require "greeting_helper"  # ERRORS!

      optional_arg :whom, default: "world", desc: "Whom to greet."
      def run
        # Require a helper file here, so it is loaded during tool execution.
        require "greeting_helper"
        # Now you can use classes defined in the helper
        puts GreetingHelper.make_greeting(whom)
      end
    end

If your Toys directory has subdirectories, lib directories will be prioritized
by how close they are to the tool being executed. For example:

    (current directory)
    |
    +- .toys/
       |
       +- .lib/   <-- available when any tool defined in this directory
       |  |           is executed
       |  |
       |  +- helper.rb   <-- visible to "greet" but not "test unit"
       |  |
       |  +- helper2.rb   <-- visible to both "greet" and "test unit"
       |
       +- greet.rb
       |
       +- test/
          |
          +- .lib/   <-- available only when tools under "test" are executed
          |  |
          |  +- helper.rb   <-- overrides the other helper.rb when
          |                     "test unit" is executed
          |
          +- unit.rb

### Preloading Ruby files

You can also provide Ruby files that are "preloaded" before tools are defined.
This is useful if those Ruby files are required by the tool definitions
themselves. Like files in the `.lib` directory, preloaded files can define Ruby
classes, modules, and other code. But preloaded files *automatically* loaded
(i.e. you do not `require` them explicitly) *before* your tools are defined.

To use preloaded files, you must define your tools inside a
[Toys directory](#toys-directories). Before any tools inside a directory are
loaded, any file named `.preload.rb` in the directory is automatically
required. Next, any Ruby files inside a subdirectory called `.preload` are also
automatically required.

For example, take the following directory structure:

    (current directory)
    |
    +- .toys/
       |
       +- .preload.rb   <-- required first
       |
       +- greet.rb   <-- defines "greet" (and subtools)
       |
       +- test/
          |
          +- .preload/
          |  |
          |  +- my_classes.rb  <-- required before unit.rb
          |  |
          |  +- my_modules.rb  <-- also required before unit.rb
          |
          +- unit.rb   <-- defines "test unit" (and its subtools)

Toys will execute

    require ".toys/.preload.rb"

first before loading any of the tools in the `.toys` directory (or any of its
subdirectories). Thus, you can define classes used by both the `greet` and the
`test unit` tool in this file.

Furthermore, Toys will also execute

    require ".toys/test/.preload/my_classes.rb"
    require ".toys/test/.preload/my_modules.rb"

first before loading any of the tools in the `test` subdirectory. Thus, any
additional classes needed by `test unit` can be defined in these files.

Either a single `.preload.rb` file or a `.preload` directory, or both, can be
used. If both are present in the same directory, the `.preload.rb` file is
loaded first before the `.preload` directory contents.

## Testing your tools

Tests play a critical part in the maintainability of any Ruby app. However, it
can be difficult to set up tests for command line tools, Rake tasks, and
similar code. Toys embraces testability by providing a dedicated place for
tests, and a set of helpers that make it easy to test tools and parts of tools.

### Writing and running tests

Toys integrates with [Minitest](https://github.com/seattlerb/minitest) to
provide a testing framework for your tools. To write tests, you must define
your tools inside a [Toys directory](#toys-directories). Create a directory
called `.test` inside that directory. You can then write Minitest tests in
files within that directory with names matching `test_*.rb`. You can also
provide other files that do not match that name pattern, as fixture data or
common test tools to load using `require_relative`. For example:

    (current directory)
    |
    +- .toys/
       |
       +- greet.rb   <-- defines "greet" (and subtools)
       |
       +- .test/
          |
          +- test_greet.rb   <-- includes tests
          |
          +- helper.rb   <-- includes helper code but not tests

Test files must be written against Minitest, and can use either unit test or
spec syntax. (RSpec support is not currently available.) The framework will
require `minitest/autorun` for you. Here is trivial example content for the
`test_greet.rb` file:

    # test_greet.rb

    require_relative "helper"

    class MyTest < Minitest::Test
      def test_hello
        assert_equal "hello", MyHelper.hello
      end
    end

And the `helper.rb` file:

    # helper.rb

    module MyHelper
      def self.hello
        "hello"
      end
    end

You can run tests using:

    toys system test

The built-in `system test` tool searches for the closest `.toys` directory, and
runs any tests it finds. In the example above, `toys system test` will find the
"hello" test in the file `.toys/.test/test_greet.rb`, run it, and display the
results.

Now, the example above defines a file called `test_greet.rb` but it doesn't
actually test our `greet` tool. So let's cover how to invoke tools from tests
so you can test their functionality.

### Testing tool functionality

Included in the Toys gem is the helper module {Toys::Testing}, which provides
methods that load and execute tools for testing purposes. You can access these
methods by including this module in your test class or `describe` block:

    class MyTest < Minitest::Test
      include Toys::Testing

      # Write your tests..
    end

The simplest way to test a tool's functionality is by running it and observing
the results. To run a tool, invoke the {Toys::Testing#toys_run_tool} method,
passing in the name of the tool and its arguments as if you were invoking the
tool from the command line. For example, recall our original `greet` tool that
takes an optional argument and a `--shout` flag:

    # greet.rb

    desc "Print a friendly greeting."
    long_desc "Prints a friendly greeting. You can customize whom to" \
                " greet, and how friendly it should be.",
              "",
              "Example:",
              ["    toys greet --shout ruby"]

    optional_arg :whom, default: "world", desc: "Whom to greet."
    flag :shout, "-s", "--shout", desc: "Greet loudly."

    def run
      greeting = "Hello, #{whom}!"
      greeting = greeting.upcase if shout
      puts greeting
    end

To execute the tool from a test, call `toys_run_tool` and pass it `["greet"]`
as the command:

    class GreetTest < Minitest::Test
      include Toys::Testing

      def test_greet_with_no_arguents
        exit_code = toys_run_tool(["greet"])
        assert_equal(0, exit_code)
      end
    end

The `toys_run_tool` method executes the tool and returns the numeric exit code,
which we can assert is 0 for a successful run. However, when we run this test,
notice that "Hello, world!" is printed to the console in the middle of the test
run. That is, of course, what our "greet" tool does. But normally, if you are
testing a tool that writes to standard out, you'll want to assert against that
output rather than have it pollute your test output. So typically, you'll wrap
a call to `toys_run_tool` in a
[`capture_subprocess_io`](http://docs.seattlerb.org/minitest/Minitest/Assertions.html#method-i-capture_subprocess_io)
block, to capture the output. So here's a complete test for our `greet` tool:

    class GreetTest < Minitest::Test
      include Toys::Testing

      def test_greet_with_no_arguents
        out, err = capture_subprocess_io do
          exit_code = toys_run_tool(["greet"])
          assert_equal(0, exit_code)
        end
        assert_equal("Hello, world!\n", out)
        assert_empty(err)
      end
    end

Generally, you should test various use cases of your tool, including the effect
of passing different arguments and flags. For example, here's a test of the
`--shout` flag:

    class GreetTest < Minitest::Test
      include Toys::Testing

      def test_greet_with_shout_flag
        out, err = capture_subprocess_io do
          exit_code = toys_run_tool(["greet", "--shout])
          assert_equal(0, exit_code)
        end
        assert_equal("HELLO, WORLD!\n", out)
        assert_empty(err)
      end
    end

It is important to note that `toys_run_tool` executes the tool *in-process* in
your tests. This may be appropriate for simple tools, but may not work for
other cases such as tools that replace the current process using
[Kernel#exec](http://ruby-doc.org/core/Kernel.html#method-i-exec), or tools
that require user interaction. For these cases, you can run a tool in a
separate forked process using the {Toys::Testing#toys_exec_tool} method, which
we shall look at next.

### Running tools out of process

The {Toys::Testing#toys_exec_tool} method, like {Toys::Testing#toys_run_tool},
executes a tool and lets you assert on its behavior. However, while
`toys_run_tool` executes the tool *within the test process*, `toys_exec_tool`
*forks a separate process* to run the tool. This insulates your tests from
anything disruptive the tool might do (such as replacing the current process).
It also provides access to the powerful subprocess control functionality
provided by Toys, letting you redirect, capture, or interact with the tool's
streams while it is running.

There are two ways to run `toys_exec_tool`: in "capture" or "controller" mode.
If you *do not* pass a block to `toys_exec_tool`, you are running in "capture"
mode. It will close the input stream, and capture the output and error streams
and return an object that provides the exit code and the stream contents. This
object is actually an instance of {Toys::Utils::Exec::Result}, the same class
returned by methods from the `:exec` mixin. Thus, we could use "capture" mode
to rewrite our greeting test as follows:

    class GreetTest < Minitest::Test
      include Toys::Testing

      def test_greet_with_no_arguents
        result = toys_exec_tool(["greet"])
        assert_equal(0, result.exit_code)
        assert_equal("Hello, world!\n", result.captured_out)
        assert_empty(result.captured_err)
      end
    end

If you pass a block to `toys_exec_tool`, you will be running in "controller"
mode. In this mode, all the tool's streams are redirected to a controller
object that is passed to your block. See the controller's interface at
{Toys::Utils::Exec::Controller} for more information.

In either mode, you can still override many aspects of the subprocess's
behavior. For example, "capture" closes the input stream by default, but you
can override this and pass data into the tool's input stream by providing the
`:in` keyword argument. The various arguments available are documented in the
{Toys::Utils::Exec} class. Here is an example, passing a string into our tool's
input stream:

    class GreetTest < Minitest::Test
      include Toys::Testing

      def test_greet_ignores_the_input_stream
        result = toys_exec_tool(["greet"], in: [:string, "I am ignored"])
        assert_equal(0, result.exit_code)
        assert_equal("Hello, world!\n", result.captured_out)
        assert_empty(result.captured_err)
      end
    end

It is important to note that `toys_exec_tool` requires the Ruby runtime and
underlying operating system to support `fork`. Standard "MRI" Ruby running on a
Mac or Linux system will work; but JRuby, TruffleRuby, and any Ruby running on
Windows, will not support `fork` and will not be able to run tests that use
`toys_exec_tool`.

### Testing helper methods

The {Toys::Testing} module also provides a way to load tools and test
individual methods without running the entire tool. This can be useful for
writing unit tests for tools that are large, complex, or otherwise should not
be executed in their entirety during testing.

To test individual methods, pass a block to {Toys::Testing#toys_load_tool}.
This method loads the tool associated with the given command line, parses the
command line arguments, and prepares the tool for execution, but does not
invoke the `run` method. The tool's context is passed to the block, and you can
then execute any of the tool's helper methods and check assertions.

Let's consider an example tool with a helper method:

    tool "hello" do
      flag :shout

      # The normal entrypoint "run" method
      def run
        puts message
      end

      # A helper method
      def message
        shout ? "HELLO" : "hello"
      end
    end

Normally, testing this tool would involve capturing and asserting against the
output printed by the `run` method. However, we can also test the `message`
method in isolation without capturing any streams by loading the tool and
calling the method directly.

    class MyTest < Minitest::Test
      include Toys::Testing

      # A test of the tool when passed no command-line arguments
      def test_message_without_shout
        # Load the tool
        toys_load_tool(["hello"]) do |tool|
          # The tool's context is passed to the block, and you can use it to
          # call the tool's methods.
          assert_equal("hello", tool.message)
        end
      end

      # Another test that includes a command-line flag
      def test_message_with_shout
        toys_load_tool(["hello", "--shout"]) do |tool|
          assert_equal("HELLO", tool.message)
        end
      end
    end

Like `toys_run_tool`, `toys_load_tool` loads the tool *in-process* in your
tests. This means it would be difficult to use it to test methods that replace
the current process or depend on user interaction. It is not currently possible
to fork `toys_load_tool` into a separate process because Minitest would not be
able to track assertions in that separate process. It is therefore appropriate
mainly for true unit tests of isolated methods.

### Organizing and selecting tests

So far we learned how to define tests in a `.test` directory located directly
within a `.toys` directory. However, it is also possible to locate `.test`
directories within subdirectories. Just like tools and tool namespaces can be
defined within a directory structure, so corresponding tests can be associated
with those tools by putting them in the same subdirectories.

Consider the following `.toys` directory that includes `build` and `deploy`
tools:

    (current directory)
    |
    +- .toys/
       |
       +- build/
       |  |
       |  +- .toys.rb   <-- defines "build" (and subtools)
       |  |
       |  +- .test/
       |     |
       |     +- test_build.rb  <-- defines tests for "build"
       |
       +- deploy/
          |
          +- .toys.rb   <-- defines "deploy" (and subtools)
          |
          +- .test/
             |
             +- test_staging.rb  <-- defines tests for "deploy"
             |
             +- test_prod.rb  <-- defines more tests for "deploy"

We've now created separate sets of tests under the `build/` and `deploy/`
subdirectories. These are associated with those tools, and we can now run those
sets of tests independently.

To run just the tests under `build/`:

    toys system test build

To run just the tests under `deploy/`:

    toys system test deploy

Now, if you do not provide a specific tool:

    toys system test

...Toys will recursively search for `.test` directories, and run *all* the
tests, including both the build and deploy tests. To disable this recursive
search, pass `--no-recursive`. In the above case, that will result in no tests
running because there are no tests defined at the top level.

It is also possible to focus on specific tests using the
[minitest-focus](https://github.com/seattlerb/minitest-focus) gem. Pass the
`--minitest-focus` argument to `toys system test` to tell it to load the gem,
and you will be able to use the `focus` keyword in your tests to temporarily
focus on a few tests.

## Using third-party gems

The toys executable itself uses only two gems: **toys** and **toys-core**. It
has no other gem dependencies besides standard gems bundled with Ruby. However,
the Ruby community has developed many resources for building command line
tools, including a variety of gems that provide alternate command line parsing,
control of the ANSI terminal, formatted output such as trees and tables, and
effects such as hidden input, progress bars, various ways to spawn and control
subprocesses, and so forth. You may find some of these gems useful when writing
your tools. Additionally, if you are using Toys for your project's build
scripts, it might be necessary to install your bundle when running some tools.

This section describes how to manage and use external gems with Toys. Note that
running Toys with `bundle exec` is generally *not* recommended. We'll discuss
the reasons for this, and what you can do instead.

### Why not "bundle exec toys"

[Bundler](https://bundler.io) is often used when a command-line program depends
on external gems. You specify the gem dependencies in a `Gemfile`, use bundler
to resolve and install those dependencies, and then run the program prefixed by
`bundle exec` to ensure those dependencies are in the Ruby load path. When
running a Rake task, for example, it is almost automatic for many Ruby
developers to run `bundle exec rake my-task`.

So why not simply run `bundle exec toys my-tool`?

In simple cases, this will work just fine. However, Toys is a much more
flexible tool than Rake, and it covers two cases that are not well served by
`bundle exec`.

First, Toys lets you define *global tools* that are defined in your home
directory or system config directory. (See the previous section on
[the Toys search path](#the-toys-search-path).) These tools are global, and can
be called from anywhere. But if they have gem dependencies, it might not be
feasible for their Gemfiles to be present in every directory from which you
might want to run them.

Second, it's possible for a variety of tools to be available together,
including both locally and globally defined tools, with potentially different
sets of dependencies. With `bundle exec`, you must choose beforehand which
bundle to use.

Although traditional `bundle exec` doesn't always work, Toys provides ways for
individual tools to manage their own gem dependencies.

### Using bundler with a tool

The recommended way for a Toys tool to depend on third-party gems is for the
tool to set up Bundler when it runs. The tool can load a bundle from an
appropriate `Gemfile` at runtime, by including the `:bundler` mixin.

Here's an example. Suppose you are writing a tool in a Rails app. It might, for
example, load the Rails environment and populate some data into the database.
Hence, it needs to run with your app's bundle, represented by your app's
`Gemfile`.

Simply `include :bundler` in your tool definition:

    tool "populate-data" do
      include :bundler

      def run
        # The bundle will be set up before the tool is run,
        # so you can now run code that depends on rails:
        require "./config/environment.rb"
        # ... etc.
      end
    end

When the `:bundler` mixin is included in a tool, it installs a
[mixin initializer](#mixin-initializers) that calls `Bundler.setup` when the
tool is *executed*. This assumes the bundle is already installed, and brings
the appropriate gems into the Ruby load path. That is, it's basically the same
as `bundle exec`, but it applies only to the running tool.

#### Applying bundler to all subtools

In many cases, you might find that bundler is needed for many or most of the
tools you write for a particular project. In this case, you might find it
convenient to use {Toys::DSL::Tool#subtool_apply} to include the bundle in all
your tools. For example:

    # Include bundler in every tool under this one
    subtool_apply do
      include :bundler
    end

    tool "one-tool" do
      # This tool will run with the bundle
      # ...
    end

    tool "another-tool" do
      # So will this tool
      # ...
    end

See the section on
[applying directives to multiple tools](#applying-directives-to-multiple-tools)
for more information on `subtool_apply`.

#### Bundler options

By default, the `:bundler` mixin will look for a `Gemfile` within the `.toys`
directory (if your tool is defined in one), and if one is not found there,
within the [context directory](#the-context-directory) (the directory
containing your `.toys` directory or `.toys.rb` file), and if one still is not
found, in the current working directory. You can change this behavior by
passing an option to the `:bundler` mixin. For example, you can search only the
current working directory by passing `search_dirs: :current` as such:

    tool "populate-data" do
      include :bundler, search_dirs: :current
      # etc...
    end

The `:search_dirs` option takes a either directory path (as a string) or a
symbol indicating a "semantic" directory. You can also pass an array of
directories that will be searched in order. For each directory, Toys will look
for a file called `.gems.rb`, `gems.rb`, or `Gemfile` (in that order) and use
the first one that it finds.

The supported "semantic directory" symbols are `:current` indicating the
current working directory, `:context` indicating the context directory, and
`:toys` indicating the Toys directory in which the tool is defined.
Furthermore, the semantic directory `:toys` is treated specially in that it
looks up the `.toys` directory hierarchy. So if your tool is defined in
`.toys/foo/bar.rb`, it will look for a Gemfile first in `.toys/foo/` and then
in `.toys/`. Additionally, when looking for a Gemfile in `:toys`, it searches
only for `.gems.rb` and `Gemfile`. A file called `gems.rb` is not treated as a
Gemfile under the `:toys` directory, because it could be a tool.

The default gemfile search path, if you do not provide the `search_dirs:`
option, is equivalent to `[:toys, :context, :current]`.

If the bundle is not installed, or is out of date, Toys will ask you whether
you want it to install the bundle first before running the tool. A tool can
also choose to install the bundle without prompting, or simply to raise an
error, by passing another option to the `:bundler` mixin. For example, to
simply install the bundle without asking for confirmation:

    tool "populate-data" do
      include :bundler, on_missing: :install
      # etc...
    end

See the documentation for {Toys::StandardMixins::Bundler} for more information
about bundler options.

#### Solving bundle conflicts

It is important to understand that the `:bundler` mixin installs the bundle
when the tool *executes*, rather than when the tool is defined. Gems in the
bundle will not be available during tool definition, so for example you
*cannot* reference bundled gems when you are setting up the tool's flags,
description, or other directives. This is so that Toys can define tools with
competing bundles. Your Rails app's tools can use that app's bundle, while your
global tools can use a different bundle. They will not conflict because Toys
will not actually load a bundle until one or the other tool is executed. (This
is of course different from using `bundle exec`, which chooses and loads a
bundle before even starting Toys.)

If a *different* bundle (i.e. a different `Gemfile`) is already in effect when
a tool is run, then the `:bundler` mixin will raise an error. Ruby will not let
you set up two different bundles at the same time. This might happen, for
example, if you use `bundle exec` to run Toys, but the tool you are running
asks for a different bundle---one more reason not to use `bundle exec` with
Toys.

It might also happen if one tool that uses one bundle, *calls* a tool that uses
a different bundle. If you need to do this, use the
{Toys::StandardMixins::Exec#exec_separate_tool} method from the `:exec` mixin,
to call the tool. This method spawns a separate process with a clean Bundler
setup for running the tool.

#### When a bundle is needed to define a tool

Usually, the `:bundler` mixin sets up your bundle when the tool is *executed*.
However, occasionally, you need the gems in the bundle to *define* a tool. This
might happen, for instance, if your bundle includes gems that define mixins or
templates used by your tool.

If you need the bundle set up immediately because its gems are needed by the
tool definition, pass the `static: true` option when including the `:bundler`
mixin. For example, if you are using the
[flame_server_toys](https://github.com/AlexWayfer/flame_server_toys) gem, which
provides a template that generates tools for the
[Flame](https://github.com/AlexWayfer/flame) web framework, you could include
the `flame_server_toys` gem in your Gemfile, and make it available for defining
tools:

    # Set up the bundle immediately.
    include :bundler, static: true

    # Now you can use the gems in the bundle when defining tools.
    require "flame_server_toys"
    expand FlameServerToys::Template

There is a big caveat to using `static: true`, which is that you are setting up
a bundle immediately, and as a result any subsequent attempt to set up or use a
different bundle will fail. (See the section on
[bundle conflicts](#solving-bundle-conflicts) for a discussion of other reasons
this can happen.) As a result, it's best not to use `static: true` unless you
*really* need it to define tools. If you do run into this problem, here are two
things you could try:

 1. "Scope" the bundle to the tool or the namespace where you need it. Toys
    makes an effort not to define a tool unless you actually need to execute it
    or one of its subtools, so if you can locate `include :bundler` inside just
    the tool or namespace that needs it, you might be able to avoid conflicts.

 2. Failing that, if you need a particular gem in order to define a tool, you
    could consider activating the gem directly rather than as part of a bundle.
    See the following section on
    [Activating gems directly](#activating-gems-directly) for details on this
    technique.

### Activating gems directly

Although we recommend the `:bundler` mixin for most cases, it is also possible
for a tool to install individual gems, using the `:gems` mixin. This mixin
provides a way for a tool to install individual gems without using Bundler.

Here's an example tool that just runs `rake`. Because it requires rake to be
installed in order to run the tool, we call the
{Toys::StandardMixins::Gems#gem} method (which is provided by the `:gems`
mixin) at the beginning of the `run` method:

    tool "rake" do
      include :gems
      remaining_args :rake_args
      def run
        gem "rake", "~> 12.0"
        Kernel.exec(["rake"] + rake_args)
      end
    end

The `gem` method takes the name of the gem, and an optional set of version
requirements. If a gem matching the given version requirements is installed, it
is activated. If not, the gem is installed (which the user can confirm or
abort). Or, if Toys is being run in a bundle, a message is printed informing
the user that they need to add the gem to their Gemfile.

If a gem satisfying the given version constraints is already activated, it
remains active. If a gem with a conflicting version is already activated, an
exception is raised.

The `:gems` mixin also provides a `gem` *directive* that ensures a gem is
installed while the tool is being defined. In general, we recommend avoiding
doing this, because it could make your tool incompatible with another tool that
might need a competing gem during its definition. Toys would not be able to
define both tools together. However, occasionally it might be useful.

Here's an example tool with flags for each of the HighLine styles. Because
highline is needed to decide what flags to define, we use the `gem` directive
to ensure highline is installed while the tool is being defined.

    tool "highline-styles-demo" do
      include :gems
      gem "highline", "~> 2.0"
      require "highline"
      HighLine::BuiltinStyles::STYLES.each do |style|
        style = style.downcase
        flag style.to_sym, "--#{style}", "Apply #{style} to the text"
      end
      def run
        # ...
      end
    end

Note these methods are a bit different from the
[gem method](http://ruby-doc.org/stdlib/libdoc/rubygems/rdoc/Kernel.html)
provided by Rubygems. The Toys version attempts to install a missing gem for
you, whereas Rubygems will just throw an exception.

### Activating gems outside the DSL

The above techniques for installing a bundle or activating a gem directly, are
all part of the tool definition DSL. However, the functionality is also
available outside the DSL---for example, from a class-based mixin.

To set up a bundle, call {Toys::Utils::Gems#bundle}. (Note that you must
`require "toys/utils/gems"` explicitly before invoking the {Toys::Utils::Gems}
class because, like all classes under `Toys::Utils`, Toys does not load it
automatically.) For example:

    require "toys/utils/gems"
    gem_utils = Toys::Utils::Gems.new
    gem_utils.bundle(search_dirs: Dir.getwd)

To activate single gems explicitly, call {Toys::Utils::Gems#activate}. For
example:

    require "toys/utils/gems"
    gem_utils = Toys::Utils::Gems.new
    gem_utils.activate("highline", "~> 2.0")

### Useful gems

Now that you know how to ensure a gem is installed, either individually or as
part of a bundle, let's look at some third-party gems that you might find
useful when writing tools.

We already saw how to use the **highline** gem. Highline generally provides two
features: terminal styling, and prompts. For these capabilities and many more,
you might also consider [TTY](https://github.com/piotrmurach/tty). It comprises
a suite of gems that you can use separately or in tandem. Here are a few
examples.

To produce styled output, consider
[Pastel](https://github.com/piotrmurach/pastel).

    tool "fancy-output" do
      def run
        require "pastel"
        pastel = Pastel.new
        puts pastel.red("Rubies!")
      end
    end

To create rich user prompts, consider
[tty-prompt](https://github.com/piotrmurach/tty-prompt).

    tool "favorite-language" do
      def run
        require "tty-prompt"
        prompt = TTY::Prompt.new
        lang = prompt.select("What is your favorite language?",
                             %w[Elixir Java Python Ruby Rust Other])
        prompt.say("#{lang} is awesome!")
      end
    end

To create tabular output, consider
[tty-table](https://github.com/piotrmurach/tty-table).

    tool "matrix" do
      def run
        require "tty-table"
        table = TTY::Table.new(["Language", "Creator"],
                               [["Ruby", "Matz"],
                                ["Python", "Guido"],
                                ["Elixir", "Jose"]])
        puts table.render(:ascii)
      end
    end

To show progress, consider
[tty-progressbar](https://github.com/piotrmurach/tty-progressbar) for
deterministic processes, or
[tty-spinner](https://github.com/piotrmurach/tty-spinner) for
non-deterministic.

    tool "waiting" do
      def run
        require "tty-progressbar"
        bar = TTY::ProgressBar.new("Waiting [:bar]", total: 30)
        30.times do
          sleep(0.1)
          bar.advance(1)
        end
      end
    end

A variety of other useful gems can also be found in
[this article](https://lab.hookops.com/ruby-cli-gems.html).

## Toys as a Rake replacement

Toys was designed to organize scripts that can be "scoped" to a project or
directory. Rake is also commonly used for this purpose: you can write a
"Rakefile" that defines rake tasks scoped to a directory. In many cases, Toys
can be used as a replacement for Rake. Indeed, the Toys repository itself
contains a `.toys.rb` file instead of a Rakefile, for running tests, builds,
and so forth.

This section will explore the differences between Toys and Rake, and describe
how to use Toys for some of the things traditionally done with Rake.

### Comparing Toys and Rake

Although Toys and Rake serve many of the same use cases, they have very
different design goals, and it is useful to understand the differences.

Rake's design is based on the classic "make" tool often provided in Unix
development environments. This design focuses on *targets* and *dependencies*,
and is meant for a world where you invoke an external compiler tool whenever
changes are made to an individual source file or any of its dependencies. This
"declarative" approach expresses very well the build process for programs
written in C and similar compiled languages.

Ruby, however, does not have an external compiler, and certainly not one that
requires separate invocation for each source file as does the C compiler. So
although Rake does support file dependencies, they are much less commonly used
than in their Makefile cousins. Instead, in practice, most Rake tasks are not
connected to a dependency at all; they are simply standalone scripts, what
would be called "phony" targets in a Makefile. Such tasks are more imperative
than declarative.

The Toys approach to build tools simply embraces the fact that our build
processes already tend to be imperative. So unlike Rake, Toys does not provide
syntax for describing targets and dependencies, since we generally don't have
them in Ruby programs. Instead, it is optimized for writing imperative tools.

For example, Rake provides a primitive mechanism for passing arguments to a
task, but it is clumsy and quite different from most Unix programs. However, to
do otherwise would clash with Rake's design goal of treating tasks as targets
and dependencies. Toys does not have those design goals, so it is able to
embrace the familiar Unix conventions for command line arguments.

Toys actually borrows some of its design from the "mix" build tool used for
Elixir and Erlang programs. Unlike C, the Erlang and Elixir compilers do their
own dependency management, so mix does not require those capabilities. Instead,
it focuses on making it easy to define imperative tasks.

All told, this boils down to the principle of using the best tool for the job.
There will be times when you need to express file-based dependencies in some of
your build tasks. Rake will continue to be your friend in those cases. However,
for imperative tasks such as "run my tests", "build my YARD documentation", or
"release my gem", you may find Toys easier to use.

### Using Toys to invoke Rake tasks

If you've already written a Rakefile for your project, Toys provides a
convenient way to invoke your existing Rake tasks using Toys. The built-in
`:rake` template reads a Rakefile and automatically generates corresponding
tools.

In the same directory as your Rakefile, create a `.toys.rb` file with the
following contents:

    # In .toys.rb
    expand :rake

Now within that directory, if you had a Rake task called `test`, you can invoke
it with:

    $ toys test

Similarly, a Rake task named `test:integration` can be invoked with either of
the following:

    $ toys test integration
    $ toys test:integration

Rake tasks with arguments are mapped to tool arguments, making it easier to
invoke those tasks using Toys. For example, consider a Rake task with two
arguments, defined as follows:

    # In Rakefile
    task :my_task, [:first, :second] do |task, args|
      do_something_with args[:first]
      do_something_else_with args[:second]
    end

would have to be invoked as follows using rake:

    $ rake my_task[value1,value2]

You may even need to escape the brackets if you are using a shell that treats
them specially. Toys will let you pass them as normal command line arguments:

    $ toys my_task value1 value2

The `:rake` template provides several options. If your Rakefile is named
something other than `Rakefile` or isn't in the current directory, you can
pass an explicit path to it when expanding the template:

    # In .toys.rb
    expand :rake, rakefile_path: "path/to/my_rakefile"

You can also choose to pass arguments as named flags rather than command line
arguments. Set `:use_flags` when expanding the template:

    # In .toys.rb
    expand :rake, use_flags: true

Now with this option, to pass arguments to the tool, use the argument names as
flags:

    $ toys my_task --first=value1 --second=value2

### From Rakefiles to Toys files

Invoking Rake tasks using Toys is an easy first step, but eventually you will
likely want to migrate some of your project's build tasks from Rake to Toys.
The remainder of this section describes the common patterns and features Toys
provides for writing build tasks that are traditionally done with Rake.

Many common Rake tasks can be generated using code provided by either Rake or
the third party library. Different libraries provide different mechanisms for
task generation. For example, a test task might be defined like this:

    require "rake/testtask"
    Rake::TestTask.new do |t|
      t.test_files = FileList["test/test*.rb"]
    end

In Toys, templates are the standard mechanism for generating tools.

    expand :minitest do |t|
      t.files = ["test/test*.rb"]
    end

The following sections will describe some of the built-in templates provided by
Toys to generate common build tools.

Note that Rakefiles and Toys files can coexist in the same directory, so you
can use either or both tools, depending on your needs.

### Running tests

Toys provides a built-in template called `:minitest` for running unit tests
with [minitest](https://github.com/seattlerb/minitest). The following example
directive uses the minitest template to create a tool called `test`:

    expand :minitest, files: ["test/test*.rb"], libs: ["lib", "ext"]

See the {Toys::Templates::Minitest} documentation for details on the available
options.

Toys also provides a built-in template called `:rspec` for running BDD examples
using [RSpec](http://rspec.info). The following example directive uses this
template to create a tool called `spec`:

    expand :rspec, pattern: "spec/**/*_spec.rb", libs: ["lib, "ext"]

See the {Toys::Templates::Rspec} documentation for details on the available
options.

If you want to enforce code style using the
[rubocop gem](https://rubygems.org/gems/rubocop), you can use the built-in
`:rubocop` template. The following directive uses this template to create a
tool called `rubocop`:

    expand :rubocop

See the {Toys::Templates::Rubocop} documentation for details on the available
options.

### Building and releasing gems

The `:gem_build` built-in template can generate a variety of build and release
tools for gems, and is a useful alternative to the Rake tasks provided by
bundler. It is implemented by {Toys::Templates::GemBuild}. The following
directive uses this template to create a tool called `build`:

    expand :gem_build

The `:gem_build` template by default looks for a gemspec file in the current
directory, and builds that gem into a `pkg` directory. You can also build a
specific gem if you have multiple gemspec files.

You can also configure the template so it also releases the gem to Rubygems
(using your stored Rubygems credentials), by setting the `push_gem` option.
For example, here is how to generate a "release" tool that builds and releases
your gem:

    expand :gem_build, name: "release", push_gem: true

See the {Toys::Templates::GemBuild} documentation for details on the various
options for build tools.

To create a "clean" tool, you can use the `:clean` built-in template. For
example:

    expand :clean, paths: ["pkg", "doc", "tmp"]

See the {Toys::Templates::Clean} documentation for details on the various
options for clean.

### Building documentation

Toys provides an `:rdoc` template for creating tools that generate RDoc
documentation, and a `:yardoc` template for creating tools that generate YARD.
Both templates provide a variety of options for controlling documentation
generation. See {Toys::Templates::Rdoc} and {Toys::Templates::Yardoc} for
detailed information.

Here's an example for YARD, creating a tool called `yardoc`:

    expand :yardoc, protected: true, markup: "markdown"

### Gem example

Let's look at a complete example that combines the techniques above to provide
all the basic tools for a Ruby gem. It includes:

* A testing tool that can be invoked using `toys test`
* Code style checking using Rubocop, invoked using `toys rubocop`
* Documentation building using Yardoc, invoked using `toys yardoc`
* Gem building, invoked using `toys build`
* Gem build and release to Rubygems.org, invoked using `toys release`
* A full CI tool, invoked using `toys ci`, that can be run from your favorite
  CI system. It runs the tests and style checks, and checks (but does not
  actually build) the documentation for warnings and completeness.

Below is the full annotated `.toys.rb` file. For many gems, you could drop this
into the gem source repo with minimal or no modifications. Indeed, it is very
similar to the Toys files provided for the **toys** and **toys-core** gems
themselves.

    # This file is .toys.rb

    # A "clean" tool that cleans out anything matched by the gitignore. You can
    # also configure it to clean specific files and directories.
    expand :clean, paths: :gitignore

    # This is the "test" tool.
    expand :minitest, libs: ["lib", "test"]

    # This is the "rubocop" tool.
    expand :rubocop

    # This is the "yardoc" tool. We cause it to fail on warnings and if there
    # are any undocumented objects, which is useful for CI. We also configure
    # the tool so it recognizes the "--no-output" flag. The CI tool will use
    # this flag to invoke yardoc but suppress output, because it just wants to
    # check for warnings.
    expand :yardoc do |t|
      t.generate_output_flag = true
      t.fail_on_warning = true
      t.fail_on_undocumented_objects = true
    end

    # The normal "build" tool that just builds a gem into the pkg directory.
    expand :gem_build

    # An "install" tool that builds the gem and installs it locally.
    expand :gem_build, name: "install", install_gem: true

    # A full gem "release" tool that builds the gem, and pushes it to rubygems.
    # This assumes your local rubygems configuration is set up with the proper
    # credentials.
    expand :gem_build, name: "release", push_gem: true

    # Now we create a full CI tool. It runs the test, rubocop, and yardoc tools
    # and checks for errors. This tool could be invoked from a CI system.
    tool "ci" do
      # The :exec mixin provides the exec_tool() method that we will use to run
      # other tools and check their exit status.
      include :exec
      # The :terminal mixin provides an enhanced "puts" method that lets you
      # write styled text to the terminal.
      include :terminal

      # A helper method, that runs a tool and outputs the result. It also
      # terminates if the tool reported an error.
      def run_stage(name, tool)
        if exec_tool(tool).success?
          puts("** #{name} passed", :green, :bold)
          puts
        else
          puts("** CI terminated: #{name} failed!", :red, :bold)
          exit(1)
        end
      end

      # The main run method. It just calls the above helper method for the
      # three tools we want to run for CI
      def run
        run_stage("Tests", ["test"])
        run_stage("Style checker", ["rubocop"])
        run_stage("Docs generation", ["yardoc", "--no-output"])
      end
    end

## Advanced tool definition techniques

This section covers some additional features that are often useful for writing
tools. I've labeled them "advanced", but all that really means is that this
user's guide didn't happen to have covered them until this section. Each of
these features is very useful for certain types of tools, and it is good at
least to know that you *can* do these things, even if you don't use them
regularly.

### Delegating tools

A tool can **delegate** to another tool, which means it uses the other tool's
flags, arguments, and execution. Effectively, it becomes an "alias"---that is,
an alternate name---for the target tool.

For example, suppose you have a tool called `test` that can be invoked with
`toys test`. You could define a tool `t` that delegates to `test`. Then,
running `toys t` will have the same effect as `toys test`.

To delegate a tool, pass the `:delegate_to` keyword argument to the `tool`
directive. For example:

    tool "test" do
      # Define test tool here...
    end

    tool "t", delegate_to: "test"

Tools can delegate to tools or namespaces. For example, you can delegate `sys`
to the built-in namespace `system`:

    tool "sys", delegate_to: "system"

That will let you run `toys sys version` (which will be the equivalent of
`toys system version`).

To delegate to a subtool, pass an array, or a string delimited by `":"` or
`"."` characters, as the target:

    tool "gem" do
      tool "test" do
        # Define the tool here
      end
    end

    tool "test", delegate_to: ["gem", "test"]

In most cases, if a tool delegates to another tool, you should not do anything
else with it. For example, it should not have its own implementation or contain
any subtools. However, there are a few exceptions. You might, for example, want
a namespace to delegate to one of its subtools:

    tool "test", delegate_to: ["test", "unit"] do
      tool "unit" do
        # Run unit tests
      end
      tool "integration" do
        # Run integration tests
      end
    end

Now `toys test` delegates to, and thus has the same effect as `toys test unit`.

It is also possible to specify the delegate via _relative_ name, using the
`:delegate_relative` keyword argument. This can be helpful if you have a set of
tools and delegates that are meant to be reused under different namespaces.

### Applying directives to multiple tools

Sometimes a group of tools are set up similarly or share a set of flags,
mixins, or other directives. You can apply a set of directives to all subtools
(recursively) of the current tool, using the {Toys::DSL::Tool#subtool_apply}
directive.

For example, it is common for tools to use the `:exec` built-in mixin to invoke
external programs. You can use `subtool_apply` to ensure that the mixin is
included in all subtools, so that you do not need to repeat the `include`
directive in every tool.

    subtool_apply do
      # Include the mixin only if the tool hasn't already done so
      unless include?(:exec)
        include :exec, exit_on_nonzero_status: true
      end
    end

    tool "my-tool" do
      def run
        # This tool has access to methods defined by the :exec mixin
        # because the above block is applied to the tool
        sh "echo hello"
      end
    end

Importantly, `subtool_apply` blocks are "applied" at the *end* of a tool's
definition. Therefore, when using `subtool_apply`, you have the ability to look
at the current definition of the tool to decide whether to apply further
changes. The `subtool_apply` block in the above example uses this technique; it
checks whether the `:exec` mixin has already been included before attempting to
include it. Thus, it is possible for a tool to "override" the inclusion, say,
to use a different configuration:

    tool "another-tool" do
      # Use a different configuration for the :exec mixin.
      # This "overrides" the subtool_apply block above.
      include :exec, exit_on_nonzero_status: false
      def run
        # This is run with exit_on_nonzero_status: false
        sh "echo hello"
      end
    end

### Custom acceptors

We saw earlier that flags and positional arguments can have acceptors, which
control the allowed format, and can also convert the string argument to a Ruby
object. By default, Toys supports the same acceptors recognized by Ruby's
OptionParser library. And like OptionParser, Toys also lets you define your own
acceptors.

Define an acceptor using the `acceptor` directive. You provide a name for the
acceptor, and specify how to validate input strings and how to convert input
strings to Ruby objects. You can then reference the acceptor in that tool or
any of its subtools or their subtools, recursively.

There are several ways to define an acceptor.

You can validate input strings against a regular expression, by passing the
regex to the `acceptor` directive. You can also optionally provide a block to
convert input strings to objects (or omit the block to use the original string
as the option value.) For example, a simple hexadecimal input acceptor might
look like this:

    acceptor("hex", /^[0-9a-fA-F]+$/) { |input| input.to_i(16) }

You can also accept enum values by passing an array of valid values to the
`acceptor` directive. Inputs will be matched against the `to_s` form of the
given values, and will be converted to the value itself. For example, one way
to accept integers from 1 to 5 is:

    acceptor("1to5", [1, 2, 3, 4, 5])

There are various other options. See the reference documentation for
{Toys::DSL::Tool#acceptor}.

An acceptor is available to the tool in which it is defined, and any subtools
and descendants defined at the same point in the Toys search path, but not from
tools defined in a different point in the search path. For example, if you
define an acceptor in a file located in a `.toys` directory, it will be visible
to descendant tools defined in that same directory, but not in a different
`.toys` directory.

A common technique, for example, would be to define an acceptor in the index
file in a Toys directory. You can then include it from any subtools defined in
other files in that same directory.

### Controlling built-in flags

Earlier we saw that certain flags are added automatically to every tool:
`--verbose`, `--quiet`, `--help`, and so forth. You may occasionally want to
disable some of these "built-in" flags. There are two ways to do so:

If you want to use one of the built-in flags for another purpose, simply define
the flag as you choose. Flags explicitly defined by your tool take precedence
over the built-ins.

For example, normally two built-in flags are provided to decrease the verbosity
level: `-q` and `--quiet`. If you define `-q` yourself (for example to activate
a "quick" mode) then `-q` will be repurposed for your flag, but `--quiet` will
still be present to decrease verbosity.

    # Repurposes -q to set the "quick" option instead of "quiet"
    flag :quick, "-q"

You can also completely disable a flag, and *not* repurpose it, using the
`disable_flag` directive. It lets you mark one or more flags as "never use".

For example, if you disable the `-q` flag, then `-q` will no longer be a
built-in flag that decreases the verbosity, but `--quiet` will remain. To
completely disable decreasing the verbosity, disable both `-q` and `--quiet`.

    # Disables -q but leaves --quiet
    disable_flag "-q"

    # Completely disables decreasing verbosity
    disable_flag "-q", "--quiet"

### Enforcing flags before args

By default, tools allow flags and positional arguments to be interspersed when
command line arguments are parsed. This matches the behavior of most common
command line binaries.

However, some tools prefer to follow the convention that all flags must appear
first, followed by positional arguments. In such a tool, once a non-flag
argument appears on the command line, all remaining arguments are treated as
positional, even if they look like a flag and start with a hyphen.

You can configure a tool to follow this alternate parsing strategy using the
`enforce_flags_before_args` directive.

The built-in tool `toys do` is an example of a tool that does this. It
recognizes its own flags (such as `--help` and `--delim`) but once positional
arguments start appearing, it wants further flags to be treated as positional
so it can pass them down to the different steps it is executing. Here is a
simplified excerpt from the implementation that tool:

    tool "do" do
      flag :delim, default: ","
      remaining_args :commands  # the commands to execute
      enforce_flags_before_args
      def run
        # Now commands includes both the commands to run and
        # the "flags" to pass to them.
        commands.each do
          # ...
        end
      end
    end

### Requiring exact flag matches

By default, tools will recognize "shortened" forms of long flags. For example,
most suppose you are defining a tool with long flags:

    tool "my-tool" do
      flag :long_flag_name, "--long-flag-name"
      flag :another_long_flag, "--another-long-flag"
      def run
        # ...
      end
    end

When you invoke this tool, you do not need to type the entire flag names.
Abbreviations will also work:

    $ toys my-tool --long --an

As long as the abbreviation is unambiguous (i.e. there is no other flag that
begins with the same string), the Toys argument parser will recognize the flag.
This is consistent with the behavior of most command line tools (and is also
the behavior of Ruby's OptionParser library.)

However, it is possible to disable this behavior and require that flags be
presented in their entirety, using the `require_exact_flag_match` directive.

    tool "my-tool" do
      require_exact_flag_match
      flag :long_flag_name, "--long-flag-name"
      flag :another_long_flag, "--another-long-flag"
      def run
        # ...
      end
    end

Now, all flags for this tool must be presented in their entirety. Abbreviations
are not allowed.

    $ toys my-tool --long-flag-name --another-long-flag

Currently you can require exact flag matches only at the tool level, applied to
all flags for that tool. You cannot set this option for individual flags.

### Disabling argument parsing

Normally Toys handles parsing command line arguments for you. This makes
writing tools easier, and also allows Toys to generate documentation
automatically for flags and arguments. However, occasionally you'll want Toys
not to perform any parsing, but just to give you the command line arguments
raw. One common case is if your tool turns around and passes its arguments
verbatim to a subprocess.

To disable argument parsing, use the `disable_argument_parsing` directive. This
directive disables parsing and validation of flags and positional arguments.
(Thus, it is incompatible with specifying any flags or arguments for the tool.)
Instead, you can retrieve the raw arguments using the {Toys::Context#args}
method.

Here is an example that wraps calls to git:

    tool "my-git" do
      desc "Prints a message, and then calls git normally"
      disable_argument_parsing
      def run
        puts "Calling my-git!"
        Kernel.exec(["git"] + args)
      end
    end

### Handling signals

If you interrupt a running tool by hitting `CTRL`-`C` or sending it a signal,
Toys will normally terminate execution and display the message `INTERRUPTED` on
the standard error stream.

If your tool needs to handle signals or inerrupts itself, you have several
options. You can rescue the `SignalException` or call `Signal.trap`. Or you can
provide a *signal handler* in your tool using the `on_signal` or `on_interrupt`
directive. These directives either provide a block or designate a named method
to handle a given signal received by the process. A separate handler must be
provided for each signal type. (The `on_interrupt` directive is simply
shorthand for registering a handler for `SIGINT`.)

If a signal or interrupt is received and is not caught via `Signal.trap`, the
following takes place:

1.  Ruby will terminate the tool's `run` method by raising a `SignalException`
    Any `ensure` blocks in the tool will be called.
2.  Toys will rescue the `SignalException` and call the signal handler, either
    a method or a block. If the handler takes an argument, Toys will pass it
    the `SignalException` object.
3.  The signal handler is then responsible for tool execution from that
    point. It can terminate execution by returning or calling `exit`, or it can
    restart or resume processing (perhaps by calling the `run` method again).
    Or it can invoke the normal Toys signal handling (i.e. terminating
    execution and displaying the message `INTERRUPTED` or `SIGNAL RECEIVED`) by
    re-raising *the same* `SignalException` object.
4.  If another signal is received or interrupt takes place during the execution
    of the handler, Toys will terminate the handler by raising a *second*
    `SignalException` (again calling any `ensure` blocks). Then, any matching
    signal handler will be called *again* for the new signal and passed the new
    exception. Any further signals will be handled similarly.

It is possible for a signal handler itself to receive signals. For example, if
you have a long-running `CTRL`-`C` interrupt handler, it itself could get
interrupted. You can tell how many signals have taken place by looking at the
`Exception#cause` property of the `SignalException`. The first signal will have
a cause of `nil`. The second signal (i.e. the first time a signal handler
itself receives a signal) will have a cause pointing to the first
`SignalException` (which in turn has a `nil` cause). The third signal's cause
points at the second, and so forth. Hence, you can determine the signal "depth"
by counting the length of the cause chain, which could be important to prevent
"infinite" signals.

Here is an example that performs a long-running task. The first two times the
task is interrupted, it is restarted. The third time, it is terminated.

    tool "long-running" do
      def long_task(is_restart)
        puts "task #{is_restart ? 're' : ''}starting..."
        sleep 10
        puts "task finished!"
      end

      def run
        long_task(false)
      end

      on_interrupt do |ex|
        # The third interrupt will have a non-nil ex.cause.cause.
        # At that time, just give up and re-raise the exception, which causes
        # it to propagate out and invoke the standard Toys interrupt handler.
        raise ex if ex.cause&.cause
        # Otherwise, restart the long task.
        long_task(true)
      end
    end

### Handling usage errors

Normally, if Toys detects a usage error (such as an unrecognized flag) while
parsing arguments, it will respond by aborting the tool and displaying the
usage error. It is possible to override this behavior by providing your own
usage error handler using the `on_usage_error` directive. This directive either
provides a block to handle usage errors, or designates a named method as the
handler.

If your handler block or method takes a parameter, Toys will pass it the array
of usage errors. Otherwise, you can get the array by calling
{Toys::Context#usage_errors}. This array will provide you with a list of the
usage errors encountered.

You can also get information about the arguments that could not be parsed from
the context. For example, the list of unrecognized flags is available from the
context key `UNMATCHED_FLAGS`.

One common technique is to redirect usage errors back to the `run` method. In
this way, `run` is called regardless of whether argument parsing succeeded or
failed.

    tool "lenient-parser" do
      flag :abc

      on_usage_error :run

      def run
        if usage_errors.empty?
          puts "Usage was correct"
        else
          puts "Usage was not correct"
        end
      end
    end

### Changing the entrypoint

The normal entrypoint for a tool is the `run` method. However, you can change
it using the {Toys::DSL::Tool#to_run} directive, providing either a different
method name as a symbol, or a block that will be called instead of a method to
run the tool.

One reason to change which method is called, is if you need to modify a tool
that was already defined by some other means (like a template expansion). For
example, the tests for the Toys gems include a number of longer-running
"integration" tests that run only when the `TOYS_TEST_INTEGRATION` environment
variable is set. The Toys gems provide standard `test` tools using the
`:minitest` template, but then modify those tools to add an `--integration`
flag. When this flag is set, the `TOYS_TEST_INTEGRATION` environment variable
gets set, causing the integration tests to run. Here's what that looks like:

    # Use the minitest template to generate the normal test tool with the
    # normal "run" method as the entrypoint
    expand :minitest, libs: ["lib", "test"], bundler: true

    # Reopen the "test" tool generated above
    tool "test" do
      # Add a flag to the tool
      flag :integration

      # Create a new run method that "wraps" the existing one
      def run_with_integration
        # First set the environment variable to activate integration tests
        # if requested
        ENV["TOYS_TEST_INTEGRATION"] = "true" if integration

        # Invoke the original run method that was defined in the template
        run
      end

      # Now set the entrypoint to our new method
      to_run :run_with_integration
    end

Passing a block to {Toys::DSL::Tool#to_run} is less common. One reason you
might do it is if you need to access variables from an outer scope. (Normally,
defining a method isolates your variables from any enclosing scopes.) Here is
an example that prints out the time that the tool was *defined* (rather than
executed).

    tool "def-time" do
      # Grab the time during tool definition
      time = Time.now

      # Define the entrypoint using a block rather than a method.
      # The block is still called in the same object context, but now has
      # access to variables from enclosing scopes.
      to_run do
        puts "Defined at #{time}"
      end
    end

The downside of this technique is that the entrypoint is not a method, and
can't be called like one. Which means, among other things, you can't "wrap" it
like we did with the test tool above.

If you need to access information from enclosing scopes, consider instead
setting options using the {Toys::DSL::Tool#static} directive, as illustrated
earlier when we discussed [defining templates](#defining-templates).

    tool "def-time" do
      # Grab the time during tool definition and set it as a static option. 
      static :time, Time.now

      # Now you can access it from a run method like any other option
      def run
        puts "Defined at #{time}"
      end
    end

### Data files

If your tools require images, archives, keys, or other such static data, Toys
provides a convenient place to put data files that can be looked up by tools
either during definition or runtime.

To use data files, you must define your tools inside a
[Toys directory](#toys-directories). Within the Toys directory, create a
directory named `.data` and copy your data files there.

You mcanay then "find" a data file by providing the relative path to the file from
the `.data` directory. When defining a tool, use the
{Toys::DSL::Tool#find_data} directive in a Toys file. Or, at tool execution
time, call {Toys::Context#find_data} (which is a convenience method for getting
the tool source object using the `TOOL_SOURCE` key, and calling
{Toys::SourceInfo#find_data} on it). In either case, `find_data` locates a
matching file (or directory) among the data files, and returns the full path to
that file system object. You can then read the file or perform any other
operation on it.

For example, take the following directory structure:

    (current directory)
    |
    +- .toys/
       |
       +- .data/
       |  |
       |  +- greeting.txt
       |  |
       |  +- desc/
       |     |
       |     +- short.txt
       |
       +- greet.rb   <-- defines "greet" (and subtools)

The data files in `.toys/.data` are available to any tool in the `.toys`
directory or any of its subdirectories. For example, suppose we want our
"greet" tool to use the contents of `greeting.txt`. We can call `find_data` to
read those contents when the tool is executed:

    # greet.rb
    desc "Print a friendly greeting."
    optional_arg :whom, default: "world", desc: "Whom to greet."
    def run
      greeting = IO.read(find_data("greeting.txt")).strip
      puts "#{greeting}, #{whom}!"
    end

You can include directories in the argument to `find_data`. For example, here
is how to use the `find_data` directive to read the short description from the
file "desc/short.txt":

    # greet.rb
    desc IO.read(find_data("desc/short.txt")).strip
    optional_arg :whom, default: "world", desc: "Whom to greet."
    def run
      greeting = IO.read(find_data("greeting.txt")).strip
      puts "#{greeting}, #{whom}!"
    end

The `find_data` mechanism will return the "closest" file or directory found.
In the example below, there is a `desc/short.txt` file in the `.data` directory
at the top level, but there is also a `desc/short.txt` file in the `.data`
directory under `test`. Tools under the `test` directory will find the more
specific data file, while other tools will find the more general file.

    (current directory)
    |
    +- .toys/
       |
       +- .data/
       |  |
       |  +- greeting.txt
       |  |
       |  +- desc/
       |     |
       |     +- short.txt  <-- default description for all tools
       |
       +- greet.rb   <-- defines "greet" (and subtools)
       |
       +- test/
          |
          +- .data/
          |  |
          |  +- desc/
          |     |
          |     +- short.txt  <-- override description for test tools
          |
          +- unit.rb   <-- defines "test unit" (and its subtools)

If, however, you find `greeting.txt` from a tool under `test`, it will still
find the more general `.toys/.data/greeting.txt` file because there is no
overriding file under `.toys/test/.data`.

### The context directory

The **context directory** for a tool is the directory containing the top level
`.toys.rb` file or the `.toys` directory within which the tool is defined. It
is sometimes useful for tools that expect to be run from a specific working
directory.

For example, suppose you have a Ruby project directory:

    my-project/
    |
    +- .toys.rb  <-- project tools defined here
    |
    +- lib/
    |
    +- test/
    |
    etc...

Now suppose you defined a tool that lists the tests:

    tool "list-tests" do
      def run
        puts Dir.glob("test/**/*.rb").join("\n")
      end
    end

This tool assumes it will be run from the main project directory (`my-project`
in the above case). However, Toys lets you invoke tools even if you are in a
subdirectory:

    $ cd lib
    $ toys list-tests  # Does not work

Rake handles this by actually changing the current working directory to the
directory containing the active Rakefile. Toys, however, does not change the
working directory unless you tell it to. You can make the `list-tests` tool
work correctly by changing the directory to the context directory (which is the
directory containing the `.toys.rb` file, i.e. the `my-project` directory.)

    tool "list-tests" do
      def run
        Dir.chdir(context_directory) do
          puts Dir.glob("test/**/*.rb").join("\n")
        end
      end
    end

Note the context directory is different from `__dir__`. It is not necessarily
the directory containing the file being executed, but the directory containing
the entire toys directory structure. So if your tool definition is inside a
`.toys` directory, it will still work:

    my-project/   <-- context_directory still points here
    |
    +- .toys/
    |  |
    |  +- list-tests.rb   <-- tool defined here
    |
    +- lib/
    |
    +- test/
    |
    etc...

This technique is particularly useful for build tools. Indeed, all the build
tools described in the section on
[Toys as a Rake Replacement](#toys-as-a-rake-replacement) automatically move
into the context directory when they execute.

#### Changing the context directory

It is even possible to modify the context directory, causing tools that use the
context directory (such as the standard build tools) to run in a different
directory. Here is an example:

Suppose you have a repository with multiple gems, each in its own directory:

    my-repo/
    |
    +- .toys.rb  <-- all project tools defined here
    |
    +- gem1/
    |  |
    |  +- lib/
    |  |
    |  +- test/
    |
    +- gem2/
    |  |
    |  +- lib/
    |  |
    |  +- test/
    |
    etc...

Assuming all the gems use the same set of build tools, it is possible to define
those tools once in a single `.toys.rb` file and have it run in a particular
gem directory depending on your current location. For example, you can cd into
`gem1` or even `gem1/lib` to have the tools run on `gem1`. Because the standard
build tools execute within the context directory, you can accomplish this by
setting the context directory to the gem directory corresponding to the current
location. That is, if the working directory is `my-repo/gem1/lib`, set the
context directory to `my-repo/gem1`. Here's what that could look like:

    # .toys.rb content

    require "pathname"
    base_dir = Pathname.new(context_directory)
    cur_dir = Pathname.new(Dir.getwd)

    # The gem name is the first segment of the relative path from the context
    # directory to the current directory.
    relative_path = cur_dir.relative_path_from(base_dir).to_s
    gem_name = relative_path.split("/").first

    # Only proceed if we're truly in a subdirectory
    if gem_name && gem_name != "." && gem_name != ".."

      # Now set the context directory to the gem directory.
      set_context_directory(base_dir.join(gem_name).to_s)

      # Define the build tools. Each of these uses the custom context directory
      # set above, and thus runs for the selected gem.
      expand :minitest
      expand :gem_build
      # etc.
    end

### Hidden tools

Tools whose name begins with an underscore (e.g. `_foo`) are called "hidden"
tools. They can be executed the same as any other tool, but are normally
omitted from the subtool list displayed in help and usage screens. You can use
hidden tools as "internal" tools that are meant to be called only as part of
the implementation of other tools.

If you pass the `--all` flag when displaying help, the help screen will include
hidden tools in the subtools list.

## Toys administration using the system tools

Toys comes with a few built-in tools, including some that let you administer
Toys itself. These tools live in the `system` namespace.

### Getting the Toys version

You can get the current version of Toys by running:

    toys system version

Note that the same output can be obtained by passing the `--version` flag to
the root tool:

    toys --version

### Upgrading Toys

To update Toys to the latest released version, run:

    toys system update

This will determine the latest version from Rubygems, and update your Toys
installation if it is not already current.

Normally it asks you for confirmation before downloading. To disable
interactive confirmation, pass the `--yes` flag.

A similar effect can of course be obtained by running `gem install toys`.

### Installing tab completion for Bash

Toys provides tab completion for the bash shell, and lets tools customize the
completions for their arguments. However, you need to install the Toys
completion tool into your shell. The following command sets up tab completion
the current shell:

    $(toys system bash-completion install)

Typically, you will want to include the above in your `.bashrc` or other bash
initialization file.

By default, this associates the Toys tab completion logic with the `toys`
executable. If you have other names or aliases for the executable, pass them as
arguments. For example, I use `t` as an alias for `toys`, and I therefore
install Toys's completion logic for `t`:

    $(toys system bash-completion install t)

You can also remove the completion logic from the current shell:

    $(toys system bash-completion remove)
    $(toys system bash-completion remove t)

At this time, bash is the only shell that is supported directly. If you are
using zsh, however, you can use the `bashcompinit` function to load the toys
bash completion (as well as other bash-based completions). This *mostly* works,
with a few caveats. Native zsh completion is on the future roadmap.

### Getting information about tools

Sometimes you need to introspect the list of defined tools. For example, you
might have a CI job that runs a Toys tool if available, but falls back to a
Rake task if no Toys tools are defined. To determine whether a tool exists, you
could use the status code returned from `system tools show`.

Two operations are available under the `system tools` namespace, one to search
and list tools, and another to get information about a specific tool. In both
cases, you can view results as either YAML or JSON. For more information, see
the help screens:

    toys system tools list --help
    toys system tools show --help

### Managing the Git cache

Toys manages a cache of files downloaded from remote Git repositories. This is
used, for example, to share tools via Git. You can manage the contents of the
Toys Git Cache using subtools under the `system git-cache` namespace. This
includes querying the cache and using it to download files from git, gathering
information about cache status, and deleting old data.

To display general information and a list of tools, run:

    toys system git-cache --help

By default, files in the Git Cache are read-only. This is to prevent code that
uses the cache from modifying those files in place and thus causing problems
for other users of the cache. However, some environments that auto-delete cache
directories might have a problem with this behavior, so the Git Cache supports
an environment variable `TOYS_GIT_CACHE_WRITABLE` which, if set to a nonempty
value, causes cache files to be writable.

## Writing your own CLI using Toys

Although Toys is not primarily designed to help you write a custom command-line
executable, you can use it in that way. Toys is factored into two gems:
**toys-core**, which includes all the underlying machinery for creating
command-line executables, and **toys**, which is really just a wrapper that
provides the `toys` executable itself and its built-in commands and behavior.
To write your own command line executable based on the Toys system, just
require the **toys-core** gem and configure your executable the way you want.

Toys-Core is modular and lets you customize much of the behavior of a command
line executable, simply by setting options or adding plugins. For example:

*   Toys itself automatically adds a number of flags, such as `--verbose` and
    `--help`, to each tool. Toys-Core lets you customize what flags (if any)
    are automatically added for your own command line executable.
*   Toys itself provides a default way to run tools that have no `run` method.
    It assumes such tools are namespaces, and displays the online help screen.
    Toys-Core lets you provide an alternate default run method for your own
    command line executable.
*   Toys itself provides several built-in tools, such as `do`, and `system`.
    Toys-Core lets you write your own command line executable with its own
    built-in tools.
*   Toys itself implements a particular search path for user-provided Toys
    files, and looks for specific file and directory names such as `.toys.rb`.
    Toys-Core lets you change the search path, the file/directory names, or
    disable user-provided Toys files altogether for your own command line
    executable. Indeed, most command line executables do not need
    user-customizable tools, and can ship with only built-in tools.
*   Toys itself has a particular way of displaying online help and reporting
    errors. Toys-Core lets your own command line executable customize these and
    many other features.

For more information, see the
[Toys-Core documentation](https://dazuma.github.io/toys/gems/toys-core/latest/).