clap_types 0.1.0

Generate strongly-typed command builders from clap command definitions
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
// Copyright (c) Meta Platforms, Inc. and affiliates.

//! Python 3.10+ code generator: emits dataclasses, argv builders, and optional
//! `subprocess` helpers. Supports both single-file modules and full package
//! layouts via `Python::package()`.
//!
//! `OutputEncoding::Json` causes the generated `parse_output` to deserialize
//! stdout via the standard library's `json.loads`. The Rust and Kotlin
//! backends leave the payload as a raw string instead, since those ecosystems
//! require the caller to choose a deserialization library and target type.

use std::collections::BTreeSet;
use std::io::Write;
use std::io::{self};
use std::path::Path;
use std::path::PathBuf;

use crate::codegen::collapse_lines;
use crate::codegen::combined_args;
use crate::codegen::command_pieces;
use crate::codegen::command_type_prefix;
use crate::codegen::ensure_unique_command_prefixes;
use crate::codegen::inherited_globals;
use crate::codegen::is_grouped_repeated;
use crate::codegen::is_required;
use crate::codegen::lower_join;
use crate::codegen::option_token;
use crate::codegen::output_encoding;
use crate::codegen::output_mode;
use crate::codegen::output_schema;
use crate::codegen::pascal_case;
use crate::codegen::quote_double;
use crate::codegen::safe_identifier;
use crate::generate::GeneratedFile;
use crate::generate::Generator;
use crate::generate::OutputContractGeneration;
use crate::model::ArgKind;
use crate::model::ArgSpec;
use crate::model::CliSpec;
use crate::model::CommandSpec;
use crate::model::ValueType;

/// Options for the Python backend.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct PythonOptions {
    /// Generated module file stem, without `.py`.
    pub module_name: Option<String>,
    /// Optional namespace class that exposes generated functions as static methods.
    pub namespace: Option<String>,
    /// Output-contract metadata and parser helpers.
    pub output_contracts: OutputContractGeneration,
}

/// Generate dependency-free Python 3.10+ command builders and subprocess helpers.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Python {
    options: PythonOptions,
}

/// Generate a Python package with one command module per clap command.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct PythonPackage {
    options: PythonOptions,
}

impl Python {
    /// Create a Python generator with default options.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Create a Python generator from explicit options.
    #[must_use]
    pub fn with_options(options: PythonOptions) -> Self {
        Self { options }
    }

    /// Set the generated module name, without `.py`.
    #[must_use]
    pub fn module_name(mut self, module_name: impl Into<String>) -> Self {
        self.options.module_name = Some(module_name.into());
        self
    }

    /// Set an optional namespace class for generated functions.
    #[must_use]
    pub fn namespace(mut self, namespace: impl Into<String>) -> Self {
        self.options.namespace = Some(namespace.into());
        self
    }

    /// Emit output-contract metadata and parser helpers.
    #[must_use]
    pub fn output_contracts(mut self) -> Self {
        self.options.output_contracts = OutputContractGeneration::Emit;
        self
    }

    /// Omit output-contract metadata and parser helpers.
    #[must_use]
    pub fn without_output_contracts(mut self) -> Self {
        self.options.output_contracts = OutputContractGeneration::Omit;
        self
    }

    /// Generate a package layout instead of a single `.py` module.
    #[must_use]
    pub fn package(self) -> PythonPackage {
        PythonPackage {
            options: self.options,
        }
    }
}

impl Generator for Python {
    fn file_name(&self, bin_name: &str) -> String {
        let stem = self
            .options
            .module_name
            .as_deref()
            .map(snake_case)
            .unwrap_or_else(|| snake_case(bin_name));
        format!("{stem}.py")
    }

    fn generate(&self, spec: &CliSpec, buf: &mut dyn Write) -> io::Result<()> {
        ensure_unique_command_prefixes(spec)?;
        let mut output = String::new();
        render_module(spec, &self.options, &mut output);
        buf.write_all(output.as_bytes())
    }
}

impl PythonPackage {
    /// Create a Python package generator with default options.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Create a Python package generator from explicit options.
    #[must_use]
    pub fn with_options(options: PythonOptions) -> Self {
        Self { options }
    }

    /// Set the generated package name.
    #[must_use]
    pub fn module_name(mut self, module_name: impl Into<String>) -> Self {
        self.options.module_name = Some(module_name.into());
        self
    }

    /// Set an optional namespace class exported from the package root.
    #[must_use]
    pub fn namespace(mut self, namespace: impl Into<String>) -> Self {
        self.options.namespace = Some(namespace.into());
        self
    }

    /// Emit output-contract metadata and parser helpers.
    #[must_use]
    pub fn output_contracts(mut self) -> Self {
        self.options.output_contracts = OutputContractGeneration::Emit;
        self
    }

    /// Omit output-contract metadata and parser helpers.
    #[must_use]
    pub fn without_output_contracts(mut self) -> Self {
        self.options.output_contracts = OutputContractGeneration::Omit;
        self
    }
}

impl Generator for PythonPackage {
    fn file_name(&self, bin_name: &str) -> String {
        self.package_name(bin_name)
    }

    fn generate(&self, _spec: &CliSpec, _buf: &mut dyn Write) -> io::Result<()> {
        Err(io::Error::new(
            io::ErrorKind::InvalidInput,
            "PythonPackage emits multiple files; use generate_to to write the package",
        ))
    }

    fn generate_files(&self, spec: &CliSpec) -> io::Result<Vec<GeneratedFile>> {
        ensure_unique_command_prefixes(spec)?;
        Ok(render_package(spec, &self.options))
    }
}

impl PythonPackage {
    fn package_name(&self, bin_name: &str) -> String {
        self.options
            .module_name
            .as_deref()
            .map(package_name)
            .unwrap_or_else(|| package_name(bin_name))
    }
}

#[derive(Debug, Clone)]
struct PackageExport {
    import_path: String,
    args_alias: String,
    build_alias: String,
    command_alias: String,
}

fn render_package(spec: &CliSpec, options: &PythonOptions) -> Vec<GeneratedFile> {
    let package = options
        .module_name
        .as_deref()
        .map(package_name)
        .unwrap_or_else(|| package_name(&spec.bin_name));
    let package_root = PathBuf::from(&package);
    let mut files = Vec::<GeneratedFile>::new();
    let mut exports = Vec::<PackageExport>::new();
    let mut package_dirs = BTreeSet::<PathBuf>::new();

    files.push(GeneratedFile::text(
        package_root.join("_runtime.py"),
        render_package_runtime(spec, options),
    ));

    let mut path = Vec::<String>::new();
    let inherited: Vec<&ArgSpec> = Vec::new();
    render_package_command_tree(
        &package_root,
        &spec.root,
        &mut path,
        &inherited,
        &mut files,
        &mut exports,
        &mut package_dirs,
    );

    for dir in package_dirs {
        files.push(GeneratedFile::text(
            dir.join("__init__.py"),
            "# Generated by clap_types. Do not edit by hand.\n",
        ));
    }

    files.push(GeneratedFile::text(
        package_root.join("__init__.py"),
        render_package_init(options, &exports),
    ));

    files
}

fn render_package_runtime(spec: &CliSpec, options: &PythonOptions) -> String {
    let mut output = String::new();
    output.push_str("# Generated by clap_types. Do not edit by hand.\n");
    output.push_str("from __future__ import annotations\n\n");
    output.push_str("from collections.abc import Mapping, Sequence\n");
    output.push_str("from dataclasses import dataclass\n");
    if options.output_contracts.is_enabled() {
        output.push_str("import json\n");
    }
    output.push_str("from os import PathLike\n");
    output.push_str("import subprocess\n");
    output.push_str("from typing import Any, TypeAlias, TypeGuard\n\n");
    output.push_str(&format!(
        "PROGRAM: str = {}\n\n",
        quote_double(&spec.bin_name)
    ));
    render_helpers(&mut output);
    if options.output_contracts.is_enabled() {
        render_output_contracts(spec, &mut output);
    }
    output.push_str("__all__ = [\n");
    output.push_str("    \"ArgScalar\",\n");
    output.push_str("    \"ArgValue\",\n");
    output.push_str("    \"CommandInvocation\",\n");
    if options.output_contracts.is_enabled() {
        output.push_str("    \"OutputContract\",\n");
        output.push_str("    \"OUTPUT_CONTRACTS\",\n");
        output.push_str("    \"parse_output\",\n");
    }
    output.push_str("    \"PathValue\",\n");
    output.push_str("    \"PROGRAM\",\n");
    output.push_str("]\n");
    output
}

fn render_package_command_tree(
    package_root: &Path,
    command: &CommandSpec,
    path: &mut Vec<String>,
    inherited: &[&ArgSpec],
    files: &mut Vec<GeneratedFile>,
    exports: &mut Vec<PackageExport>,
    package_dirs: &mut BTreeSet<PathBuf>,
) {
    let rel_path = command_module_path(package_root, command, path);
    add_package_dirs(package_root, &rel_path, package_dirs);
    let import_path = import_path(package_root, &rel_path);
    let type_prefix = command_type_prefix(command, path);
    let fn_prefix = command_function_prefix(command, path);

    files.push(GeneratedFile::text(
        rel_path,
        render_package_command_module(command, path, inherited),
    ));
    exports.push(PackageExport {
        import_path,
        args_alias: format!("{type_prefix}Args"),
        build_alias: format!("build_{fn_prefix}_args"),
        command_alias: format!("{fn_prefix}_command"),
    });

    let next_inherited = inherited_globals(inherited, command);
    for subcommand in &command.subcommands {
        path.push(subcommand.name.clone());
        render_package_command_tree(
            package_root,
            subcommand,
            path,
            &next_inherited,
            files,
            exports,
            package_dirs,
        );
        path.pop();
    }
}

fn render_package_command_module(
    command: &CommandSpec,
    path: &[String],
    inherited: &[&ArgSpec],
) -> String {
    let mut output = String::new();
    let all_args = combined_args(inherited, command);
    let has_required = all_args.iter().copied().any(is_required);

    output.push_str("# Generated by clap_types. Do not edit by hand.\n");
    output.push_str("from __future__ import annotations\n\n");
    output.push_str("from dataclasses import dataclass\n");
    output.push_str("from os import PathLike\n");
    if all_args.iter().any(|arg| !arg.possible_values.is_empty()) {
        output.push_str("from typing import Literal\n");
    }
    output.push('\n');
    render_package_runtime_imports(&all_args, command, path, &mut output);
    output.push_str("\n\n");

    render_dataclass(command, "Args", &all_args, &mut output);
    output.push_str("\n\n");
    render_build_function(
        command,
        path,
        inherited,
        "Args",
        "build_args",
        has_required,
        &mut output,
    );
    output.push_str("\n\n");
    render_command_function("Args", "build_args", "command", has_required, &mut output);
    output.push_str("\n\n");
    output.push_str("__all__ = [\"Args\", \"build_args\", \"command\"]\n");
    output
}

fn render_package_runtime_imports(
    args: &[&ArgSpec],
    command: &CommandSpec,
    path: &[String],
    output: &mut String,
) {
    let mut imports = vec!["CommandInvocation", "PROGRAM"];
    if args
        .iter()
        .any(|arg| python_type(arg).contains("PathValue"))
    {
        imports.push("PathValue");
    }
    if args
        .iter()
        .any(|arg| matches!(arg.kind, ArgKind::Positional))
    {
        imports.push("_push_values");
    }
    if args.iter().any(|arg| {
        matches!(arg.kind, ArgKind::Option) && !arg.value.repeated && !is_grouped_repeated(arg)
    }) {
        imports.push("_push_option");
    }
    if args.iter().any(|arg| {
        matches!(arg.kind, ArgKind::Option) && arg.value.repeated && !is_grouped_repeated(arg)
    }) {
        imports.push("_push_repeated_option");
    }
    if args
        .iter()
        .any(|arg| matches!(arg.kind, ArgKind::Option) && is_grouped_repeated(arg))
    {
        imports.push("_push_grouped_repeated_option");
    }

    output.push_str(&format!(
        "from {}_runtime import (\n",
        runtime_relative_prefix(command, path)
    ));
    for import in imports {
        output.push_str(&format!("    {import},\n"));
    }
    output.push_str(")\n");
}

fn render_package_init(options: &PythonOptions, exports: &[PackageExport]) -> String {
    let mut names = vec!["CommandInvocation".to_owned()];
    let mut functions = Vec::<String>::new();
    let mut output = String::new();

    output.push_str("# Generated by clap_types. Do not edit by hand.\n");
    output.push_str("from __future__ import annotations\n\n");
    if options.output_contracts.is_enabled() {
        output.push_str(
            "from ._runtime import CommandInvocation, OutputContract, OUTPUT_CONTRACTS, parse_output\n",
        );
        names.push("OutputContract".to_owned());
        names.push("OUTPUT_CONTRACTS".to_owned());
        names.push("parse_output".to_owned());
    } else {
        output.push_str("from ._runtime import CommandInvocation\n");
    }

    for export in exports {
        output.push_str(&format!(
            "from .{} import (\n    Args as {},\n    build_args as {},\n    command as {},\n)\n",
            export.import_path, export.args_alias, export.build_alias, export.command_alias
        ));
        names.push(export.args_alias.clone());
        names.push(export.build_alias.clone());
        names.push(export.command_alias.clone());
        functions.push(export.build_alias.clone());
        functions.push(export.command_alias.clone());
    }
    output.push_str("\n\n");

    if let Some(namespace) = options.namespace.as_deref() {
        let namespace = safe_pascal_identifier(namespace);
        render_namespace(&namespace, &functions, &mut output);
        names.push(namespace);
    }

    render_all(names, &mut output);
    output
}

fn command_module_path(package_root: &Path, command: &CommandSpec, path: &[String]) -> PathBuf {
    let mut rel_path = package_root.to_path_buf();
    if path.is_empty() {
        rel_path.push("_root.py");
        return rel_path;
    }

    let stems = path
        .iter()
        .map(|piece| module_stem(piece))
        .collect::<Vec<_>>();
    if command.subcommands.is_empty() {
        for stem in &stems[..stems.len() - 1] {
            rel_path.push(stem);
        }
        rel_path.push(format!("{}.py", stems.last().expect("non-empty path")));
    } else {
        for stem in stems {
            rel_path.push(stem);
        }
        rel_path.push("_root.py");
    }

    rel_path
}

fn add_package_dirs(package_root: &Path, file_path: &Path, package_dirs: &mut BTreeSet<PathBuf>) {
    let mut current = file_path.parent();
    while let Some(dir) = current {
        if dir == package_root {
            break;
        }
        package_dirs.insert(dir.to_path_buf());
        current = dir.parent();
    }
}

fn import_path(package_root: &Path, file_path: &Path) -> String {
    let without_package = file_path
        .strip_prefix(package_root)
        .expect("command module lives under package root");
    let mut components = without_package
        .iter()
        .map(|component| component.to_string_lossy().into_owned())
        .collect::<Vec<_>>();
    if let Some(last) = components.last_mut() {
        if let Some(stripped) = last.strip_suffix(".py") {
            *last = stripped.to_owned();
        }
    }
    components.join(".")
}

fn runtime_relative_prefix(command: &CommandSpec, path: &[String]) -> String {
    let parent_depth = if path.is_empty() {
        0
    } else if command.subcommands.is_empty() {
        path.len().saturating_sub(1)
    } else {
        path.len()
    };
    ".".repeat(parent_depth + 1)
}

fn render_module(spec: &CliSpec, options: &PythonOptions, output: &mut String) {
    let mut exports = Vec::<String>::new();
    let mut functions = Vec::<String>::new();

    output.push_str("# Generated by clap_types. Do not edit by hand.\n");
    output.push_str("from __future__ import annotations\n\n");
    output.push_str("from collections.abc import Mapping, Sequence\n");
    output.push_str("from dataclasses import dataclass\n");
    if options.output_contracts.is_enabled() {
        output.push_str("import json\n");
    }
    output.push_str("from os import PathLike\n");
    output.push_str("import subprocess\n");
    output.push_str("from typing import Any, Literal, TypeAlias, TypeGuard\n\n");
    output.push_str(&format!(
        "PROGRAM: str = {}\n\n",
        quote_double(&spec.bin_name)
    ));
    render_helpers(output);

    exports.push("CommandInvocation".to_owned());
    if options.output_contracts.is_enabled() {
        render_output_contracts(spec, output);
        exports.push("OutputContract".to_owned());
        exports.push("OUTPUT_CONTRACTS".to_owned());
        exports.push("parse_output".to_owned());
    }

    let mut path = Vec::<String>::new();
    let inherited: Vec<&ArgSpec> = Vec::new();
    render_command_tree(
        &spec.root,
        &mut path,
        &inherited,
        output,
        &mut exports,
        &mut functions,
    );

    if let Some(namespace) = options.namespace.as_deref() {
        let namespace = safe_pascal_identifier(namespace);
        render_namespace(&namespace, &functions, output);
        exports.push(namespace);
    }

    render_all(exports, output);
}

fn render_helpers(output: &mut String) {
    output.push_str(
        r#"ArgScalar: TypeAlias = str | int | float | bool | PathLike[str]
ArgValue: TypeAlias = ArgScalar | Sequence[ArgScalar]
PathValue: TypeAlias = str | PathLike[str]


@dataclass(frozen=True)
class CommandInvocation:
    """A concrete CLI invocation built from generated arguments."""

    program: str
    args: tuple[str, ...]

    def argv(self) -> list[str]:
        """Return the full argv list including the executable."""
        return [self.program, *self.args]

    def run(
        self,
        *,
        check: bool = False,
        capture_output: bool = False,
        cwd: str | PathLike[str] | None = None,
        env: Mapping[str, str] | None = None,
        input: str | None = None,
        timeout: float | None = None,
        **kwargs: Any,
    ) -> subprocess.CompletedProcess[str]:
        """Run the invocation with `subprocess.run` using text mode."""
        return subprocess.run(
            self.argv(),
            check=check,
            capture_output=capture_output,
            text=True,
            cwd=cwd,
            env=env,
            input=input,
            timeout=timeout,
            **kwargs,
        )


def _stringify(value: ArgScalar) -> str:
    if isinstance(value, bool):
        return "true" if value else "false"
    return str(value)


def _is_sequence(value: ArgValue) -> TypeGuard[Sequence[ArgScalar]]:
    return isinstance(value, Sequence) and not isinstance(
        value,
        (str, bytes, bytearray),
    )


def _push_values(argv: list[str], value: ArgValue) -> None:
    if _is_sequence(value):
        for item in value:
            argv.append(_stringify(item))
        return

    argv.append(_stringify(value))


def _push_option(argv: list[str], option: str, value: ArgValue) -> None:
    argv.append(option)
    _push_values(argv, value)


def _push_repeated_option(argv: list[str], option: str, value: ArgValue) -> None:
    if _is_sequence(value):
        for item in value:
            _push_option(argv, option, item)
        return

    _push_option(argv, option, value)


def _push_grouped_repeated_option(
    argv: list[str],
    option: str,
    groups: Sequence[Sequence[ArgScalar]],
) -> None:
    for group in groups:
        _push_option(argv, option, list(group))


"#,
    );
}

fn render_output_contracts(spec: &CliSpec, output: &mut String) {
    output.push_str(
        r#"@dataclass(frozen=True)
class OutputContract:
    """Structured output metadata for one command."""

    command_path: tuple[str, ...]
    encoding: str
    mode: str
    type_name: str
    schema: str | None = None


OUTPUT_CONTRACTS: tuple[OutputContract, ...] = (
"#,
    );
    for contract in &spec.outputs {
        output.push_str("    OutputContract(\n");
        output.push_str(&format!(
            "        command_path={},\n",
            python_tuple(&contract.command_path)
        ));
        output.push_str(&format!(
            "        encoding={},\n",
            quote_double(output_encoding(contract.encoding))
        ));
        output.push_str(&format!(
            "        mode={},\n",
            quote_double(output_mode(contract.mode))
        ));
        output.push_str(&format!(
            "        type_name={},\n",
            quote_double(&contract.type_name)
        ));
        if let Some(schema) = contract.schema.as_ref().map(output_schema) {
            output.push_str(&format!("        schema={},\n", quote_double(schema)));
        }
        output.push_str("    ),\n");
    }
    output.push_str(
        r#")


def parse_output(contract: OutputContract, stdout: str) -> object:
    """Parse stdout according to dependency-free framing metadata."""
    if contract.mode == "interactive":
        return stdout
    if contract.encoding == "json":
        return json.loads(stdout)
    if contract.encoding == "json-lines":
        return [json.loads(line) for line in stdout.splitlines() if line]
    if contract.encoding == "text":
        return stdout
    raise ValueError(f"Unsupported output encoding: {contract.encoding}")


"#,
    );
}

fn python_tuple(values: &[String]) -> String {
    match values {
        [] => "()".to_owned(),
        [value] => format!("({},)", quote_double(value)),
        _ => format!(
            "({},)",
            values
                .iter()
                .map(|value| quote_double(value))
                .collect::<Vec<_>>()
                .join(", ")
        ),
    }
}

fn render_command_tree(
    command: &CommandSpec,
    path: &mut Vec<String>,
    inherited: &[&ArgSpec],
    output: &mut String,
    exports: &mut Vec<String>,
    functions: &mut Vec<String>,
) {
    render_command(command, path, inherited, output, exports, functions);

    let next_inherited = inherited_globals(inherited, command);

    for subcommand in &command.subcommands {
        path.push(subcommand.name.clone());
        render_command_tree(
            subcommand,
            path,
            &next_inherited,
            output,
            exports,
            functions,
        );
        path.pop();
    }
}

fn render_command(
    command: &CommandSpec,
    path: &[String],
    inherited: &[&ArgSpec],
    output: &mut String,
    exports: &mut Vec<String>,
    functions: &mut Vec<String>,
) {
    let type_prefix = command_type_prefix(command, path);
    let args_name = format!("{type_prefix}Args");
    let fn_prefix = command_function_prefix(command, path);
    let build_fn = format!("build_{fn_prefix}_args");
    let command_fn = format!("{fn_prefix}_command");
    let all_args = combined_args(inherited, command);
    let has_required = all_args.iter().copied().any(is_required);

    render_dataclass(command, &args_name, &all_args, output);
    output.push_str("\n\n");
    render_build_function(
        command,
        path,
        inherited,
        &args_name,
        &build_fn,
        has_required,
        output,
    );
    output.push_str("\n\n");
    render_command_function(&args_name, &build_fn, &command_fn, has_required, output);
    output.push_str("\n\n");

    exports.push(args_name);
    exports.push(build_fn.clone());
    exports.push(command_fn.clone());
    functions.push(build_fn);
    functions.push(command_fn);
}

fn render_dataclass(
    command: &CommandSpec,
    args_name: &str,
    args: &[&ArgSpec],
    output: &mut String,
) {
    output.push_str("@dataclass(frozen=True, kw_only=True)\n");
    output.push_str(&format!("class {args_name}:\n"));

    if let Some(doc) = command.about.as_deref().or(command.long_about.as_deref()) {
        output.push_str(&format!("    \"\"\"{}\"\"\"\n", docstring(doc)));
    } else {
        output.push_str("    \"\"\"Arguments for this command.\"\"\"\n");
    }

    if args.is_empty() {
        output.push_str("\n    pass\n");
        return;
    }

    for arg in sorted_dataclass_args(args) {
        output.push('\n');
        for comment in field_comments(arg) {
            output.push_str(&format!("    # {comment}\n"));
        }

        let name = property_name(arg);
        let ty = python_type(arg);
        if is_required(arg) {
            output.push_str(&format!("    {name}: {ty}\n"));
        } else {
            output.push_str(&format!("    {name}: {ty} | None = None\n"));
        }
    }
}

fn sorted_dataclass_args<'a>(args: &[&'a ArgSpec]) -> Vec<&'a ArgSpec> {
    let mut sorted = args.to_vec();
    sorted.sort_by_key(|arg| !is_required(arg));
    sorted
}

fn render_build_function(
    command: &CommandSpec,
    path: &[String],
    inherited: &[&ArgSpec],
    args_name: &str,
    build_fn: &str,
    has_required: bool,
    output: &mut String,
) {
    if has_required {
        output.push_str(&format!(
            "def {build_fn}(args: {args_name}) -> tuple[str, ...]:\n"
        ));
    } else {
        output.push_str(&format!(
            "def {build_fn}(args: {args_name} | None = None) -> tuple[str, ...]:\n"
        ));
    }

    output.push_str("    \"\"\"Build argv arguments for this command.\"\"\"\n");

    if has_required {
        output.push_str("    argv: list[str] = []\n");
    } else {
        output.push_str(&format!("    args = args or {args_name}()\n"));
        output.push_str("    argv: list[str] = []\n");
    }

    for &arg in inherited {
        render_arg_builder(arg, output);
    }

    for token in path {
        output.push_str(&format!("    argv.append({})\n", quote_double(token)));
    }

    for arg in &command.args {
        if !inherited.iter().any(|existing| existing.id == arg.id) {
            render_arg_builder(arg, output);
        }
    }

    output.push_str("    return tuple(argv)\n");
}

fn render_command_function(
    args_name: &str,
    build_fn: &str,
    command_fn: &str,
    has_required: bool,
    output: &mut String,
) {
    if has_required {
        output.push_str(&format!(
            "def {command_fn}(\n    args: {args_name},\n    *,\n    program: str | PathLike[str] = PROGRAM,\n) -> CommandInvocation:\n"
        ));
    } else {
        output.push_str(&format!(
            "def {command_fn}(\n    args: {args_name} | None = None,\n    *,\n    program: str | PathLike[str] = PROGRAM,\n) -> CommandInvocation:\n"
        ));
    }

    output.push_str("    \"\"\"Build a subprocess-ready command invocation.\"\"\"\n");
    output.push_str(&format!(
        "    return CommandInvocation(str(program), {build_fn}(args))\n"
    ));
}

fn render_arg_builder(arg: &ArgSpec, output: &mut String) {
    let prop = property_name(arg);
    let option = option_token(arg);
    let accessor = format!("args.{prop}");

    match arg.kind {
        ArgKind::FlagTrue => {
            if let Some(option) = option {
                output.push_str(&format!("    if {accessor} is True:\n"));
                output.push_str(&format!("        argv.append({})\n", quote_double(&option)));
            }
        }
        ArgKind::FlagFalse => {
            if let Some(option) = option {
                output.push_str(&format!("    if {accessor} is False:\n"));
                output.push_str(&format!("        argv.append({})\n", quote_double(&option)));
            }
        }
        ArgKind::Counter => {
            if let Some(option) = option {
                output.push_str(&format!("    for _ in range({accessor} or 0):\n"));
                output.push_str(&format!("        argv.append({})\n", quote_double(&option)));
            }
        }
        ArgKind::Option => {
            if let Some(option) = option {
                let helper = if is_grouped_repeated(arg) {
                    "_push_grouped_repeated_option"
                } else if arg.value.repeated {
                    "_push_repeated_option"
                } else {
                    "_push_option"
                };

                if is_required(arg) {
                    output.push_str(&format!(
                        "    {helper}(argv, {}, {accessor})\n",
                        quote_double(&option)
                    ));
                } else {
                    output.push_str(&format!("    if {accessor} is not None:\n"));
                    output.push_str(&format!(
                        "        {helper}(argv, {}, {accessor})\n",
                        quote_double(&option)
                    ));
                }
            }
        }
        ArgKind::Positional => {
            if is_required(arg) {
                output.push_str(&format!("    _push_values(argv, {accessor})\n"));
            } else {
                output.push_str(&format!("    if {accessor} is not None:\n"));
                output.push_str(&format!("        _push_values(argv, {accessor})\n"));
            }
        }
    }
}

fn render_namespace(namespace: &str, functions: &[String], output: &mut String) {
    output.push_str(&format!("class {namespace}:\n"));
    output.push_str("    \"\"\"Namespace for generated command builders.\"\"\"\n\n");

    for function in functions {
        output.push_str(&format!("    {function} = staticmethod({function})\n"));
    }

    output.push_str("\n\n");
}

fn render_all(mut exports: Vec<String>, output: &mut String) {
    exports.sort();
    exports.dedup();

    output.push_str("__all__ = [\n");
    for export in exports {
        output.push_str(&format!("    {},\n", quote_double(&export)));
    }
    output.push_str("]\n");
}

fn field_comments(arg: &ArgSpec) -> Vec<String> {
    let mut comments = Vec::new();

    if let Some(help) = arg.help.as_deref().or(arg.long_help.as_deref()) {
        comments.push(collapse_lines(help));
    }

    if !arg.defaults.is_empty() {
        comments.push(format!("Clap default: {}.", arg.defaults.join(", ")));
    }

    comments
}

fn python_type(arg: &ArgSpec) -> String {
    let scalar = python_scalar_type(arg);

    if !matches!(arg.kind, ArgKind::Option | ArgKind::Positional) {
        return scalar;
    }

    if is_grouped_repeated(arg) {
        // Each inner sequence is one occurrence's values; the outer sequence
        // holds all occurrences. Both layers accept tuple or list so callers
        // can mix shapes naturally.
        let inner = format!("tuple[{scalar}, ...] | list[{scalar}]");
        format!("tuple[{inner}, ...] | list[{inner}]")
    } else if arg.value.repeated || arg.value.arity.allows_multiple() {
        format!("tuple[{scalar}, ...] | list[{scalar}]")
    } else {
        scalar
    }
}

fn python_scalar_type(arg: &ArgSpec) -> String {
    if !arg.possible_values.is_empty() {
        return format!(
            "Literal[{}]",
            arg.possible_values
                .iter()
                .map(|value| quote_double(&value.name))
                .collect::<Vec<_>>()
                .join(", ")
        );
    }

    match arg.kind {
        ArgKind::FlagTrue | ArgKind::FlagFalse => "bool".to_owned(),
        ArgKind::Counter => "int".to_owned(),
        ArgKind::Option | ArgKind::Positional => match arg.value.ty {
            ValueType::Bool => "bool".to_owned(),
            // Python `int` is arbitrary precision, so wide integers are fine.
            ValueType::Integer | ValueType::BigInteger => "int".to_owned(),
            ValueType::Float => "float".to_owned(),
            ValueType::Path | ValueType::OsString => "PathValue".to_owned(),
            ValueType::Unknown | ValueType::String => "str".to_owned(),
        },
    }
}

fn command_function_prefix(command: &CommandSpec, path: &[String]) -> String {
    let joined = command_pieces(command, path)
        .iter()
        .map(|piece| snake_case(piece))
        .collect::<Vec<_>>()
        .join("_");
    safe_identifier(&joined, is_reserved)
}

fn property_name(arg: &ArgSpec) -> String {
    safe_identifier(&snake_case(&arg.id), is_reserved)
}

fn snake_case(input: &str) -> String {
    let output = lower_join(input, "_");
    if output.is_empty() {
        "value".to_owned()
    } else {
        output
    }
}

fn package_name(input: &str) -> String {
    safe_identifier(&snake_case(input), is_reserved)
}

fn module_stem(input: &str) -> String {
    safe_identifier(&snake_case(input), is_reserved)
}

fn safe_pascal_identifier(input: &str) -> String {
    // Python's hard keywords (`None`, `True`, `False`) are PascalCase, so even
    // a class name that survives identifier rules can collide. Reuse the same
    // reserved-word check applied to fields and functions.
    safe_identifier(&pascal_case(input), is_reserved)
}

fn is_reserved(identifier: &str) -> bool {
    RESERVED.contains(&identifier)
}

const RESERVED: &[&str] = &[
    "False", "None", "True", "and", "as", "assert", "async", "await", "break", "class", "continue",
    "def", "del", "elif", "else", "except", "finally", "for", "from", "global", "if", "import",
    "in", "is", "lambda", "nonlocal", "not", "or", "pass", "raise", "return", "try", "while",
    "with", "yield",
];

fn docstring(input: &str) -> String {
    collapse_lines(input).replace("\"\"\"", "\\\"\\\"\\\"")
}

#[cfg(test)]
mod tests {
    use clap::Arg;
    use clap::ArgAction;
    use clap::Command;
    use clap::value_parser;

    use crate::Python;
    use crate::generate;

    #[test]
    fn generates_typed_python_builders() {
        let cmd = Command::new("demo-tool")
            .about("Demo tool")
            .arg(
                Arg::new("config")
                    .long("config")
                    .help("Path to config")
                    .value_parser(value_parser!(std::path::PathBuf))
                    .action(ArgAction::Set),
            )
            .arg(Arg::new("verbose").short('v').action(ArgAction::Count))
            .arg(
                Arg::new("dry-run")
                    .long("dry-run")
                    .action(ArgAction::SetTrue),
            )
            .arg(
                Arg::new("jobs")
                    .long("jobs")
                    .value_parser(value_parser!(u16))
                    .action(ArgAction::Set),
            )
            .arg(
                Arg::new("mode")
                    .long("mode")
                    .value_parser(["fast", "slow"])
                    .action(ArgAction::Set),
            )
            .subcommand(Command::new("run").arg(Arg::new("target").required(true)));

        let mut output = Vec::<u8>::new();
        generate(
            Python::new()
                .module_name("demo_bindings")
                .namespace("DemoTool"),
            &cmd,
            "demo-tool",
            &mut output,
        )
        .expect("python generation works");
        let output = String::from_utf8(output).expect("python is utf-8");

        assert!(output.contains("class DemoToolArgs:"));
        assert!(output.contains("config: PathValue | None = None"));
        assert!(output.contains("jobs: int | None = None"));
        assert!(output.contains("mode: Literal[\"fast\", \"slow\"] | None = None"));
        assert!(output.contains("def run_command(\n    args: RunArgs,"));
        assert!(output.contains("argv.append(\"run\")"));
        assert!(output.contains("class DemoTool:"));
        assert!(!output.contains("help_command"));
    }

    #[test]
    fn grouped_repeated_option_emits_nested_type_and_dispatches_correctly() {
        // ArgAction::Append + num_args(2) is `--pair a b --pair c d`; the
        // generated type must allow nested sequences and the builder must
        // dispatch to _push_grouped_repeated_option so each group is emitted
        // as a separate `--pair v1 v2` occurrence.
        let cmd = Command::new("demo-tool").arg(
            Arg::new("pair")
                .long("pair")
                .num_args(2)
                .action(ArgAction::Append),
        );

        let mut output = Vec::<u8>::new();
        generate(Python::new(), &cmd, "demo-tool", &mut output).expect("python generation works");
        let output = String::from_utf8(output).expect("python is utf-8");

        assert!(
            output.contains(
                "pair: tuple[tuple[str, ...] | list[str], ...] | list[tuple[str, ...] | list[str]] | None = None"
            ),
            "expected nested-sequence type for grouped repeated option, got:\n{output}"
        );
        assert!(
            output.contains("_push_grouped_repeated_option(argv, \"--pair\", args.pair)"),
            "expected _push_grouped_repeated_option dispatch, got:\n{output}"
        );
    }

    #[test]
    fn variadic_positionals_emit_required_sequences_in_order() {
        let cmd = Command::new("demo-tool").subcommand(
            Command::new("copy")
                .arg(Arg::new("source").required(true))
                .arg(
                    Arg::new("mode")
                        .long("mode")
                        .value_parser(["fast", "safe"])
                        .action(ArgAction::Set),
                )
                .arg(Arg::new("paths").num_args(1..).required(true)),
        );

        let mut output = Vec::<u8>::new();
        generate(Python::new(), &cmd, "demo-tool", &mut output).expect("python generation works");
        let output = String::from_utf8(output).expect("python is utf-8");

        assert!(
            output.contains("paths: tuple[str, ...] | list[str]"),
            "expected variadic positional sequence type, got:\n{output}"
        );
        assert!(
            output.contains("_push_values(argv, args.paths)"),
            "expected variadic positional builder, got:\n{output}"
        );
        let source_index = output
            .find("_push_values(argv, args.source)")
            .expect("source positional builder");
        let mode_index = output
            .find("if args.mode is not None:")
            .expect("mode option builder");
        let paths_index = output
            .find("_push_values(argv, args.paths)")
            .expect("variadic positional builder");
        assert!(source_index < mode_index);
        assert!(mode_index < paths_index);
    }

    #[test]
    fn sanitizes_python_reserved_namespace() {
        let cmd = Command::new("demo-tool").arg(Arg::new("input").required(true));
        let mut output = Vec::<u8>::new();
        generate(
            Python::new().namespace("None"),
            &cmd,
            "demo-tool",
            &mut output,
        )
        .expect("python generation works");
        let output = String::from_utf8(output).expect("python is utf-8");

        assert!(
            output.contains("class None_:"),
            "expected `class None_:` after reserved-word sanitization, got:\n{output}"
        );
        assert!(!output.contains("class None:"));
    }

    #[test]
    fn emits_output_contracts_when_enabled() {
        let spec = crate::CliSpec {
            bin_name: "demo".to_owned(),
            root: crate::CommandSpec {
                name: "demo".to_owned(),
                display_name: None,
                about: None,
                long_about: None,
                args: Vec::new(),
                subcommands: Vec::new(),
            },
            outputs: vec![crate::OutputSpec {
                command_path: vec!["watch".to_owned()],
                encoding: crate::OutputEncoding::JsonLines,
                mode: crate::OutputMode::Streaming,
                type_name: "WatchEvent".to_owned(),
                schema: Some(crate::OutputSchema::JsonSchema(
                    "{\"type\":\"object\"}".to_owned(),
                )),
            }],
        };

        let mut output = Vec::<u8>::new();
        crate::Generator::generate(&Python::new().output_contracts(), &spec, &mut output)
            .expect("python generation works");
        let output = String::from_utf8(output).expect("python is utf-8");

        assert!(output.contains("class OutputContract:"));
        assert!(output.contains("encoding=\"json-lines\""));
        assert!(output.contains("def parse_output("));
        assert!(output.contains("if contract.mode == \"interactive\":"));
    }
}