boxlite-cli 0.10.2

Command-line interface for BoxLite container runtime
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
//! CLI definition and argument parsing for boxlite-cli.
//! This module contains all CLI-related code including the main CLI structure,
//! subcommands, and flag definitions.

use anyhow::Context;
use boxlite::experimental::custom_kernel::{KernelFormat, KernelOptions, configure};
use boxlite::experimental::{
    EXPERIMENTAL_FEATURES_ENV, ExperimentalFeature, ExperimentalFeatures, RuntimeBuilder,
};
use boxlite::runtime::options::{
    InboundNetworkConfig, NetworkMode, OutboundNetworkConfig, PortProtocol, PortSpec, VolumeSpec,
};
use boxlite::{
    BoxCommand, BoxOptions, BoxliteOptions, BoxliteRestOptions, BoxliteRuntime,
    ContainerCapabilities, ImageRegistry, NetworkRateLimit, NetworkSpec,
};
use clap::error::ErrorKind;
use clap::parser::ValueSource;
use clap::{
    Arg, ArgMatches, Args, Command, CommandFactory, FromArgMatches, Parser, Subcommand, ValueEnum,
};
use clap_complete::shells::{Bash, Fish, Zsh};
use std::ffi::OsString;
use std::io::{IsTerminal, Write};
use std::path::{Path, PathBuf};

/// Helper to parse CLI environment variables and apply them to BoxOptions
pub fn apply_env_vars(env: &[String], opts: &mut BoxOptions) {
    apply_env_vars_with_lookup(env, opts, |k| std::env::var(k).ok())
}

/// Helper to parse CLI environment variables with custom lookup for host variables
pub fn apply_env_vars_with_lookup<F>(env: &[String], opts: &mut BoxOptions, lookup: F)
where
    F: Fn(&str) -> Option<String>,
{
    opts.env.extend(resolve_env_vars_with_lookup(env, lookup));
}

fn resolve_env_vars_with_lookup<F>(env: &[String], lookup: F) -> Vec<(String, String)>
where
    F: Fn(&str) -> Option<String>,
{
    let mut resolved = Vec::with_capacity(env.len());
    for env_str in env {
        if let Some((k, v)) = env_str.split_once('=') {
            resolved.push((k.to_string(), v.to_string()));
        } else if let Some(val) = lookup(env_str) {
            resolved.push((env_str.to_string(), val));
        } else {
            tracing::warn!(
                "Environment variable '{}' not found on host, skipping",
                env_str
            );
        }
    }
    resolved
}

// ============================================================================
// CLI Definition
// ============================================================================

#[derive(Parser, Debug)]
#[command(name = "boxlite", author, version, about = "BoxLite CLI")]
pub struct Cli {
    #[command(flatten)]
    pub global: GlobalFlags,

    #[command(subcommand)]
    pub command: Commands,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum GlobalOption {
    Debug,
    Home,
    Registry,
    Config,
    Url,
    Profile,
    PathPrefix,
}

impl GlobalOption {
    const ALL: &'static [Self] = &[
        Self::Debug,
        Self::Home,
        Self::Registry,
        Self::Config,
        Self::Url,
        Self::Profile,
        Self::PathPrefix,
    ];

    const fn id(self) -> &'static str {
        match self {
            Self::Debug => "debug",
            Self::Home => "home",
            Self::Registry => "registry",
            Self::Config => "config",
            Self::Url => "url",
            Self::Profile => "profile",
            Self::PathPrefix => "path_prefix",
        }
    }

    const fn long(self) -> &'static str {
        match self {
            Self::PathPrefix => "path-prefix",
            _ => self.id(),
        }
    }

    const fn propagates_value(self) -> bool {
        !matches!(self, Self::Registry)
    }

    fn clear(self, global: &mut GlobalFlags) {
        match self {
            Self::Debug => global.debug = false,
            Self::Home => global.home = None,
            Self::Registry => global.registry.clear(),
            Self::Config => global.config = None,
            Self::Url => global.url = None,
            Self::Profile => global.profile = None,
            Self::PathPrefix => global.path_prefix = None,
        }
    }
}

#[derive(Debug)]
struct CommandScope {
    name: &'static str,
    globals: &'static [GlobalOption],
    children: &'static [CommandScope],
}

impl CommandScope {
    fn allows(&self, option: GlobalOption) -> bool {
        self.globals.contains(&option)
    }
}

const ALL_GLOBALS: &[GlobalOption] = GlobalOption::ALL;
const BOX_GLOBALS: &[GlobalOption] = &[
    GlobalOption::Debug,
    GlobalOption::Home,
    GlobalOption::Config,
    GlobalOption::Url,
    GlobalOption::Profile,
    GlobalOption::PathPrefix,
];
const LOCAL_IMAGE_GLOBALS: &[GlobalOption] = &[
    GlobalOption::Debug,
    GlobalOption::Home,
    GlobalOption::Registry,
    GlobalOption::Config,
];
const LOCAL_GLOBALS: &[GlobalOption] = &[
    GlobalOption::Debug,
    GlobalOption::Home,
    GlobalOption::Config,
];
const AUTH_GLOBALS: &[GlobalOption] = &[
    GlobalOption::Debug,
    GlobalOption::Home,
    GlobalOption::Profile,
];
const AUTH_URL_GLOBALS: &[GlobalOption] = &[
    GlobalOption::Debug,
    GlobalOption::Home,
    GlobalOption::Url,
    GlobalOption::Profile,
];
const VOLUME_GLOBALS: &[GlobalOption] = &[
    GlobalOption::Debug,
    GlobalOption::Home,
    GlobalOption::Url,
    GlobalOption::Profile,
    GlobalOption::PathPrefix,
];

const AUTH_SCOPES: &[CommandScope] = &[
    CommandScope {
        name: "login",
        globals: AUTH_URL_GLOBALS,
        children: &[],
    },
    CommandScope {
        name: "logout",
        globals: AUTH_GLOBALS,
        children: &[],
    },
    CommandScope {
        name: "status",
        globals: AUTH_URL_GLOBALS,
        children: &[],
    },
    CommandScope {
        name: "whoami",
        globals: AUTH_URL_GLOBALS,
        children: &[],
    },
];
const NETWORK_SCOPES: &[CommandScope] = &[CommandScope {
    name: "tunnel",
    globals: BOX_GLOBALS,
    children: &[],
}];
const VOLUME_SCOPES: &[CommandScope] = &[
    CommandScope {
        name: "create",
        globals: VOLUME_GLOBALS,
        children: &[],
    },
    CommandScope {
        name: "ls",
        globals: VOLUME_GLOBALS,
        children: &[],
    },
    CommandScope {
        name: "get",
        globals: VOLUME_GLOBALS,
        children: &[],
    },
    CommandScope {
        name: "rm",
        globals: VOLUME_GLOBALS,
        children: &[],
    },
];
const COMMAND_SCOPES: &[CommandScope] = &[
    CommandScope {
        name: "run",
        globals: ALL_GLOBALS,
        children: &[],
    },
    CommandScope {
        name: "exec",
        globals: BOX_GLOBALS,
        children: &[],
    },
    CommandScope {
        name: "create",
        globals: ALL_GLOBALS,
        children: &[],
    },
    CommandScope {
        name: "list",
        globals: BOX_GLOBALS,
        children: &[],
    },
    CommandScope {
        name: "rm",
        globals: BOX_GLOBALS,
        children: &[],
    },
    CommandScope {
        name: "start",
        globals: BOX_GLOBALS,
        children: &[],
    },
    CommandScope {
        name: "stop",
        globals: BOX_GLOBALS,
        children: &[],
    },
    CommandScope {
        name: "restart",
        globals: BOX_GLOBALS,
        children: &[],
    },
    CommandScope {
        name: "pull",
        globals: LOCAL_IMAGE_GLOBALS,
        children: &[],
    },
    CommandScope {
        name: "images",
        globals: LOCAL_GLOBALS,
        children: &[],
    },
    CommandScope {
        name: "inspect",
        globals: BOX_GLOBALS,
        children: &[],
    },
    CommandScope {
        name: "cp",
        globals: BOX_GLOBALS,
        children: &[],
    },
    CommandScope {
        name: "info",
        globals: LOCAL_GLOBALS,
        children: &[],
    },
    CommandScope {
        name: "logs",
        globals: LOCAL_GLOBALS,
        children: &[],
    },
    CommandScope {
        name: "stats",
        globals: BOX_GLOBALS,
        children: &[],
    },
    CommandScope {
        name: "network",
        globals: BOX_GLOBALS,
        children: NETWORK_SCOPES,
    },
    CommandScope {
        name: "serve",
        globals: LOCAL_IMAGE_GLOBALS,
        children: &[],
    },
    CommandScope {
        name: "auth",
        globals: AUTH_GLOBALS,
        children: AUTH_SCOPES,
    },
    CommandScope {
        name: "volume",
        globals: VOLUME_GLOBALS,
        children: VOLUME_SCOPES,
    },
    CommandScope {
        name: "completion",
        globals: &[],
        children: &[],
    },
];

/// Build one command tree for runtime parsing, help, and completion.
///
/// Root copies preserve `boxlite --option command` compatibility but are not
/// global. Each command receives only the positive set it owns, so Clap itself
/// rejects an irrelevant option written after the command and renders the same
/// scope in help. Environment bindings live on those scoped copies so an
/// irrelevant ambient value is never parsed. Singular values use Clap's
/// documented propagation back to the root `GlobalFlags`; repeatable values
/// are folded explicitly because propagation selects one level's `MatchedArg`
/// rather than appending values from independently defined levels.
pub fn command() -> Command {
    let command = Cli::command();
    let global_args = command
        .get_arguments()
        .filter(|argument| argument.is_global_set())
        .cloned()
        .collect::<Vec<_>>();
    let command = command.mut_args(|argument| {
        if argument.is_global_set() {
            argument.global(false).env(None::<&'static str>)
        } else {
            argument
        }
    });
    apply_command_scopes(command, COMMAND_SCOPES, &global_args)
}

fn apply_command_scopes(
    command: Command,
    scopes: &'static [CommandScope],
    global_args: &[Arg],
) -> Command {
    for subcommand in command.get_subcommands() {
        assert!(
            scopes
                .iter()
                .any(|scope| scope.name == subcommand.get_name()),
            "missing global-option scope for `boxlite {}`",
            subcommand.get_name()
        );
    }
    for scope in scopes {
        assert!(
            command
                .get_subcommands()
                .any(|subcommand| subcommand.get_name() == scope.name),
            "global-option scope references missing command `{}`",
            scope.name
        );
    }

    command.mut_subcommands(|subcommand| {
        let scope = scopes
            .iter()
            .find(|scope| scope.name == subcommand.get_name())
            .expect("command scope checked above");
        let local_ids = subcommand
            .get_arguments()
            .map(|argument| argument.get_id().clone())
            .collect::<Vec<_>>();
        let scoped_args = global_args
            .iter()
            .filter(|argument| {
                scope
                    .globals
                    .iter()
                    .any(|option| option.id() == argument.get_id().as_str())
            })
            .filter(|argument| !local_ids.contains(argument.get_id()))
            .cloned()
            .map(|argument| {
                let option = GlobalOption::ALL
                    .iter()
                    .copied()
                    .find(|option| option.id() == argument.get_id().as_str())
                    .expect("scoped argument must be a declared global option");
                argument.global(option.propagates_value())
            });
        apply_command_scopes(subcommand.args(scoped_args), scope.children, global_args)
    })
}

/// Parse through [`command`] so the runtime parser and rendered help use the
/// same command-capability policy.
pub fn parse() -> Cli {
    try_parse_from(std::env::args_os()).unwrap_or_else(|error| error.exit())
}

pub(crate) fn try_parse_from<I, T>(args: I) -> Result<Cli, clap::Error>
where
    I: IntoIterator<Item = T>,
    T: Into<OsString> + Clone,
{
    let mut parser = command();
    let mut matches = parser.clone().try_get_matches_from(args)?;
    let command_path = selected_command_path(&matches);
    let scope = command_scope(&command_path).expect("parsed command must have a declared scope");

    if let Some(option) = GlobalOption::ALL.iter().copied().find(|option| {
        !scope.allows(*option)
            && matches.value_source(option.id()) == Some(ValueSource::CommandLine)
    }) {
        return Err(irrelevant_global_error(&mut parser, &command_path, option));
    }

    if let Some(home) = matches.get_one::<PathBuf>("home")
        && scope.allows(GlobalOption::Home)
        && !home.is_absolute()
    {
        return Err(parser.error(
            ErrorKind::ValueValidation,
            format!(
                "BoxLite home must be an absolute path, got: {}",
                home.display()
            ),
        ));
    }

    for id in ["url", "profile", "path_prefix", "config"] {
        if scope.globals.iter().any(|option| option.id() == id)
            && matches.value_source(id) == Some(ValueSource::CommandLine)
            && matches.get_one::<String>(id).is_some_and(String::is_empty)
        {
            let option = id.replace('_', "-");
            return Err(parser.error(
                ErrorKind::ValueValidation,
                format!("--{option} cannot be empty"),
            ));
        }
    }
    let registry_values = command_line_values_along_selected_path(&matches, "registry");
    if scope.allows(GlobalOption::Registry) && registry_values.iter().any(String::is_empty) {
        return Err(parser.error(ErrorKind::ValueValidation, "--registry cannot be empty"));
    }

    let mut cli = Cli::from_arg_matches_mut(&mut matches)?;
    if scope.allows(GlobalOption::Registry) {
        cli.global.registry = registry_values;
    }
    clear_irrelevant_environment_globals(&mut cli.global, scope);
    merge_root_login_url(&mut cli);
    normalize_empty_urls(&mut cli);
    Ok(cli)
}

fn command_line_values_along_selected_path(matches: &ArgMatches, id: &str) -> Vec<String> {
    let mut values = Vec::new();
    let mut current = matches;
    loop {
        if let Ok(Some(level_values)) = current.try_get_many::<String>(id) {
            values.extend(level_values.cloned());
        }
        let Some((_, child)) = current.subcommand() else {
            break;
        };
        current = child;
    }
    values
}

fn selected_command_path(matches: &ArgMatches) -> Vec<&str> {
    let mut path = Vec::new();
    let mut current = matches;
    while let Some((name, child)) = current.subcommand() {
        path.push(name);
        current = child;
    }
    path
}

fn command_scope(path: &[&str]) -> Option<&'static CommandScope> {
    let mut scopes = COMMAND_SCOPES;
    let mut selected = None;
    for name in path {
        let scope = scopes.iter().find(|scope| scope.name == *name)?;
        selected = Some(scope);
        scopes = scope.children;
    }
    selected
}

fn irrelevant_global_error(
    parser: &mut Command,
    command_path: &[&str],
    option: GlobalOption,
) -> clap::Error {
    parser.error(
        ErrorKind::UnknownArgument,
        format!(
            "--{} is not valid for `boxlite {}`",
            option.long(),
            command_path.join(" ")
        ),
    )
}

/// Irrelevant values supplied by ambient environment variables are ignored:
/// they are not command options and must not make an unrelated command fail.
fn clear_irrelevant_environment_globals(global: &mut GlobalFlags, scope: &CommandScope) {
    for option in GlobalOption::ALL {
        if !scope.allows(*option) {
            option.clear(global);
        }
    }
}

fn merge_root_login_url(cli: &mut Cli) {
    if let Commands::Auth(crate::commands::auth::AuthArgs {
        command: crate::commands::auth::AuthCommand::Login(login),
    }) = &mut cli.command
        && login.url.is_none()
    {
        login.url.clone_from(&cli.global.url);
    }
}

fn normalize_empty_urls(cli: &mut Cli) {
    if cli.global.url.as_deref() == Some("") {
        cli.global.url = None;
    }
    if let Commands::Auth(crate::commands::auth::AuthArgs {
        command: crate::commands::auth::AuthCommand::Login(login),
    }) = &mut cli.command
        && login.url.as_deref() == Some("")
    {
        login.url = None;
    }
}

#[derive(Subcommand, Debug)]
#[non_exhaustive]
pub enum Commands {
    /// Run a command in a new box
    Run(crate::commands::run::RunArgs),
    /// Execute a command in a running box
    Exec(crate::commands::exec::ExecArgs),
    /// Create a new box
    Create(crate::commands::create::CreateArgs),

    /// List boxes
    #[command(visible_alias = "ls", visible_alias = "ps")]
    List(crate::commands::list::ListArgs),

    /// Remove one or more boxes
    Rm(crate::commands::rm::RmArgs),

    /// Start one or more stopped boxes
    Start(crate::commands::start::StartArgs),

    /// Stop one or more running boxes
    Stop(crate::commands::stop::StopArgs),

    /// Restart one or more boxes
    Restart(crate::commands::restart::RestartArgs),

    /// Pull an image from a registry
    Pull(crate::commands::pull::PullArgs),

    /// List images
    Images(crate::commands::images::ImagesArgs),

    /// Display detailed information on a box
    Inspect(crate::commands::inspect::InspectArgs),

    /// Copy files/folders between host and box
    Cp(crate::commands::cp::CpArgs),

    /// Display system-wide runtime information
    Info(crate::commands::info::InfoArgs),

    /// Show logs from a box
    Logs(crate::commands::logs::LogsArgs),

    /// Display resource usage statistics for a box
    Stats(crate::commands::stats::StatsArgs),

    /// Manage box networking
    Network(crate::commands::network::NetworkArgs),

    /// Start a long-running REST API server
    Serve(crate::commands::serve::ServeArgs),

    /// Authenticate with a remote BoxLite server
    Auth(crate::commands::auth::AuthArgs),

    /// Manage named volumes on a REST server
    Volume(crate::commands::volume::VolumeArgs),

    /// Generate shell completion script (hidden from help)
    #[command(hide = true)]
    Completion(CompletionArgs),
}

/// Shell for which to generate completion script.
#[derive(ValueEnum, Clone, Debug)]
#[value(rename_all = "lower")]
pub enum Shell {
    Bash,
    Zsh,
    Fish,
}

/// Arguments for the completion subcommand.
#[derive(Args, Debug)]
pub struct CompletionArgs {
    /// Shell to generate completion for (bash, zsh, fish).
    pub shell: Shell,
}

/// Writes a completion script for the given shell to `out`.
pub fn generate_completion(shell: &Shell, name: &str, out: &mut dyn Write) {
    let mut cmd = completion_command();
    match shell {
        Shell::Bash => clap_complete::generate(Bash, &mut cmd, name, out),
        Shell::Zsh => clap_complete::generate(Zsh, &mut cmd, name, out),
        Shell::Fish => clap_complete::generate(Fish, &mut cmd, name, out),
    }
}

fn completion_command() -> Command {
    // clap_complete's AOT generators enumerate hidden arguments. Runtime,
    // help, and scoped globals already share `command()`; this final projection
    // removes only experimental arguments from completion discovery.
    command()
        .mut_subcommand("run", without_hidden_arguments)
        .mut_subcommand("create", without_hidden_arguments)
}

fn without_hidden_arguments(command: Command) -> Command {
    let name = match command.get_name() {
        "run" => "run",
        "create" => "create",
        other => panic!("completion projection is not defined for {other}"),
    };
    let about = command.get_about().cloned();
    let long_about = command.get_long_about().cloned();
    let display_order = command.get_display_order();
    let visible_arguments = command
        .get_arguments()
        .filter(|argument| !argument.is_hide_set())
        .cloned()
        .collect::<Vec<_>>();
    let subcommands = command.get_subcommands().cloned().collect::<Vec<_>>();

    let mut projected = Command::new(name)
        .display_order(display_order)
        .args(visible_arguments)
        .subcommands(subcommands);
    if let Some(about) = about {
        projected = projected.about(about);
    }
    if let Some(long_about) = long_about {
        projected = projected.long_about(long_about);
    }
    projected
}

pub(crate) fn experimental_features_from_env() -> boxlite::BoxliteResult<ExperimentalFeatures> {
    match std::env::var(EXPERIMENTAL_FEATURES_ENV) {
        Ok(value) => ExperimentalFeatures::parse(&value),
        Err(std::env::VarError::NotPresent) => Ok(ExperimentalFeatures::default()),
        Err(std::env::VarError::NotUnicode(_)) => Err(boxlite::BoxliteError::Config(format!(
            "{EXPERIMENTAL_FEATURES_ENV} must contain valid UTF-8"
        ))),
    }
}

// ============================================================================
// GLOBAL FLAGS
// ============================================================================

#[derive(Args, Debug, Clone)]
pub struct GlobalFlags {
    /// Enable debug output
    #[arg(long, global = true)]
    pub debug: bool,

    /// Absolute BoxLite runtime and credential home directory
    #[arg(long, global = true, env = "BOXLITE_HOME")]
    pub home: Option<std::path::PathBuf>,

    /// Image registry to use (can be specified multiple times)
    #[arg(long, global = true, value_name = "REGISTRY")]
    pub registry: Vec<String>,

    /// Configuration file path (optional)
    ///
    /// Specifies the JSON configuration file containing BoxLite options such as image_registries.
    /// If not provided, uses default options (no config file is loaded from $BOXLITE_HOME).
    #[arg(long, global = true)]
    pub config: Option<String>,

    /// Connect to a remote BoxLite REST API server instead of local runtime.
    #[arg(long, global = true, env = "BOXLITE_REST_URL")]
    pub url: Option<String>,

    /// Named profile in `<BOXLITE_HOME>/credentials.toml`. Lets one machine
    /// hold separate remote logins. Defaults to `default` if neither flag nor
    /// env is set.
    #[arg(long, global = true, env = "BOXLITE_PROFILE")]
    pub profile: Option<String>,

    /// Routing-slot value for the URL path (`/v1/<prefix>/...`).
    /// Opaque — the server decides what this means (org id, workspace,
    /// catalog, …); the value typically comes from the `auth login`
    /// flow capturing `Principal.path_prefix`. This flag overrides
    /// the stored profile's path_prefix for users whose credential
    /// has scope over multiple routing values (e.g. multiple orgs on
    /// the same account). Unset → uses the stored profile's
    /// path_prefix, then empty (URL skips the segment —
    /// `/v1/boxes/...`).
    #[arg(long = "path-prefix", global = true, env = "BOXLITE_REST_PATH_PREFIX")]
    pub path_prefix: Option<String>,

    #[arg(skip)]
    pub(crate) experimental_features: ExperimentalFeatures,
}

impl GlobalFlags {
    pub(crate) fn credential_store(&self) -> crate::credentials::CredentialStore {
        crate::credentials::CredentialStore::new(self.home.clone())
    }

    /// Resolve which credential profile to read/write. Order: explicit
    /// `--profile` flag (which clap also fills from `BOXLITE_PROFILE`) > the
    /// hard-coded `default` name. Keeping this in one helper means a future
    /// "tab through last-used profile" UX has exactly one place to change.
    pub fn resolved_profile(&self) -> String {
        self.profile
            .as_deref()
            .filter(|s| !s.is_empty())
            .unwrap_or(crate::credentials::DEFAULT_PROFILE)
            .to_string()
    }

    pub fn experimental_features(&self) -> &ExperimentalFeatures {
        &self.experimental_features
    }

    /// Resolve runtime options from config file and CLI overrides (--home, --registry).
    pub fn resolve_runtime_options(&self) -> anyhow::Result<BoxliteOptions> {
        let mut options = if let Some(config_path) = &self.config {
            crate::config::load_config(Path::new(config_path))?
        } else {
            BoxliteOptions::default()
        };

        if let Some(cli_home) = &self.home {
            options.home_dir = cli_home.clone();
        }

        if !self.registry.is_empty() {
            options.image_registries = self
                .registry
                .iter()
                .map(|host| ImageRegistry::https(host).with_search(true))
                .chain(options.image_registries)
                .collect();
        }

        Ok(options)
    }

    /// Create a runtime from pre-resolved options (avoids resolving twice when caller already has options).
    pub fn create_runtime_with_options(
        &self,
        options: BoxliteOptions,
    ) -> anyhow::Result<BoxliteRuntime> {
        RuntimeBuilder::new(options)
            .with_features(self.experimental_features.clone())
            .build()
            .map_err(Into::into)
    }

    /// Whether this invocation talks to a server rather than the embedded
    /// runtime.
    ///
    /// Resolved exactly as `create_runtime` does, so the two cannot disagree
    /// about which backend a command is aimed at. It re-reads the credential
    /// file rather than threading the resolution through; the cost is one extra
    /// read on the two commands that ask.
    pub fn targets_rest(&self) -> anyhow::Result<bool> {
        let stored = self.load_selected_profile()?;
        let env_api_key = std::env::var("BOXLITE_API_KEY").ok();
        Ok(self.resolve_rest_options(stored, env_api_key).is_some())
    }

    pub fn create_runtime(&self) -> anyhow::Result<BoxliteRuntime> {
        let stored = self.load_selected_profile()?;
        // Clap reads BOXLITE_REST_URL into `self.url`; BOXLITE_API_KEY is the
        // one credential env we still consult directly here.
        let env_api_key = std::env::var("BOXLITE_API_KEY").ok();

        match self.resolve_rest_options(stored, env_api_key) {
            Some(opts) => BoxliteRuntime::rest(opts).map_err(Into::into),
            None => {
                // No URL anywhere → local runtime, unchanged behavior.
                let options = self.resolve_runtime_options()?;
                self.create_runtime_with_options(options)
            }
        }
    }

    fn load_selected_profile(&self) -> anyhow::Result<Option<crate::credentials::Profile>> {
        let profile = self.resolved_profile();
        self.credential_store()
            .load_named(&profile)
            .with_context(|| format!("loading credential profile `{profile}`"))
    }

    /// Build REST connection options from the selected credential profile and
    /// the ambient `BOXLITE_API_KEY`. Returns `None` when no URL is configured
    /// (the caller then falls back to the local runtime). Pure — takes the
    /// resolved profile and env key as arguments and touches neither disk nor
    /// process environment — so the precedence below is unit-testable.
    ///
    /// Precedence (each axis independent):
    /// - URL: `--url` / `BOXLITE_REST_URL` > stored profile.
    /// - routing slot (`path_prefix`): `--path-prefix` /
    ///   `BOXLITE_REST_PATH_PREFIX` > stored profile.
    /// - bearer credential: `BOXLITE_API_KEY` > stored profile.
    ///
    /// `BOXLITE_API_KEY` overrides ONLY the bearer credential — the selected
    /// profile's url and path_prefix still apply, so `--profile p1` keeps
    /// routing to its tenant (`/v1/<prefix>/…`) even with an ambient key set.
    /// Building the options bare in that branch (instead of starting from the
    /// profile) was the cause of the prefix-less `/v1/boxes` 404 against a
    /// multi-tenant server.
    pub(crate) fn resolve_rest_options(
        &self,
        stored: Option<crate::credentials::Profile>,
        env_api_key: Option<String>,
    ) -> Option<BoxliteRestOptions> {
        let url = self
            .url
            .clone()
            .or_else(|| stored.as_ref().map(|p| p.url.clone()))?;

        // Start from the stored profile so its url + path_prefix (routing
        // slot) survive; the env key below overrides only the bearer.
        let mut opts = match stored {
            Some(profile) => {
                let mut from_profile = crate::credentials::into_rest_options(profile);
                // `--url` (resolved above) wins over the stored URL.
                from_profile.url = self.url.clone().unwrap_or(from_profile.url);
                from_profile
            }
            None => BoxliteRestOptions::new(url),
        };

        if let Some(key) = env_api_key.filter(|k| !k.is_empty()) {
            opts = opts.with_api_key(key);
        }

        // `--path-prefix` flag (or `BOXLITE_REST_PATH_PREFIX`, both filled by
        // clap into `self.path_prefix`) overrides the profile's routing slot.
        // Leaving it alone when the flag is unset means the profile's value
        // wins; if neither is set the URL builder skips the segment entirely
        // (`/v1/boxes/...`, the empty-prefix single-tenant shape).
        if let Some(path_prefix) = self.path_prefix.as_ref().filter(|s| !s.is_empty()) {
            opts.path_prefix = Some(path_prefix.clone());
        }

        Some(opts)
    }
}

// ============================================================================
// PROCESS FLAGS
// ============================================================================

#[derive(Args, Debug, Clone)]
pub struct ProcessFlags {
    /// Keep STDIN open even if not attached
    #[arg(short, long, conflicts_with = "detach")]
    pub interactive: bool,

    /// Allocate a pseudo-TTY (stdout and stderr are merged in TTY mode)
    #[arg(short, long)]
    pub tty: bool,

    /// Set environment variables
    #[arg(short = 'e', long = "env")]
    pub env: Vec<String>,

    /// Working directory inside the box
    #[arg(short = 'w', long = "workdir")]
    pub workdir: Option<String>,

    /// User to run the command as (format: <name|uid>[:<group|gid>])
    #[arg(short = 'u', long = "user")]
    pub user: Option<String>,
}

impl ProcessFlags {
    /// Apply process configuration to BoxOptions
    pub fn apply_to(&self, opts: &mut BoxOptions) -> anyhow::Result<()> {
        self.apply_to_with_lookup(opts, |k| std::env::var(k).ok())
    }

    /// Internal helper for dependency injection of environment variables
    fn apply_to_with_lookup<F>(&self, opts: &mut BoxOptions, lookup: F) -> anyhow::Result<()>
    where
        F: Fn(&str) -> Option<String>,
    {
        opts.working_dir = self.workdir.clone();
        apply_env_vars_with_lookup(&self.env, opts, lookup);
        if let Some(ref user) = self.user {
            opts.user = Some(user.clone());
        }
        // `-t` is a property of the container's init, which COMMAND now is, so
        // it has to be decided here at create time rather than at attach.
        opts.tty = self.tty;
        Ok(())
    }

    /// Validate process flags
    pub fn validate(&self, detach: bool) -> anyhow::Result<()> {
        // Check TTY mode only in non-detach mode
        if !detach && self.tty && !std::io::stdin().is_terminal() {
            anyhow::bail!("the input device is not a TTY.");
        }

        Ok(())
    }

    /// Configures a BoxCommand with process flags (env, workdir, tty)
    pub fn configure_command(&self, mut cmd: BoxCommand) -> BoxCommand {
        for (key, value) in resolve_env_vars_with_lookup(&self.env, |key| std::env::var(key).ok()) {
            cmd = cmd.env(key, value);
        }

        if let Some(ref w) = self.workdir {
            cmd = cmd.working_dir(w);
        }

        if self.tty {
            cmd = cmd.tty(true);
        }

        if let Some(ref user) = self.user {
            cmd = cmd.user(user);
        }

        cmd
    }
}

// ============================================================================
// CAPABILITY FLAGS
// ============================================================================

#[derive(Args, Debug, Clone, Default)]
pub struct CapabilityFlags {
    /// Add a Linux capability to the container (repeatable; `ALL` is supported)
    #[arg(long = "cap-add", value_name = "CAPABILITY")]
    pub cap_add: Vec<String>,

    /// Drop a Linux capability from the container (repeatable; `ALL` is supported)
    #[arg(long = "cap-drop", value_name = "CAPABILITY")]
    pub cap_drop: Vec<String>,
}

impl CapabilityFlags {
    pub fn apply_to(&self, opts: &mut BoxOptions) {
        if self.cap_add.is_empty() && self.cap_drop.is_empty() {
            return;
        }
        opts.advanced
            .set_capabilities(Some(ContainerCapabilities {
                add: self.cap_add.clone(),
                drop: self.cap_drop.clone(),
            }))
            .expect("apply_to runs before options are resolved");
    }
}

// ============================================================================
// RESOURCE FLAGS
// ============================================================================

#[derive(Args, Debug, Clone)]
pub struct ResourceFlags {
    /// Number of CPUs
    #[arg(long)]
    pub cpus: Option<u32>,

    /// Memory limit (in MiB)
    #[arg(long)]
    pub memory: Option<u32>,

    /// Container rootfs disk size (in GB). The COW overlay is sparse —
    /// actual on-disk usage grows as the workload writes. The virtual
    /// size is `max(this, base image size)`; smaller values are ignored.
    /// Default (unset) sizes the overlay to exactly the base image,
    /// leaving no headroom — set this for workloads that write
    /// significant data (in-box `docker pull`, `apt install`, `npm
    /// install`, build caches, etc.).
    #[arg(long = "disk-size", value_name = "GB")]
    pub disk_size_gb: Option<u64>,
}

impl ResourceFlags {
    pub fn apply_to(&self, opts: &mut BoxOptions) {
        if let Some(cpus) = self.cpus {
            if cpus > 255 {
                tracing::warn!("CPU limit capped at 255 (requested {})", cpus);
            }
            opts.cpus = Some(cpus.min(255) as u8);
        }
        if let Some(mem) = self.memory {
            opts.memory_mib = Some(mem);
        }
        if let Some(gb) = self.disk_size_gb {
            opts.disk_size_gb = Some(gb);
        }
    }
}

// ============================================================================
// KERNEL FLAGS
// ============================================================================

#[derive(Clone, Copy, Debug, ValueEnum)]
#[value(rename_all = "kebab-case")]
pub enum KernelFormatArg {
    Auto,
    Raw,
    Elf,
    PeGz,
    ImageBz2,
    ImageGz,
    ImageZstd,
}

impl From<KernelFormatArg> for KernelFormat {
    fn from(format: KernelFormatArg) -> Self {
        match format {
            KernelFormatArg::Auto => Self::Auto,
            KernelFormatArg::Raw => Self::Raw,
            KernelFormatArg::Elf => Self::Elf,
            KernelFormatArg::PeGz => Self::PeGz,
            KernelFormatArg::ImageBz2 => Self::ImageBz2,
            KernelFormatArg::ImageGz => Self::ImageGz,
            KernelFormatArg::ImageZstd => Self::ImageZstd,
        }
    }
}

// Direct Linux boot options. Companion flags are valid only with `--kernel`.
#[derive(Args, Debug, Clone, Default)]
pub struct KernelFlags {
    /// Boot with this Linux kernel image instead of BoxLite's bundled kernel.
    #[arg(long, value_name = "PATH", hide = true)]
    pub kernel: Option<PathBuf>,

    /// Kernel image format. Auto-detected by default.
    #[arg(
        long,
        value_enum,
        value_name = "FORMAT",
        requires = "kernel",
        hide = true
    )]
    pub kernel_format: Option<KernelFormatArg>,

    /// Initial ramdisk to load with the custom kernel.
    #[arg(long, value_name = "PATH", requires = "kernel", hide = true)]
    pub initramfs: Option<PathBuf>,

    /// Replace libkrun's default Linux kernel command line.
    #[arg(
        long = "kernel-args",
        value_name = "ARGS",
        requires = "kernel",
        hide = true
    )]
    pub kernel_args: Option<String>,
}

impl KernelFlags {
    pub fn require_enabled(&self, features: &ExperimentalFeatures) -> boxlite::BoxliteResult<()> {
        if self.kernel.is_none() {
            return Ok(());
        }

        features.require(ExperimentalFeature::CustomKernel)
    }

    pub fn apply_to(&self, opts: &mut BoxOptions) {
        let Some(path) = &self.kernel else {
            return;
        };
        configure(
            opts,
            KernelOptions {
                path: path.clone(),
                format: self.kernel_format.map(Into::into).unwrap_or_default(),
                initramfs: self.initramfs.clone(),
                command_line: self.kernel_args.clone(),
            },
        );
    }
}

// ============================================================================
// NETWORK FLAGS
// ============================================================================

#[derive(Args, Debug, Clone)]
pub struct NetworkFlags {
    /// Network mode: "enabled" (default — full or allow-listed egress) or
    /// "disabled" (no interface at all; gvproxy is not started and the guest
    /// has no eth0).
    #[arg(long = "network", value_name = "MODE")]
    pub network: Option<String>,

    /// Restrict TCP and UDP egress to the listed hosts/IPs (repeatable);
    /// everything else is DNS-sinkholed and dropped. Implies network=enabled.
    /// Patterns: exact host, "*.example.com", IP, or CIDR. Hostname rules need
    /// TLS SNI / HTTP Host inspection, so a hostname-only list denies all UDP;
    /// add the IP or CIDR to keep UDP open. Incompatible with `--network
    /// disabled`.
    #[arg(long = "allow-net", value_name = "HOST")]
    pub allow_net: Vec<String>,

    /// Inbound mode: "enabled" (default — services the box exposes are
    /// publicly reachable) or "disabled" (private, unreachable from outside
    /// the box).
    #[arg(long = "inbound", value_name = "MODE")]
    pub inbound: Option<String>,

    /// Cap what the box sends, in kilobits/sec (guest to internet). Unset or 0
    /// leaves it uncapped. Local runtime only. Verified on Linux; on macOS the
    /// guest link is a datagram socket whose sender behaviour under
    /// backpressure is unverified, so this may drop frames instead of slowing
    /// the guest down.
    #[arg(long = "net-tx-kbps", value_name = "KBPS")]
    pub net_tx_kbps: Option<u64>,

    /// Cap what reaches the box, in kilobits/sec (internet to guest). Unset or
    /// 0 leaves it uncapped. Local runtime only. Paced the same way on Linux
    /// and macOS.
    #[arg(long = "net-rx-kbps", value_name = "KBPS")]
    pub net_rx_kbps: Option<u64>,
}

impl NetworkFlags {
    pub fn apply_to(&self, opts: &mut BoxOptions) -> anyhow::Result<()> {
        // The rate limit is independent of mode and allowlist, so it is
        // assigned before the early return below. Folding it into that
        // condition instead would mean a lone --net-tx-kbps is silently dropped
        // the day someone adds another flag and forgets to extend the guard.
        opts.advanced.network_rate_limit = NetworkRateLimit {
            tx_kbps: self.net_tx_kbps,
            rx_kbps: self.net_rx_kbps,
        };

        // Leave BoxOptions::default() (outbound Enabled/full access, inbound
        // Enabled/public) untouched when no flag is given, so a bare `run`
        // behaves as before.
        if self.network.is_none() && self.allow_net.is_empty() && self.inbound.is_none() {
            return Ok(());
        }
        let mode = match self.network.as_deref() {
            Some(value) => value.parse::<NetworkMode>()?,
            None => NetworkMode::Enabled,
        };
        let inbound_mode = match self.inbound.as_deref() {
            Some(value) => value.parse::<NetworkMode>()?,
            None => NetworkMode::Enabled,
        };
        opts.network = NetworkSpec::try_from(OutboundNetworkConfig {
            mode,
            allow_net: self.allow_net.clone(),
        })?;
        opts.inbound_network = NetworkSpec::try_from(InboundNetworkConfig {
            mode: inbound_mode,
            allow_net: Vec::new(),
        })?;
        Ok(())
    }
}

// ============================================================================
// PUBLISH (PORT) FLAGS
// ============================================================================

#[derive(Args, Debug, Clone)]
pub struct PublishFlags {
    /// Publish a TCP box port locally (boxPort uses an automatic host port)
    #[arg(short = 'p', long = "publish", value_name = "PORT")]
    pub publish: Vec<String>,
}

impl PublishFlags {
    pub fn apply_to(&self, opts: &mut BoxOptions) -> anyhow::Result<()> {
        for s in &self.publish {
            let spec = parse_publish_spec(s)?;
            opts.ports.push(spec);
        }
        Ok(())
    }
}

/// Parse a single publish spec: `[hostPort:]boxPort[/tcp]`.
/// - `boxPort` → host_port=None, guest_port=boxPort
/// - `hostPort:boxPort` → host_port=Some(hostPort), guest_port=boxPort
fn parse_publish_spec(s: &str) -> anyhow::Result<PortSpec> {
    let s = s.trim();
    if s.is_empty() {
        anyhow::bail!("empty port spec");
    }
    let (rest, protocol) = match s.split_once('/') {
        Some((r, proto)) => {
            let p = if proto.eq_ignore_ascii_case("tcp") {
                PortProtocol::Tcp
            } else if proto.eq_ignore_ascii_case("udp") {
                anyhow::bail!("UDP port forwarding is not implemented; use TCP")
            } else {
                anyhow::bail!("invalid protocol {:?}; use tcp", proto)
            };
            (r.trim(), p)
        }
        None => (s, PortProtocol::Tcp),
    };
    let parts: Vec<&str> = rest.splitn(2, ':').map(str::trim).collect();
    let (host_port, guest_port) = match parts.as_slice() {
        [guest] => {
            let g = parse_port(guest)?;
            (None, g)
        }
        [host, guest] => {
            let h = parse_port(host)?;
            let g = parse_port(guest)?;
            (Some(h), g)
        }
        _ => anyhow::bail!(
            "invalid port spec {:?}; use hostPort:boxPort or boxPort[/tcp]",
            s
        ),
    };
    Ok(PortSpec {
        host_port,
        guest_port,
        protocol,
        host_ip: None,
    })
}

fn parse_port(s: &str) -> anyhow::Result<u16> {
    let n: u16 = s
        .parse()
        .map_err(|_| anyhow::anyhow!("invalid port number {:?}", s))?;
    if n == 0 {
        anyhow::bail!("port must be 1-65535");
    }
    Ok(n)
}

// ============================================================================
// VOLUME FLAGS
// ============================================================================

#[derive(Args, Debug, Clone)]
pub struct VolumeFlags {
    /// Mount a volume: VOLUME:BOX_PATH for a managed volume, HOST_PATH:BOX_PATH[:options]
    /// for a host bind (host paths start with `/`, `./`, `~` or a drive letter), or
    /// BOX_PATH[:options] for an anonymous volume
    #[arg(short = 'v', long = "volume", value_name = "VOLUME")]
    pub volume: Vec<String>,
}

/// Resolve base directory for anonymous volumes: explicit home, or BOXLITE_HOME, or ~/.boxlite, or temp dir.
fn anonymous_volume_base(home: Option<&std::path::Path>) -> std::path::PathBuf {
    home.map(std::path::PathBuf::from)
        .or_else(|| {
            std::env::var("BOXLITE_HOME")
                .ok()
                .map(std::path::PathBuf::from)
        })
        .or_else(|| {
            dirs::home_dir().map(|mut p| {
                p.push(".boxlite");
                p
            })
        })
        .unwrap_or_else(std::env::temp_dir)
}

/// Make a host bind path absolute.
///
/// `volumespec` classified this as a path without touching the filesystem, so a
/// relative one is resolved here — the same split Docker uses, where the client
/// absolutizes and only the resolved path travels on.
fn resolve_host_path(path: String) -> anyhow::Result<String> {
    // A Windows path is absolute even where `Path::is_relative` says otherwise:
    // on Unix `C:\data` has no leading `/`, so without this it would be
    // canonicalized against the working directory and fail.
    if !std::path::Path::new(&path).is_relative()
        || crate::volumespec::is_windows_drive_prefix(&path)
    {
        return Ok(path);
    }

    let absolute = std::fs::canonicalize(&path)
        .map_err(|e| anyhow::anyhow!("volume host path {:?}: {}", path, e))?;
    Ok(absolute.to_string_lossy().into_owned())
}

impl VolumeFlags {
    /// Apply volume flags to options. Pass `home` for anonymous volume storage (e.g. from GlobalFlags).
    pub fn apply_to(
        &self,
        opts: &mut BoxOptions,
        home: Option<&std::path::Path>,
    ) -> anyhow::Result<()> {
        let base = anonymous_volume_base(home);
        for value in self.volume.iter() {
            let mount = crate::volumespec::parse(value)?;

            let spec = match mount.origin {
                // Held as written; the server resolves an id or a name.
                crate::volumespec::MountOrigin::ManagedVolume(volume) => {
                    // Neither the server nor the REST client accepts one yet;
                    // saying so here beats a downgrade the caller never sees.
                    if mount.read_only {
                        anyhow::bail!(
                            "read-only managed volumes are not supported yet; \
                             mount {volume:?} read-write"
                        );
                    }
                    VolumeSpec::managed_volume(volume, mount.guest_path)
                }

                crate::volumespec::MountOrigin::BindMount(path) => {
                    VolumeSpec::bind_mount(resolve_host_path(path)?, mount.guest_path)
                }

                crate::volumespec::MountOrigin::Anonymous => {
                    // Random id for the directory name (same approach as Podman:
                    // cryptographically random to avoid collisions under any load).
                    let unique = ulid::Ulid::new().to_string();
                    let dir = base.join("volumes").join("anonymous").join(unique);
                    std::fs::create_dir_all(&dir).map_err(|e| {
                        anyhow::anyhow!("failed to create anonymous volume dir {:?}: {}", dir, e)
                    })?;
                    VolumeSpec::bind_mount(dir.to_string_lossy().into_owned(), mount.guest_path)
                }
            };

            opts.volumes.push(VolumeSpec {
                read_only: mount.read_only,
                ..spec
            });
        }
        Ok(())
    }
}

// ============================================================================
// MANAGEMENT FLAGS
// ============================================================================

/// Parse a lifecycle duration: bare seconds, or one `s`/`m`/`h`/`d` suffix.
///
/// Bare digits stay seconds so the flags accept the same integers the wire
/// contract carries; the suffixes exist because a fortnight in seconds is not
/// a thing anyone should have to type.
fn parse_duration_seconds(raw: &str) -> Result<u32, String> {
    let raw = raw.trim();
    if raw.is_empty() {
        return Err("duration must not be empty".to_string());
    }

    let (digits, multiplier) = match raw.as_bytes()[raw.len() - 1] {
        b's' => (&raw[..raw.len() - 1], 1),
        b'm' => (&raw[..raw.len() - 1], 60),
        b'h' => (&raw[..raw.len() - 1], 3_600),
        b'd' => (&raw[..raw.len() - 1], 86_400),
        _ => (raw, 1),
    };

    let value: u32 = digits.parse().map_err(|_| {
        format!("duration {raw:?} must be a whole number of seconds, or use s/m/h/d")
    })?;
    value
        .checked_mul(multiplier)
        .ok_or_else(|| format!("duration {raw:?} is too large"))
}

#[derive(Args, Debug, Clone)]
pub struct ManagementFlags {
    /// Assign a name to the box
    #[arg(long)]
    pub name: Option<String>,

    /// Stop the box after this much inactivity; `0` disables.
    ///
    /// Seconds when bare, or a suffixed duration: `30s`, `15m`, `2h`, `7d`.
    /// Requires a server (`--url`, or a configured profile): the deadline is
    /// swept on a timer, which only `boxlite serve` and the cloud run. Against
    /// the embedded runtime a non-zero value is rejected.
    #[arg(long = "auto-stop", value_name = "DURATION", value_parser = parse_duration_seconds)]
    pub auto_stop: Option<u32>,

    /// Delete the box this long after it stops; `0` disables.
    ///
    /// Same grammar and the same server requirement as `--auto-stop`.
    #[arg(long = "auto-delete", value_name = "DURATION", value_parser = parse_duration_seconds)]
    pub auto_delete: Option<u32>,

    /// Refuse operations that would implicitly wake a stopped box.
    ///
    /// Without this, exec/files/attach against a stopped box start it first.
    /// A box that has never run is unaffected: its first boot is not a resume.
    /// Enforced by the server; the embedded runtime records the preference but
    /// has no request path to refuse.
    #[arg(long = "no-auto-resume")]
    pub no_auto_resume: bool,

    /// Sandbox security: `enable` (default) or `disable` (case-insensitive).
    /// Absent → the box uses `SecurityOptions::default()` = enable, the
    /// fully-isolated profile. Use `--security=disable` to turn the sandbox
    /// off (master switch + all sub-protections) when debugging.
    #[arg(long, env = "BOXLITE_SECURITY")]
    pub security: Option<String>,

    /// Require nested virtualization for workloads in the box.
    /// Starting the box fails if the host cannot provide it.
    #[arg(long = "nested-virtualization", hide = true)]
    pub nested_virtualization: bool,
}

impl ManagementFlags {
    pub fn require_enabled(&self, features: &ExperimentalFeatures) -> boxlite::BoxliteResult<()> {
        if !self.nested_virtualization {
            return Ok(());
        }

        features.require(ExperimentalFeature::NestedVirtualization)
    }

    /// Refuse a deadline nothing will act on.
    ///
    /// Both deadlines need a sweeper, which only a server runs. Against the
    /// embedded runtime `--auto-stop` is refused by the engine anyway
    /// (`reject_local_unsupported_options`), and `--auto-delete` is worse than
    /// refused: `removes_on_stop()` is `effective_auto_delete() > 0`, so it
    /// would silently delete the box at its stop — the `--rm` behaviour the
    /// parser declares mutually exclusive with it. Failing here says so, and
    /// says it the same way for both flags.
    pub fn require_sweeper(&self, targets_rest: bool) -> anyhow::Result<()> {
        if targets_rest {
            return Ok(());
        }
        for (flag, seconds) in [
            ("--auto-stop", self.auto_stop),
            ("--auto-delete", self.auto_delete),
        ] {
            if seconds.is_some_and(|seconds| seconds > 0) {
                anyhow::bail!(
                    "{flag} needs a server to enforce it — point at one with --url \
                     (or a configured profile). Use `boxlite run --rm` when \
                     immediate local cleanup is required."
                );
            }
        }
        Ok(())
    }

    pub fn apply_to(&self, opts: &mut BoxOptions) -> anyhow::Result<()> {
        // The CLI default (like `docker run`) is to keep the box. `run --rm`
        // overrides this sentinel at its command boundary; `create` does not
        // expose remove-on-stop because it always creates a detached box.
        opts.auto_delete = Some(0);

        // An explicit deadline replaces that sentinel. It has to be written
        // after it, not before: the line above is unconditional, so setting
        // these first would have `--auto-delete 3600` overwritten by the `0`
        // that means "no deadline". `--rm` and `--auto-delete` are mutually
        // exclusive at the parser, so only one of them is ever meaningful.
        if let Some(seconds) = self.auto_delete {
            opts.auto_delete = Some(seconds);
        }
        if let Some(seconds) = self.auto_stop {
            opts.auto_stop = Some(seconds);
        }
        if self.no_auto_resume {
            opts.auto_resume = Some(false);
        }
        if let Some(ref preset) = self.security {
            // Bubble the typo'd-preset error all the way back to the
            // CLI exit so the operator sees the offending value.
            opts.advanced.security =
                boxlite::SecurityOptions::from_preset(preset).map_err(anyhow::Error::from)?;
        }
        if self.nested_virtualization {
            opts.advanced.nested_virtualization = true;
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use boxlite::runtime::options::NetworkSpec;
    use std::fs;

    fn parse_projected(args: &[&str]) -> Cli {
        try_parse_from(args).expect("parse")
    }

    #[test]
    fn command_tree_owns_after_command_scope_rejection() {
        for args in [
            &["boxlite", "serve", "--url", "https://irrelevant.test"][..],
            &["boxlite", "ls", "--registry", "registry.example.test"][..],
            &[
                "boxlite",
                "auth",
                "logout",
                "--url",
                "https://irrelevant.test",
            ][..],
            &[
                "boxlite",
                "volume",
                "list",
                "--config",
                "/tmp/irrelevant.json",
            ][..],
            &["boxlite", "completion", "bash", "--debug"][..],
        ] {
            let error = command()
                .try_get_matches_from(args)
                .expect_err("the command tree must reject an out-of-scope option");
            assert_eq!(error.kind(), ErrorKind::UnknownArgument, "{args:?}");
        }
    }

    #[test]
    fn scoped_command_tree_is_valid_and_preserves_root_and_leaf_flag_positions() {
        command().debug_assert();

        for args in [
            ["boxlite", "--url", "https://example.test", "run", "alpine"],
            ["boxlite", "run", "--url", "https://example.test", "alpine"],
        ] {
            let cli = parse_projected(&args);
            assert_eq!(cli.global.url.as_deref(), Some("https://example.test"));
            assert!(matches!(cli.command, Commands::Run(_)));
        }

        for args in [
            &["boxlite", "run", "--path-prefix", "", "alpine"][..],
            &["boxlite", "run", "--profile", "", "alpine"][..],
            &["boxlite", "run", "--registry", "", "alpine"][..],
            &["boxlite", "run", "--config", "", "alpine"][..],
        ] {
            let error = try_parse_from(args).expect_err("empty global value must be rejected");
            assert_eq!(error.kind(), ErrorKind::ValueValidation, "{args:?}");
        }

        for args in [
            ["boxlite", "--url", "https://example.test", "auth", "login"],
            ["boxlite", "auth", "login", "--url", "https://example.test"],
        ] {
            let cli = parse_projected(&args);
            let Commands::Auth(auth) = cli.command else {
                panic!("expected auth");
            };
            let crate::commands::auth::AuthCommand::Login(login) = auth.command else {
                panic!("expected login");
            };
            assert_eq!(login.url.as_deref(), Some("https://example.test"));
        }

        for args in [
            &["boxlite", "--url", "https://example.test", "auth", "status"][..],
            &["boxlite", "auth", "status", "--url", "https://example.test"][..],
        ] {
            let cli = parse_projected(args);
            assert_eq!(cli.global.url.as_deref(), Some("https://example.test"));
        }

        let error = try_parse_from(["boxlite", "auth", "--url", "https://example.test", "status"])
            .expect_err("leaf-only --url must follow the auth leaf");
        assert_eq!(error.kind(), ErrorKind::UnknownArgument);
    }

    #[test]
    fn root_and_leaf_repeatable_global_values_are_both_preserved() {
        let cli = parse_projected(&[
            "boxlite",
            "--registry",
            "root.example.test",
            "run",
            "--registry",
            "leaf.example.test",
            "alpine",
        ]);

        assert_eq!(
            cli.global.registry,
            ["root.example.test", "leaf.example.test"]
        );
    }

    #[test]
    fn auth_login_rejects_an_explicit_empty_leaf_url() {
        let error = try_parse_from(["boxlite", "auth", "login", "--url", ""])
            .expect_err("an explicit empty URL must not fall through to another target");

        assert_eq!(error.kind(), ErrorKind::InvalidValue);
    }

    fn global_option_tokens(id: &str) -> Vec<&'static str> {
        match id {
            "debug" => vec!["--debug"],
            "home" => vec!["--home", "/tmp/boxlite-strict-options"],
            "registry" => vec!["--registry", "registry.example.test"],
            "config" => vec!["--config", "/tmp/boxlite-config.json"],
            "url" => vec!["--url", "https://example.test"],
            "profile" => vec!["--profile", "test-profile"],
            "path_prefix" => vec!["--path-prefix", "tenant"],
            other => panic!("missing command-line spelling for {other}"),
        }
    }

    const REGISTRY: &[&str] = &["registry"];
    const LOCAL_WITHOUT_REGISTRY: &[&str] = &["registry", "url", "profile", "path_prefix"];
    const LOCAL_WITH_REGISTRY: &[&str] = &["url", "profile", "path_prefix"];
    const AUTH_WITH_URL: &[&str] = &["registry", "config", "path_prefix"];
    const AUTH_WITHOUT_URL: &[&str] = &["registry", "config", "url", "path_prefix"];
    const VOLUME: &[&str] = &["registry", "config"];
    const ALL_GLOBALS: &[&str] = &[
        "debug",
        "home",
        "registry",
        "config",
        "url",
        "profile",
        "path_prefix",
    ];
    const IRRELEVANT_GLOBAL_CASES: &[(&[&str], usize, &[&str])] = &[
        (&["boxlite", "exec", "box", "--", "true"], 1, REGISTRY),
        (&["boxlite", "list"], 1, REGISTRY),
        (&["boxlite", "ls"], 1, REGISTRY),
        (&["boxlite", "ps"], 1, REGISTRY),
        (&["boxlite", "rm", "box"], 1, REGISTRY),
        (&["boxlite", "start", "box"], 1, REGISTRY),
        (&["boxlite", "stop", "box"], 1, REGISTRY),
        (&["boxlite", "restart", "box"], 1, REGISTRY),
        (&["boxlite", "inspect", "box"], 1, REGISTRY),
        (&["boxlite", "cp", "box:/src", "/tmp/dst"], 1, REGISTRY),
        (&["boxlite", "stats", "box"], 1, REGISTRY),
        (
            &["boxlite", "network", "tunnel", "box", "3000"],
            2,
            REGISTRY,
        ),
        (&["boxlite", "images"], 1, LOCAL_WITHOUT_REGISTRY),
        (&["boxlite", "info"], 1, LOCAL_WITHOUT_REGISTRY),
        (&["boxlite", "logs", "box"], 1, LOCAL_WITHOUT_REGISTRY),
        (&["boxlite", "pull", "alpine"], 1, LOCAL_WITH_REGISTRY),
        (&["boxlite", "serve"], 1, LOCAL_WITH_REGISTRY),
        (&["boxlite", "auth", "login"], 2, AUTH_WITH_URL),
        (&["boxlite", "auth", "status"], 2, AUTH_WITH_URL),
        (&["boxlite", "auth", "whoami"], 2, AUTH_WITH_URL),
        (&["boxlite", "auth", "logout"], 2, AUTH_WITHOUT_URL),
        (&["boxlite", "volume", "create"], 2, VOLUME),
        (&["boxlite", "volume", "ls"], 2, VOLUME),
        (&["boxlite", "volume", "list"], 2, VOLUME),
        (&["boxlite", "volume", "get", "volume"], 2, VOLUME),
        (&["boxlite", "volume", "inspect", "volume"], 2, VOLUME),
        (&["boxlite", "volume", "rm", "volume"], 2, VOLUME),
        (&["boxlite", "volume", "delete", "volume"], 2, VOLUME),
        (&["boxlite", "completion", "bash"], 1, ALL_GLOBALS),
    ];

    fn assert_irrelevant_global_rejected(base: &[&str], command_depth: usize, id: &str) {
        let tokens = global_option_tokens(id);
        let mut positions = vec![1, command_depth + 1];
        if command_depth > 1 {
            positions.push(2);
        }
        positions.push(
            base.iter()
                .position(|token| *token == "--")
                .unwrap_or(base.len()),
        );
        positions.sort_unstable();
        positions.dedup();

        for position in positions {
            let mut args = base.to_vec();
            args.splice(position..position, tokens.iter().copied());
            let error = try_parse_from(&args).expect_err("irrelevant option must be rejected");
            assert_eq!(error.kind(), ErrorKind::UnknownArgument, "{args:?}");
            assert!(
                error.to_string().contains(tokens[0]),
                "error must identify {}: {error}",
                tokens[0]
            );
        }
    }

    #[test]
    fn every_command_path_rejects_irrelevant_global_options() {
        for (base, command_depth, hidden) in IRRELEVANT_GLOBAL_CASES {
            for id in *hidden {
                assert_irrelevant_global_rejected(base, *command_depth, id);
            }
        }
    }

    #[test]
    fn command_tree_rejects_irrelevant_options_encountered_before_help() {
        for args in [
            &[
                "boxlite",
                "exec",
                "--registry",
                "registry.example.test",
                "--help",
            ][..],
            &[
                "boxlite",
                "auth",
                "logout",
                "--url=https://irrelevant.test",
                "--help",
            ][..],
            &[
                "boxlite",
                "volume",
                "list",
                "--config",
                "/tmp/irrelevant.json",
                "--help",
            ][..],
            &["boxlite", "completion", "--debug", "bash", "--help"][..],
        ] {
            let error = try_parse_from(args).expect_err("irrelevant option precedes help");
            assert_eq!(error.kind(), ErrorKind::UnknownArgument, "{args:?}");
        }
    }

    #[test]
    fn display_actions_remain_terminal() {
        for args in [
            &[
                "boxlite",
                "--help",
                "serve",
                "--url",
                "https://example.test",
            ][..],
            &[
                "boxlite",
                "-h",
                "completion",
                "--url",
                "https://example.test",
            ][..],
            &[
                "boxlite",
                "serve",
                "--help",
                "--url",
                "https://example.test",
            ][..],
            &[
                "boxlite",
                "auth",
                "logout",
                "--help",
                "--url",
                "https://example.test",
            ][..],
            &[
                "boxlite",
                "--registry",
                "registry.example.test",
                "exec",
                "--help",
            ][..],
            &["boxlite", "--url", "https://example.test", "help", "serve"][..],
        ] {
            let error = try_parse_from(args).expect_err("help exits through clap");
            assert_eq!(error.kind(), ErrorKind::DisplayHelp, "{args:?}");
        }

        let error = try_parse_from([
            "boxlite",
            "--registry",
            "registry.example.test",
            "--version",
        ])
        .expect_err("version exits through clap");
        assert_eq!(error.kind(), ErrorKind::DisplayVersion);
    }

    #[test]
    fn completion_uses_the_same_global_capability_matrix() {
        let mut completion = completion_command();
        completion.build();

        let serve = completion
            .get_subcommands()
            .find(|subcommand| subcommand.get_name() == "serve")
            .unwrap();
        assert!(!serve.get_arguments().any(|arg| arg.get_id() == "url"));

        let run = completion
            .get_subcommands()
            .find(|subcommand| subcommand.get_name() == "run")
            .unwrap();
        assert!(run.get_arguments().any(|arg| arg.get_id() == "url"));
    }

    #[test]
    fn parser_rejects_options_that_cannot_affect_the_command() {
        for args in [
            &[
                "boxlite",
                "exec",
                "--entrypoint",
                "/bin/sh",
                "box",
                "--",
                "true",
            ][..],
            &["boxlite", "create", "--detach", "alpine"][..],
            &["boxlite", "create", "--rm", "alpine"][..],
            &["boxlite", "images", "--all"][..],
            &["boxlite", "run", "-i", "-d", "alpine"][..],
            &["boxlite", "exec", "-i", "-d", "box", "--", "true"][..],
            &["boxlite", "rm", "--all", "box"][..],
            &["boxlite", "inspect", "--latest", "box"][..],
            &["boxlite", "auth", "login", "--callback-port", "0"][..],
            &["boxlite", "list", "--quiet", "--format", "json"][..],
            &["boxlite", "images", "--quiet", "--format", "json"][..],
            &["boxlite", "volume", "ls", "--quiet", "--format", "json"][..],
            &["boxlite", "serve", "--api-key", ""][..],
        ] {
            assert!(Cli::try_parse_from(args).is_err(), "accepted {args:?}");
        }

        let cli = Cli::try_parse_from(["boxlite", "run", "--entrypoint", "/bin/sh", "alpine"])
            .expect("run owns --entrypoint");
        let Commands::Run(run) = cli.command else {
            panic!("expected run");
        };
        assert_eq!(run.entrypoint.as_deref(), Some("/bin/sh"));

        let cli = Cli::try_parse_from(["boxlite", "cp", "--no-include-parent", "box:/src", "/dst"])
            .expect("inverse parent option parses");
        let Commands::Cp(cp) = cli.command else {
            panic!("expected cp");
        };
        assert!(cp.no_include_parent);
    }
    use std::path::PathBuf;
    use tempfile::TempDir;

    #[test]
    fn test_apply_env_vars_with_lookup() {
        let mut opts = BoxOptions::default();
        let current_env = vec![
            "TEST_VAR=test_value".to_string(),
            "TEST_HOST_VAR".to_string(),
            "NON_EXISTENT_VAR".to_string(),
        ];

        apply_env_vars_with_lookup(&current_env, &mut opts, |k| {
            if k == "TEST_HOST_VAR" {
                Some("host_value".to_string())
            } else {
                None
            }
        });

        assert!(
            opts.env
                .contains(&("TEST_VAR".to_string(), "test_value".to_string()))
        );

        assert!(
            opts.env
                .contains(&("TEST_HOST_VAR".to_string(), "host_value".to_string()))
        );

        assert!(!opts.env.iter().any(|(k, _)| k == "NON_EXISTENT_VAR"));
    }

    #[test]
    fn resolve_runtime_options_prepends_cli_registries_to_config() {
        let temp_dir = TempDir::new().unwrap();
        let config_path = temp_dir.path().join("config.json");
        fs::write(
            &config_path,
            r#"{
                "image_registries": [
                    {
                        "host": "registry.local:5000",
                        "transport": "http",
                        "search": true
                    }
                ]
            }"#,
        )
        .unwrap();

        let flags = GlobalFlags {
            debug: false,
            home: Some(temp_dir.path().join("home")),
            registry: vec!["cli.registry.local".to_string()],
            config: Some(config_path.display().to_string()),
            url: None,
            profile: None,
            path_prefix: None,
            experimental_features: ExperimentalFeatures::default(),
        };

        let options = flags.resolve_runtime_options().unwrap();

        assert_eq!(options.home_dir, temp_dir.path().join("home"));
        assert_eq!(
            options.image_registries,
            vec![
                ImageRegistry::https("cli.registry.local").with_search(true),
                ImageRegistry::http("registry.local:5000").with_search(true),
            ]
        );
    }

    fn rest_flags(
        url: Option<&str>,
        profile: Option<&str>,
        path_prefix: Option<&str>,
    ) -> GlobalFlags {
        GlobalFlags {
            debug: false,
            home: None,
            registry: vec![],
            config: None,
            url: url.map(str::to_string),
            profile: profile.map(str::to_string),
            path_prefix: path_prefix.map(str::to_string),
            experimental_features: ExperimentalFeatures::default(),
        }
    }

    fn api_key_profile(path_prefix: Option<&str>) -> crate::credentials::Profile {
        crate::credentials::Profile {
            url: "https://api.example.com".to_string(),
            path_prefix: path_prefix.map(str::to_string),
            auth_method: crate::credentials::AuthMethod::ApiKey,
            api_key: Some(secrecy::SecretString::from("profile-bearer".to_string())),
            ..Default::default()
        }
    }

    #[test]
    fn api_key_env_preserves_profile_path_prefix() {
        // Regression: an ambient BOXLITE_API_KEY must override only the bearer,
        // not silently discard the selected profile's routing slot — dropping
        // it made the URL builder emit the prefix-less `/v1/boxes` shape, which
        // a multi-tenant server rejects with 404.
        let flags = rest_flags(None, Some("p1"), None);
        let opts = flags
            .resolve_rest_options(
                Some(api_key_profile(Some("acme"))),
                Some("env-key".to_string()),
            )
            .expect("REST options resolved");
        assert_eq!(
            opts.path_prefix.as_deref(),
            Some("acme"),
            "profile routing slot must survive an ambient BOXLITE_API_KEY"
        );
    }

    #[tokio::test]
    async fn api_key_env_overrides_profile_bearer_but_keeps_prefix() {
        // Confirmed precedence: env key wins for the bearer, profile prefix stays.
        let flags = rest_flags(None, Some("p1"), None);
        let opts = flags
            .resolve_rest_options(
                Some(api_key_profile(Some("acme"))),
                Some("env-key".to_string()),
            )
            .expect("REST options resolved");

        let token = opts
            .credential
            .expect("credential set")
            .get_token()
            .await
            .expect("token")
            .token;
        assert_eq!(
            token, "env-key",
            "BOXLITE_API_KEY overrides the profile bearer"
        );
        assert_eq!(
            opts.path_prefix.as_deref(),
            Some("acme"),
            "prefix preserved alongside the overridden bearer"
        );
    }

    #[test]
    fn api_key_env_without_profile_has_no_prefix() {
        // No profile → no routing slot, even with a key (single-tenant shape).
        let flags = rest_flags(Some("https://api.example.com"), None, None);
        let opts = flags
            .resolve_rest_options(None, Some("env-key".to_string()))
            .expect("REST options resolved");
        assert!(opts.path_prefix.is_none());
    }

    #[test]
    fn test_resource_flags_cpu_cap() {
        let flags = ResourceFlags {
            cpus: Some(1000),
            memory: None,
            disk_size_gb: None,
        };

        let mut opts = BoxOptions::default();
        flags.apply_to(&mut opts);

        assert_eq!(opts.cpus, Some(255));
    }

    #[test]
    fn test_resource_flags_disk_size_plumbed() {
        // --disk-size <GB> must reach BoxOptions.disk_size_gb verbatim so the
        // COW overlay in container_rootfs::create_cow_disk picks up
        // max(user_size, base_image_size). A regression that drops this
        // flag would leave agent-workflow tests at base-image size and
        // they'd silently ENOSPC mid-`docker pull`.
        let flags = ResourceFlags {
            cpus: None,
            memory: None,
            disk_size_gb: Some(10),
        };

        let mut opts = BoxOptions::default();
        flags.apply_to(&mut opts);

        assert_eq!(opts.disk_size_gb, Some(10));
    }

    #[test]
    fn test_resource_flags_disk_size_default_unset() {
        // No --disk-size on the command line means BoxOptions.disk_size_gb
        // stays None — container_rootfs::create_cow_disk's `if let Some`
        // branch is skipped and the COW disk is exactly the base image
        // size. This is the documented default; the test pins it so a
        // refactor that injects a fallback (`unwrap_or(N)`) would fail.
        let flags = ResourceFlags {
            cpus: None,
            memory: None,
            disk_size_gb: None,
        };

        let mut opts = BoxOptions::default();
        flags.apply_to(&mut opts);

        assert_eq!(opts.disk_size_gb, None);
    }

    #[test]
    fn custom_kernel_flags_are_applied_as_one_boot_configuration() {
        let flags = KernelFlags {
            kernel: Some(PathBuf::from("/tmp/vmlinux")),
            kernel_format: Some(KernelFormatArg::Elf),
            initramfs: Some(PathBuf::from("/tmp/initramfs.img")),
            kernel_args: Some("console=ttyS0 panic=-1".to_string()),
        };

        let mut opts = BoxOptions::default();
        flags.apply_to(&mut opts);

        let kernel = opts.advanced.kernel.expect("custom kernel configuration");
        assert_eq!(kernel.path, PathBuf::from("/tmp/vmlinux"));
        assert_eq!(kernel.format, KernelFormat::Elf);
        assert_eq!(kernel.initramfs, Some(PathBuf::from("/tmp/initramfs.img")));
        assert_eq!(
            kernel.command_line.as_deref(),
            Some("console=ttyS0 panic=-1")
        );
    }

    #[test]
    fn custom_kernel_gate_uses_explicit_feature_state() {
        let flags = KernelFlags {
            kernel: Some(PathBuf::from("/tmp/vmlinux")),
            ..Default::default()
        };

        let error = flags
            .require_enabled(&ExperimentalFeatures::default())
            .expect_err("custom kernel must be disabled by default");
        assert!(
            error
                .to_string()
                .contains("ExperimentalFeature::CustomKernel")
        );

        let enabled = ExperimentalFeatures::parse("custom-kernel").unwrap();
        flags.require_enabled(&enabled).unwrap();
    }

    #[test]
    fn nested_virtualization_gate_uses_explicit_feature_state() {
        let flags = ManagementFlags {
            nested_virtualization: true,
            auto_stop: None,
            auto_delete: None,
            no_auto_resume: false,
            name: None,
            security: None,
        };

        let error = flags
            .require_enabled(&ExperimentalFeatures::default())
            .expect_err("nested virtualization must be disabled by default");
        assert!(
            error
                .to_string()
                .contains("ExperimentalFeature::NestedVirtualization")
        );

        let enabled = ExperimentalFeatures::parse("nested-virtualization").unwrap();
        flags.require_enabled(&enabled).unwrap();
    }

    #[test]
    fn experimental_flags_are_hidden_from_help() {
        for command in ["run", "create"] {
            let error = Cli::try_parse_from(["boxlite", command, "--help"]).unwrap_err();
            assert_eq!(error.kind(), clap::error::ErrorKind::DisplayHelp);

            let help = error.to_string();
            for rc_flag in [
                "--kernel",
                "--kernel-format",
                "--initramfs",
                "--kernel-args",
                "--nested-virtualization",
            ] {
                assert!(
                    !help.contains(rc_flag),
                    "{rc_flag} leaked into {command} help:\n{help}"
                );
            }
            assert!(
                !help.contains("\nAdvanced boot options:\n"),
                "empty advanced boot section leaked into {command} help:\n{help}"
            );
        }
    }

    #[test]
    fn experimental_flags_are_hidden_from_completions() {
        for shell in [Shell::Bash, Shell::Zsh, Shell::Fish] {
            let mut output = Vec::new();
            generate_completion(&shell, "boxlite", &mut output);
            let completion = String::from_utf8(output).unwrap();

            for name in [
                "kernel",
                "kernel-format",
                "initramfs",
                "kernel-args",
                "nested-virtualization",
            ] {
                let rc_flag = match shell {
                    Shell::Fish => format!("-l {name}"),
                    Shell::Bash | Shell::Zsh => format!("--{name}"),
                };
                assert!(
                    !completion.contains(&rc_flag),
                    "{rc_flag} leaked into {shell:?} completion"
                );
            }
            for name in ["rootfs", "cpus", "network"] {
                let stable_flag = match shell {
                    Shell::Fish => format!("-l {name}"),
                    Shell::Bash | Shell::Zsh => format!("--{name}"),
                };
                assert!(
                    completion.contains(&stable_flag),
                    "{stable_flag} missing from {shell:?} completion"
                );
            }
        }
    }

    #[test]
    fn kernel_companion_flags_require_kernel() {
        let error = Cli::try_parse_from([
            "boxlite",
            "run",
            "--initramfs",
            "/tmp/initramfs.img",
            "alpine",
        ])
        .unwrap_err();

        assert_eq!(
            error.kind(),
            clap::error::ErrorKind::MissingRequiredArgument
        );
    }

    fn network_flags(network: Option<&str>, allow_net: &[&str]) -> NetworkFlags {
        NetworkFlags {
            network: network.map(str::to_string),
            allow_net: allow_net.iter().map(|s| s.to_string()).collect(),
            inbound: None,
            net_tx_kbps: None,
            net_rx_kbps: None,
        }
    }

    /// apply_to returns early when no mode or allowlist flag is set. A rate
    /// limit must survive that path, or `--net-tx-kbps` on its own is dropped
    /// with no error at all.
    #[test]
    fn rate_limit_flags_apply_without_any_other_network_flag() {
        let mut opts = BoxOptions::default();
        let mut flags = network_flags(None, &[]);
        flags.net_tx_kbps = Some(10_000);

        flags.apply_to(&mut opts).expect("apply_to");

        assert_eq!(opts.advanced.network_rate_limit.tx_kbps, Some(10_000));
        assert_eq!(opts.advanced.network_rate_limit.rx_kbps, None);
        // The early return still protects the untouched network defaults.
        assert!(matches!(
            opts.network,
            boxlite::NetworkSpec::Enabled { ref allow_net } if allow_net.is_empty()
        ));
    }

    #[test]
    fn rate_limit_flags_apply_alongside_an_allowlist() {
        let mut opts = BoxOptions::default();
        let mut flags = network_flags(None, &["example.com"]);
        flags.net_tx_kbps = Some(1_000);
        flags.net_rx_kbps = Some(2_000);

        flags.apply_to(&mut opts).expect("apply_to");

        assert_eq!(opts.advanced.network_rate_limit.tx_kbps, Some(1_000));
        assert_eq!(opts.advanced.network_rate_limit.rx_kbps, Some(2_000));
    }

    #[test]
    fn test_network_flags_default_left_untouched() {
        // Neither flag set => BoxOptions::default() network is preserved
        // (Enabled, empty allow_net), so a bare `run` keeps full access.
        let mut opts = BoxOptions::default();
        network_flags(None, &[])
            .apply_to(&mut opts)
            .expect("no-op apply");

        assert!(
            matches!(opts.network, NetworkSpec::Enabled { ref allow_net } if allow_net.is_empty())
        );
    }

    #[test]
    fn test_network_flags_disabled() {
        // --network disabled => NetworkSpec::Disabled (no eth0, gvproxy off).
        let mut opts = BoxOptions::default();
        network_flags(Some("disabled"), &[])
            .apply_to(&mut opts)
            .expect("disabled is valid");

        assert!(matches!(opts.network, NetworkSpec::Disabled));
    }

    #[test]
    fn test_network_flags_allow_net_implies_enabled() {
        // --allow-net without --network => Enabled with the egress allowlist,
        // matching the REST NetworkConfig{mode, allow_net} mapping.
        let mut opts = BoxOptions::default();
        network_flags(None, &["api.openai.com", "10.0.0.0/8"])
            .apply_to(&mut opts)
            .expect("allow-net implies enabled");

        match opts.network {
            NetworkSpec::Enabled { allow_net } => {
                assert_eq!(allow_net, vec!["api.openai.com", "10.0.0.0/8"]);
            }
            other => panic!("expected Enabled with allowlist, got {other:?}"),
        }
    }

    #[test]
    fn test_network_flags_disabled_with_allow_net_is_rejected() {
        // --network disabled + --allow-net is contradictory; the error comes
        // from NetworkSpec::try_from (single source of truth), not the CLI.
        let mut opts = BoxOptions::default();
        let err = network_flags(Some("disabled"), &["api.openai.com"])
            .apply_to(&mut opts)
            .expect_err("disabled + allow-net must error");

        assert!(err.to_string().contains("allow_net"));
    }

    #[test]
    fn test_network_flags_invalid_mode_is_rejected() {
        // Unknown mode strings surface NetworkMode::from_str's error rather
        // than silently defaulting to enabled.
        let mut opts = BoxOptions::default();
        let err = network_flags(Some("bridge"), &[])
            .apply_to(&mut opts)
            .expect_err("unknown mode must error");

        assert!(err.to_string().contains("network mode"));
    }

    #[test]
    fn test_network_flags_inbound_disabled_sets_private() {
        // --inbound disabled alone (no --network/--allow-net) still applies,
        // and leaves outbound at its Enabled/full-access default.
        let mut opts = BoxOptions::default();
        let mut flags = network_flags(None, &[]);
        flags.inbound = Some("disabled".to_string());
        flags.apply_to(&mut opts).expect("disabled is valid");

        assert!(matches!(opts.inbound_network, NetworkSpec::Disabled));
        assert!(
            matches!(opts.network, NetworkSpec::Enabled { ref allow_net } if allow_net.is_empty())
        );
    }

    #[test]
    fn cli_rejects_inbound_allow_net_flag() {
        // The flag is deliberately not exposed until inbound allowlist
        // enforcement exists — a flag that always errors would advertise a
        // feature that doesn't work.
        let err = Cli::try_parse_from([
            "boxlite",
            "run",
            "--inbound-allow-net",
            "10.0.0.0/8",
            "alpine:latest",
        ])
        .expect_err("unknown flag must fail parsing");
        assert_eq!(err.kind(), clap::error::ErrorKind::UnknownArgument);
    }

    #[test]
    fn cli_parses_run_with_inbound_flags() {
        let cli = Cli::try_parse_from(["boxlite", "run", "--inbound", "disabled", "alpine:latest"])
            .expect("parse");
        let Commands::Run(args) = cli.command else {
            panic!("expected Run")
        };
        assert_eq!(args.network.inbound.as_deref(), Some("disabled"));
    }

    #[test]
    fn test_network_flags_invalid_inbound_mode_is_rejected() {
        let mut opts = BoxOptions::default();
        let mut flags = network_flags(None, &[]);
        flags.inbound = Some("bridge".to_string());
        let err = flags
            .apply_to(&mut opts)
            .expect_err("unknown inbound mode must error");

        assert!(err.to_string().contains("network mode"));
    }

    fn process_flags() -> ProcessFlags {
        ProcessFlags {
            interactive: false,
            tty: false,
            env: Vec::new(),
            workdir: None,
            user: None,
        }
    }

    /// `-t` has to reach `BoxOptions.tty`, because the terminal now belongs to
    /// the *container's* init rather than to an exec: nothing downstream can
    /// add it later. When this mapping was missing, `run -it` still parsed,
    /// still put the local terminal in raw mode, and still ran — just against a
    /// process on pipes, with no prompt and no job control.
    #[test]
    fn test_process_flags_tty_reaches_box_options() {
        let mut opts = BoxOptions::default();
        assert!(!opts.tty, "a box is not a terminal by default");

        let mut flags = process_flags();
        flags.tty = true;
        flags.apply_to(&mut opts).expect("tty apply");

        assert!(opts.tty, "-t must make the main command a terminal");
    }

    #[test]
    fn test_parse_publish_spec_host_box() {
        let spec = super::parse_publish_spec("18789:18789").unwrap();
        assert_eq!(spec.host_port, Some(18789));
        assert_eq!(spec.guest_port, 18789);
        assert!(matches!(spec.protocol, PortProtocol::Tcp));
    }

    #[test]
    fn test_parse_publish_spec_host_box_tcp() {
        let spec = super::parse_publish_spec("8080:80/tcp").unwrap();
        assert_eq!(spec.host_port, Some(8080));
        assert_eq!(spec.guest_port, 80);
        assert!(matches!(spec.protocol, PortProtocol::Tcp));
    }

    #[test]
    fn test_parse_publish_spec_box_only() {
        let spec = super::parse_publish_spec("80").unwrap();
        assert_eq!(spec.host_port, None);
        assert_eq!(spec.guest_port, 80);
    }

    #[test]
    fn test_parse_publish_spec_udp_is_rejected() {
        let err = super::parse_publish_spec("53:53/udp").unwrap_err();
        assert!(err.to_string().contains("UDP port forwarding"));
    }

    #[test]
    fn test_parse_publish_spec_invalid_protocol() {
        assert!(super::parse_publish_spec("80:80/sctp").is_err());
    }

    #[test]
    fn test_parse_publish_spec_invalid_port() {
        assert!(super::parse_publish_spec("0:80").is_err());
        assert!(super::parse_publish_spec("99999:80").is_err());
    }

    #[test]
    fn test_publish_flags_apply_to() {
        let flags = PublishFlags {
            publish: vec!["18789:18789".to_string(), "8080:80/tcp".to_string()],
        };
        let mut opts = BoxOptions::default();
        flags.apply_to(&mut opts).unwrap();
        assert_eq!(opts.ports.len(), 2);
        assert_eq!(opts.ports[0].host_port, Some(18789));
        assert_eq!(opts.ports[0].guest_port, 18789);
        assert_eq!(opts.ports[1].host_port, Some(8080));
        assert_eq!(opts.ports[1].guest_port, 80);
    }

    #[test]
    fn test_volume_flags_apply_to() {
        let flags = VolumeFlags {
            volume: vec![
                "/host/data:/guest/data".to_string(),
                "/readonly:/ro:ro".to_string(),
            ],
        };
        let mut opts = BoxOptions::default();
        flags.apply_to(&mut opts, None).unwrap();
        assert_eq!(opts.volumes.len(), 2);
        assert_eq!(opts.volumes[0].host_path, "/host/data");
        assert_eq!(opts.volumes[0].guest_path, "/guest/data");
        assert!(!opts.volumes[0].read_only);
        assert_eq!(opts.volumes[1].host_path, "/readonly");
        assert_eq!(opts.volumes[1].guest_path, "/ro");
        assert!(opts.volumes[1].read_only);
    }

    #[test]
    fn test_volume_flags_apply_to_windows_style() {
        let flags = VolumeFlags {
            volume: vec![
                r"C:\host\data:/guest/data".to_string(),
                r"D:\readonly:/ro:ro".to_string(),
            ],
        };
        let mut opts = BoxOptions::default();
        flags.apply_to(&mut opts, None).unwrap();
        assert_eq!(opts.volumes.len(), 2);
        assert_eq!(opts.volumes[0].host_path, r"C:\host\data");
        assert_eq!(opts.volumes[0].guest_path, "/guest/data");
        assert!(!opts.volumes[0].read_only);
        assert_eq!(opts.volumes[1].host_path, r"D:\readonly");
        assert_eq!(opts.volumes[1].guest_path, "/ro");
        assert!(opts.volumes[1].read_only);
    }

    /// `-v my-data:/data` reaches `BoxOptions` as a managed volume, not a host
    /// bind — the whole point of adopting Docker's rule. It must not touch the
    /// filesystem on the way: no canonicalize, no "path does not exist".
    #[test]
    fn test_volume_flags_apply_to_managed_volume() {
        let flags = VolumeFlags {
            volume: vec![
                "my-data:/data".to_string(),
                "vol_01K2EXAMPLE:/cache".to_string(),
            ],
        };
        let mut opts = BoxOptions::default();
        flags.apply_to(&mut opts, None).unwrap();

        assert_eq!(opts.volumes.len(), 2);
        assert_eq!(opts.volumes[0].managed_volume.as_deref(), Some("my-data"));
        assert_eq!(opts.volumes[0].host_path, "");
        assert_eq!(opts.volumes[0].guest_path, "/data");
        assert!(!opts.volumes[0].read_only);
        assert_eq!(
            opts.volumes[1].managed_volume.as_deref(),
            Some("vol_01K2EXAMPLE")
        );
        assert_eq!(opts.volumes[1].guest_path, "/cache");
    }

    /// `:ro` on a managed volume is refused, not quietly downgraded. Neither
    /// the server nor the REST client accepts one, and a caller who believes a
    /// mount is protected when it is writable is the failure worth preventing.
    #[test]
    fn test_volume_flags_reject_read_only_managed_volume() {
        let flags = VolumeFlags {
            volume: vec!["my-data:/data:ro".to_string()],
        };
        let mut opts = BoxOptions::default();

        let error = flags
            .apply_to(&mut opts, None)
            .expect_err("read-only managed volumes must be refused")
            .to_string();

        assert!(error.contains("read-only"), "{error}");
        assert!(error.contains("my-data"), "{error}");
        assert!(opts.volumes.is_empty());
    }

    /// A host bind may still be read-only — the restriction is specific to
    /// managed volumes, not to `:ro`.
    #[test]
    fn test_volume_flags_still_allow_read_only_host_binds() {
        let flags = VolumeFlags {
            volume: vec!["/host/data:/data:ro".to_string()],
        };
        let mut opts = BoxOptions::default();
        flags.apply_to(&mut opts, None).unwrap();

        assert_eq!(opts.volumes[0].host_path, "/host/data");
        assert!(opts.volumes[0].read_only);
    }

    /// A host bind keeps `managed_volume` unset, so the two `-v` forms stay
    /// distinguishable all the way to the wire.
    #[test]
    fn test_volume_flags_apply_to_leaves_host_binds_unmanaged() {
        let flags = VolumeFlags {
            volume: vec!["/host/data:/guest/data".to_string()],
        };
        let mut opts = BoxOptions::default();
        flags.apply_to(&mut opts, None).unwrap();

        assert_eq!(opts.volumes[0].managed_volume, None);
        assert_eq!(opts.volumes[0].host_path, "/host/data");
    }

    #[test]
    fn test_volume_flags_apply_to_anonymous() {
        let base = std::env::temp_dir();
        let flags = VolumeFlags {
            volume: vec!["/data".to_string(), "/cache:ro".to_string()],
        };
        let mut opts = BoxOptions::default();
        flags.apply_to(&mut opts, Some(&base)).unwrap();
        assert_eq!(opts.volumes.len(), 2);
        assert_eq!(opts.volumes[0].guest_path, "/data");
        assert!(
            opts.volumes[0].host_path.contains("anonymous"),
            "anonymous volume host_path should contain 'anonymous': {}",
            opts.volumes[0].host_path
        );
        assert!(std::path::Path::new(&opts.volumes[0].host_path).exists());
        assert_eq!(opts.volumes[1].guest_path, "/cache");
        assert!(opts.volumes[1].read_only);
        assert!(opts.volumes[1].host_path.contains("anonymous"));
    }

    // ─── auth subcommand parse tests ───────────────────────────────────────

    use crate::commands::{auth::AuthCommand, network::NetworkCommand};
    use clap::Parser;

    // ─── tunnel parse tests ────────────────────────────────────────────────

    #[test]
    fn tunnel_parses_box_and_port() {
        let cli =
            Cli::try_parse_from(["boxlite", "network", "tunnel", "mybox", "3000"]).expect("parse");
        let Commands::Network(network) = cli.command else {
            panic!("expected Commands::Network");
        };
        let NetworkCommand::Tunnel(args) = network.command;
        assert_eq!(args.target, "mybox");
        assert_eq!(args.port, 3000);
        assert!(args.listen.is_none());
    }

    #[test]
    fn network_tunnel_accepts_listener_forms() {
        for listen in [
            "8080",
            "0",
            "127.0.0.1:8080",
            "[::1]:8080",
            "unix:/tmp/app.sock",
        ] {
            Cli::try_parse_from([
                "boxlite", "network", "tunnel", "mybox", "3000", "--listen", listen,
            ])
            .unwrap_or_else(|error| panic!("{listen} should parse: {error}"));
        }
    }

    #[test]
    fn network_tunnel_rejects_ambiguous_listener_forms() {
        for listen in [
            "localhost:8080",
            "::1:8080",
            ":8080",
            "unix:relative.sock",
            "127.0.0.1",
        ] {
            let result = Cli::try_parse_from([
                "boxlite", "network", "tunnel", "mybox", "3000", "--listen", listen,
            ]);
            assert!(result.is_err(), "{listen} must be rejected");
        }
    }

    #[test]
    fn tunnel_is_not_a_top_level_command() {
        let result = Cli::try_parse_from(["boxlite", "tunnel", "mybox", "3000"]);
        assert!(result.is_err(), "tunnel must be nested under network");
    }

    #[test]
    fn tunnel_rejects_port_zero_at_parse() {
        let result = Cli::try_parse_from(["boxlite", "network", "tunnel", "mybox", "0"]);
        assert!(result.is_err(), "port 0 must be rejected by the parser");
    }

    /// `boxlite port BOX` existed earlier on this branch and was withdrawn:
    /// resolved bindings are reported through `boxlite inspect`, and remote
    /// access goes through `boxlite network tunnel`.
    #[test]
    fn withdrawn_port_subcommand_is_rejected() {
        let result = Cli::try_parse_from(["boxlite", "port", "mybox"]);
        assert!(
            result.is_err(),
            "the withdrawn port command must not come back"
        );
    }

    #[test]
    fn auth_login_parses_with_no_flags() {
        let cli = Cli::try_parse_from(["boxlite", "auth", "login"]).expect("parse");
        let Commands::Auth(args) = cli.command else {
            panic!("expected Commands::Auth");
        };
        assert!(matches!(args.command, AuthCommand::Login(_)));
    }

    #[test]
    fn auth_logout_parses() {
        let cli = Cli::try_parse_from(["boxlite", "auth", "logout"]).expect("parse");
        let Commands::Auth(args) = cli.command else {
            panic!("expected Commands::Auth");
        };
        assert!(matches!(args.command, AuthCommand::Logout(_)));
    }

    #[test]
    fn auth_status_parses() {
        let cli = Cli::try_parse_from(["boxlite", "auth", "status"]).expect("parse");
        let Commands::Auth(args) = cli.command else {
            panic!("expected Commands::Auth");
        };
        assert!(matches!(args.command, AuthCommand::Status));
    }

    // ─── volume subcommand parse tests ─────────────────────────────────────

    use crate::commands::volume::VolumeCommand;

    #[test]
    fn volume_create_takes_an_optional_name() {
        // Without --name the server names the volume after its id.
        let cli = Cli::try_parse_from(["boxlite", "volume", "create"]).expect("parse");
        let Commands::Volume(args) = cli.command else {
            panic!("expected Commands::Volume");
        };
        let VolumeCommand::Create(create) = args.command else {
            panic!("expected VolumeCommand::Create");
        };
        assert_eq!(create.name, None);

        let cli = Cli::try_parse_from(["boxlite", "volume", "create", "--name", "my-data"])
            .expect("parse");
        let Commands::Volume(args) = cli.command else {
            panic!("expected Commands::Volume");
        };
        let VolumeCommand::Create(create) = args.command else {
            panic!("expected VolumeCommand::Create");
        };
        assert_eq!(create.name.as_deref(), Some("my-data"));

        // The name is a flag, not a positional: a bare argument is still rejected.
        assert!(Cli::try_parse_from(["boxlite", "volume", "create", "data"]).is_err());
    }

    #[test]
    fn volume_ls_list_alias_parses() {
        // The `list` visible alias must resolve to the same Ls variant.
        for verb in ["ls", "list"] {
            let cli = Cli::try_parse_from(["boxlite", "volume", verb]).expect("parse");
            let Commands::Volume(args) = cli.command else {
                panic!("expected Commands::Volume for {verb}");
            };
            assert!(
                matches!(args.command, VolumeCommand::Ls(_)),
                "{verb} should map to Ls"
            );
        }
    }

    #[test]
    fn volume_get_inspect_alias_parses() {
        for verb in ["get", "inspect"] {
            let cli = Cli::try_parse_from(["boxlite", "volume", verb, "vol-123"]).expect("parse");
            let Commands::Volume(args) = cli.command else {
                panic!("expected Commands::Volume for {verb}");
            };
            let VolumeCommand::Get(get) = args.command else {
                panic!("{verb} should map to Get");
            };
            assert_eq!(get.id, "vol-123");
        }
    }

    #[test]
    fn volume_rm_takes_multiple_ids_and_force() {
        let cli = Cli::try_parse_from(["boxlite", "volume", "rm", "-f", "a", "b"]).expect("parse");
        let Commands::Volume(args) = cli.command else {
            panic!("expected Commands::Volume");
        };
        let VolumeCommand::Rm(rm) = args.command else {
            panic!("expected VolumeCommand::Rm");
        };
        assert!(rm.force);
        assert_eq!(rm.ids, vec!["a", "b"]);
    }

    #[test]
    fn volume_rm_requires_an_id() {
        // `num_args = 1..` + required means a bare `volume rm` must error.
        assert!(Cli::try_parse_from(["boxlite", "volume", "rm"]).is_err());
    }

    #[test]
    fn auth_login_api_key_stdin_parses() {
        // --api-key-stdin is the only non-interactive credential path
        // after the device-flow removal; it must parse cleanly.
        let cli = Cli::try_parse_from(["boxlite", "auth", "login", "--api-key-stdin"])
            .expect("--api-key-stdin should parse");
        let Commands::Auth(args) = cli.command else {
            panic!("expected Commands::Auth");
        };
        let AuthCommand::Login(login) = args.command else {
            panic!("expected AuthCommand::Login");
        };
        assert!(login.api_key_stdin);
    }

    // ============================================================
    // ManagementFlags --security
    //
    // Side A (setting valid) — `--security=disable` lands as
    // SecurityOptions::disabled() on the resulting BoxOptions.
    // Side B (setting invalid) — surfaces back as an
    // anyhow::Error pointing at the offending value. Reverting the
    // `from_preset(preset)?` call in apply_to flips both red.
    // ============================================================

    fn lifecycle_flags(
        auto_stop: Option<u32>,
        auto_delete: Option<u32>,
        no_auto_resume: bool,
    ) -> ManagementFlags {
        ManagementFlags {
            name: None,
            security: None,
            nested_virtualization: false,
            auto_stop,
            auto_delete,
            no_auto_resume,
        }
    }

    #[test]
    fn an_explicit_delete_deadline_survives_the_rm_sentinel() {
        // `apply_to` writes `auto_delete = Some(0)` unconditionally as the
        // "no --rm" default. Applying the flag before that line would have the
        // deadline silently overwritten by 0 — the box would never be swept.
        let mut opts = BoxOptions::default();
        lifecycle_flags(None, Some(3_600), false)
            .apply_to(&mut opts)
            .expect("flags must apply");

        assert_eq!(opts.auto_delete, Some(3_600));
    }

    #[test]
    fn lifecycle_flags_reach_box_options() {
        let mut opts = BoxOptions::default();
        lifecycle_flags(Some(900), None, true)
            .apply_to(&mut opts)
            .expect("flags must apply");

        assert_eq!(opts.auto_stop, Some(900));
        assert_eq!(opts.auto_resume, Some(false));
    }

    #[test]
    fn omitted_lifecycle_flags_leave_the_existing_defaults_alone() {
        // Absent flags must not invent policy: auto_stop and auto_resume stay
        // unset, and auto_delete keeps the historical `--rm` encoding.
        let mut opts = BoxOptions::default();
        lifecycle_flags(None, None, false)
            .apply_to(&mut opts)
            .expect("flags must apply");

        assert_eq!(opts.auto_stop, None);
        assert_eq!(opts.auto_resume, None);
        assert_eq!(opts.auto_delete, Some(0));
    }

    #[test]
    fn durations_accept_bare_seconds_and_suffixes() {
        assert_eq!(parse_duration_seconds("30"), Ok(30));
        assert_eq!(parse_duration_seconds("30s"), Ok(30));
        assert_eq!(parse_duration_seconds("15m"), Ok(900));
        assert_eq!(parse_duration_seconds("2h"), Ok(7_200));
        assert_eq!(parse_duration_seconds("7d"), Ok(604_800));
        assert_eq!(parse_duration_seconds("0"), Ok(0));

        for bad in ["", "m", "-5", "1.5h", "abc", "10x"] {
            assert!(
                parse_duration_seconds(bad).is_err(),
                "{bad:?} must not parse"
            );
        }
    }

    #[test]
    fn a_deadline_without_a_server_is_refused_not_silently_reinterpreted() {
        // Against the embedded runtime `--auto-stop` is rejected by the engine
        // and `--auto-delete` would quietly become remove-on-stop, deleting the
        // box at its stop instead of after the delay. Both must fail here, with
        // the flag named.
        for (auto_stop, auto_delete, flag) in [
            (Some(900), None, "--auto-stop"),
            (None, Some(3_600), "--auto-delete"),
        ] {
            let err = lifecycle_flags(auto_stop, auto_delete, false)
                .require_sweeper(false)
                .expect_err("a deadline with no sweeper must be refused");
            assert!(
                err.to_string().contains(flag),
                "error must name {flag}: {err}"
            );
        }
    }

    #[test]
    fn a_deadline_is_allowed_against_a_server_and_zero_is_always_allowed() {
        assert!(
            lifecycle_flags(Some(900), Some(3_600), false)
                .require_sweeper(true)
                .is_ok(),
            "a server runs the sweeper, so both deadlines are fine"
        );
        // `0` disables rather than requesting enforcement, so it needs nothing.
        assert!(
            lifecycle_flags(Some(0), Some(0), false)
                .require_sweeper(false)
                .is_ok(),
            "a disabled deadline asks nothing of the runtime"
        );
    }

    #[test]
    fn rm_and_auto_delete_cannot_be_combined() {
        // They mean different things — remove on stop versus remove after a
        // delay — and `apply_to` resolves them by letting --auto-delete win,
        // so the parser has to be what stops a caller asking for both.
        let conflict = Cli::try_parse_from([
            "boxlite",
            "run",
            "--rm",
            "--auto-delete",
            "1h",
            "alpine:latest",
        ]);

        assert!(
            conflict.is_err(),
            "--rm with --auto-delete must be rejected"
        );

        // `--rm` rides on the wire as `auto_delete = 1`, and the contract
        // requires `auto_delete > auto_stop`, so this pair could only ever fail
        // server-side complaining about a field the caller never set. The
        // parser has to be what says no.
        let with_auto_stop = Cli::try_parse_from([
            "boxlite",
            "run",
            "--rm",
            "--auto-stop",
            "60",
            "alpine:latest",
        ]);

        assert!(
            with_auto_stop.is_err(),
            "--rm with --auto-stop must be rejected"
        );
    }

    #[test]
    fn management_security_preset_applies_to_box_options() {
        let flags = ManagementFlags {
            name: None,
            security: Some("disable".to_string()),
            nested_virtualization: false,
            auto_stop: None,
            auto_delete: None,
            no_auto_resume: false,
        };
        let mut opts = BoxOptions::default();
        flags.apply_to(&mut opts).expect("setting must apply");
        assert_eq!(
            opts.advanced.security,
            boxlite::SecurityOptions::disabled(),
            "advanced.security must equal SecurityOptions::disabled()"
        );
    }

    #[test]
    fn management_security_preset_absent_leaves_default() {
        let flags = ManagementFlags {
            name: None,
            security: None,
            nested_virtualization: false,
            auto_stop: None,
            auto_delete: None,
            no_auto_resume: false,
        };
        let mut opts = BoxOptions::default();
        flags
            .apply_to(&mut opts)
            .expect("absent preset must succeed");
        assert_eq!(
            opts.advanced.security,
            boxlite::SecurityOptions::default(),
            "absent preset must leave the default in place"
        );
    }

    #[test]
    fn management_security_preset_typo_surfaces_anyhow_error() {
        let flags = ManagementFlags {
            name: None,
            security: Some("ultra".to_string()),
            nested_virtualization: false,
            auto_stop: None,
            auto_delete: None,
            no_auto_resume: false,
        };
        let mut opts = BoxOptions::default();
        let err = flags
            .apply_to(&mut opts)
            .expect_err("unknown preset must reject at apply_to");
        let msg = err.to_string();
        assert!(msg.contains("ultra"), "got {msg}");
    }

    #[test]
    fn nested_virtualization_flag_applies_to_box_options() {
        let cli =
            Cli::try_parse_from(["boxlite", "run", "--nested-virtualization", "alpine:latest"])
                .expect("nested virtualization flag should parse");
        let Commands::Run(args) = cli.command else {
            panic!("expected run command");
        };

        let mut opts = BoxOptions::default();
        assert!(!opts.advanced.nested_virtualization);
        args.management
            .apply_to(&mut opts)
            .expect("nested virtualization should apply");

        assert!(opts.advanced.nested_virtualization);
    }
}