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
// Copyright (c) Meta Platforms, Inc. and affiliates.

//! Kotlin/JVM code generator: emits data classes, enum classes, argv builders,
//! and optional `ProcessBuilder` conveniences for desktop callers.
//!
//! `OutputEncoding::Json` returns the raw stdout string wrapped in
//! `ParsedOutput.Json`; callers pick a deserialization library
//! (`kotlinx.serialization`, Jackson, Moshi, ...) and a target type. The
//! TypeScript, Flow, and Python backends deserialize eagerly via the runtime's
//! built-in JSON parser instead.

use std::io::Write;
use std::io::{self};

use crate::codegen::collapse_lines;
use crate::codegen::combined_args;
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::option_token;
use crate::codegen::output_schema;
use crate::codegen::pascal_case;
use crate::codegen::quote_double;
use crate::codegen::safe_identifier;
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::OutputEncoding;
use crate::model::OutputMode;
use crate::model::ValueType;

/// Options for the Kotlin backend.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct KotlinOptions {
    /// Generated file stem, without `.kt`.
    pub module_name: Option<String>,
    /// Optional Kotlin package declaration.
    pub package_name: Option<String>,
    /// Output-contract metadata and parser helpers.
    pub output_contracts: OutputContractGeneration,
}

/// Generate dependency-free Kotlin/JVM command builders for desktop clients.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Kotlin {
    options: KotlinOptions,
}

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

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

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

    /// Set a Kotlin package declaration.
    #[must_use]
    pub fn package_name(mut self, package_name: impl Into<String>) -> Self {
        self.options.package_name = Some(package_name.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 Kotlin {
    fn file_name(&self, bin_name: &str) -> String {
        let stem = self
            .options
            .module_name
            .as_deref()
            .map(pascal_case)
            .unwrap_or_else(|| format!("{}Bindings", pascal_case(bin_name)));
        format!("{stem}.kt")
    }

    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())
    }
}

fn render_module(spec: &CliSpec, options: &KotlinOptions, output: &mut String) {
    output.push_str("// Generated by clap_types. Do not edit by hand.\n");
    if let Some(package_name) = options.package_name.as_deref() {
        output.push_str(&format!("package {package_name}\n\n"));
    }
    output.push_str(&format!(
        "const val PROGRAM: String = {}\n\n",
        quote_double(&spec.bin_name)
    ));
    render_invocation(output);
    render_helpers(output);
    if options.output_contracts.is_enabled() {
        render_output_contracts(spec, output);
    }

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

fn render_invocation(output: &mut String) {
    output.push_str(
        r#"data class CommandInvocation(
    val program: String,
    val args: List<String>,
) {
    fun argv(): List<String> = listOf(program) + args

    fun processBuilder(): ProcessBuilder = ProcessBuilder(argv())

    fun start(): Process = processBuilder().start()
}

"#,
    );
}

fn render_helpers(output: &mut String) {
    output.push_str(
        r#"private fun stringify(value: Any): String =
    when (value) {
        is Boolean -> if (value) "true" else "false"
        else -> value.toString()
    }

private fun pushOne(argv: MutableList<String>, value: Any) {
    argv.add(stringify(value))
}

private fun pushValues(argv: MutableList<String>, values: Iterable<*>, name: String) {
    for (value in values) {
        val item = requireNotNull(value) { "CLI argument '$name' values cannot be null" }
        pushOne(argv, item)
    }
}

private fun pushOption(argv: MutableList<String>, option: String, value: Any) {
    argv.add(option)
    pushOne(argv, value)
}

private fun pushOptionValues(argv: MutableList<String>, option: String, values: Iterable<*>) {
    argv.add(option)
    pushValues(argv, values, option)
}

private fun pushRepeatedOption(argv: MutableList<String>, option: String, values: Iterable<*>) {
    for (value in values) {
        val item = requireNotNull(value) { "CLI option '$option' values cannot be null" }
        pushOption(argv, option, item)
    }
}

private fun pushGroupedRepeatedOption(
    argv: MutableList<String>,
    option: String,
    groups: Iterable<Iterable<*>>,
) {
    for (group in groups) {
        pushOptionValues(argv, option, group)
    }
}

"#,
    );
}

fn render_output_contracts(spec: &CliSpec, output: &mut String) {
    output.push_str(
        r#"enum class OutputEncoding {
    JSON,
    JSON_LINES,
    TEXT,
}

enum class OutputMode {
    BUFFERED,
    STREAMING,
    INTERACTIVE,
}

data class OutputContract(
    val commandPath: List<String>,
    val encoding: OutputEncoding,
    val mode: OutputMode,
    val typeName: String,
    val schema: String? = null,
)

sealed class ParsedOutput {
    data class Json(val value: String) : ParsedOutput()
    data class JsonLines(val values: List<String>) : ParsedOutput()
    data class Text(val value: String) : ParsedOutput()
    data class Interactive(val value: String) : ParsedOutput()
}

val OUTPUT_CONTRACTS: List<OutputContract> = listOf(
"#,
    );
    for contract in &spec.outputs {
        output.push_str("    OutputContract(\n");
        output.push_str(&format!(
            "        commandPath = listOf({}),\n",
            contract
                .command_path
                .iter()
                .map(|piece| quote_double(piece))
                .collect::<Vec<_>>()
                .join(", ")
        ));
        output.push_str(&format!(
            "        encoding = OutputEncoding.{},\n",
            kotlin_output_encoding(contract.encoding)
        ));
        output.push_str(&format!(
            "        mode = OutputMode.{},\n",
            kotlin_output_mode(contract.mode)
        ));
        output.push_str(&format!(
            "        typeName = {},\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#")

fun parseOutput(contract: OutputContract, stdout: String): ParsedOutput {
    if (contract.mode == OutputMode.INTERACTIVE) {
        return ParsedOutput.Interactive(stdout)
    }

    return when (contract.encoding) {
        OutputEncoding.JSON -> ParsedOutput.Json(stdout)
        OutputEncoding.JSON_LINES ->
            ParsedOutput.JsonLines(stdout.lineSequence().filter { it.isNotEmpty() }.toList())
        OutputEncoding.TEXT -> ParsedOutput.Text(stdout)
    }
}

"#,
    );
}

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

    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);
        path.pop();
    }
}

fn render_command(
    command: &CommandSpec,
    path: &[String],
    inherited: &[&ArgSpec],
    output: &mut String,
) {
    let type_prefix = command_type_prefix(command, path);
    let args_name = format!("{type_prefix}Args");
    let build_fn = format!("build{type_prefix}Command");
    let command_fn = format!("{}Command", lower_camel(&type_prefix));
    let all_args = combined_args(inherited, command);
    let has_required = all_args.iter().copied().any(is_required);

    render_enums(&type_prefix, &all_args, output);
    render_args_class(command, &type_prefix, &args_name, &all_args, output);
    output.push('\n');
    render_build_function(
        command,
        path,
        inherited,
        &args_name,
        &build_fn,
        has_required,
        output,
    );
    output.push('\n');
    let args_param = if has_required {
        format!("args: {args_name}")
    } else {
        format!("args: {args_name} = {args_name}()")
    };
    output.push_str(&format!(
        "fun {command_fn}({args_param}, program: String = PROGRAM): CommandInvocation =\n"
    ));
    output.push_str(&format!(
        "    CommandInvocation(program, {build_fn}(args))\n\n"
    ));
}

fn render_enums(type_prefix: &str, args: &[&ArgSpec], output: &mut String) {
    for &arg in args {
        if arg.possible_values.is_empty() {
            continue;
        }
        let enum_name = enum_name(type_prefix, arg);
        output.push_str(&format!(
            "enum class {enum_name}(private val wireValue: String) {{\n"
        ));
        for value in &arg.possible_values {
            output.push_str(&format!(
                "    {}({}),\n",
                enum_variant(&value.name),
                quote_double(&value.name)
            ));
        }
        output.push_str("    ;\n\n");
        output.push_str("    override fun toString(): String = wireValue\n");
        output.push_str("}\n\n");
    }
}

fn render_args_class(
    command: &CommandSpec,
    type_prefix: &str,
    args_name: &str,
    args: &[&ArgSpec],
    output: &mut String,
) {
    if let Some(doc) = command.about.as_deref().or(command.long_about.as_deref()) {
        output.push_str(&format!("/** {} */\n", doc_comment(doc)));
    }

    if args.is_empty() {
        output.push_str(&format!("class {args_name}\n"));
        return;
    }

    output.push_str(&format!("data class {args_name}(\n"));
    for arg in sorted_constructor_args(args) {
        for comment in field_comments(arg) {
            output.push_str(&format!("    /** {comment} */\n"));
        }
        output.push_str(&format!(
            "    val {}: {}{},\n",
            property_name(arg),
            kotlin_type(type_prefix, arg),
            kotlin_default(arg)
        ));
    }
    output.push_str(")\n");
}

fn sorted_constructor_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,
) {
    let args_param = if has_required {
        format!("args: {args_name}")
    } else {
        format!("args: {args_name} = {args_name}()")
    };
    output.push_str(&format!("fun {build_fn}({args_param}): List<String> {{\n"));
    output.push_str("    val argv = mutableListOf<String>()\n");

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

    for token in path {
        output.push_str(&format!("    argv.add({})\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 argv\n");
    output.push_str("}\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}) {{\n"));
                output.push_str(&format!("        argv.add({})\n", quote_double(&option)));
                output.push_str("    }\n");
            }
        }
        ArgKind::FlagFalse => {
            if let Some(option) = option {
                output.push_str(&format!("    if ({accessor} == false) {{\n"));
                output.push_str(&format!("        argv.add({})\n", quote_double(&option)));
                output.push_str("    }\n");
            }
        }
        ArgKind::Counter => {
            if let Some(option) = option {
                output.push_str(&format!("    repeat({accessor}) {{\n"));
                output.push_str(&format!("        argv.add({})\n", quote_double(&option)));
                output.push_str("    }\n");
            }
        }
        ArgKind::Option => {
            if let Some(option) = option {
                render_value_builder(arg, &accessor, &option, true, output);
            }
        }
        ArgKind::Positional => render_value_builder(arg, &accessor, "", false, output),
    }
}

fn render_value_builder(
    arg: &ArgSpec,
    accessor: &str,
    option: &str,
    is_option: bool,
    output: &mut String,
) {
    let option = quote_double(option);
    if is_grouped_repeated(arg) {
        if is_option {
            output.push_str(&format!("    if ({accessor}.isNotEmpty()) {{\n"));
            output.push_str(&format!(
                "        pushGroupedRepeatedOption(argv, {option}, {accessor})\n"
            ));
            output.push_str("    }\n");
        } else {
            output.push_str(&format!("    for (group in {accessor}) {{\n"));
            output.push_str(&format!(
                "        pushValues(argv, group, {})\n",
                quote_double(&arg.id)
            ));
            output.push_str("    }\n");
        }
    } else if arg.value.repeated || arg.value.arity.allows_multiple() {
        if is_option {
            output.push_str(&format!("    if ({accessor}.isNotEmpty()) {{\n"));
            if arg.value.repeated {
                output.push_str(&format!(
                    "        pushRepeatedOption(argv, {option}, {accessor})\n"
                ));
            } else {
                output.push_str(&format!(
                    "        pushOptionValues(argv, {option}, {accessor})\n"
                ));
            }
            output.push_str("    }\n");
        } else {
            output.push_str(&format!(
                "    pushValues(argv, {accessor}, {})\n",
                quote_double(&arg.id)
            ));
        }
    } else if is_required(arg) {
        if is_option {
            output.push_str(&format!("    pushOption(argv, {option}, {accessor})\n"));
        } else {
            output.push_str(&format!("    pushOne(argv, {accessor})\n"));
        }
    } else if is_option {
        output.push_str(&format!("    {accessor}?.let {{ value ->\n"));
        output.push_str(&format!("        pushOption(argv, {option}, value)\n"));
        output.push_str("    }\n");
    } else {
        output.push_str(&format!("    {accessor}?.let {{ value ->\n"));
        output.push_str("        pushOne(argv, value)\n");
        output.push_str("    }\n");
    }
}

fn kotlin_type(type_prefix: &str, arg: &ArgSpec) -> String {
    let scalar = kotlin_scalar_type(type_prefix, arg);
    match arg.kind {
        ArgKind::FlagTrue => "Boolean".to_owned(),
        ArgKind::FlagFalse => "Boolean?".to_owned(),
        ArgKind::Counter => "Int".to_owned(),
        ArgKind::Option | ArgKind::Positional => {
            if is_grouped_repeated(arg) {
                format!("List<List<{scalar}>>")
            } else if arg.value.repeated || arg.value.arity.allows_multiple() {
                format!("List<{scalar}>")
            } else if is_required(arg) {
                scalar
            } else {
                format!("{scalar}?")
            }
        }
    }
}

fn kotlin_scalar_type(type_prefix: &str, arg: &ArgSpec) -> String {
    if !arg.possible_values.is_empty() {
        return enum_name(type_prefix, arg);
    }

    match arg.kind {
        ArgKind::FlagTrue | ArgKind::FlagFalse => "Boolean".to_owned(),
        ArgKind::Counter => "Int".to_owned(),
        ArgKind::Option | ArgKind::Positional => match arg.value.ty {
            ValueType::Bool => "Boolean".to_owned(),
            ValueType::Integer => "Int".to_owned(),
            ValueType::BigInteger => "Long".to_owned(),
            ValueType::Float => "Double".to_owned(),
            ValueType::Unknown | ValueType::String | ValueType::OsString | ValueType::Path => {
                "String".to_owned()
            }
        },
    }
}

fn kotlin_default(arg: &ArgSpec) -> &'static str {
    match arg.kind {
        ArgKind::FlagTrue => " = false",
        ArgKind::FlagFalse => " = null",
        ArgKind::Counter => " = 0",
        ArgKind::Option | ArgKind::Positional => {
            if is_grouped_repeated(arg) || arg.value.repeated || arg.value.arity.allows_multiple() {
                if is_required(arg) {
                    ""
                } else {
                    " = emptyList()"
                }
            } else if is_required(arg) {
                ""
            } else {
                " = null"
            }
        }
    }
}

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

fn enum_name(type_prefix: &str, arg: &ArgSpec) -> String {
    format!("{type_prefix}{}", pascal_case(&arg.id))
}

fn enum_variant(value: &str) -> String {
    safe_identifier(&pascal_case(value), is_reserved)
}

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(doc_comment(help));
    }
    if !arg.defaults.is_empty() {
        comments.push(format!("Clap default: {}.", arg.defaults.join(", ")));
    }
    comments
}

fn lower_camel(input: &str) -> String {
    let pascal = pascal_case(input);
    let mut chars = pascal.chars();
    match chars.next() {
        Some(first) => format!("{}{}", first.to_ascii_lowercase(), chars.as_str()),
        None => "value".to_owned(),
    }
}

fn doc_comment(input: &str) -> String {
    collapse_lines(input).replace("*/", "* /")
}

fn kotlin_output_encoding(encoding: OutputEncoding) -> &'static str {
    match encoding {
        OutputEncoding::Json => "JSON",
        OutputEncoding::JsonLines => "JSON_LINES",
        OutputEncoding::Text => "TEXT",
    }
}

fn kotlin_output_mode(mode: OutputMode) -> &'static str {
    match mode {
        OutputMode::Buffered => "BUFFERED",
        OutputMode::Streaming => "STREAMING",
        OutputMode::Interactive => "INTERACTIVE",
    }
}

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

const RESERVED: &[&str] = &[
    "as",
    "break",
    "class",
    "continue",
    "do",
    "else",
    "false",
    "for",
    "fun",
    "if",
    "in",
    "interface",
    "is",
    "null",
    "object",
    "package",
    "return",
    "super",
    "this",
    "throw",
    "true",
    "try",
    "typealias",
    "typeof",
    "val",
    "var",
    "when",
    "while",
];

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

    use crate::Kotlin;
    use crate::generate;

    #[test]
    fn generates_typed_kotlin_builders() {
        let cmd = Command::new("demo-tool")
            .arg(Arg::new("verbose").short('v').action(ArgAction::Count))
            .subcommand(
                Command::new("run")
                    .arg(Arg::new("target").required(true))
                    .arg(Arg::new("mode").long("mode").value_parser(["fast", "slow"]))
                    .arg(
                        Arg::new("threads")
                            .long("threads")
                            .value_parser(value_parser!(u16)),
                    ),
            );

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

        assert!(output.contains("const val PROGRAM: String = \"demo-tool\""));
        assert!(output.contains("data class RunArgs("));
        assert!(output.contains("enum class RunMode"));
        assert!(output.contains("val target: String,"));
        assert!(output.contains("val threads: Int? = null,"));
        assert!(output.contains("argv.add(\"run\")"));
        assert!(output.contains("pushOption(argv, \"--threads\", value)"));
    }

    #[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(&Kotlin::new().output_contracts(), &spec, &mut output)
            .expect("kotlin generation works");
        let output = String::from_utf8(output).expect("kotlin is utf-8");

        assert!(output.contains("val OUTPUT_CONTRACTS: List<OutputContract>"));
        assert!(output.contains("encoding = OutputEncoding.JSON_LINES"));
        assert!(output.contains("fun parseOutput("));
    }

    #[test]
    fn variadic_positionals_emit_required_lists_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(Kotlin::new(), &cmd, "demo-tool", &mut output).expect("kotlin generation works");
        let output = String::from_utf8(output).expect("kotlin is utf-8");

        assert!(
            output.contains("val paths: List<String>,"),
            "expected variadic positional list type, got:\n{output}"
        );
        assert!(
            output.contains("pushValues(argv, args.paths, \"paths\")"),
            "expected variadic positional builder, got:\n{output}"
        );
        let source_index = output
            .find("pushOne(argv, args.source)")
            .expect("source positional builder");
        let mode_index = output.find("args.mode?.let").expect("mode option builder");
        let paths_index = output
            .find("pushValues(argv, args.paths, \"paths\")")
            .expect("variadic positional builder");
        assert!(source_index < mode_index);
        assert!(mode_index < paths_index);
    }
}