clish 0.1.0-beta.5

Elegant CLI framework for Rust.
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
use clish::parse::CommandEntry;
use clish::prelude::*;

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

fn find_cmd(name: &str) -> &'static CommandEntry {
    clish::inventory::iter::<CommandEntry>()
        .find(|c| c.name == name)
        .unwrap_or_else(|| panic!("command '{name}' not found in inventory"))
}

fn run_ok(name: &str, args: &[&str]) {
    let cmd = find_cmd(name);
    let args: Vec<String> = args.iter().map(|s| s.to_string()).collect();
    let result = (cmd.run)(&args);
    assert!(
        result.is_ok(),
        "run('{name}', {args:?}) expected Ok, got Err: {:?}",
        result
    );
}

fn run_err(name: &str, args: &[&str]) -> String {
    let cmd = find_cmd(name);
    let args: Vec<String> = args.iter().map(|s| s.to_string()).collect();
    let result = (cmd.run)(&args);
    match result {
        Err(e) => e,
        Ok(()) => panic!("run('{name}', {args:?}) expected Err, got Ok"),
    }
}

// ---------------------------------------------------------------------------
// Test commands
// ---------------------------------------------------------------------------

#[command(help = "Required positional and named args")]
fn cmd_required(name: Pos<String>, greeting: Named<String>) {
    assert_eq!(name, "world");
    assert_eq!(greeting, "hello");
}

#[command(help = "Optional positional")]
fn cmd_optional_pos(name: Pos<Option<String>>) {
    assert_eq!(name, Some("alice".to_string()));
}

#[command(help = "Optional positional with None")]
fn cmd_optional_pos_none(name: Pos<Option<String>>) {
    assert_eq!(name, None);
}

#[command(help = "Variadic positional")]
fn cmd_variadic(files: Pos<Vec<String>>) {
    assert_eq!(files, vec!["a", "b", "c"]);
}

#[command(help = "Empty variadic")]
fn cmd_empty_variadic(files: Pos<Vec<String>>) {
    assert!(files.is_empty());
}

#[command(help = "Optional named")]
fn cmd_optional_named(name: Named<Option<String>>) {
    assert_eq!(name, Some("bob".to_string()));
}

#[command(help = "Optional named absent")]
fn cmd_optional_named_absent(name: Named<Option<String>>) {
    assert_eq!(name, None);
}

#[command(help = "Repeatable named")]
fn cmd_repeatable(tag: Named<Vec<String>>) {
    assert_eq!(tag, vec!["x", "y", "z"]);
}

#[command(help = "Empty repeatable named")]
fn cmd_empty_repeatable(tag: Named<Vec<String>>) {
    assert!(tag.is_empty());
}

#[command(help = "Flag")]
fn cmd_flag(verbose: bool) {
    assert!(verbose);
}

#[command(help = "Flag absent")]
fn cmd_flag_absent(verbose: bool) {
    assert!(!verbose);
}

#[command(help = "Parsed types")]
fn cmd_parsed(count: Pos<u32>, port: Named<u16>) {
    assert_eq!(count, 42);
    assert_eq!(port, 8080);
}

#[command(
    help = "Short alias",
    param(verbose, help = "Verbose output", short = 'v')
)]
fn cmd_short(name: Pos<String>, verbose: Named<String>) {
    assert_eq!(name, "foo");
    assert_eq!(verbose, "yes");
}

#[command(help = "Custom name")]
fn cmd_custom(cli_name: Pos<String>) {
    assert_eq!(cli_name, "bar");
}

#[command(
    help = "Default fallback",
    param(name, help = "Name", default = "default_val")
)]
fn cmd_default(name: Named<String>) {
    assert_eq!(name, "default_val");
}

#[command(
    help = "Env fallback",
    param(name, help = "Name", env = "CLISH_TEST_ENV_FALLBACK_VAR")
)]
fn cmd_env(name: Named<String>) {
    assert_eq!(name, "from_env");
}

#[command(
    help = "Choices validation",
    param(mode, help = "Operating mode", choices = ["fast", "slow", "auto"])
)]
fn cmd_choices(mode: Named<String>) {
    assert_eq!(mode, "fast");
}

#[command(
    help = "Conflicts",
    param(verbose, help = "Verbose", short = 'v'),
    param(quiet, help = "Quiet", short = 'q', conflicts_with = ["verbose"])
)]
fn cmd_conflicts(verbose: bool, quiet: bool) {
    // Only one of them should be possible; we test conflict via run_err
    assert!(verbose || quiet);
}

#[command(
    help = "Requires",
    param(host, help = "Host"),
    param(port, help = "Port", requires = ["host"])
)]
fn cmd_requires(host: Named<String>, port: Named<u16>) {
    assert_eq!(host, "localhost");
    assert_eq!(port, 3000);
}

#[command(
    help = "Hidden param",
    param(secret, help = "Secret value", hide = true)
)]
fn cmd_hidden_param(name: Pos<String>, secret: Named<String>) {
    assert_eq!(name, "visible");
    assert_eq!(secret, "shh");
}

#[command(help = "Placeholder")]
fn cmd_placeholder(name: Pos<String>) {
    assert_eq!(name, "test");
}

#[command(help = "Value hint")]
fn cmd_value_hint(path: Pos<String>) {
    assert_eq!(path, "/tmp");
}

#[command(
    help = "Custom cli name via param",
    param(_x, help = "Custom name", name = "custom-name")
)]
fn cmd_param_name(_x: Named<String>) {
    // accessed via --custom-name
}

/// Short help from doc comment
///
/// Long details from doc comment
#[command]
fn cmd_doc_comment(name: Pos<String>) {
    assert_eq!(name, "doc");
}

#[command(
    help = "Aliases",
    aliases = ["al1", "al2"]
)]
fn cmd_aliases(name: Pos<String>) {
    assert_eq!(name, "alias_target");
}

#[command(help = "Hidden command", hidden = true)]
fn cmd_hidden_cmd(name: Pos<String>) {
    assert_eq!(name, "hidden_ok");
}

#[command(
    help = "Deprecated command",
    deprecated = true,
    deprecation_note = "use cmd_active instead"
)]
fn cmd_deprecated_cmd(name: Pos<String>) {
    assert_eq!(name, "dep_ok");
}

#[command(help = "Name override", name = "renamed-command")]
fn original_name(name: Pos<String>) {
    assert_eq!(name, "via_rename");
}

#[command(help = "Equals form parsing")]
fn cmd_equals(value: Named<String>) {
    assert_eq!(value, "direct");
}

#[command(
    help = "Bundled flags",
    param(a, short = 'a'),
    param(b, short = 'b'),
    param(c, short = 'c')
)]
fn cmd_bundled(a: bool, b: bool, c: bool) {
    assert!(a);
    assert!(b);
    assert!(c);
}

#[command(help = "Double dash separator")]
fn cmd_double_dash(files: Pos<Vec<String>>) {
    assert_eq!(files, vec!["a", "--b", "c"]);
}

#[command(help = "Named with default and no value")]
fn cmd_named_default_absent(_name: Named<String>) {}

// ===========================================================================
// Tests: Command metadata
// ===========================================================================

#[test]
fn test_cmd_metadata_help_from_attribute() {
    let cmd = find_cmd("cmd_required");
    assert_eq!(cmd.help, "Required positional and named args");
}

#[test]
fn test_cmd_metadata_help_from_doc_comment() {
    let cmd = find_cmd("cmd_doc_comment");
    assert_eq!(cmd.help, "Short help from doc comment");
    assert_eq!(cmd.details, "Long details from doc comment");
}

#[test]
fn test_cmd_metadata_name_override() {
    let cmd = find_cmd("renamed-command");
    assert!(cmd.name == "renamed-command");
    // The original function name should NOT be in inventory
    assert!(
        clish::inventory::iter::<CommandEntry>()
            .find(|c| c.name == "original_name")
            .is_none(),
        "original_name should not be in inventory when name= is used"
    );
}

#[test]
fn test_cmd_metadata_aliases() {
    let cmd = find_cmd("cmd_aliases");
    assert_eq!(cmd.aliases, &["al1", "al2"]);
}

#[test]
fn test_cmd_metadata_hidden() {
    let cmd = find_cmd("cmd_hidden_cmd");
    assert!(cmd.hidden);
}

#[test]
fn test_cmd_metadata_not_hidden() {
    // A normal command should not be hidden
    let cmd = find_cmd("cmd_required");
    assert!(!cmd.hidden);
}

#[test]
fn test_cmd_metadata_deprecated() {
    let cmd = find_cmd("cmd_deprecated_cmd");
    assert!(cmd.deprecated);
    assert_eq!(cmd.deprecation_note, "use cmd_active instead");
}

#[test]
fn test_cmd_metadata_not_deprecated() {
    let cmd = find_cmd("cmd_required");
    assert!(!cmd.deprecated);
    assert!(cmd.deprecation_note.is_empty());
}

// ===========================================================================
// Tests: Runtime parsing - success paths
// ===========================================================================

#[test]
fn test_required_positional_and_named() {
    run_ok("cmd_required", &["world", "--greeting", "hello"]);
}

#[test]
fn test_optional_positional_present() {
    run_ok("cmd_optional_pos", &["alice"]);
}

#[test]
fn test_optional_positional_absent() {
    run_ok("cmd_optional_pos_none", &[]);
}

#[test]
fn test_variadic_positional() {
    run_ok("cmd_variadic", &["a", "b", "c"]);
}

#[test]
fn test_variadic_positional_empty() {
    run_ok("cmd_empty_variadic", &[]);
}

#[test]
fn test_optional_named_present() {
    run_ok("cmd_optional_named", &["--name", "bob"]);
}

#[test]
fn test_optional_named_absent() {
    run_ok("cmd_optional_named_absent", &[]);
}

#[test]
fn test_repeatable_named() {
    run_ok(
        "cmd_repeatable",
        &["--tag", "x", "--tag", "y", "--tag", "z"],
    );
}

#[test]
fn test_repeatable_named_empty() {
    run_ok("cmd_empty_repeatable", &[]);
}

#[test]
fn test_flag_present() {
    run_ok("cmd_flag", &["--verbose"]);
}

#[test]
fn test_flag_absent() {
    run_ok("cmd_flag_absent", &[]);
}

#[test]
fn test_parsed_types() {
    run_ok("cmd_parsed", &["42", "--port", "8080"]);
}

#[test]
fn test_short_alias_named() {
    run_ok("cmd_short", &["foo", "-v", "yes"]);
}

// NOTE: -nvalue (attached) form for named options is not currently supported
// by the parser; only bundled flags are supported for multi-char short args.

#[test]
fn test_equals_form() {
    run_ok("cmd_equals", &["--value=direct"]);
}

#[test]
fn test_custom_param_name() {
    run_ok("cmd_param_name", &["--custom-name", "val"]);
}

// ===========================================================================
// Tests: param() attribute features
// ===========================================================================

#[test]
fn test_param_short() {
    run_ok("cmd_short", &["foo", "-v", "yes"]);
}

#[test]
fn test_param_default() {
    run_ok("cmd_default", &[]);
}

#[test]
fn test_param_env() {
    const ENV_VAR: &str = "CLISH_TEST_ENV_FALLBACK_VAR";
    // SAFETY: test-only env var manipulation
    unsafe { std::env::set_var(ENV_VAR, "from_env") };
    // Call cmd_env without --name, it should pick up value from env var
    run_ok("cmd_env", &[]);
    unsafe { std::env::remove_var(ENV_VAR) };
}

#[test]
fn test_param_choices_valid() {
    run_ok("cmd_choices", &["--mode", "fast"]);
}

#[test]
fn test_param_choices_invalid() {
    let err = run_err("cmd_choices", &["--mode", "invalid"]);
    assert!(
        err.contains("invalid choice") || err.contains("expected one of"),
        "got: {err}"
    );
}

#[test]
fn test_param_choices_via_short() {
    run_ok("cmd_short", &["foo", "-v", "yes"]);
}

#[test]
fn test_param_hidden_still_works() {
    // A hidden parameter should still be functional at runtime
    run_ok("cmd_hidden_param", &["visible", "--secret", "shh"]);
}

#[test]
fn test_param_placeholder_appears_in_help() {
    // Placeholder is a help-text feature, verify it's stored
    let cmd = find_cmd("cmd_placeholder");
    let param = cmd.params.iter().find(|p| p.name == "name").unwrap();
    assert_eq!(param.placeholder, "");
    // The basic cmd_placeholder has no placeholder set, so it should be empty
}

// ===========================================================================
// Tests: Parsing forms
// ===========================================================================

#[test]
fn test_long_equals() {
    run_ok("cmd_equals", &["--value=direct"]);
}

#[test]
fn test_long_space() {
    run_ok("cmd_equals", &["--value", "direct"]);
}

#[test]
fn test_short_space() {
    run_ok("cmd_short", &["foo", "-v", "yes"]);
}

#[test]
fn test_bundled_flags() {
    run_ok("cmd_bundled", &["-abc"]);
}

#[test]
fn test_double_dash() {
    run_ok("cmd_double_dash", &["--", "a", "--b", "c"]);
}

#[test]
fn test_double_dash_with_positional_before() {
    run_ok("cmd_double_dash", &["a", "--", "--b", "c"]);
}

// ===========================================================================
// Tests: Error paths
// ===========================================================================

#[test]
fn test_missing_required_positional() {
    let err = run_err("cmd_required", &["--greeting", "hello"]);
    assert!(
        err.contains("missing argument") || err.contains("MissingArgument"),
        "got: {err}"
    );
}

#[test]
fn test_missing_required_named() {
    let err = run_err("cmd_required", &["world"]);
    // Since greeting is required, missing it should produce an error
    assert!(err.contains("missing value"), "got: {err}");
}

#[test]
fn test_unknown_option() {
    let err = run_err(
        "cmd_required",
        &["world", "--greeting", "hello", "--unknown"],
    );
    assert!(err.contains("unknown option"), "got: {err}");
}

#[test]
fn test_unknown_short_flag() {
    let err = run_err("cmd_required", &["world", "--greeting", "hello", "-Z"]);
    assert!(err.contains("unknown option"), "got: {err}");
}

#[test]
fn test_invalid_value_parse() {
    let err = run_err("cmd_parsed", &["42", "--port", "not_a_number"]);
    assert!(
        err.contains("invalid value") || err.contains("invalid"),
        "got: {err}"
    );
}

#[test]
fn test_invalid_positional_parse() {
    let err = run_err("cmd_parsed", &["not_a_number", "--port", "8080"]);
    assert!(
        err.contains("invalid value") || err.contains("invalid"),
        "got: {err}"
    );
}

#[test]
fn test_conflicts_error() {
    let err = run_err("cmd_conflicts", &["-v", "-q"]);
    assert!(
        err.contains("conflict") || err.contains("cannot be used together"),
        "got: {err}"
    );
}

#[test]
fn test_requires_error() {
    // port requires host, but host is missing
    let err = run_err("cmd_requires", &["--port", "3000"]);
    assert!(err.contains("requires"), "got: {err}");
}

#[test]
fn test_missing_value_for_named() {
    // --port with no value at end
    let err = run_err("cmd_parsed", &["42", "--port"]);
    assert!(err.contains("missing value"), "got: {err}");
}

#[test]
fn test_empty_variadic() {
    run_ok("cmd_empty_variadic", &[]);
}

// ===========================================================================
// Tests: ParamEntry metadata from CommandEntry
// ===========================================================================

#[test]
fn test_param_entry_kinds() {
    let cmd = find_cmd("cmd_required");
    assert_eq!(cmd.params[0].kind, "positional");
    assert_eq!(cmd.params[1].kind, "named");
}

#[test]
fn test_param_entry_flag_kind() {
    let cmd = find_cmd("cmd_flag");
    assert_eq!(cmd.params[0].kind, "flag");
}

#[test]
fn test_param_entry_optional_positional_kind() {
    let cmd = find_cmd("cmd_optional_pos");
    assert_eq!(cmd.params[0].kind, "positional_optional");
}

#[test]
fn test_param_entry_variadic_kind() {
    let cmd = find_cmd("cmd_variadic");
    assert_eq!(cmd.params[0].kind, "positional_variadic");
}

#[test]
fn test_param_entry_optional_named_kind() {
    let cmd = find_cmd("cmd_optional_named");
    assert_eq!(cmd.params[0].kind, "named");
}

#[test]
fn test_param_entry_repeatable_kind() {
    let cmd = find_cmd("cmd_repeatable");
    assert_eq!(cmd.params[0].kind, "named");
}

#[test]
fn test_param_entry_short() {
    let cmd = find_cmd("cmd_short");
    // The verbose param should have short = 'v'
    let verbose = cmd.params.iter().find(|p| p.name == "verbose").unwrap();
    assert_eq!(verbose.short, 'v');
}

#[test]
fn test_param_entry_no_short() {
    let cmd = find_cmd("cmd_required");
    let greeting = cmd.params.iter().find(|p| p.name == "greeting").unwrap();
    assert_eq!(greeting.short, '\0');
}

// ===========================================================================
// Tests: Command name remapping
// ===========================================================================

#[test]
fn test_name_override_in_inventory() {
    // The command with `name = "renamed-command"` should be findable by that name
    let cmd = find_cmd("renamed-command");
    assert_eq!(cmd.name, "renamed-command");
}

// ===========================================================================
// Tests: Oneshot mode
// ===========================================================================

// Oneshot commands need special handling since they require app!(cmd) syntax.
// We test the CommandEntry directly instead.

#[test]
fn test_oneshot_compatible_command() {
    // A command without name, aliases, hidden, or deprecated is oneshot-compatible
    let cmd = find_cmd("cmd_required");
    assert_eq!(cmd.name, "cmd_required");
    assert!(cmd.aliases.is_empty());
    assert!(!cmd.hidden);
    assert!(!cmd.deprecated);
}

// ===========================================================================
// Tests: Deprecation warning (handled via eprintln in the run closure)
// ===========================================================================

#[test]
fn test_deprecated_command_still_runs() {
    run_ok("cmd_deprecated_cmd", &["dep_ok"]);
}

// ===========================================================================
// Tests: Short flags and bundled short flags
// ===========================================================================

// --- Commands used for short/bundled testing ---

#[command(
    help = "Mixed flags and named options",
    param(x, short = 'x'),
    param(y, short = 'y'),
    param(z, short = 'z')
)]
fn cmd_xyz_flags(x: bool, y: bool, z: bool) {
    assert!(x, "expected x=true");
    assert!(y, "expected y=true");
    assert!(z, "expected z=true");
}

#[command(
    help = "Partial flags",
    param(a, short = 'a'),
    param(b, short = 'b'),
    param(c, short = 'c')
)]
fn cmd_partial_flags(a: bool, b: bool, c: bool) {
    assert!(a, "expected a=true");
    assert!(b, "expected b=true");
    assert!(!c, "expected c=false");
}

// --- Tests ---

#[test]
fn test_bundled_three_flags() {
    // -xyz sets all three flags at once
    run_ok("cmd_xyz_flags", &["-xyz"]);
}

#[test]
fn test_bundled_reverse_order() {
    // -zyx should also work (order within bundle does not matter)
    let cmd = find_cmd("cmd_xyz_flags");
    let args: Vec<String> = vec!["-zyx"].iter().map(|s| s.to_string()).collect();
    let result = (cmd.run)(&args);
    assert!(result.is_ok(), "bundled -zyx failed: {result:?}");
}

#[test]
fn test_bundled_partial_flags() {
    // Only a and b, not c
    run_ok("cmd_partial_flags", &["-ab"]);
}

#[test]
fn test_bundled_flag_with_unknown_char_fails() {
    let err = run_err("cmd_xyz_flags", &["-xyzQ"]);
    assert!(
        err.contains("unknown option"),
        "expected unknown option error, got: {err}"
    );
}

#[test]
fn test_bundled_flag_named_option_in_bundle_fails() {
    // cmd_bundled has only flags (-a -b -c). If we use a command that has a
    // named option with a short alias, bundling it with flags should fail
    // because the parser rejects named-option shorts inside bundles.
    // Re-use cmd_short which has param(verbose, short = 'v') as Named<String>.
    let err = run_err("cmd_short", &["foo", "-av"]);
    // -a is unknown here, but -v is a named option short inside bundle -> MissingValue
    assert!(
        err.contains("missing value") || err.contains("unknown option"),
        "expected missing value or unknown option error, got: {err}"
    );
}

#[test]
fn test_individual_short_flag() {
    run_ok("cmd_xyz_flags", &["-x", "-y", "-z"]);
}

#[test]
fn test_individual_short_flag_mixed_with_long() {
    let cmd = find_cmd("cmd_xyz_flags");
    // Mix -x short with --y long
    let args: Vec<String> = vec!["-x", "--y", "-z"]
        .iter()
        .map(|s| s.to_string())
        .collect();
    let result = (cmd.run)(&args);
    assert!(result.is_ok(), "mixed short+long flags failed: {result:?}");
}

#[test]
fn test_short_named_option_separate_arg() {
    // -v followed by value as next arg
    run_ok("cmd_short", &["foo", "-v", "yes"]);
}

#[test]
fn test_short_named_option_with_long_positional() {
    // --verbose long form should also work
    run_ok("cmd_short", &["foo", "--verbose", "yes"]);
}

#[test]
fn test_short_flag_with_long_positional_before() {
    // Positional arg before short flags
    run_ok("cmd_bundled", &["-abc"]);
}

#[test]
fn test_short_flag_not_present() {
    // No flags at all should leave all as false
    run_ok("cmd_flag_absent", &[]);
}

#[test]
fn test_bundled_single_versus_bundle_equivalence() {
    // Verify that -ab (bundle) has the same effect as -a -b (separate)
    // Both should set a=true, b=true, c=false
    run_ok("cmd_partial_flags", &["-ab"]);
    run_ok("cmd_partial_flags", &["-a", "-b"]);
}

#[test]
fn test_short_flag_after_positional() {
    // Positional args first, then short flags
    run_ok("cmd_bundled", &["-ab", "-c"]);
}

// ===========================================================================
// Tests: Named option with -- separator
// ===========================================================================

#[test]
fn test_named_option_after_double_dash_is_positional() {
    // Everything after -- is treated as positional
    run_ok("cmd_double_dash", &["--", "a", "--b", "c"]);
}

// ===========================================================================
// Long tests: verify ALL ParamEntry fields are populated correctly
// ===========================================================================

#[test]
fn test_param_entry_all_fields_for_cmd_required() {
    let cmd = find_cmd("cmd_required");

    let name = cmd.params.iter().find(|p| p.name == "name").unwrap();
    assert_eq!(name.kind, "positional");
    assert_eq!(name.help, "");
    assert_eq!(name.details, "");
    assert_eq!(name.short, '\0');
    assert_eq!(name.placeholder, "");
    assert!(!name.hide);
    assert_eq!(name.default, "");
    assert_eq!(name.env, "");
    assert!(name.choices.is_empty());
    assert!(name.conflicts_with.is_empty());
    assert!(name.requires.is_empty());
    assert_eq!(name.value_hint, "");

    let greeting = cmd.params.iter().find(|p| p.name == "greeting").unwrap();
    assert_eq!(greeting.kind, "named");
    assert_eq!(greeting.help, "");
    assert_eq!(greeting.details, "");
    assert_eq!(greeting.short, '\0');
    assert_eq!(greeting.placeholder, "");
    assert!(!greeting.hide);
    assert_eq!(greeting.default, "");
    assert_eq!(greeting.env, "");
    assert!(greeting.choices.is_empty());
    assert!(greeting.conflicts_with.is_empty());
    assert!(greeting.requires.is_empty());
    assert_eq!(greeting.value_hint, "");
}

// ===========================================================================
// Tests: Multiple commands in inventory
// ===========================================================================

#[test]
fn test_inventory_contains_all_commands() {
    let names: Vec<&str> = clish::inventory::iter::<CommandEntry>()
        .map(|c| c.name)
        .collect();
    assert!(
        names.contains(&"cmd_required"),
        "should contain cmd_required, got: {names:?}"
    );
    assert!(
        names.contains(&"cmd_variadic"),
        "should contain cmd_variadic, got: {names:?}"
    );
    assert!(
        names.contains(&"cmd_flag"),
        "should contain cmd_flag, got: {names:?}"
    );
    assert!(
        names.contains(&"cmd_parsed"),
        "should contain cmd_parsed, got: {names:?}"
    );
}