perforce-cli 0.2.3

A type-safe builder library for spawning Perforce (p4) commands, with compile-time option state isolation and multi-version support.
Documentation
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
#[cfg(not(feature = "lt2020_1"))]
use std::ffi::OsStr;
use std::path::{Path, PathBuf};
use std::process::{Child, Command, Stdio};

use super::{ExclusiveOption, SubCommand, Unselected};
use crate::global::GlobalOpts;
use crate::spawn::ParameterizedSpawn;

// ---------------------------------------------------------------------------
// ExtensionEntry
// ---------------------------------------------------------------------------

/// Entry point of `p4 extension`, holding the binary and global options.
///
/// # Description
///
/// The following operations are available: [`sample`](Self::sample),
/// [`package`](Self::package), [`install`](Self::install),
/// [`delete`](Self::delete), [`configure`](Self::configure),
#[cfg_attr(not(feature = "lt2020_1"), doc = " [`run`](Self::run),")]
/// or [`list`](Self::list).
///
#[cfg_attr(
    feature = "lt2020_1",
    doc = "`p4 extension` manages the installation, versioning, and configuration of Helix Core Server Extensions. This command requires super access granted by p4 protect.",
    doc = "",
    doc = "For server extensions, see p4 help serverextensionintro and p4 help serverextensiondev. See also Helix Core Extensions Developer Guide."
)]
#[cfg_attr(
    all(feature = "lt2020_2", not(feature = "lt2020_1")),
    doc = "`p4 extension` manages the installation, versioning, and configuration of Helix Core Server Extensions.",
    doc = "",
    doc = "Extensions provide a way to customize the behavior of the Helix Core Server with user-supplied logic. Extensions are self-contained packages of third-party code and assets run within the Helix Core Server. Extensions can be used for many use cases, such as change submission validation, form validation, external authentication, external job fix integration, external archive integration, and command policies. For a list of events that Extensions can register for, see the list of trigger types in the output of p4 help triggers.",
    doc = "",
    doc = "Extensions coexist with triggers (see p4 triggers), but offer more functionality. The Helix Core Server runs Extensions natively, without relying on external processes, so Extensions provide a portable, versioned runtime with automatic replication and a programmatic API.",
    doc = "",
    doc = "Extensions are versioned within the Helix Core Server and are stored in a special depot, named .p4-extensions by default. The extension depot is only accessible to the super user and by read-only commands. The extension depot is created automatically.",
    doc = "",
    doc = "Installing or upgrading an Extension creates a changelist with information about pre- and post-install versions.",
    doc = "",
    doc = "For more information about Extensions, see p4 help serverextensionintro and Helix Core Extensions Developer Guide."
)]
#[cfg_attr(
    all(feature = "lt2025_1", not(feature = "lt2020_2")),
    doc = "`p4 extension` manages the installation, versioning, and configuration of Helix Core Server Extensions.",
    doc = "",
    doc = "Extensions are a means for customizing parts of the Helix Core with user-supplied logic. Extensions are self-contained packages of third-party code and assets run within the Helix Core Server, used for change submission validation, form validation, external authentication, external job fix integration, external archive integration, and command policies. For a list of events that Extensions can register for, see the list of trigger types in the output of p4 help triggers.",
    doc = "",
    doc = "The Helix Core Server runs Extensions natively, without relying on external processes, so it can provide a portable, versioned runtime with automatic replication and a programmatic API.",
    doc = "",
    doc = "Extensions coexist with triggers (see p4 triggers), but Extensions offer more functionality.",
    doc = "",
    doc = "Extensions are versioned within the Helix Core Server and are stored in a special depot, named .p4-extensions by default. The extension depot is only accessible to the super user and by read-only commands. The extension depot is created automatically.",
    doc = "",
    doc = "Installing or upgrading an Extension creates a changelist with information about pre- and post-install versions.",
    doc = "",
    doc = "The p4 extension command requires super access granted by p4 protect. Users who are members of groups specified in the ExtAllowedGroups field of an Extension's global configuration and who are either a depot or repo owner can create or configure instances of file-based Extensions to operate on the sections of the server they manage.",
    doc = ""
)]
#[cfg_attr(
    all(feature = "lt2024_2", not(feature = "lt2020_2")),
    doc = "For more information about Extensions, see p4 help serverextensionintro and Helix Core Extensions Developer Guide."
)]
#[cfg_attr(
    all(feature = "lt2025_1", not(feature = "lt2024_2")),
    doc = "For more information about Extensions, see p4 help serverextensionintro and P4 Extensions Documentation."
)]
#[cfg_attr(
    not(feature = "lt2025_1"),
    doc = "Create and configure P4 Extensions.",
    doc = "",
    doc = "Extensions are a means for customizing parts of the P4 with user-supplied logic. Extensions are self-contained packages of third-party code and assets that run within the P4 Server. Typical use cases are change submission validation, form validation, external authentication, external job fix integration, external archive integration, and command policies.",
    doc = "",
    doc = "An Extension can implement custom commands that a user can run. For example, `p4 extension --run myInstanceConfig validateFileSize pathToFile` where validateFileSize represents a custom command and pathToFile represents an argument that is relevant to the custom command.",
    doc = "",
    doc = "Extensions coexist with p4 triggers, but offer more functionality. To learn more, see Advantages over triggers in the Extension overview at P4 Server Administration Documentation.",
    doc = "",
    doc = "The P4 Server runs extensions natively, without relying on external processes. Extensions run in a portable, versioned runtime with automatic replication and a programmatic API, which is documented at P4 Server Administration Documentation.",
    doc = "",
    doc = "The p4 extension command requires super access granted by p4 protect. Users who are members of groups specified in the ExtAllowedGroups field of an extension's global configuration and who are either a depot or repo owner can create or configure instances of file-based extensions to operate on the sections of the server they manage.",
    doc = "",
    doc = "Extensions are versioned within the P4 Server and are stored in the built-in .p4-extensions depot. The extension depot is only accessible to the super user and by read-only commands.",
    doc = "",
    doc = "Installing or upgrading an extension creates a changelist with information about pre/post install versions. Multiple revisions of an extension can be active at any given time and can be referenced by their revision number."
)]
pub struct ExtensionEntry {
    bin: PathBuf,

    global_opts: GlobalOpts,
}

impl ExtensionEntry {
    /// Creates an `ExtensionEntry` with the given p4 binary path and global
    /// options.
    pub(crate) fn new(bin: PathBuf, global_opts: GlobalOpts) -> Self {
        Self { bin, global_opts }
    }

    /// # Description
    ///
    /// --sample name
    ///
    #[cfg_attr(
        feature = "lt2020_1",
        doc = "Create a skeleton of a new Extension in a directory.  Pass it the name of the Extension to be created."
    )]
    #[cfg_attr(
        all(feature = "lt2020_2", not(feature = "lt2020_1")),
        doc = "Creates a skeleton of a new Extension.  The argument is the name of the Extension to create as well as the name of the local directory where the files are placed.  The sample Extension has placeholder values that should be replaced when developing the code. (See Naming rules.)"
    )]
    #[cfg_attr(
        all(feature = "lt2022_2", not(feature = "lt2020_2")),
        doc = "Create a skeleton of a new Extension in a directory.  Pass it the name of the Extension to be created. (See Naming rules.) `p4 extension --sample myExt` This allows you to then use the `--package` option."
    )]
    #[cfg_attr(
        not(feature = "lt2022_2"),
        doc = "Create a skeleton of a new Extension in a directory.  Pass it the name of the Extension to be created. (See Naming rules.) `p4 extension --sample ExtName` This allows you to then use the `--package` option."
    )]
    pub fn sample(self, name: impl Into<String>) -> Extension<Sample> {
        Extension {
            bin: self.bin,
            global_opts: self.global_opts,
            op: Sample { name: name.into() },
        }
    }

    /// # Description
    ///
    /// --package dir
    ///
    #[cfg_attr(
        feature = "lt2020_1",
        doc = "The `--package` flag creates a packaged Extension file from the named directory of files.  The resulting file has the same name as the directory, but with the .p4-extension suffix."
    )]
    #[cfg_attr(
        all(feature = "lt2020_2", not(feature = "lt2020_1")),
        doc = "Creates a packaged Extension file from the named directory of files, preparing it for `--install` on the server.  The resulting file has the same name as the directory, but with the .p4-extension suffix. Packaging is done client-side and requires a version 2020.1 or later command-line client."
    )]
    #[cfg_attr(
        all(feature = "lt2022_2", not(feature = "lt2020_2")),
        doc = "The `--package` option creates a packaged Extension file from the named directory of files.  For example, `p4 extension --package myExt` The resulting file has the same name as the directory, but with the .p4-extension suffix. An Extension must be packaged before it can be installed by using the `--install` option."
    )]
    #[cfg_attr(
        not(feature = "lt2022_2"),
        doc = "The `--package` option creates a packaged Extension file from the named directory of files.  For example, `p4 extension --package ExtName` The resulting file has the same name as the directory, but with the .p4-extension suffix. An Extension must be packaged before it can be installed by using the `--install` option."
    )]
    pub fn package(self, dir: impl Into<String>) -> Extension<Package> {
        Extension {
            bin: self.bin,
            global_opts: self.global_opts,
            op: Package {
                dir: dir.into(),
                sign: None,
            },
        }
    }

    /// # Description
    ///
    /// --install file
    ///
    #[cfg_attr(
        feature = "lt2020_1",
        doc = "The full name of an Extension file on the client. The command installs it server-side. Packaged Extensions have the p4-extension file suffix."
    )]
    #[cfg_attr(
        all(feature = "lt2020_2", not(feature = "lt2020_1")),
        doc = "The `--install` flag is passed the full name of a packaged Extension file residing on the client and installs it server-side, where it can then be instantiated with `--configure`."
    )]
    #[cfg_attr(
        all(feature = "lt2022_2", not(feature = "lt2020_2")),
        doc = "The full name of an Extension file on the client. The command installs it server-side. Packaged Extensions have the p4-extension file suffix. For example, `p4 extension --install myExt.p4-extension --yes`",
        doc = "",
        doc = "# Note",
        doc = "",
        doc = "To actually perform the install or delete, the `--yes` option is required. Without `--yes`, the p4 extension command merely reports what it would do without actually performing the install or delete."
    )]
    #[cfg_attr(
        not(feature = "lt2022_2"),
        doc = "The full name of an Extension file on the client. The command installs it server-side. Packaged Extensions have the p4-extension file suffix. For example, `p4 extension --install ExtName.p4-extension --yes`",
        doc = "",
        doc = "# Note",
        doc = "",
        doc = "To actually perform the install or delete, the `--yes` option is required. Without `--yes`, the p4 extension command merely reports what it would do without actually performing the install or delete."
    )]
    pub fn install(self, file: impl Into<String>) -> Extension<Install<Unselected>> {
        Extension {
            bin: self.bin,
            global_opts: self.global_opts,
            op: Install {
                file: file.into(),
                mode: Unselected,
            },
        }
    }

    /// # Description
    ///
    /// --delete extFQN
    ///
    #[cfg_attr(
        feature = "lt2020_1",
        doc = "Deletes an extension, its configurations, or both. To remove one specific revision and its instances, include the `--revision` option. To remove only the named instance config, include the `--name` option. To delete file-based instances by the part of the depot they affect, include the `--path` option. To remove all revisions of an extension and its configurations, omit the `--name` option."
    )]
    #[cfg_attr(
        all(feature = "lt2020_2", not(feature = "lt2020_1")),
        doc = "Deletes an extension, its configurations, or both. To remove the named instance config, include the `--name` option. For example: `p4 extension --delete myCompany::fileSizeCheck --name=fileSizeCheck-instance --yes` To remove all revisions of an extension and its configurations, omit the `--name` option. For example: `p4 extension --delete myCompany::fileSizeCheck` To remove one specific revision and its instances, include the `--revision` option. For example: `p4 extension --delete myCompany::fileSizeCheck --name=fileSizeCheck-instance --revision=2 --yes` To delete file-based instances registered under the specified path, use the `--path` option. For example: `p4 extension --delete ExampleInc::extD1 --path=//depot/... --yes`"
    )]
    #[cfg_attr(
        all(feature = "lt2022_2", not(feature = "lt2020_2")),
        doc = "Deletes an extension, its configurations, or both. To remove the named instance config, include the `--name` option. For example: `p4 extension --delete myCompany::fileSizeCheck --name=fileSizeCheck-instance --yes` To remove all revisions of an extension and its configurations, omit the `--name` option. For example: `p4 extension --delete myCompany::fileSizeCheck` To remove one specific revision and its instances, include the `--revision` option. For example: `p4 extension --delete myCompany::fileSizeCheck --name=fileSizeCheck-instance --revision=2 --yes` To delete file-based instances registered under the specified path, use the `--path` option. For example: `p4 extension --delete ExampleInc::extD1 --path=//depot/... --yes`",
        doc = "",
        doc = "# Note",
        doc = "",
        doc = "To actually perform the install or delete, the `--yes` option is required. Without `--yes`, the p4 extension command merely reports what it would do without actually performing the install or delete."
    )]
    #[cfg_attr(
        all(feature = "lt2025_1", not(feature = "lt2022_2")),
        doc = "Deletes an extension, its configurations, or both. To remove the named instance config, include the `--name` option: `p4 extension --delete myCompany::fileSizeCheck --name fileSizeCheck-instance --yes` To remove all revisions of an extension and its configurations, omit the `--name` option: `p4 extension --delete myCompany::fileSizeCheck` To remove one specific revision and its instances, include the `--revision` option: `p4 extension --delete myCompany::fileSizeCheck --revision=2 --yes` To delete file-based instances registered under the specified path: `p4 extension --delete ExampleInc::extD1 --path //depot/... --yes`",
        doc = "",
        doc = "# Note",
        doc = "",
        doc = "To actually perform the install or delete, the `--yes` option is required. Without `--yes`, the p4 extension command merely reports what it would do without actually performing the install or delete.",
        doc = "",
        doc = "extFQN means fully-qualified name: the combination of the Extension's namespace, name, and optional depot file revision."
    )]
    #[cfg_attr(
        not(feature = "lt2025_1"),
        doc = "Deletes an extension, its configurations, or both. To remove the named instance config, include the `--name` option: `p4 extension --delete myCompany::fileSizeCheck --name fileSizeCheck-instance --yes` To remove all revisions of an extension and its configurations, omit the `--name` option: `p4 extension --delete myCompany::fileSizeCheck` To remove one specific revision and its instances, include the `--revision` option: `p4 extension --delete myCompany::fileSizeCheck --revision=2 --yes` To delete file-based instances registered under the specified path: `p4 extension --delete ExampleInc::extD1 --path //depot/... --yes`",
        doc = "",
        doc = "# Note",
        doc = "",
        doc = "To perform the install or delete, the `--yes` option is required. Without `--yes`, the p4 extension command merely reports what it would do without actually performing the install or delete.",
        doc = "",
        doc = "extFQN means fully-qualified name: the combination of the Extension's namespace, name, and optional depot file revision."
    )]
    pub fn delete(self, fqn: impl Into<String>) -> Extension<Delete<Unselected>> {
        Extension {
            bin: self.bin,
            global_opts: self.global_opts,
            op: Delete {
                fqn: fqn.into(),
                mode: Unselected,
            },
        }
    }

    /// # Description
    ///
    /// --configure extFQN
    ///
    #[cfg_attr(
        feature = "lt2020_1",
        doc = "Supplies details about an Extension's configuration. To create a global configuration, use `--configure` with the fully-qualified name of the Extension; to create an instance configuration, combine it with the `--name` option."
    )]
    #[cfg_attr(
        all(feature = "lt2020_2", not(feature = "lt2020_1")),
        doc = "The `--configure` flag is used to supply details about an Extension's configuration.  There are two parts to configuring an Extension: the global configuration the instance configuration. To create a global configuration, use `--configure` with the FQN of the Extension. To create an instance configuration, use the `--configure` option with the `--name` option. The `--name` option takes the name of the configuration to create or modify.  More than one named instance is allowed for most event types that an Extension can be coded to work with, such as the change-submit event. However, the auth-check event allows only one named instance."
    )]
    #[cfg_attr(
        all(feature = "lt2022_2", not(feature = "lt2020_2")),
        doc = "An Extension is not active until it has been configured. The default global configuration is: `p4 extension --configure ExampleInc::myExt` and your global configuration might be: `p4 extension --configure myCompany::fileSizeCheck` and the corresponding instance configuration might be: `p4 extension --configure myCompany::extName --name Release1.0-fileSizeCheckInst` An Extension can have multiple configurations, depending on the events it registers for.  Each configuration of an Extension can point to a different version of the Extension."
    )]
    #[cfg_attr(
        all(feature = "lt2025_2", not(feature = "lt2022_2")),
        doc = "An Extension is not active until it has been configured. In this syntax: `p4 extension --configure extFQN [ --revision=n ] [ -o | -i ]` extFQN means fully-qualified name: the combination of the Extension's namespace, name, and optional depot file revision. The default global configuration is: `p4 extension --configure ExampleInc::myExt` and your global configuration might be: `p4 extension --configure myCompany::fileSizeCheck` and the corresponding instance configuration might be: `p4 extension --configure myCompany::extName --name Release1.0-fileSizeCheckInst` An Extension can have multiple configurations, depending on the events it registers for.  Each configuration of an Extension can point to a different version of the Extension."
    )]
    #[cfg_attr(
        not(feature = "lt2025_2"),
        doc = "An Extension is not active until it has been configured. In this syntax: `p4 extension --configure extFQN [ --revision=n ] [ -o | -i ]` extFQN means fully-qualified name: the combination of the Extension's namespace, name, and optional depot file revision. The default global configuration is: `p4 extension --configure ExampleInc::myExt` and your global configuration might be: `p4 extension --configure myCompany::fileSizeCheck` and the corresponding instance configuration might be: `p4 extension --configure myCompany::extName --name Release1.0-fileSizeCheckInst` An Extension can have multiple configurations, depending on the events it registers for.  Each configuration of an Extension can point to a different version of the Extension.",
        doc = "",
        doc = "Since 2025.2, you can prevent users from creating multiple configurations of the same extension. To learn more,, see the limit_instance field under Server Extension JSON manifest fields in the P4 Server Administration Documentation."
    )]
    pub fn configure(self, fqn: impl Into<String>) -> Extension<Configure<Unselected>> {
        Extension {
            bin: self.bin,
            global_opts: self.global_opts,
            op: Configure {
                fqn: fqn.into(),
                revision: None,
                name: None,
                mode: Unselected,
            },
        }
    }

    /// # Description
    ///
    /// --run instName
    ///
    #[cfg_attr(
        all(feature = "lt2020_2", not(feature = "lt2020_1")),
        doc = "Executes the Extension-supplied command. An Extension can implement its own end-user commands that behave similarly to native Helix Core commands in that they can have tagged output, send RPC-level messages, and open forms client-side. However, Extension-supplied commands are restricted to reporting output and cannot access client-side content. The `--run` option takes an instance configuration as its argument, and any other arguments are left to the Extension to interpret."
    )]
    #[cfg_attr(
        not(feature = "lt2020_2"),
        doc = "Executes the specified Extension. An Extension can implement custom end-user commands, executed by passing the `--run` flag the name of an instance config and supplying any optional arguments the Extension might need."
    )]
    #[cfg(not(feature = "lt2020_1"))]
    pub fn run(self, inst: impl Into<String>) -> Extension<Run> {
        Extension {
            bin: self.bin,
            global_opts: self.global_opts,
            op: Run { inst: inst.into() },
        }
    }

    /// # Description
    ///
    /// --list
    ///
    #[cfg_attr(
        feature = "lt2020_1",
        doc = "Displays the installed extensions and their instances.  If this option is specified with the `--type=extensions`, `--list` option, the output shows information about which extensions are installed, and `--type=configs` shows information about the different configurations."
    )]
    #[cfg_attr(
        all(feature = "lt2020_2", not(feature = "lt2020_1")),
        doc = "Displays information about installed Extensions. The value of the `--type` flag determines which data is reported.  Values for `--type` are: extensions configs, further narrowed by global and instance Adding the `--path` option filters instance configuration output to instances whose events are path-based and match the specified filespec."
    )]
    #[cfg_attr(
        all(feature = "lt2022_2", not(feature = "lt2020_2")),
        doc = "Displays the installed extensions and their instances. To list all Extensions: `p4 extension --list --type=extensions` To list all instance configurations: `p4 extension --list --type=configs` You can replace `--type=configs` with `--type=global` or `--type=instance` to filter results accordingly. Adding `--path=<path>` filters output to instance configurations that are path-based and their path argument matches the given `<path>`. The output of `p4 extension --list --type=extensions` indicates the status of extension configuration: No Global Config, No Instance Config, Incomplete Global Config, Incomplete Instance Config, or OK.",
        doc = "",
        doc = "# Note",
        doc = "",
        doc = "For an extension to be active, an extension must have a global configuration and at least one instance configuration."
    )]
    #[cfg_attr(
        not(feature = "lt2022_2"),
        doc = "Displays the installed extensions and their instances. To list all Extensions: `p4 extension --list --type extensions` To list all instance configurations: `p4 extension --list --type configs` You can replace `--type configs` with `--type global` or `--type instance` to filter results accordingly. Adding `--path path` filters output to instance configurations that are path-based and their path argument matches the given path. The output of `p4 extension --list --type extensions` indicates the status of extension configuration: No Global Config, No Instance Config, Incomplete Global Config, Incomplete Instance Config, or OK.",
        doc = "",
        doc = "# Note",
        doc = "",
        doc = "For an extension to be active, an extension must have a global configuration and at least one instance configuration."
    )]
    pub fn list(self) -> Extension<List> {
        Extension {
            bin: self.bin,
            global_opts: self.global_opts,
            op: List {
                ty: None,

                #[cfg(not(feature = "lt2022_2"))]
                path: None,
            },
        }
    }
}

// ---------------------------------------------------------------------------
// Extension<T> — command carrier
// ---------------------------------------------------------------------------

/// A `p4 extension` command with a selected operation.
///
/// # Syntax
///
/// ```text
/// p4 [g-opts] extension --sample extName
/// p4 [g-opts] extension --package dir [--sign keyDir]
#[cfg_attr(
    feature = "lt2020_1",
    doc = "p4 [g-opts] extension --install extName.p4-extension"
)]
#[cfg_attr(
    not(feature = "lt2020_1"),
    doc = "p4 [g-opts] extension --install extName.p4-extension [--yes]"
)]
/// p4 [g-opts] extension --install certificateFile --cert [--comment]
/// p4 [g-opts] extension --delete extFQN [--name instName] [--revision=n] [--path FileSpec] [--yes]
/// p4 [g-opts] extension --delete fingerprint --cert
/// p4 [g-opts] extension --configure extFQN [--revision=n] [-o | -i]
/// p4 [g-opts] extension --configure extFQN [--revision=n] [-o | -i] --name instName
/// p4 [g-opts] extension --run instName [extArguments]
/// p4 [g-opts] extension --list --type type [--path FileSpec]
/// ```
///
/// Manages the installation, versioning, and configuration of extensions.
/// The type parameter selects the operation, such as [`Sample`], [`Package`],
/// [`Install`], [`Delete`], [`Configure`],
#[cfg_attr(feature = "lt2020_1", doc = "or [`List`].")]
#[cfg_attr(not(feature = "lt2020_1"), doc = "[`Run`], or [`List`].")]
pub struct Extension<T: ExclusiveOption> {
    bin: PathBuf,

    global_opts: GlobalOpts,

    op: T,
}

impl<T: ExclusiveOption> Extension<T> {
    /// Returns the path of the p4 binary.
    pub fn get_bin(&self) -> &Path {
        &self.bin
    }

    /// Sets the path of the p4 binary.
    pub fn set_bin(&mut self, v: impl Into<PathBuf>) -> &mut Self {
        self.bin = v.into();
        self
    }

    /// Sets the path of the p4 binary.
    pub fn bin(mut self, v: impl Into<PathBuf>) -> Self {
        self.bin = v.into();
        self
    }

    /// # Description
    ///
    /// g-opts
    ///
    #[cfg_attr(
        all(feature = "lt2018_2", not(feature = "lt2017_1")),
        doc = "See [Global Options](GlobalOpts)."
    )]
    #[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
    pub fn get_global_opts(&self) -> &GlobalOpts {
        &self.global_opts
    }

    /// # Description
    ///
    /// g-opts
    ///
    #[cfg_attr(
        all(feature = "lt2018_2", not(feature = "lt2017_1")),
        doc = "See [Global Options](GlobalOpts)."
    )]
    #[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
    pub fn set_global_opts(&mut self, v: GlobalOpts) -> &mut Self {
        self.global_opts = v;
        self
    }

    /// # Description
    ///
    /// g-opts
    ///
    #[cfg_attr(
        all(feature = "lt2018_2", not(feature = "lt2017_1")),
        doc = "See [Global Options](GlobalOpts)."
    )]
    #[cfg_attr(not(feature = "lt2018_2"), doc = "See [Global options](GlobalOpts).")]
    pub fn global_opts(mut self, v: GlobalOpts) -> Self {
        self.global_opts = v;
        self
    }
}

impl<T: ExclusiveOption> SubCommand for Extension<T> {
    fn name(&self) -> &str {
        "extension"
    }

    fn inject_local_args(&self, command: &mut Command) {
        self.op.inject_args(command);
    }

    fn global_opts(&self) -> Option<&GlobalOpts> {
        Some(&self.global_opts)
    }
}

impl<T: ExclusiveOption> ParameterizedSpawn<()> for Extension<T> {
    type Output = Child;
    type Error = std::io::Error;

    /// Spawns the selected `p4 extension` operation as a child process with
    /// piped standard output and error streams; use the returned [`Child`]
    /// handle to wait for it or interact with it.
    fn spawn_with(&mut self, (): ()) -> Result<Self::Output, Self::Error> {
        self.setup_command(&self.bin)
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()
    }
}

// ---------------------------------------------------------------------------
// sample
// ---------------------------------------------------------------------------

/// The `--sample` operation of `p4 extension`.
///
/// Creates a skeleton of a new extension in a directory named after it.
/// Selected with [`ExtensionEntry::sample`].
#[derive(Debug, Clone, Default)]
pub struct Sample {
    name: String,
}

impl ExclusiveOption for Sample {
    fn inject_args(&self, command: &mut Command) {
        command.arg("--sample").arg(&self.name);
    }
}

impl Extension<Sample> {
    /// Returns the name of the extension skeleton to create.
    pub fn get_name(&self) -> &str {
        &self.op.name
    }

    /// Sets the name of the extension skeleton to create.
    pub fn set_name(&mut self, v: impl Into<String>) -> &mut Self {
        self.op.name = v.into();
        self
    }

    /// Sets the name of the extension skeleton to create.
    pub fn name(mut self, v: impl Into<String>) -> Self {
        self.op.name = v.into();
        self
    }
}

// ---------------------------------------------------------------------------
// package
// ---------------------------------------------------------------------------

/// The `--package` operation of `p4 extension`.
///
/// Packages a directory of extension files into a `.p4-extension` file.
/// Selected with [`ExtensionEntry::package`].
#[derive(Debug, Clone, Default)]
pub struct Package {
    dir: String,

    sign: Option<String>,
}

impl ExclusiveOption for Package {
    fn inject_args(&self, command: &mut Command) {
        command.arg("--package").arg(&self.dir);

        if let Some(key_dir) = &self.sign {
            command.arg("--sign").arg(key_dir);
        }
    }
}

impl Extension<Package> {
    /// Returns the directory of extension files to package.
    pub fn get_dir(&self) -> &str {
        &self.op.dir
    }

    /// Sets the directory of extension files to package.
    pub fn set_dir(&mut self, v: impl Into<String>) -> &mut Self {
        self.op.dir = v.into();
        self
    }

    /// Sets the directory of extension files to package.
    pub fn dir(mut self, v: impl Into<String>) -> Self {
        self.op.dir = v.into();
        self
    }

    /// # Description
    ///
    /// --sign keyDir
    ///
    /// Can be supplied in `--package`.
    #[cfg(not(feature = "lt2022_2"))]
    pub fn get_sign(&self) -> Option<&String> {
        self.op.sign.as_ref()
    }

    /// # Description
    ///
    /// --sign keyDir
    ///
    /// Can be supplied in `--package`.
    #[cfg(not(feature = "lt2022_2"))]
    pub fn set_sign(&mut self, v: impl Into<String>) -> &mut Self {
        self.op.sign = Some(v.into());
        self
    }

    /// # Description
    ///
    /// --sign keyDir
    ///
    /// Can be supplied in `--package`.
    #[cfg(not(feature = "lt2022_2"))]
    pub fn sign(mut self, v: impl Into<String>) -> Self {
        self.op.sign = Some(v.into());
        self
    }
}

// ---------------------------------------------------------------------------
// install
// ---------------------------------------------------------------------------

/// The extension-file install state of `--install`.
///
/// Adds [`--yes`](Extension::yes) to perform the install for real; without
/// it the command only reports what it would do.
#[cfg(not(feature = "lt2020_1"))]
#[derive(Debug, Clone, Copy, Default)]
pub struct InstallExt {
    yes: bool,

    #[cfg(not(feature = "lt2022_2"))]
    allow_unsigned: bool,
}

#[cfg(not(feature = "lt2020_1"))]
impl ExclusiveOption for InstallExt {
    fn inject_args(&self, command: &mut Command) {
        if self.yes {
            command.arg("--yes");
        }

        #[cfg(not(feature = "lt2022_2"))]
        if self.allow_unsigned {
            command.arg("--allow-unsigned");
        }
    }
}

/// The certificate install state of `--install`.
///
/// Installs a certificate file with `--cert`, optionally recording a
/// [`--comment`](Extension::comment).
#[derive(Debug, Clone, Default)]
#[cfg(not(feature = "lt2022_2"))]
pub struct InstallCert {
    comment: Option<String>,
}

#[cfg(not(feature = "lt2022_2"))]
impl ExclusiveOption for InstallCert {
    fn inject_args(&self, command: &mut Command) {
        command.arg("--cert");

        if let Some(comment) = &self.comment {
            command.arg("--comment").arg(comment);
        }
    }
}

/// The `--install` operation of `p4 extension`.
///
#[cfg_attr(
    feature = "lt2020_1",
    doc = "The `M` type parameter only ever takes [`Unselected`]; `--yes` cannot be supplied to `--install`."
)]
#[cfg_attr(
    all(feature = "lt2022_2", not(feature = "lt2020_1")),
    doc = "The type parameter selects what is installed: [`Unselected`] for a plain install, or [`InstallExt`] for an extension package."
)]
#[cfg_attr(
    not(feature = "lt2022_2"),
    doc = "The type parameter selects what is installed: [`Unselected`] for a plain install, [`InstallExt`] for an extension package, or [`InstallCert`] for a certificate."
)]
/// Selected with [`ExtensionEntry::install`].
#[derive(Debug, Clone, Default)]
pub struct Install<M = Unselected> {
    file: String,

    mode: M,
}

impl<M: ExclusiveOption> ExclusiveOption for Install<M> {
    fn inject_args(&self, command: &mut Command) {
        command.arg("--install").arg(&self.file);
        self.mode.inject_args(command);
    }
}

impl Extension<Install<Unselected>> {
    /// Returns the packaged extension or certificate file to install.
    pub fn get_file(&self) -> &str {
        &self.op.file
    }

    /// Sets the packaged extension or certificate file to install.
    pub fn set_file(&mut self, v: impl Into<String>) -> &mut Self {
        self.op.file = v.into();
        self
    }

    /// Sets the packaged extension or certificate file to install.
    pub fn file(mut self, v: impl Into<String>) -> Self {
        self.op.file = v.into();
        self
    }

    /// # Description
    ///
    /// --yes
    ///
    /// To actually perform the install or delete, the `--yes` option is
    /// required. Without `--yes`, the p4 extension command merely reports
    /// what it would do without actually performing the install or delete.
    /// `-y` is equivalent to `--yes`.
    #[cfg(not(feature = "lt2020_1"))]
    pub fn yes(self) -> Extension<Install<InstallExt>> {
        Extension {
            bin: self.bin,
            global_opts: self.global_opts,
            op: Install {
                file: self.op.file,
                mode: InstallExt {
                    yes: true,

                    #[cfg(not(feature = "lt2022_2"))]
                    allow_unsigned: false,
                },
            },
        }
    }

    /// # Description
    ///
    /// --cert
    ///
    /// Can be supplied in `--install` or `--delete`. For example,
    /// `p4 extension --install path/certificate.txt --cert --comment "We trust ACTools"`.
    #[cfg(not(feature = "lt2022_2"))]
    pub fn cert(self) -> Extension<Install<InstallCert>> {
        Extension {
            bin: self.bin,
            global_opts: self.global_opts,
            op: Install {
                file: self.op.file,
                mode: InstallCert { comment: None },
            },
        }
    }

    /// # Description
    ///
    /// --comment
    ///
    /// Can be supplied in `--install` or `--delete`. For example,
    /// `p4 extension --install path/certificate.txt --cert --comment "We trust ACTools"`.
    #[cfg(not(feature = "lt2022_2"))]
    pub fn comment(self, v: impl Into<String>) -> Extension<Install<InstallCert>> {
        Extension {
            bin: self.bin,
            global_opts: self.global_opts,
            op: Install {
                file: self.op.file,
                mode: InstallCert {
                    comment: Some(v.into()),
                },
            },
        }
    }
}

#[cfg(not(feature = "lt2020_1"))]
impl Extension<Install<InstallExt>> {
    /// # Description
    ///
    /// --yes
    ///
    /// To actually perform the install or delete, the `--yes` option is
    /// required. Without `--yes`, the p4 extension command merely reports
    /// what it would do without actually performing the install or delete.
    /// `-y` is equivalent to `--yes`.
    pub fn get_yes(&self) -> bool {
        self.op.mode.yes
    }

    /// # Description
    ///
    /// --yes
    ///
    /// To actually perform the install or delete, the `--yes` option is
    /// required. Without `--yes`, the p4 extension command merely reports
    /// what it would do without actually performing the install or delete.
    /// `-y` is equivalent to `--yes`.
    pub fn set_yes(&mut self, v: bool) -> &mut Self {
        self.op.mode.yes = v;
        self
    }

    /// # Description
    ///
    /// --yes
    ///
    /// To actually perform the install or delete, the `--yes` option is
    /// required. Without `--yes`, the p4 extension command merely reports
    /// what it would do without actually performing the install or delete.
    /// `-y` is equivalent to `--yes`.
    pub fn yes(mut self, v: bool) -> Self {
        self.op.mode.yes = v;
        self
    }

    /// # Description
    ///
    /// --allow-unsigned
    ///
    #[cfg_attr(
        all(
            feature = "lt2025_1",
            not(feature = "lt2022_2"),
            feature = "lt2024_2",
            not(feature = "lt2024_1")
        ),
        doc = "(Optional) If included in the commands to install (or delete) an extension, it is the same as if the Configurables reference configurable were set to 1 so that the extension does not need to be signed."
    )]
    #[cfg_attr(
        not(all(
            feature = "lt2025_1",
            not(feature = "lt2022_2"),
            feature = "lt2024_2",
            not(feature = "lt2024_1")
        )),
        doc = "(Optional) If included in the commands to install (or delete) an extension, it is the same as if the server.extensions.allow.unsigned configurable were set to 1 so that the extension does not need to be signed."
    )]
    #[cfg(not(feature = "lt2022_2"))]
    pub fn get_allow_unsigned(&self) -> bool {
        self.op.mode.allow_unsigned
    }

    /// # Description
    ///
    /// --allow-unsigned
    ///
    #[cfg_attr(
        all(
            feature = "lt2025_1",
            not(feature = "lt2022_2"),
            feature = "lt2024_2",
            not(feature = "lt2024_1")
        ),
        doc = "(Optional) If included in the commands to install (or delete) an extension, it is the same as if the Configurables reference configurable were set to 1 so that the extension does not need to be signed."
    )]
    #[cfg_attr(
        not(all(
            feature = "lt2025_1",
            not(feature = "lt2022_2"),
            feature = "lt2024_2",
            not(feature = "lt2024_1")
        )),
        doc = "(Optional) If included in the commands to install (or delete) an extension, it is the same as if the server.extensions.allow.unsigned configurable were set to 1 so that the extension does not need to be signed."
    )]
    #[cfg(not(feature = "lt2022_2"))]
    pub fn set_allow_unsigned(&mut self, v: bool) -> &mut Self {
        self.op.mode.allow_unsigned = v;
        self
    }

    /// # Description
    ///
    /// --allow-unsigned
    ///
    #[cfg_attr(
        all(
            feature = "lt2025_1",
            not(feature = "lt2022_2"),
            feature = "lt2024_2",
            not(feature = "lt2024_1")
        ),
        doc = "(Optional) If included in the commands to install (or delete) an extension, it is the same as if the Configurables reference configurable were set to 1 so that the extension does not need to be signed."
    )]
    #[cfg_attr(
        not(all(
            feature = "lt2025_1",
            not(feature = "lt2022_2"),
            feature = "lt2024_2",
            not(feature = "lt2024_1")
        )),
        doc = "(Optional) If included in the commands to install (or delete) an extension, it is the same as if the server.extensions.allow.unsigned configurable were set to 1 so that the extension does not need to be signed."
    )]
    #[cfg(not(feature = "lt2022_2"))]
    pub fn allow_unsigned(mut self, v: bool) -> Self {
        self.op.mode.allow_unsigned = v;
        self
    }
}

#[cfg(not(feature = "lt2022_2"))]
impl Extension<Install<InstallCert>> {
    /// # Description
    ///
    /// --comment
    ///
    /// Can be supplied in `--install` or `--delete`. For example,
    /// `p4 extension --install path/certificate.txt --cert --comment "We trust ACTools"`.
    pub fn get_comment(&self) -> Option<&String> {
        self.op.mode.comment.as_ref()
    }

    /// # Description
    ///
    /// --comment
    ///
    /// Can be supplied in `--install` or `--delete`. For example,
    /// `p4 extension --install path/certificate.txt --cert --comment "We trust ACTools"`.
    pub fn set_comment(&mut self, v: impl Into<String>) -> &mut Self {
        self.op.mode.comment = Some(v.into());
        self
    }

    /// # Description
    ///
    /// --comment
    ///
    /// Can be supplied in `--install` or `--delete`. For example,
    /// `p4 extension --install path/certificate.txt --cert --comment "We trust ACTools"`.
    pub fn comment(mut self, v: impl Into<String>) -> Self {
        self.op.mode.comment = Some(v.into());
        self
    }
}

// ---------------------------------------------------------------------------
// delete
// ---------------------------------------------------------------------------

/// The extension delete state of `--delete`.
///
/// Narrows the deletion with [`--name`](Extension::name),
/// [`--revision`](Extension::revision), or [`--path`](Extension::path), and
/// performs it for real with [`--yes`](Extension::yes).
#[derive(Debug, Clone, Default)]
pub struct DeleteExt {
    yes: bool,

    #[cfg(not(feature = "lt2020_1"))]
    name: Option<String>,

    revision: Option<String>,

    path: Option<String>,

    #[cfg(not(feature = "lt2022_2"))]
    allow_unsigned: bool,
}

impl ExclusiveOption for DeleteExt {
    fn inject_args(&self, command: &mut Command) {
        #[cfg(not(feature = "lt2020_1"))]
        if let Some(name) = &self.name {
            command.arg("--name").arg(name);
        }

        if let Some(revision) = &self.revision {
            command.arg(format!("--revision={revision}"));
        }

        if let Some(path) = &self.path {
            command.arg("--path").arg(path);
        }

        if self.yes {
            command.arg("--yes");
        }

        #[cfg(not(feature = "lt2022_2"))]
        if self.allow_unsigned {
            command.arg("--allow-unsigned");
        }
    }
}

/// The certificate delete state of `--delete`.
///
/// Deletes a certificate by fingerprint with `--cert`. Selected with
/// [`Extension::cert`].
#[derive(Debug, Clone, Copy, Default)]
#[cfg(not(feature = "lt2022_2"))]
pub struct DeleteCert;

#[cfg(not(feature = "lt2022_2"))]
impl ExclusiveOption for DeleteCert {
    fn inject_args(&self, command: &mut Command) {
        command.arg("--cert");
    }
}

/// The `--delete` operation of `p4 extension`.
///
/// The type parameter selects what is deleted:
#[cfg_attr(
    feature = "lt2022_2",
    doc = "[`Unselected`] or [`DeleteExt`] for an extension."
)]
#[cfg_attr(
    not(feature = "lt2022_2"),
    doc = "[`Unselected`] or [`DeleteExt`] for an extension, [`DeleteCert`] for a certificate."
)]
/// Selected with [`ExtensionEntry::delete`].
#[derive(Debug, Clone, Default)]
pub struct Delete<M = Unselected> {
    fqn: String,

    mode: M,
}

impl<M: ExclusiveOption> ExclusiveOption for Delete<M> {
    fn inject_args(&self, command: &mut Command) {
        command.arg("--delete").arg(&self.fqn);
        self.mode.inject_args(command);
    }
}

impl Extension<Delete<Unselected>> {
    /// Returns the fully-qualified name (or certificate fingerprint) to
    /// delete.
    pub fn get_fqn(&self) -> &str {
        &self.op.fqn
    }

    /// Sets the fully-qualified name (or certificate fingerprint) to delete.
    pub fn set_fqn(&mut self, v: impl Into<String>) -> &mut Self {
        self.op.fqn = v.into();
        self
    }

    /// Sets the fully-qualified name (or certificate fingerprint) to delete.
    pub fn fqn(mut self, v: impl Into<String>) -> Self {
        self.op.fqn = v.into();
        self
    }

    /// # Description
    ///
    /// --yes
    ///
    /// To actually perform the install or delete, the `--yes` option is
    /// required. Without `--yes`, the p4 extension command merely reports
    /// what it would do without actually performing the install or delete.
    /// `-y` is equivalent to `--yes`.
    pub fn yes(self) -> Extension<Delete<DeleteExt>> {
        self.delete_ext(|mode| mode.yes = true)
    }

    /// # Description
    ///
    /// --name instName
    ///
    /// To remove the named instance config, include the `--name` option.
    #[cfg(not(feature = "lt2020_1"))]
    pub fn name(self, v: impl Into<String>) -> Extension<Delete<DeleteExt>> {
        let name = v.into();
        self.delete_ext(|mode| mode.name = Some(name))
    }

    /// # Description
    ///
    /// --revision=n
    ///
    /// To remove one specific revision and its instances, include the
    /// `--revision` option.
    pub fn revision(self, v: impl Into<String>) -> Extension<Delete<DeleteExt>> {
        let revision = v.into();
        self.delete_ext(|mode| mode.revision = Some(revision))
    }

    /// # Description
    ///
    /// --path FileSpec
    ///
    /// To delete file-based instances registered under the specified path,
    /// use the `--path` option.
    pub fn path(self, v: impl Into<String>) -> Extension<Delete<DeleteExt>> {
        let path = v.into();
        self.delete_ext(|mode| mode.path = Some(path))
    }

    /// # Description
    ///
    /// --cert
    ///
    /// Can be supplied in `--install` or `--delete`. For example,
    /// `p4 extension --install path/certificate.txt --cert --comment "We trust ACTools"`.
    #[cfg(not(feature = "lt2022_2"))]
    pub fn cert(self) -> Extension<Delete<DeleteCert>> {
        Extension {
            bin: self.bin,
            global_opts: self.global_opts,
            op: Delete {
                fqn: self.op.fqn,
                mode: DeleteCert,
            },
        }
    }

    fn delete_ext(mut self, f: impl FnOnce(&mut DeleteExt)) -> Extension<Delete<DeleteExt>> {
        let mut mode = DeleteExt::default();
        f(&mut mode);
        Extension {
            bin: self.bin,
            global_opts: self.global_opts,
            op: Delete {
                fqn: std::mem::take(&mut self.op.fqn),
                mode,
            },
        }
    }
}

impl Extension<Delete<DeleteExt>> {
    /// # Description
    ///
    /// --yes
    ///
    /// To actually perform the install or delete, the `--yes` option is
    /// required. Without `--yes`, the p4 extension command merely reports
    /// what it would do without actually performing the install or delete.
    /// `-y` is equivalent to `--yes`.
    pub fn get_yes(&self) -> bool {
        self.op.mode.yes
    }

    /// # Description
    ///
    /// --yes
    ///
    /// To actually perform the install or delete, the `--yes` option is
    /// required. Without `--yes`, the p4 extension command merely reports
    /// what it would do without actually performing the install or delete.
    /// `-y` is equivalent to `--yes`.
    pub fn set_yes(&mut self, v: bool) -> &mut Self {
        self.op.mode.yes = v;
        self
    }

    /// # Description
    ///
    /// --yes
    ///
    /// To actually perform the install or delete, the `--yes` option is
    /// required. Without `--yes`, the p4 extension command merely reports
    /// what it would do without actually performing the install or delete.
    /// `-y` is equivalent to `--yes`.
    pub fn yes(mut self, v: bool) -> Self {
        self.op.mode.yes = v;
        self
    }

    /// # Description
    ///
    /// --name instName
    ///
    /// To remove the named instance config, include the `--name` option.
    #[cfg(not(feature = "lt2020_1"))]
    pub fn get_name(&self) -> Option<&String> {
        self.op.mode.name.as_ref()
    }

    /// # Description
    ///
    /// --name instName
    ///
    /// To remove the named instance config, include the `--name` option.
    #[cfg(not(feature = "lt2020_1"))]
    pub fn set_name(&mut self, v: impl Into<String>) -> &mut Self {
        self.op.mode.name = Some(v.into());
        self
    }

    /// # Description
    ///
    /// --name instName
    ///
    /// To remove the named instance config, include the `--name` option.
    #[cfg(not(feature = "lt2020_1"))]
    pub fn name(mut self, v: impl Into<String>) -> Self {
        self.op.mode.name = Some(v.into());
        self
    }

    /// # Description
    ///
    /// --revision=n
    ///
    /// To remove one specific revision and its instances, include the
    /// `--revision` option.
    pub fn get_revision(&self) -> Option<&String> {
        self.op.mode.revision.as_ref()
    }

    /// # Description
    ///
    /// --revision=n
    ///
    /// To remove one specific revision and its instances, include the
    /// `--revision` option.
    pub fn set_revision(&mut self, v: impl Into<String>) -> &mut Self {
        self.op.mode.revision = Some(v.into());
        self
    }

    /// # Description
    ///
    /// --revision=n
    ///
    /// To remove one specific revision and its instances, include the
    /// `--revision` option.
    pub fn revision(mut self, v: impl Into<String>) -> Self {
        self.op.mode.revision = Some(v.into());
        self
    }

    /// # Description
    ///
    /// --path FileSpec
    ///
    /// To delete file-based instances registered under the specified path,
    /// use the `--path` option.
    pub fn get_path(&self) -> Option<&String> {
        self.op.mode.path.as_ref()
    }

    /// # Description
    ///
    /// --path FileSpec
    ///
    /// To delete file-based instances registered under the specified path,
    /// use the `--path` option.
    pub fn set_path(&mut self, v: impl Into<String>) -> &mut Self {
        self.op.mode.path = Some(v.into());
        self
    }

    /// # Description
    ///
    /// --path FileSpec
    ///
    /// To delete file-based instances registered under the specified path,
    /// use the `--path` option.
    pub fn path(mut self, v: impl Into<String>) -> Self {
        self.op.mode.path = Some(v.into());
        self
    }

    /// # Description
    ///
    /// --allow-unsigned
    ///
    #[cfg_attr(
        all(
            feature = "lt2025_1",
            not(feature = "lt2022_2"),
            feature = "lt2024_2",
            not(feature = "lt2024_1")
        ),
        doc = "(Optional) If included in the commands to install (or delete) an extension, it is the same as if the Configurables reference configurable were set to 1 so that the extension does not need to be signed."
    )]
    #[cfg_attr(
        not(all(
            feature = "lt2025_1",
            not(feature = "lt2022_2"),
            feature = "lt2024_2",
            not(feature = "lt2024_1")
        )),
        doc = "(Optional) If included in the commands to install (or delete) an extension, it is the same as if the server.extensions.allow.unsigned configurable were set to 1 so that the extension does not need to be signed."
    )]
    #[cfg(not(feature = "lt2022_2"))]
    pub fn get_allow_unsigned(&self) -> bool {
        self.op.mode.allow_unsigned
    }

    /// # Description
    ///
    /// --allow-unsigned
    ///
    #[cfg_attr(
        all(
            feature = "lt2025_1",
            not(feature = "lt2022_2"),
            feature = "lt2024_2",
            not(feature = "lt2024_1")
        ),
        doc = "(Optional) If included in the commands to install (or delete) an extension, it is the same as if the Configurables reference configurable were set to 1 so that the extension does not need to be signed."
    )]
    #[cfg_attr(
        not(all(
            feature = "lt2025_1",
            not(feature = "lt2022_2"),
            feature = "lt2024_2",
            not(feature = "lt2024_1")
        )),
        doc = "(Optional) If included in the commands to install (or delete) an extension, it is the same as if the server.extensions.allow.unsigned configurable were set to 1 so that the extension does not need to be signed."
    )]
    #[cfg(not(feature = "lt2022_2"))]
    pub fn set_allow_unsigned(&mut self, v: bool) -> &mut Self {
        self.op.mode.allow_unsigned = v;
        self
    }

    /// # Description
    ///
    /// --allow-unsigned
    ///
    #[cfg_attr(
        all(
            feature = "lt2025_1",
            not(feature = "lt2022_2"),
            feature = "lt2024_2",
            not(feature = "lt2024_1")
        ),
        doc = "(Optional) If included in the commands to install (or delete) an extension, it is the same as if the Configurables reference configurable were set to 1 so that the extension does not need to be signed."
    )]
    #[cfg_attr(
        not(all(
            feature = "lt2025_1",
            not(feature = "lt2022_2"),
            feature = "lt2024_2",
            not(feature = "lt2024_1")
        )),
        doc = "(Optional) If included in the commands to install (or delete) an extension, it is the same as if the server.extensions.allow.unsigned configurable were set to 1 so that the extension does not need to be signed."
    )]
    #[cfg(not(feature = "lt2022_2"))]
    pub fn allow_unsigned(mut self, v: bool) -> Self {
        self.op.mode.allow_unsigned = v;
        self
    }
}

// ---------------------------------------------------------------------------
// configure
// ---------------------------------------------------------------------------

/// The `-i` state of `--configure`.
///
/// Reads the extension definition from standard input. Selected with
/// [`Extension::stdin`].
#[derive(Debug, Clone, Copy, Default)]
pub struct Stdin;

impl ExclusiveOption for Stdin {
    fn inject_args(&self, command: &mut Command) {
        command.arg("-i");
    }
}

/// The `-o` state of `--configure`.
///
/// Writes the extension definition to standard output. Selected with
/// [`Extension::stdout`].
#[derive(Debug, Clone, Copy, Default)]
pub struct Stdout;

impl ExclusiveOption for Stdout {
    fn inject_args(&self, command: &mut Command) {
        command.arg("-o");
    }
}

/// The `--configure` operation of `p4 extension`.
///
/// The type parameter selects where the extension definition goes:
/// [`Unselected`] to edit it in a form, [`Stdin`] (`-i`) to read it from
/// standard input, or [`Stdout`] (`-o`) to write it to standard output.
/// Selected with [`ExtensionEntry::configure`].
#[derive(Debug, Clone, Default)]
pub struct Configure<M = Unselected> {
    fqn: String,

    revision: Option<String>,

    name: Option<String>,

    mode: M,
}

impl<M: ExclusiveOption> ExclusiveOption for Configure<M> {
    fn inject_args(&self, command: &mut Command) {
        command.arg("--configure").arg(&self.fqn);

        if let Some(revision) = &self.revision {
            command.arg(format!("--revision={revision}"));
        }

        self.mode.inject_args(command);

        if let Some(name) = &self.name {
            command.arg("--name").arg(name);
        }
    }
}

impl Extension<Configure<Unselected>> {
    /// # Description
    ///
    /// -i
    ///
    #[cfg_attr(
        not(feature = "lt2022_1"),
        doc = "Read the extension definition from standard input."
    )]
    pub fn stdin(self) -> Extension<Configure<Stdin>> {
        Extension {
            bin: self.bin,
            global_opts: self.global_opts,
            op: Configure {
                fqn: self.op.fqn,
                revision: self.op.revision,
                name: self.op.name,
                mode: Stdin,
            },
        }
    }

    /// # Description
    ///
    /// -o
    ///
    #[cfg_attr(
        not(feature = "lt2022_1"),
        doc = "Write the extension definition from to standard output."
    )]
    pub fn stdout(self) -> Extension<Configure<Stdout>> {
        Extension {
            bin: self.bin,
            global_opts: self.global_opts,
            op: Configure {
                fqn: self.op.fqn,
                revision: self.op.revision,
                name: self.op.name,
                mode: Stdout,
            },
        }
    }
}

impl<M: ExclusiveOption> Extension<Configure<M>> {
    /// Returns the fully-qualified name of the extension to configure.
    pub fn get_fqn(&self) -> &str {
        &self.op.fqn
    }

    /// Sets the fully-qualified name of the extension to configure.
    pub fn set_fqn(&mut self, v: impl Into<String>) -> &mut Self {
        self.op.fqn = v.into();
        self
    }

    /// Sets the fully-qualified name of the extension to configure.
    pub fn fqn(mut self, v: impl Into<String>) -> Self {
        self.op.fqn = v.into();
        self
    }

    /// # Description
    ///
    /// --revision=n
    ///
    #[cfg_attr(
        feature = "lt2020_1",
        doc = "Which depot version of the Extension to configure.  The default is the head revision.  This option applies to the `--configure` option."
    )]
    #[cfg_attr(
        all(feature = "lt2022_1", not(feature = "lt2020_1")),
        doc = "Which depot version of the Extension to configure.  The default is the head revision.  This option applies to the `--configure` option. This flag applies to the `--configure` option when used with or without the `--name` option."
    )]
    #[cfg_attr(
        all(feature = "lt2022_2", not(feature = "lt2022_1")),
        doc = "Which depot version of the Extension to configure.  The default is the head revision.  This option applies to the `--configure` option. This flag applies to the `--configure` option when used with or without the `--name` option. It is optional to include a # followed by a revision number, such as ExampleInc::extName#6."
    )]
    #[cfg_attr(
        not(feature = "lt2022_2"),
        doc = "Which depot version of the Extension to configure.  The default is the head revision.  This option applies to the `--configure` option. This flag applies to the `--configure` option when used with or without the `--name` option."
    )]
    pub fn get_revision(&self) -> Option<&String> {
        self.op.revision.as_ref()
    }

    /// # Description
    ///
    /// --revision=n
    ///
    #[cfg_attr(
        feature = "lt2020_1",
        doc = "Which depot version of the Extension to configure.  The default is the head revision.  This option applies to the `--configure` option."
    )]
    #[cfg_attr(
        all(feature = "lt2022_1", not(feature = "lt2020_1")),
        doc = "Which depot version of the Extension to configure.  The default is the head revision.  This option applies to the `--configure` option. This flag applies to the `--configure` option when used with or without the `--name` option."
    )]
    #[cfg_attr(
        all(feature = "lt2022_2", not(feature = "lt2022_1")),
        doc = "Which depot version of the Extension to configure.  The default is the head revision.  This option applies to the `--configure` option. This flag applies to the `--configure` option when used with or without the `--name` option. It is optional to include a # followed by a revision number, such as ExampleInc::extName#6."
    )]
    #[cfg_attr(
        not(feature = "lt2022_2"),
        doc = "Which depot version of the Extension to configure.  The default is the head revision.  This option applies to the `--configure` option. This flag applies to the `--configure` option when used with or without the `--name` option."
    )]
    pub fn set_revision(&mut self, v: impl Into<String>) -> &mut Self {
        self.op.revision = Some(v.into());
        self
    }

    /// # Description
    ///
    /// --revision=n
    ///
    #[cfg_attr(
        feature = "lt2020_1",
        doc = "Which depot version of the Extension to configure.  The default is the head revision.  This option applies to the `--configure` option."
    )]
    #[cfg_attr(
        all(feature = "lt2022_1", not(feature = "lt2020_1")),
        doc = "Which depot version of the Extension to configure.  The default is the head revision.  This option applies to the `--configure` option. This flag applies to the `--configure` option when used with or without the `--name` option."
    )]
    #[cfg_attr(
        all(feature = "lt2022_2", not(feature = "lt2022_1")),
        doc = "Which depot version of the Extension to configure.  The default is the head revision.  This option applies to the `--configure` option. This flag applies to the `--configure` option when used with or without the `--name` option. It is optional to include a # followed by a revision number, such as ExampleInc::extName#6."
    )]
    #[cfg_attr(
        not(feature = "lt2022_2"),
        doc = "Which depot version of the Extension to configure.  The default is the head revision.  This option applies to the `--configure` option. This flag applies to the `--configure` option when used with or without the `--name` option."
    )]
    pub fn revision(mut self, v: impl Into<String>) -> Self {
        self.op.revision = Some(v.into());
        self
    }

    /// # Description
    ///
    /// --name instName
    ///
    #[cfg_attr(
        feature = "lt2020_1",
        doc = "The name of the configuration to create or modify."
    )]
    #[cfg_attr(
        all(feature = "lt2022_2", not(feature = "lt2020_1")),
        doc = "The name of the instance configuration to create or modify."
    )]
    #[cfg_attr(
        not(feature = "lt2022_2"),
        doc = "The name of the instance configuration to create, modify, or delete."
    )]
    pub fn get_name(&self) -> Option<&String> {
        self.op.name.as_ref()
    }

    /// # Description
    ///
    /// --name instName
    ///
    #[cfg_attr(
        feature = "lt2020_1",
        doc = "The name of the configuration to create or modify."
    )]
    #[cfg_attr(
        all(feature = "lt2022_2", not(feature = "lt2020_1")),
        doc = "The name of the instance configuration to create or modify."
    )]
    #[cfg_attr(
        not(feature = "lt2022_2"),
        doc = "The name of the instance configuration to create, modify, or delete."
    )]
    pub fn set_name(&mut self, v: impl Into<String>) -> &mut Self {
        self.op.name = Some(v.into());
        self
    }

    /// # Description
    ///
    /// --name instName
    ///
    #[cfg_attr(
        feature = "lt2020_1",
        doc = "The name of the configuration to create or modify."
    )]
    #[cfg_attr(
        all(feature = "lt2022_2", not(feature = "lt2020_1")),
        doc = "The name of the instance configuration to create or modify."
    )]
    #[cfg_attr(
        not(feature = "lt2022_2"),
        doc = "The name of the instance configuration to create, modify, or delete."
    )]
    pub fn name(mut self, v: impl Into<String>) -> Self {
        self.op.name = Some(v.into());
        self
    }
}

// ---------------------------------------------------------------------------
// run
// ---------------------------------------------------------------------------

/// The `--run` operation of `p4 extension`.
///
/// Executes an extension-supplied command on an instance configuration.
/// Selected with [`ExtensionEntry::run`].
///
/// Extra arguments the extension might need are passed at spawn time:
/// `ParameterizedSpawn<()>` spawns the command with no extra arguments, and
/// `ParameterizedSpawn<(S,)>` spawns it with an arbitrary number of them.
#[cfg(not(feature = "lt2020_1"))]
#[derive(Debug, Clone, Default)]
pub struct Run {
    inst: String,
}

#[cfg(not(feature = "lt2020_1"))]
impl ExclusiveOption for Run {
    fn inject_args(&self, command: &mut Command) {
        command.arg("--run").arg(&self.inst);
    }
}

#[cfg(not(feature = "lt2020_1"))]
impl<S, I> ParameterizedSpawn<(S,)> for Extension<Run>
where
    S: IntoIterator<Item = I>,
    I: AsRef<OsStr>,
{
    type Output = Child;
    type Error = std::io::Error;

    /// Spawns `p4 extension --run instName` with the given extra arguments
    /// passed through to the extension, with piped standard output and error
    /// streams; use the returned [`Child`] handle to wait for it or interact
    /// with it.
    fn spawn_with(&mut self, (arguments,): (S,)) -> Result<Self::Output, Self::Error> {
        self.setup_command(&self.bin)
            .args(arguments)
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()
    }
}

#[cfg(not(feature = "lt2020_1"))]
impl Extension<Run> {
    /// Returns the instance configuration whose extension is executed.
    pub fn get_inst(&self) -> &str {
        &self.op.inst
    }

    /// Sets the instance configuration whose extension is executed.
    pub fn set_inst(&mut self, v: impl Into<String>) -> &mut Self {
        self.op.inst = v.into();
        self
    }

    /// Sets the instance configuration whose extension is executed.
    pub fn inst(mut self, v: impl Into<String>) -> Self {
        self.op.inst = v.into();
        self
    }
}

// ---------------------------------------------------------------------------
// list
// ---------------------------------------------------------------------------

/// The `--type` value of `--list`.
///
/// Determines which data is reported: installed extensions, their global
/// configurations, or their instance configurations.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Type {
    /// Reports the installed extensions.
    Extensions,

    /// Reports both global and instance configurations.
    Configs,

    /// Reports global configurations.
    #[cfg(not(feature = "lt2020_1"))]
    Global,

    /// Reports instance configurations.
    #[cfg(not(feature = "lt2020_1"))]
    Instance,

    /// Reports the installed certificates.
    #[cfg(not(feature = "lt2022_2"))]
    Certs,
}

impl Type {
    fn to_str(&self) -> &'static str {
        match self {
            Type::Extensions => "extensions",
            Type::Configs => "configs",

            #[cfg(not(feature = "lt2020_1"))]
            Type::Global => "global",

            #[cfg(not(feature = "lt2020_1"))]
            Type::Instance => "instance",

            #[cfg(not(feature = "lt2022_2"))]
            Type::Certs => "certs",
        }
    }
}

/// The `--list` operation of `p4 extension`.
///
/// Displays the installed extensions and their configurations, as selected
/// by [`--type`](Extension::ty). Selected with [`ExtensionEntry::list`].
#[derive(Debug, Clone, Default)]
pub struct List {
    ty: Option<Type>,

    #[cfg(not(feature = "lt2022_2"))]
    path: Option<String>,
}

impl ExclusiveOption for List {
    fn inject_args(&self, command: &mut Command) {
        command.arg("--list");

        if let Some(ty) = &self.ty {
            command.arg("--type").arg(ty.to_str());
        }

        #[cfg(not(feature = "lt2022_2"))]
        if let Some(path) = &self.path {
            command.arg("--path").arg(path);
        }
    }
}

impl Extension<List> {
    /// # Description
    ///
    /// --type type
    ///
    #[cfg_attr(
        feature = "lt2022_2",
        doc = "Determines which data is reported: the installed extensions, their global configurations, or their instance configurations."
    )]
    #[cfg_attr(
        not(feature = "lt2022_2"),
        doc = "Must be supplied with `--list`. Possible type values: extensions, global, instance, certs, configs (both global and instance). For example, `p4 extension --list --type certs`."
    )]
    pub fn get_ty(&self) -> Option<Type> {
        self.op.ty
    }

    /// # Description
    ///
    /// --type type
    ///
    #[cfg_attr(
        feature = "lt2022_2",
        doc = "Determines which data is reported: the installed extensions, their global configurations, or their instance configurations."
    )]
    #[cfg_attr(
        not(feature = "lt2022_2"),
        doc = "Must be supplied with `--list`. Possible type values: extensions, global, instance, certs, configs (both global and instance). For example, `p4 extension --list --type certs`."
    )]
    pub fn set_ty(&mut self, v: Type) -> &mut Self {
        self.op.ty = Some(v);
        self
    }

    /// # Description
    ///
    /// --type type
    ///
    #[cfg_attr(
        feature = "lt2022_2",
        doc = "Determines which data is reported: the installed extensions, their global configurations, or their instance configurations."
    )]
    #[cfg_attr(
        not(feature = "lt2022_2"),
        doc = "Must be supplied with `--list`. Possible type values: extensions, global, instance, certs, configs (both global and instance). For example, `p4 extension --list --type certs`."
    )]
    pub fn ty(mut self, v: Type) -> Self {
        self.op.ty = Some(v);
        self
    }

    /// # Description
    ///
    /// --path FileSpec
    ///
    /// Filters output to instance configurations that are path-based and
    /// whose path argument matches the given filespec.
    #[cfg(not(feature = "lt2022_2"))]
    pub fn get_path(&self) -> Option<&String> {
        self.op.path.as_ref()
    }

    /// # Description
    ///
    /// --path FileSpec
    ///
    /// Filters output to instance configurations that are path-based and
    /// whose path argument matches the given filespec.
    #[cfg(not(feature = "lt2022_2"))]
    pub fn set_path(&mut self, v: impl Into<String>) -> &mut Self {
        self.op.path = Some(v.into());
        self
    }

    /// # Description
    ///
    /// --path FileSpec
    ///
    /// Filters output to instance configurations that are path-based and
    /// whose path argument matches the given filespec.
    #[cfg(not(feature = "lt2022_2"))]
    pub fn path(mut self, v: impl Into<String>) -> Self {
        self.op.path = Some(v.into());
        self
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::cmd::args_of;

    fn args(cmd: &impl SubCommand) -> Vec<String> {
        args_of(&cmd.setup_command("p4"))
    }

    fn entry() -> ExtensionEntry {
        ExtensionEntry::new("p4".into(), GlobalOpts::new())
    }

    /// Dry-run checks of the assembled `p4 extension` command line; no
    /// process is spawned.
    #[test]
    fn sample_injects_name() {
        assert_eq!(
            args(&entry().sample("ExtName")),
            ["extension", "--sample", "ExtName"]
        );
    }

    #[test]
    fn package_without_sign() {
        assert_eq!(
            args(&entry().package("ExtName")),
            ["extension", "--package", "ExtName"]
        );
    }

    #[cfg(not(feature = "lt2022_2"))]
    #[test]
    fn package_with_sign() {
        assert_eq!(
            args(&entry().package("ExtName").sign("keys")),
            ["extension", "--package", "ExtName", "--sign", "keys"]
        );
    }

    #[test]
    fn install_dry_run_has_no_yes() {
        assert_eq!(
            args(&entry().install("ExtName.p4-extension")),
            ["extension", "--install", "ExtName.p4-extension"]
        );
    }

    #[cfg(not(feature = "lt2020_1"))]
    #[test]
    fn install_ext_yes() {
        assert_eq!(
            args(&entry().install("ExtName.p4-extension").yes()),
            ["extension", "--install", "ExtName.p4-extension", "--yes"]
        );
    }

    #[cfg(all(not(feature = "lt2020_1"), not(feature = "lt2022_2")))]
    #[test]
    fn install_ext_yes_and_allow_unsigned() {
        assert_eq!(
            args(
                &entry()
                    .install("ExtName.p4-extension")
                    .yes()
                    .allow_unsigned(true)
            ),
            [
                "extension",
                "--install",
                "ExtName.p4-extension",
                "--yes",
                "--allow-unsigned"
            ]
        );
    }

    #[cfg(not(feature = "lt2022_2"))]
    #[test]
    fn install_cert_with_comment() {
        assert_eq!(
            args(&entry().install("cert.txt").comment("We trust ACTools")),
            [
                "extension",
                "--install",
                "cert.txt",
                "--cert",
                "--comment",
                "We trust ACTools"
            ]
        );
    }

    #[cfg(not(feature = "lt2022_2"))]
    #[test]
    fn install_cert_without_comment() {
        assert_eq!(
            args(&entry().install("cert.txt").cert()),
            ["extension", "--install", "cert.txt", "--cert"]
        );
    }

    #[test]
    fn delete_dry_run_has_no_options() {
        assert_eq!(
            args(&entry().delete("myCompany::ext")),
            ["extension", "--delete", "myCompany::ext"]
        );
    }

    #[test]
    fn delete_ext_with_revision_path_yes() {
        assert_eq!(
            args(
                &entry()
                    .delete("myCompany::ext")
                    .revision("2")
                    .path("//depot/...")
                    .yes(true)
            ),
            [
                "extension",
                "--delete",
                "myCompany::ext",
                "--revision=2",
                "--path",
                "//depot/...",
                "--yes"
            ]
        );
    }

    #[cfg(not(feature = "lt2020_1"))]
    #[test]
    fn delete_ext_with_name() {
        assert_eq!(
            args(&entry().delete("myCompany::ext").name("inst").yes(true)),
            [
                "extension",
                "--delete",
                "myCompany::ext",
                "--name",
                "inst",
                "--yes"
            ]
        );
    }

    #[cfg(not(feature = "lt2022_2"))]
    #[test]
    fn delete_cert() {
        assert_eq!(
            args(&entry().delete("AA:BB:CC").cert()),
            ["extension", "--delete", "AA:BB:CC", "--cert"]
        );
    }

    #[test]
    fn configure_global_only() {
        assert_eq!(
            args(&entry().configure("myCompany::ext")),
            ["extension", "--configure", "myCompany::ext"]
        );
    }

    #[test]
    fn configure_instance_with_revision_and_stdin() {
        assert_eq!(
            args(
                &entry()
                    .configure("myCompany::ext")
                    .revision("2")
                    .name("inst")
                    .stdin()
            ),
            [
                "extension",
                "--configure",
                "myCompany::ext",
                "--revision=2",
                "-i",
                "--name",
                "inst"
            ]
        );
    }

    #[test]
    fn configure_stdout() {
        assert_eq!(
            args(&entry().configure("myCompany::ext").stdout()),
            ["extension", "--configure", "myCompany::ext", "-o"]
        );
    }

    #[cfg(not(feature = "lt2020_1"))]
    #[test]
    fn run_without_arguments() {
        assert_eq!(args(&entry().run("inst")), ["extension", "--run", "inst"]);
    }

    /// The extra-argument form is a separate `ParameterizedSpawn` impl that
    /// appends the argument list at spawn time.
    #[cfg(not(feature = "lt2020_1"))]
    #[test]
    fn run_accepts_arbitrary_arguments_at_spawn() {
        fn assert_arguments_spawn<S, I>()
        where
            S: IntoIterator<Item = I>,
            I: AsRef<OsStr>,
            Extension<Run>: ParameterizedSpawn<(S,)>,
        {
        }

        assert_arguments_spawn::<Vec<String>, String>();
        assert_arguments_spawn::<[&str; 2], &str>();
    }

    #[test]
    fn list_with_type() {
        assert_eq!(
            args(&entry().list().ty(Type::Configs)),
            ["extension", "--list", "--type", "configs"]
        );
    }

    #[cfg(not(feature = "lt2022_2"))]
    #[test]
    fn list_with_type_and_path() {
        assert_eq!(
            args(&entry().list().ty(Type::Instance).path("//depot/...")),
            [
                "extension",
                "--list",
                "--type",
                "instance",
                "--path",
                "//depot/..."
            ]
        );
    }

    #[test]
    fn global_opts_are_injected_once() {
        let cmd = ExtensionEntry::new(
            "p4".into(),
            GlobalOpts::new().port("localhost:1666").quiet_mode(true),
        )
        .configure("myCompany::ext");

        assert_eq!(
            args(&cmd),
            [
                "-p",
                "localhost:1666",
                "-q",
                "extension",
                "--configure",
                "myCompany::ext"
            ]
        );
    }

    #[test]
    fn bin_accessors_update_executable_path() {
        let mut cmd = entry().sample("ExtName");
        assert_eq!(cmd.get_bin(), Path::new("p4"));

        cmd.set_bin("/opt/perforce/bin/p4");
        assert_eq!(cmd.get_bin(), Path::new("/opt/perforce/bin/p4"));

        let cmd = cmd.bin("p4custom");
        assert_eq!(cmd.get_bin(), Path::new("p4custom"));
    }

    #[test]
    fn delete_ext_accessors_update_mode() {
        let mut cmd = entry().delete("myCompany::ext").yes();
        assert!(cmd.get_yes());

        cmd.set_yes(false);
        assert!(!cmd.get_yes());

        cmd.set_revision("3");
        assert_eq!(cmd.get_revision(), Some(&"3".to_string()));
    }
}