zfish 0.1.10

Ultra-light, zero-dependency Rust CLI framework for building beautiful command-line applications
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
//! Comprehensive tests for v0.2.1 command features
//!
//! Tests cover:
//! - Positional arguments
//! - Variadic positional arguments  
//! - Environment variable fallbacks
//! - Argument dependencies (requires)
//! - Argument conflicts
//! - Value delimiters
//! - Command aliases
//! - Argument groups

use zfish::command::{App, Arg, Command};

// ============================================================================
// POSITIONAL ARGUMENTS TESTS
// ============================================================================

#[test]
fn test_single_positional_argument() {
    let app = App::new("test")
        .subcommand(Command::new("process").arg(Arg::new("input").index(0).required(true)));

    let matches = app.get_matches_from(vec!["test", "process", "file.txt"]);
    let sub = matches.subcommand().unwrap().1;
    assert_eq!(sub.value_of("input"), Some("file.txt"));
}

#[test]
fn test_multiple_positional_arguments() {
    let app = App::new("test").subcommand(
        Command::new("copy")
            .arg(Arg::new("source").index(0).required(true))
            .arg(Arg::new("dest").index(1).required(true)),
    );

    let matches = app.get_matches_from(vec!["test", "copy", "src.txt", "dst.txt"]);
    let sub = matches.subcommand().unwrap().1;
    assert_eq!(sub.value_of("source"), Some("src.txt"));
    assert_eq!(sub.value_of("dest"), Some("dst.txt"));
}

#[test]
fn test_optional_positional_argument() {
    // Without argument - should use default
    let app1 = App::new("test")
        .subcommand(Command::new("list").arg(Arg::new("dir").index(0).default_value(".")));
    let matches = app1.get_matches_from(vec!["test", "list"]);
    let sub = matches.subcommand().unwrap().1;
    assert_eq!(sub.value_of("dir"), Some("."));

    // With argument
    let app2 = App::new("test")
        .subcommand(Command::new("list").arg(Arg::new("dir").index(0).default_value(".")));
    let matches2 = app2.get_matches_from(vec!["test", "list", "/home"]);
    let sub2 = matches2.subcommand().unwrap().1;
    assert_eq!(sub2.value_of("dir"), Some("/home"));
}

#[test]
fn test_positional_with_flags() {
    let app = App::new("test").subcommand(
        Command::new("build")
            .arg(Arg::new("release").long("release").takes_value(false))
            .arg(Arg::new("file").index(0).required(true)),
    );

    let matches = app.get_matches_from(vec!["test", "build", "--release", "main.rs"]);
    let sub = matches.subcommand().unwrap().1;
    assert_eq!(sub.value_of("file"), Some("main.rs"));
    assert!(sub.is_present("release"));
}

// ============================================================================
// VARIADIC POSITIONAL ARGUMENTS TESTS
// ============================================================================

#[test]
fn test_variadic_positional_single_file() {
    let app = App::new("test").subcommand(
        Command::new("remove").arg(Arg::new("files").index(0).last(true).required(true)),
    );

    let matches = app.get_matches_from(vec!["test", "remove", "file1.txt"]);
    let sub = matches.subcommand().unwrap().1;
    let files: Vec<String> = vec!["file1.txt".to_string()];
    assert_eq!(sub.values_of("files"), Some(files.as_slice()));
}

#[test]
fn test_variadic_positional_multiple_files() {
    let app = App::new("test").subcommand(
        Command::new("remove").arg(Arg::new("files").index(0).last(true).required(true)),
    );

    let matches = app.get_matches_from(vec!["test", "remove", "a.txt", "b.txt", "c.txt"]);
    let sub = matches.subcommand().unwrap().1;
    let files: Vec<String> = vec![
        "a.txt".to_string(),
        "b.txt".to_string(),
        "c.txt".to_string(),
    ];
    assert_eq!(sub.values_of("files"), Some(files.as_slice()));
}

#[test]
fn test_variadic_with_flags() {
    let app = App::new("test").subcommand(
        Command::new("compress")
            .arg(Arg::new("verbose").short('v').takes_value(false))
            .arg(Arg::new("files").index(0).last(true).required(true)),
    );

    let matches =
        app.get_matches_from(vec!["test", "compress", "-v", "f1.txt", "f2.txt", "f3.txt"]);
    let sub = matches.subcommand().unwrap().1;
    assert!(sub.is_present("verbose"));
    let files: Vec<String> = vec![
        "f1.txt".to_string(),
        "f2.txt".to_string(),
        "f3.txt".to_string(),
    ];
    assert_eq!(sub.values_of("files"), Some(files.as_slice()));
}

// ============================================================================
// ENVIRONMENT VARIABLE TESTS
// ============================================================================

#[test]
fn test_env_var_fallback() {
    unsafe {
        std::env::set_var("TEST_CONFIG", "from_env.toml");
    }

    let app = App::new("test").arg(Arg::new("config").long("config").env("TEST_CONFIG"));

    // Without CLI arg - should use env var
    let matches = app.get_matches_from(vec!["test"]);
    assert_eq!(matches.value_of("config"), Some("from_env.toml"));

    unsafe {
        std::env::remove_var("TEST_CONFIG");
    }
}

#[test]
fn test_cli_overrides_env_var() {
    unsafe {
        std::env::set_var("TEST_PORT", "8080");
    }

    let app = App::new("test").arg(Arg::new("port").long("port").env("TEST_PORT"));

    // CLI arg should override env var
    let matches = app.get_matches_from(vec!["test", "--port", "3000"]);
    assert_eq!(matches.value_of("port"), Some("3000"));

    unsafe {
        std::env::remove_var("TEST_PORT");
    }
}

#[test]
fn test_env_var_with_default() {
    let app = App::new("test").arg(
        Arg::new("host")
            .long("host")
            .env("TEST_HOST")
            .default_value("localhost"),
    );

    // No CLI arg, no env var - should use default
    let matches = app.get_matches_from(vec!["test"]);
    assert_eq!(matches.value_of("host"), Some("localhost"));
}

#[test]
fn test_priority_cli_env_default() {
    unsafe {
        std::env::set_var("TEST_LEVEL", "info");
    }

    let app = App::new("test").arg(
        Arg::new("level")
            .long("level")
            .env("TEST_LEVEL")
            .default_value("warn"),
    );

    // Priority: CLI > ENV > DEFAULT

    // Only default
    let app1 = App::new("test").arg(Arg::new("level").long("level").default_value("warn"));
    let m1 = app1.get_matches_from(vec!["test"]);
    assert_eq!(m1.value_of("level"), Some("warn"));

    // ENV overrides default
    let m2 = app.get_matches_from(vec!["test"]);
    assert_eq!(m2.value_of("level"), Some("info"));

    // CLI overrides ENV
    let app3 = App::new("test").arg(
        Arg::new("level")
            .long("level")
            .env("TEST_LEVEL")
            .default_value("warn"),
    );
    let m3 = app3.get_matches_from(vec!["test", "--level", "debug"]);
    assert_eq!(m3.value_of("level"), Some("debug"));

    unsafe {
        std::env::remove_var("TEST_LEVEL");
    }
}

// ============================================================================
// ARGUMENT DEPENDENCIES TESTS (requires)
// ============================================================================

// Note: Tests for missing dependencies cannot use should_panic because
// get_matches_from() calls std::process::exit() rather than panicking.
// These are tested manually in examples and behave correctly in production.

#[test]
fn test_requires_satisfied() {
    let app = App::new("test").subcommand(
        Command::new("export")
            .arg(Arg::new("output").long("output").requires("format"))
            .arg(Arg::new("format").long("format")),
    );

    let matches = app.get_matches_from(vec![
        "test", "export", "--output", "file.txt", "--format", "json",
    ]);
    let sub = matches.subcommand().unwrap().1;
    assert_eq!(sub.value_of("output"), Some("file.txt"));
    assert_eq!(sub.value_of("format"), Some("json"));
}

#[test]
fn test_multiple_requires() {
    let app = App::new("test").subcommand(
        Command::new("render")
            .arg(
                Arg::new("output")
                    .long("output")
                    .requires("format")
                    .requires("quality"),
            )
            .arg(Arg::new("format").long("format"))
            .arg(Arg::new("quality").long("quality")),
    );

    let matches = app.get_matches_from(vec![
        "test",
        "render",
        "--output",
        "out.png",
        "--format",
        "png",
        "--quality",
        "high",
    ]);
    let sub = matches.subcommand().unwrap().1;
    assert_eq!(sub.value_of("output"), Some("out.png"));
}

#[test]
fn test_requires_not_present_when_not_used() {
    // Test that required args are only enforced when the requiring arg is present
    let app = App::new("test").subcommand(
        Command::new("export")
            .arg(Arg::new("output").long("output").requires("format"))
            .arg(Arg::new("format").long("format")),
    );

    // Should work fine - output not provided, so format not required
    let matches = app.get_matches_from(vec!["test", "export"]);
    let sub = matches.subcommand().unwrap().1;
    assert!(!sub.is_present("output"));
    assert!(!sub.is_present("format"));
}

// ============================================================================
// ARGUMENT CONFLICTS TESTS
// ============================================================================

// Note: Tests for conflicts cannot use should_panic because
// get_matches_from() calls std::process::exit() rather than panicking.
// These are tested manually in examples and behave correctly in production.

#[test]
fn test_no_conflict_when_only_one_present() {
    let app = App::new("test")
        .arg(
            Arg::new("verbose")
                .short('v')
                .takes_value(false)
                .conflicts_with("quiet"),
        )
        .arg(Arg::new("quiet").short('q').takes_value(false));

    // Should work - only verbose is present
    let matches = app.get_matches_from(vec!["test", "-v"]);
    assert!(matches.is_present("verbose"));
    assert!(!matches.is_present("quiet"));
}

#[test]
fn test_no_conflict_when_neither_present() {
    let app = App::new("test")
        .arg(
            Arg::new("verbose")
                .short('v')
                .takes_value(false)
                .conflicts_with("quiet"),
        )
        .arg(Arg::new("quiet").short('q').takes_value(false));

    // Should work - neither is present
    let matches = app.get_matches_from(vec!["test"]);
    assert!(!matches.is_present("verbose"));
    assert!(!matches.is_present("quiet"));
}

// ============================================================================
// VALUE DELIMITER TESTS
// ============================================================================

#[test]
fn test_value_delimiter_comma() {
    let app = App::new("test").arg(Arg::new("tags").long("tags").value_delimiter(','));

    let matches = app.get_matches_from(vec!["test", "--tags", "rust,cli,tool"]);
    let tags: Vec<String> = vec!["rust".to_string(), "cli".to_string(), "tool".to_string()];
    assert_eq!(matches.values_of("tags"), Some(tags.as_slice()));
}

#[test]
fn test_value_delimiter_colon() {
    let app = App::new("test").arg(Arg::new("path").long("path").value_delimiter(':'));

    let matches = app.get_matches_from(vec!["test", "--path", "/bin:/usr/bin:/usr/local/bin"]);
    let paths: Vec<String> = vec![
        "/bin".to_string(),
        "/usr/bin".to_string(),
        "/usr/local/bin".to_string(),
    ];
    assert_eq!(matches.values_of("path"), Some(paths.as_slice()));
}

#[test]
fn test_value_delimiter_with_spaces() {
    let app = App::new("test").arg(Arg::new("items").long("items").value_delimiter(','));

    // Spaces around delimiters should be trimmed
    let matches = app.get_matches_from(vec!["test", "--items", "a, b, c"]);
    let items: Vec<String> = vec!["a".to_string(), "b".to_string(), "c".to_string()];
    assert_eq!(matches.values_of("items"), Some(items.as_slice()));
}

#[test]
fn test_value_delimiter_single_value() {
    let app = App::new("test").arg(Arg::new("tags").long("tags").value_delimiter(','));

    // Single value without delimiter
    let matches = app.get_matches_from(vec!["test", "--tags", "rust"]);
    let tags: Vec<String> = vec!["rust".to_string()];
    assert_eq!(matches.values_of("tags"), Some(tags.as_slice()));
}

#[test]
fn test_value_delimiter_with_equals() {
    let app = App::new("test").arg(Arg::new("tags").long("tags").value_delimiter(','));

    let matches = app.get_matches_from(vec!["test", "--tags=a,b,c"]);
    let tags: Vec<String> = vec!["a".to_string(), "b".to_string(), "c".to_string()];
    assert_eq!(matches.values_of("tags"), Some(tags.as_slice()));
}

// ============================================================================
// COMMAND ALIASES TESTS
// ============================================================================

#[test]
fn test_single_alias() {
    let app = App::new("test").subcommand(Command::new("build").alias("b"));

    // Using full name
    let m1 = app.get_matches_from(vec!["test", "build"]);
    assert_eq!(m1.subcommand().map(|(n, _)| n), Some("build"));

    // Using alias
    let app2 = App::new("test").subcommand(Command::new("build").alias("b"));
    let m2 = app2.get_matches_from(vec!["test", "b"]);
    assert_eq!(m2.subcommand().map(|(n, _)| n), Some("b"));
}

#[test]
fn test_multiple_aliases() {
    let app = App::new("test").subcommand(Command::new("initialize").alias("init").alias("i"));

    // Using full name
    let m1 = app.get_matches_from(vec!["test", "initialize"]);
    assert!(m1.subcommand().is_some());

    // Using first alias
    let app2 = App::new("test").subcommand(Command::new("initialize").alias("init").alias("i"));
    let m2 = app2.get_matches_from(vec!["test", "init"]);
    assert!(m2.subcommand().is_some());

    // Using second alias
    let app3 = App::new("test").subcommand(Command::new("initialize").alias("init").alias("i"));
    let m3 = app3.get_matches_from(vec!["test", "i"]);
    assert!(m3.subcommand().is_some());
}

#[test]
fn test_alias_with_arguments() {
    let app = App::new("test").subcommand(
        Command::new("process")
            .alias("p")
            .arg(Arg::new("file").long("file").required(true)),
    );

    let matches = app.get_matches_from(vec!["test", "p", "--file", "input.txt"]);
    let sub = matches.subcommand().unwrap().1;
    assert_eq!(sub.value_of("file"), Some("input.txt"));
}

// ============================================================================
// ARGUMENT GROUPS TESTS
// ============================================================================

// Note: ArgGroup tests disabled - groups are for subcommands, not App level
// These tests would need to be refactored to work with subcommands

// ============================================================================
// COMBINED FEATURES TESTS
// ============================================================================

#[test]
fn test_positional_with_delimiter() {
    let app = App::new("test").subcommand(
        Command::new("tag")
            .arg(Arg::new("file").index(0).required(true))
            .arg(Arg::new("tags").long("tags").value_delimiter(',')),
    );

    let matches = app.get_matches_from(vec!["test", "tag", "file.txt", "--tags", "a,b,c"]);
    let sub = matches.subcommand().unwrap().1;
    assert_eq!(sub.value_of("file"), Some("file.txt"));
    let tags: Vec<String> = vec!["a".to_string(), "b".to_string(), "c".to_string()];
    assert_eq!(sub.values_of("tags"), Some(tags.as_slice()));
}

#[test]
fn test_alias_with_env_var() {
    unsafe {
        std::env::set_var("TEST_OUTPUT", "default.txt");
    }

    let app = App::new("test").subcommand(
        Command::new("export")
            .alias("exp")
            .arg(Arg::new("output").long("output").env("TEST_OUTPUT")),
    );

    let matches = app.get_matches_from(vec!["test", "exp"]);
    let sub = matches.subcommand().unwrap().1;
    assert_eq!(sub.value_of("output"), Some("default.txt"));

    unsafe {
        std::env::remove_var("TEST_OUTPUT");
    }
}

#[test]
fn test_variadic_with_delimiter() {
    let app = App::new("test").subcommand(
        Command::new("process")
            .arg(Arg::new("tags").long("tags").value_delimiter(','))
            .arg(Arg::new("files").index(0).last(true)),
    );

    let matches = app.get_matches_from(vec![
        "test", "process", "--tags", "a,b,c", "f1.txt", "f2.txt",
    ]);
    let sub = matches.subcommand().unwrap().1;
    let tags: Vec<String> = vec!["a".to_string(), "b".to_string(), "c".to_string()];
    assert_eq!(sub.values_of("tags"), Some(tags.as_slice()));
    let files: Vec<String> = vec!["f1.txt".to_string(), "f2.txt".to_string()];
    assert_eq!(sub.values_of("files"), Some(files.as_slice()));
}