fli 1.2.2

The commander.js like CLI Parser 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
use crate::command::{FliCallbackData, FliCommand};
use crate::error::FliError;
use crate::option_parser::{InputArgsParser, Value, ValueTypes};
use std::sync::atomic::{AtomicBool, Ordering};

#[test]
fn test_fli_command_creation() {
    let cmd = FliCommand::new("test", "A test command");
    assert_eq!(cmd.get_name(), "test");
    assert_eq!(cmd.get_description(), "A test command");
}

#[test]
fn test_add_sub_command() {
    let mut parent = FliCommand::new("parent", "Parent command");
    let child = FliCommand::new("child", "Child command");

    parent.add_sub_command(child);
    assert!(parent.has_sub_command("child"));
}

#[test]
fn test_has_sub_command() {
    let mut cmd = FliCommand::new("main", "Main command");
    let sub = FliCommand::new("sub", "Sub command");

    assert!(!cmd.has_sub_command("sub"));
    cmd.add_sub_command(sub);
    assert!(cmd.has_sub_command("sub"));
}

#[test]
fn test_get_sub_command() {
    let mut parent = FliCommand::new("parent", "Parent command");
    let child = FliCommand::new("child", "Child command");

    parent.add_sub_command(child);

    let retrieved = parent.get_sub_command("child");
    assert!(retrieved.is_some());
    assert_eq!(retrieved.unwrap().get_name(), "child");
}

#[test]
fn test_get_nonexistent_sub_command() {
    let cmd = FliCommand::new("main", "Main command");
    assert!(cmd.get_sub_command("nonexistent").is_none());
}

#[test]
fn test_add_option_to_command() {
    let mut cmd = FliCommand::new("test", "Test command");

    cmd.add_option(
        "verbose",
        "Enable verbose output",
        "-v",
        "--verbose",
        ValueTypes::OptionalSingle(Some(Value::Bool(false))),
    );

    let parser = cmd.get_option_parser();
    assert!(parser.has_option("-v"));
    assert!(parser.has_option("--verbose"));
}

#[test]
fn test_multiple_options() {
    let mut cmd = FliCommand::new("build", "Build command");

    cmd.add_option(
        "release",
        "Build in release mode",
        "-r",
        "--release",
        ValueTypes::OptionalSingle(Some(Value::Bool(false))),
    );

    cmd.add_option(
        "target",
        "Target directory",
        "-t",
        "--target",
        ValueTypes::RequiredSingle(Value::Str(String::new())),
    );

    let parser = cmd.get_option_parser();
    // 2 options + 1 default help option = 3
    assert_eq!(parser.get_options().len(), 3);
    assert!(parser.has_option("-r"));
    assert!(parser.has_option("--target"));
}

#[test]
fn test_callback_data_creation() {
    let mut cmd = FliCommand::new("test", "Test");
    let parser = cmd.get_option_parser().clone();
    let args = vec!["arg1".to_string(), "arg2".to_string()];
    let arg_parser = InputArgsParser::new("test".to_string(), vec![]);

    let data = FliCallbackData::new(cmd.clone(), parser, args.clone(), arg_parser);

    assert_eq!(data.command.get_name(), "test");
    assert_eq!(data.arguments.len(), 2);
    assert_eq!(data.arguments[0], "arg1");
}

#[test]
fn test_callback_data_get_option_value() {
    let mut cmd = FliCommand::new("test", "Test");
    cmd.add_option(
        "name",
        "Name option",
        "-n",
        "--name",
        ValueTypes::RequiredSingle(Value::Str("default".to_string())),
    );

    let mut parser = cmd.get_option_parser().clone();
    parser
        .update_option_value(
            "-n",
            ValueTypes::RequiredSingle(Value::Str("Alice".to_string())),
        )
        .unwrap();

    let arg_parser = InputArgsParser::new("test".to_string(), vec![]);
    let data = FliCallbackData::new(cmd.clone(), parser, vec![], arg_parser);

    let value = data.get_option_value("name");
    assert!(value.is_some());
    assert_eq!(value.unwrap().as_str(), Some("Alice"));
}

#[test]
fn test_callback_data_get_option_with_dash() {
    let mut cmd = FliCommand::new("test", "Test");
    cmd.add_option(
        "verbose",
        "Verbose mode",
        "-v",
        "--verbose",
        ValueTypes::OptionalSingle(Some(Value::Bool(false))),
    );

    let parser = cmd.get_option_parser().clone();
    let arg_parser = InputArgsParser::new("test".to_string(), vec![]);
    let data = FliCallbackData::new(cmd, parser, vec![], arg_parser);

    // Should work with both formats
    let value1 = data.get_option_value("-v");
    let value2 = data.get_option_value("--verbose");
    let value3 = data.get_option_value("verbose");

    assert!(value1.is_some() || value2.is_some() || value3.is_some());
}

#[test]
fn test_nested_subcommands() {
    let mut root = FliCommand::new("git", "Git command");
    let mut remote = FliCommand::new("remote", "Remote commands");
    let add = FliCommand::new("add", "Add remote");

    remote.add_sub_command(add);
    root.add_sub_command(remote);

    assert!(root.has_sub_command("remote"));
    let remote_cmd = root.get_sub_command("remote").unwrap();
    assert!(remote_cmd.has_sub_command("add"));
}

#[test]
fn test_command_clone() {
    let mut cmd = FliCommand::new("test", "Test command");
    cmd.add_option(
        "opt",
        "Option",
        "-o",
        "--opt",
        ValueTypes::OptionalSingle(Some(Value::Bool(false))),
    );

    let mut cloned = cmd.clone();
    assert_eq!(cloned.get_name(), cmd.get_name());
    assert!(cloned.get_option_parser().has_option("-o"));
}

// ============================================================================
// Inheritable Options Tests (Command Level)
// ============================================================================

#[test]
fn test_command_with_parser_constructor() {
    use crate::option_parser::CommandOptionsParserBuilder;

    let mut builder = CommandOptionsParserBuilder::new();
    builder.add_option(
        "test",
        "Test option",
        "-t",
        "--test",
        ValueTypes::OptionalSingle(Some(Value::Bool(false))),
    );

    let mut cmd = FliCommand::with_parser("mycmd", "My command", builder);

    assert_eq!(cmd.get_name(), "mycmd");
    assert_eq!(cmd.get_description(), "My command");
    assert!(cmd.get_option_parser().has_option("-t"));
    // Help flag should be auto-added
    assert!(cmd.get_option_parser().has_option("--help"));
}

#[test]
fn test_subcommand_inherits_parent_options() {
    let mut parent = FliCommand::new("parent", "Parent command");

    // Add options to parent
    parent.add_option(
        "verbose",
        "Verbose output",
        "-v",
        "--verbose",
        ValueTypes::OptionalSingle(Some(Value::Bool(false))),
    );
    parent.add_option(
        "debug",
        "Debug mode",
        "-d",
        "--debug",
        ValueTypes::OptionalSingle(Some(Value::Bool(false))),
    );

    // Mark options as inheritable
    parent.get_option_parser().mark_inheritable("-v").unwrap();
    parent.get_option_parser().mark_inheritable("-d").unwrap();

    // Create subcommand using subcommand() method
    let child = parent.subcommand("child", "Child command");

    // Child should have inherited options
    assert!(child.get_option_parser().has_option("-v"));
    assert!(child.get_option_parser().has_option("--verbose"));
    assert!(child.get_option_parser().has_option("-d"));
    assert!(child.get_option_parser().has_option("--debug"));
}

#[test]
fn test_subcommand_inherits_only_marked_options() {
    let mut parent = FliCommand::new("parent", "Parent command");

    // Add multiple options
    parent.add_option(
        "verbose",
        "Verbose",
        "-v",
        "--verbose",
        ValueTypes::OptionalSingle(Some(Value::Bool(false))),
    );
    parent.add_option(
        "quiet",
        "Quiet",
        "-q",
        "--quiet",
        ValueTypes::OptionalSingle(Some(Value::Bool(false))),
    );
    parent.add_option(
        "debug",
        "Debug",
        "-d",
        "--debug",
        ValueTypes::OptionalSingle(Some(Value::Bool(false))),
    );

    // Mark only verbose as inheritable
    parent.get_option_parser().mark_inheritable("-v").unwrap();

    // Create subcommand
    let child = parent.subcommand("child", "Child command");

    // Child should have only the verbose option, not quiet or debug
    assert!(child.get_option_parser().has_option("-v"));
    assert!(!child.get_option_parser().has_option("-q"));
    assert!(!child.get_option_parser().has_option("-d"));
}

#[test]
fn test_nested_subcommands_inherit_options() {
    let mut root = FliCommand::new("root", "Root command");

    // Add option to root
    root.add_option(
        "verbose",
        "Verbose",
        "-v",
        "--verbose",
        ValueTypes::OptionalSingle(Some(Value::Bool(false))),
    );
    root.get_option_parser().mark_inheritable("-v").unwrap();

    // Create first level subcommand
    let level1 = root.subcommand("level1", "Level 1");
    level1.add_option(
        "debug",
        "Debug",
        "-d",
        "--debug",
        ValueTypes::OptionalSingle(Some(Value::Bool(false))),
    );
    level1
        .get_option_parser()
        .mark_inheritable_many(&["-v", "-d"])
        .unwrap();

    // Create second level subcommand
    let level2 = level1.subcommand("level2", "Level 2");

    // Level 2 should have both -v (from root) and -d (from level1)
    assert!(level2.get_option_parser().has_option("-v"));
    assert!(level2.get_option_parser().has_option("-d"));
}

#[test]
fn test_mark_inheritable_with_long_flag() {
    let mut parent = FliCommand::new("parent", "Parent");

    parent.add_option(
        "verbose",
        "Verbose",
        "-v",
        "--verbose",
        ValueTypes::OptionalSingle(Some(Value::Bool(false))),
    );

    // Mark using long flag
    parent
        .get_option_parser()
        .mark_inheritable("--verbose")
        .unwrap();

    let child = parent.subcommand("child", "Child");

    // Should work with both short and long flags
    assert!(child.get_option_parser().has_option("-v"));
    assert!(child.get_option_parser().has_option("--verbose"));
}

#[test]
fn test_mark_inheritable_many_mixed_flags() {
    let mut parent = FliCommand::new("parent", "Parent");

    parent.add_option(
        "verbose",
        "Verbose",
        "-v",
        "--verbose",
        ValueTypes::OptionalSingle(Some(Value::Bool(false))),
    );
    parent.add_option(
        "quiet",
        "Quiet",
        "-q",
        "--quiet",
        ValueTypes::OptionalSingle(Some(Value::Bool(false))),
    );
    parent.add_option(
        "debug",
        "Debug",
        "-d",
        "--debug",
        ValueTypes::OptionalSingle(Some(Value::Bool(false))),
    );

    // Mix short and long flags
    parent
        .get_option_parser()
        .mark_inheritable_many(&["-v", "--quiet", "-d"])
        .unwrap();

    let child = parent.subcommand("child", "Child");

    assert!(child.get_option_parser().has_option("-v"));
    assert!(child.get_option_parser().has_option("-q"));
    assert!(child.get_option_parser().has_option("-d"));
}

#[test]
fn test_inheritable_options_with_default_values() {
    let mut parent = FliCommand::new("parent", "Parent");

    parent.add_option(
        "config",
        "Config file",
        "-c",
        "--config",
        ValueTypes::OptionalSingle(Some(Value::Str("default.toml".to_string()))),
    );

    parent.get_option_parser().mark_inheritable("-c").unwrap();

    let child = parent.subcommand("child", "Child");

    // Child should inherit the option with its default value
    assert!(child.get_option_parser().has_option("-c"));
}

#[test]
fn test_inheritable_options_builder_creates_independent_copy() {
    let mut parent = FliCommand::new("parent", "Parent");

    parent.add_option(
        "verbose",
        "Verbose",
        "-v",
        "--verbose",
        ValueTypes::OptionalSingle(Some(Value::Bool(false))),
    );
    parent.get_option_parser().mark_inheritable("-v").unwrap();

    // Create child1 and add option to it
    {
        let child1 = parent.subcommand("child1", "Child 1");
        child1.add_option(
            "opt1",
            "Option 1",
            "-o",
            "--opt1",
            ValueTypes::OptionalSingle(Some(Value::Bool(false))),
        );
        // child1 should have both inherited and its own option
        assert!(child1.get_option_parser().has_option("-v"));
        assert!(child1.get_option_parser().has_option("-o"));
    }

    // Create child2 separately
    let child2 = parent.subcommand("child2", "Child 2");

    // child2 should not have child1's option
    assert!(!child2.get_option_parser().has_option("-o"));
    // But should have inherited option
    assert!(child2.get_option_parser().has_option("-v"));
}

#[test]
fn test_mark_nonexistent_option_as_inheritable() {
    let mut cmd = FliCommand::new("test", "Test");

    // Try to mark an option that doesn't exist
    let result = cmd.get_option_parser().mark_inheritable("-v");

    assert!(result.is_err());
}

#[test]
fn test_subcommand_adds_help_flag_to_inherited_options() {
    let mut parent = FliCommand::new("parent", "Parent");

    parent.add_option(
        "verbose",
        "Verbose",
        "-v",
        "--verbose",
        ValueTypes::OptionalSingle(Some(Value::Bool(false))),
    );
    parent.get_option_parser().mark_inheritable("-v").unwrap();

    let child = parent.subcommand("child", "Child");

    // Child should have both inherited option and auto-added help
    assert!(child.get_option_parser().has_option("-v"));
    assert!(child.get_option_parser().has_option("--help"));
}

// ============================================================================
// Zero-Argument Command Tests (run() behavior)
// ============================================================================

static ZERO_ARG_CB_CALLED: AtomicBool = AtomicBool::new(false);

fn zero_arg_callback(_data: &FliCallbackData) {
    ZERO_ARG_CB_CALLED.store(true, Ordering::SeqCst);
}

#[test]
fn test_run_zero_arg_command_with_callback() {
    // A command with a callback but no args/options should succeed.
    // Simulates: `myapp list` where `list` takes no arguments.
    ZERO_ARG_CB_CALLED.store(false, Ordering::SeqCst);

    let mut cmd = FliCommand::new("list", "List all items");
    cmd.set_callback(zero_arg_callback);

    let parser = InputArgsParser::new("list".to_string(), vec![]);
    let result = cmd.run(parser);

    assert!(
        result.is_ok(),
        "Zero-arg command with callback should succeed, got: {:?}",
        result
    );
    assert!(
        ZERO_ARG_CB_CALLED.load(Ordering::SeqCst),
        "Callback should have been called"
    );
}

#[test]
fn test_run_zero_arg_command_without_callback_fails() {
    // A command with NO callback and no args should return InvalidUsage.
    let mut cmd = FliCommand::new("list", "List all items");
    // No callback set

    let parser = InputArgsParser::new("list".to_string(), vec![]);
    let result = cmd.run(parser);

    assert!(
        matches!(result, Err(FliError::InvalidUsage(_))),
        "Zero-arg command without callback should fail with InvalidUsage, got: {:?}",
        result
    );
}

static OPTIONAL_OPTS_CB_CALLED: AtomicBool = AtomicBool::new(false);

fn optional_opts_callback(_data: &FliCallbackData) {
    OPTIONAL_OPTS_CB_CALLED.store(true, Ordering::SeqCst);
}

#[test]
fn test_run_command_with_only_optional_options_and_callback() {
    // A command with optional options (none provided) and a callback should succeed.
    // Simulates: `myapp list` where list has optional `-v` but user doesn't pass it.
    OPTIONAL_OPTS_CB_CALLED.store(false, Ordering::SeqCst);

    let mut cmd = FliCommand::new("list", "List all items");
    cmd.add_option(
        "verbose",
        "Show details",
        "-v",
        "--verbose",
        ValueTypes::OptionalSingle(Some(Value::Bool(false))),
    );
    cmd.set_callback(optional_opts_callback);

    let parser = InputArgsParser::new("list".to_string(), vec![]);
    let result = cmd.run(parser);

    assert!(
        result.is_ok(),
        "Command with optional options and callback should succeed, got: {:?}",
        result
    );
    assert!(
        OPTIONAL_OPTS_CB_CALLED.load(Ordering::SeqCst),
        "Callback should have been called"
    );
}