cli-engine 0.9.3

Rust CLI framework for consistent command modules
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
use cli_engine::{
    BuildInfo, Cli, CliConfig, CommandContext, CommandResult, CommandSpec, CredentialResolver,
    GroupSpec, Module, RuntimeCommandSpec, RuntimeGroupSpec, StreamSender,
};
use serde_json::{Value, json};

#[derive(Debug, Clone, clap::Args)]
struct GreetArgs {
    #[arg(long)]
    name: String,

    #[arg(long, default_value = "1")]
    count: u32,
}

fn greet_module() -> Module {
    Module::new("Greet", |_context| {
        RuntimeGroupSpec::new(GroupSpec::new("greet", "Greeting commands"))
            .with_command(greet_command())
    })
}

fn greet_command() -> RuntimeCommandSpec {
    RuntimeCommandSpec::new_typed::<GreetArgs, _, _, _>(
        CommandSpec::from_args::<GreetArgs>("hello", "Say hello").no_auth(true),
        async |_credential: CredentialResolver, args: GreetArgs| {
            let messages: Vec<String> = (0..args.count)
                .map(|_| format!("Hello, {}!", args.name))
                .collect();
            Ok(CommandResult::new(json!({ "messages": messages })))
        },
    )
}

fn derive_cli() -> Cli {
    Cli::new(
        CliConfig::new("derive-test", "Derive Test CLI", "derive-test")
            .with_build(BuildInfo::new("0.1.0"))
            .with_module(greet_module()),
    )
}

#[tokio::test]
async fn derive_bridge_parses_typed_args_and_returns_result() {
    let cli = derive_cli();

    let result = cli
        .run([
            "derive-test",
            "greet",
            "hello",
            "--name",
            "World",
            "--output",
            "json",
        ])
        .await;
    assert_eq!(result.exit_code, 0, "stderr: {}", result.rendered);
    let json: Value = serde_json::from_str(&result.rendered).expect("valid json");
    assert_eq!(json["data"]["messages"], json!(["Hello, World!"]));
}

#[tokio::test]
async fn derive_bridge_respects_default_values() {
    let cli = derive_cli();

    let result = cli
        .run([
            "derive-test",
            "greet",
            "hello",
            "--name",
            "Jay",
            "--count",
            "3",
            "--output",
            "json",
        ])
        .await;
    assert_eq!(result.exit_code, 0, "stderr: {}", result.rendered);
    let json: Value = serde_json::from_str(&result.rendered).expect("valid json");
    assert_eq!(
        json["data"]["messages"],
        json!(["Hello, Jay!", "Hello, Jay!", "Hello, Jay!"])
    );
}

#[tokio::test]
async fn derive_bridge_reports_missing_required_arg() {
    let cli = derive_cli();

    let result = cli.run(["derive-test", "greet", "hello"]).await;
    assert_ne!(result.exit_code, 0);
    assert!(result.rendered.contains("required"), "{}", result.rendered);
}

// --- typed_args() via new_with_context ---

#[derive(Debug, Clone, clap::Args)]
struct InfoArgs {
    #[arg(long)]
    tag: String,
}

fn context_cli() -> Cli {
    let info_command = RuntimeCommandSpec::new_with_context(
        CommandSpec::from_args::<InfoArgs>("info", "Show info").no_auth(true),
        async |context: CommandContext| {
            let args: InfoArgs = context.typed_args()?;
            Ok(CommandResult::new(json!({
                "tag": args.tag,
                "command_path": context.command_path,
            })))
        },
    );

    let module = Module::new("Context", move |_context| {
        RuntimeGroupSpec::new(GroupSpec::new("ctx", "Context commands"))
            .with_command(info_command.clone())
    });

    Cli::new(
        CliConfig::new("ctx-test", "Context Test CLI", "ctx-test")
            .with_build(BuildInfo::new("0.1.0"))
            .with_module(module),
    )
}

#[tokio::test]
async fn typed_args_works_from_new_with_context_handler() {
    let cli = context_cli();

    let result = cli
        .run([
            "ctx-test", "ctx", "info", "--tag", "hello", "--output", "json",
        ])
        .await;
    assert_eq!(result.exit_code, 0, "output: {}", result.rendered);
    let json: Value = serde_json::from_str(&result.rendered).expect("valid json");
    assert_eq!(json["data"]["tag"], "hello");
    assert_eq!(json["data"]["command_path"], "ctx:info");
}

// --- positional arguments ---

#[derive(Debug, Clone, clap::Args)]
struct EchoArgs {
    /// The message to echo.
    message: String,

    #[arg(long, default_value = "false")]
    uppercase: bool,
}

fn positional_cli() -> Cli {
    let echo_command = RuntimeCommandSpec::new_typed::<EchoArgs, _, _, _>(
        CommandSpec::from_args::<EchoArgs>("echo", "Echo a message").no_auth(true),
        async |_credential: CredentialResolver, args: EchoArgs| {
            let msg = if args.uppercase {
                args.message.to_uppercase()
            } else {
                args.message
            };
            Ok(CommandResult::new(json!({ "output": msg })))
        },
    );

    let module = Module::new("Echo", move |_context| {
        RuntimeGroupSpec::new(GroupSpec::new("util", "Utility commands"))
            .with_command(echo_command.clone())
    });

    Cli::new(
        CliConfig::new("pos-test", "Positional Test CLI", "pos-test")
            .with_build(BuildInfo::new("0.1.0"))
            .with_module(module),
    )
}

#[tokio::test]
async fn derive_bridge_handles_positional_arguments() {
    let cli = positional_cli();

    let result = cli
        .run([
            "pos-test",
            "util",
            "echo",
            "hello world",
            "--output",
            "json",
        ])
        .await;
    assert_eq!(result.exit_code, 0, "output: {}", result.rendered);
    let json: Value = serde_json::from_str(&result.rendered).expect("valid json");
    assert_eq!(json["data"]["output"], "hello world");
}

#[tokio::test]
async fn derive_bridge_handles_positional_with_flags() {
    let cli = positional_cli();

    let result = cli
        .run([
            "pos-test",
            "util",
            "echo",
            "test",
            "--uppercase",
            "--output",
            "json",
        ])
        .await;
    assert_eq!(result.exit_code, 0, "output: {}", result.rendered);
    let json: Value = serde_json::from_str(&result.rendered).expect("valid json");
    assert_eq!(json["data"]["output"], "TEST");
}

// --- flattened structs ---

#[derive(Debug, Clone, clap::Args)]
struct Pagination {
    #[arg(long, default_value = "20")]
    page_size: u32,

    #[arg(long, default_value = "0")]
    page: u32,
}

#[derive(Debug, Clone, clap::Args)]
struct SearchArgs {
    #[arg(long)]
    query: String,

    #[command(flatten)]
    pagination: Pagination,
}

fn flatten_cli() -> Cli {
    let search_command = RuntimeCommandSpec::new_typed::<SearchArgs, _, _, _>(
        CommandSpec::from_args::<SearchArgs>("find", "Search items").no_auth(true),
        async |_credential: CredentialResolver, args: SearchArgs| {
            Ok(CommandResult::new(json!({
                "query": args.query,
                "page_size": args.pagination.page_size,
                "page": args.pagination.page,
            })))
        },
    );

    let module = Module::new("Search", move |_context| {
        RuntimeGroupSpec::new(GroupSpec::new("items", "Item commands"))
            .with_command(search_command.clone())
    });

    Cli::new(
        CliConfig::new("flat-test", "Flatten Test CLI", "flat-test")
            .with_build(BuildInfo::new("0.1.0"))
            .with_module(module),
    )
}

#[tokio::test]
async fn derive_bridge_handles_flattened_structs() {
    let cli = flatten_cli();

    let result = cli
        .run([
            "flat-test",
            "items",
            "find",
            "--query",
            "rust",
            "--page-size",
            "50",
            "--output",
            "json",
        ])
        .await;
    assert_eq!(result.exit_code, 0, "output: {}", result.rendered);
    let json: Value = serde_json::from_str(&result.rendered).expect("valid json");
    assert_eq!(json["data"]["query"], "rust");
    assert_eq!(json["data"]["page_size"], 50);
    assert_eq!(json["data"]["page"], 0);
}

// --- output shorthand aliases ---

#[tokio::test]
async fn json_shorthand_flag_produces_json_output() {
    let cli = derive_cli();

    let result = cli
        .run(["derive-test", "greet", "hello", "--name", "World", "--json"])
        .await;
    assert_eq!(result.exit_code, 0, "stderr: {}", result.rendered);
    let json: Value = serde_json::from_str(&result.rendered).expect("valid json");
    assert_eq!(json["data"]["messages"], json!(["Hello, World!"]));
}

#[tokio::test]
async fn toon_shorthand_flag_produces_toon_output() {
    let cli = derive_cli();

    let result = cli
        .run(["derive-test", "greet", "hello", "--name", "World", "--toon"])
        .await;
    assert_eq!(result.exit_code, 0, "output: {}", result.rendered);
    assert!(
        !result.rendered.starts_with('{'),
        "toon output should not be raw JSON: {}",
        result.rendered
    );
}

#[tokio::test]
async fn human_shorthand_flag_produces_human_output() {
    let cli = derive_cli();

    let result = cli
        .run([
            "derive-test",
            "greet",
            "hello",
            "--name",
            "World",
            "--human",
        ])
        .await;
    assert_eq!(result.exit_code, 0, "output: {}", result.rendered);
    assert!(
        !result.rendered.starts_with('{'),
        "human output should not be raw JSON: {}",
        result.rendered
    );
}

// --- new_typed_with_context ---

#[derive(Debug, Clone, clap::Args)]
struct WhoamiArgs {
    #[arg(long)]
    tag: String,
}

fn typed_with_context_cli() -> Cli {
    let whoami_command = RuntimeCommandSpec::new_typed_with_context::<WhoamiArgs, _, _, _>(
        CommandSpec::from_args::<WhoamiArgs>("whoami", "Show tag and command path").no_auth(true),
        async |context: CommandContext, args: WhoamiArgs| {
            Ok(CommandResult::new(json!({
                "tag": args.tag,
                "command_path": context.command_path,
            })))
        },
    );

    let module = Module::new("Ident", move |_context| {
        RuntimeGroupSpec::new(GroupSpec::new("ident", "Identity commands"))
            .with_command(whoami_command.clone())
    });

    Cli::new(
        CliConfig::new("typed-ctx-test", "Typed Context Test CLI", "typed-ctx-test")
            .with_build(BuildInfo::new("0.1.0"))
            .with_module(module),
    )
}

#[tokio::test]
async fn new_typed_with_context_exposes_parsed_args_and_command_path() {
    let cli = typed_with_context_cli();

    let result = cli
        .run([
            "typed-ctx-test",
            "ident",
            "whoami",
            "--tag",
            "hello",
            "--output",
            "json",
        ])
        .await;
    assert_eq!(result.exit_code, 0, "output: {}", result.rendered);
    let json: Value = serde_json::from_str(&result.rendered).expect("valid json");
    assert_eq!(json["data"]["tag"], "hello");
    assert_eq!(json["data"]["command_path"], "ident:whoami");
}

#[tokio::test]
async fn new_typed_with_context_reports_missing_required_arg() {
    let cli = typed_with_context_cli();

    let result = cli.run(["typed-ctx-test", "ident", "whoami"]).await;
    assert_ne!(result.exit_code, 0);
    assert!(result.rendered.contains("required"), "{}", result.rendered);
}

// --- new_typed_streaming ---

#[derive(Debug, Clone, clap::Args)]
struct CountArgs {
    #[arg(long)]
    label: String,

    #[arg(long, default_value = "2")]
    count: u32,
}

fn typed_streaming_cli() -> Cli {
    let count_command = RuntimeCommandSpec::new_typed_streaming::<CountArgs, _, _>(
        CommandSpec::from_args::<CountArgs>("count", "Stream counted events").no_auth(true),
        async |_context: CommandContext, args: CountArgs, sender: StreamSender| {
            // Successful NDJSON events are written straight to real stdout by
            // the writer task (see tests/streaming.rs), so this test can't
            // capture them via `result.rendered`. Prove the typed args were
            // parsed correctly by failing loudly — into `rendered` — if they
            // don't match what was passed on the command line.
            if args.label != "tick" || args.count != 3 {
                return Err(cli_engine::CliCoreError::message(format!(
                    "unexpected parsed args: label={:?} count={}",
                    args.label, args.count
                )));
            }
            for index in 0..args.count {
                sender
                    .send(json!({ "label": args.label, "index": index }))
                    .await;
            }
            Ok(())
        },
    );

    let module = Module::new("Stream", move |_context| {
        RuntimeGroupSpec::new(GroupSpec::new("stream", "Streaming commands"))
            .with_command(count_command.clone())
    });

    Cli::new(
        CliConfig::new(
            "typed-stream-test",
            "Typed Streaming Test CLI",
            "typed-stream-test",
        )
        .with_build(BuildInfo::new("0.1.0"))
        .with_module(module),
    )
}

#[tokio::test]
async fn new_typed_streaming_parses_args_before_invoking_handler() {
    let cli = typed_streaming_cli();

    let result = cli
        .run([
            "typed-stream-test",
            "stream",
            "count",
            "--label",
            "tick",
            "--count",
            "3",
        ])
        .await;
    assert_eq!(result.exit_code, 0, "output: {}", result.rendered);
    assert!(
        result.rendered.is_empty(),
        "successful stream should produce no rendered envelope, got: {:?}",
        result.rendered
    );
}

#[tokio::test]
async fn new_typed_streaming_reports_missing_required_arg() {
    let cli = typed_streaming_cli();

    let result = cli
        .run(["typed-stream-test", "stream", "count", "--count", "3"])
        .await;
    assert_ne!(result.exit_code, 0);
    assert!(result.rendered.contains("required"), "{}", result.rendered);
}

// --- ArgGroup passthrough via from_args ---

#[derive(Debug, Clone, clap::Args)]
#[group(required = true, multiple = true)]
struct UpdateArgs {
    #[arg(long)]
    label: Option<String>,

    #[arg(long)]
    description: Option<String>,

    #[arg(long)]
    status: Option<String>,
}

fn update_cli() -> Cli {
    let update_command = RuntimeCommandSpec::new_typed::<UpdateArgs, _, _, _>(
        CommandSpec::from_args::<UpdateArgs>("update", "Update at least one field").no_auth(true),
        async |_credential: CredentialResolver, args: UpdateArgs| {
            Ok(CommandResult::new(json!({
                "label": args.label,
                "description": args.description,
                "status": args.status,
            })))
        },
    );

    let module = Module::new("Update", move |_context| {
        RuntimeGroupSpec::new(GroupSpec::new("thing", "Thing commands"))
            .with_command(update_command.clone())
    });

    Cli::new(
        CliConfig::new("group-test", "Group Test CLI", "group-test")
            .with_build(BuildInfo::new("0.1.0"))
            .with_module(module),
    )
}

#[tokio::test]
async fn arg_group_from_args_rejects_when_none_present() {
    let cli = update_cli();

    let result = cli.run(["group-test", "thing", "update"]).await;
    assert_ne!(result.exit_code, 0);
}

#[tokio::test]
async fn arg_group_from_args_accepts_exactly_one() {
    let cli = update_cli();

    let result = cli
        .run([
            "group-test",
            "thing",
            "update",
            "--label",
            "new-label",
            "--output",
            "json",
        ])
        .await;
    assert_eq!(result.exit_code, 0, "output: {}", result.rendered);
    let json: Value = serde_json::from_str(&result.rendered).expect("valid json");
    assert_eq!(json["data"]["label"], "new-label");
    assert!(json["data"]["description"].is_null());
}

#[tokio::test]
async fn arg_group_from_args_accepts_multiple_when_group_allows_multiple() {
    let cli = update_cli();

    let result = cli
        .run([
            "group-test",
            "thing",
            "update",
            "--label",
            "new-label",
            "--status",
            "ACTIVE",
            "--output",
            "json",
        ])
        .await;
    assert_eq!(result.exit_code, 0, "output: {}", result.rendered);
    let json: Value = serde_json::from_str(&result.rendered).expect("valid json");
    assert_eq!(json["data"]["label"], "new-label");
    assert_eq!(json["data"]["status"], "ACTIVE");
}

#[derive(Debug, Clone, clap::Args)]
#[group(required = true, multiple = false)]
struct VersionArgs {
    #[arg(long)]
    major: bool,

    #[arg(long)]
    minor: bool,
}

fn version_cli() -> Cli {
    let bump_command = RuntimeCommandSpec::new_typed::<VersionArgs, _, _, _>(
        CommandSpec::from_args::<VersionArgs>("bump", "Bump exactly one version component")
            .no_auth(true),
        async |_credential: CredentialResolver, args: VersionArgs| {
            Ok(CommandResult::new(
                json!({ "major": args.major, "minor": args.minor }),
            ))
        },
    );

    let module = Module::new("Version", move |_context| {
        RuntimeGroupSpec::new(GroupSpec::new("version", "Version commands"))
            .with_command(bump_command.clone())
    });

    Cli::new(
        CliConfig::new(
            "exclusive-group-test",
            "Exclusive Group Test CLI",
            "exclusive-group-test",
        )
        .with_build(BuildInfo::new("0.1.0"))
        .with_module(module),
    )
}

#[tokio::test]
async fn arg_group_from_args_rejects_mutually_exclusive_flags_together() {
    let cli = version_cli();

    let result = cli
        .run([
            "exclusive-group-test",
            "version",
            "bump",
            "--major",
            "--minor",
        ])
        .await;
    assert_ne!(result.exit_code, 0);
}

#[tokio::test]
async fn arg_group_from_args_accepts_one_of_mutually_exclusive_flags() {
    let cli = version_cli();

    let result = cli
        .run([
            "exclusive-group-test",
            "version",
            "bump",
            "--major",
            "--output",
            "json",
        ])
        .await;
    assert_eq!(result.exit_code, 0, "output: {}", result.rendered);
    let json: Value = serde_json::from_str(&result.rendered).expect("valid json");
    assert_eq!(json["data"]["major"], true);
    assert_eq!(json["data"]["minor"], false);
}