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
use crate::app::Fli;
use crate::option_parser::{Value, ValueTypes};

#[test]
fn test_fli_new() {
    let app = Fli::new("myapp", "1.0.0", "My application");

    assert_eq!(app.name, "myapp");
    assert_eq!(app.version, "1.0.0");
    assert_eq!(app.description, "My application");
}

#[test]
fn test_add_command() {
    use crate::command::FliCommand;

    let mut app = Fli::new("cli", "1.0.0", "CLI app");
    let cmd = FliCommand::new("serve", "Start server");

    app.add_command(cmd);
    assert!(app.root_command.has_sub_command("serve"));
}

#[test]
fn test_command_method() {
    let mut app = Fli::new("cli", "1.0.0", "CLI app");

    let result = app.command("build", "Build the project");
    assert!(result.is_ok());

    let cmd = result.unwrap();
    assert_eq!(cmd.get_name(), "build");
}

#[test]
fn test_add_option_to_app() {
    let mut app = Fli::new("cli", "1.0.0", "CLI app");

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

    assert!(app.root_command.get_option_parser().has_option("-v"));
    assert!(app.root_command.get_option_parser().has_option("--verbose"));
}

#[test]
fn test_add_multiple_options() {
    let mut app = Fli::new("cli", "1.0.0", "CLI app");

    app.add_option("verbose", "Verbose", "-v", "--verbose", ValueTypes::OptionalSingle(Some(Value::Bool(false))));
    app.add_option(
        "output",
        "Output file",
        "-o",
        "--output",
        ValueTypes::RequiredSingle(Value::Str(String::new())),
    );
    app.add_option("quiet", "Quiet mode", "-q", "--quiet", ValueTypes::OptionalSingle(Some(Value::Bool(false))));

    let parser = app.root_command.get_option_parser();
    // 3 options + 1 default help option = 4
    assert_eq!(parser.get_options().len(), 4);
    assert!(parser.has_option("-v"));
    assert!(parser.has_option("--output"));
    assert!(parser.has_option("-q"));
}

#[test]
fn test_command_with_options() {
    let mut app = Fli::new("cli", "1.0.0", "CLI app");

    let cmd = app.command("serve", "Start server").unwrap();
    cmd.add_option(
        "port",
        "Port to listen on",
        "-p",
        "--port",
        ValueTypes::RequiredSingle(Value::Int(8080)),
    );

    let serve_cmd = app.root_command.get_sub_command_mut("serve").unwrap();
    assert!(serve_cmd.get_option_parser().has_option("-p"));
}

#[test]
fn test_nested_commands() {
    let mut app = Fli::new("docker", "1.0.0", "Docker CLI");

    app.command("container", "Manage containers").unwrap();

    let container_cmd = app.root_command.get_sub_command("container").unwrap();
    assert_eq!(container_cmd.get_name(), "container");
}

#[test]
fn test_with_debug() {
    use crate::display;

    let _app = Fli::new("cli", "1.0.0", "CLI app").with_debug();

    assert!(display::is_debug_enabled());
    display::disable_debug(); // Clean up
}

#[test]
fn test_add_debug_option() {
    let mut app = Fli::new("cli", "1.0.0", "CLI app");
    app.add_debug_option();

    assert!(app.root_command.get_option_parser().has_option("-D"));
    assert!(app.root_command.get_option_parser().has_option("--debug"));
}

#[test]
fn test_multiple_commands() {
    let mut app = Fli::new("git", "2.0.0", "Git CLI");

    app.command("clone", "Clone repository").unwrap();
    app.command("pull", "Pull changes").unwrap();
    app.command("push", "Push changes").unwrap();

    assert!(app.root_command.has_sub_command("clone"));
    assert!(app.root_command.has_sub_command("pull"));
    assert!(app.root_command.has_sub_command("push"));
}

#[test]
fn test_command_chaining() {
    let mut app = Fli::new("cli", "1.0.0", "CLI app");

    let result = app.command("build", "Build project").and_then(|cmd| {
        cmd.add_option(
            "release",
            "Release mode",
            "-r",
            "--release",
            ValueTypes::OptionalSingle(Some(Value::Bool(false))),
        );
        Ok(cmd)
    });

    assert!(result.is_ok());
}

#[test]
fn test_root_command_has_no_name() {
    let app = Fli::new("cli", "1.0.0", "CLI app");
    // Root command should have empty string as name
    assert_eq!(app.root_command.get_name(), "");
}

#[test]
fn test_app_clone_fields() {
    let app = Fli::new("myapp", "2.0.0", "My awesome app");

    assert_eq!(app.name, "myapp");
    assert_eq!(app.version, "2.0.0");
    assert_eq!(app.description, "My awesome app");
}

// ============================================================================
// Inheritable Options Tests
// ============================================================================

#[test]
fn test_mark_inheritable_single_option() {
    let mut app = Fli::new("cli", "1.0.0", "CLI app");

    // Add an option
    app.add_option(
        "verbose",
        "Enable verbose",
        "-v",
        "--verbose",
        ValueTypes::OptionalSingle(Some(Value::Bool(false))),
    );

    // Mark it as inheritable
    let result = app.mark_inheritable("-v");
    assert!(result.is_ok());
}

#[test]
fn test_mark_inheritable_nonexistent_option() {
    let mut app = Fli::new("cli", "1.0.0", "CLI app");

    // Try to mark a non-existent option
    let result = app.mark_inheritable("-v");
    assert!(result.is_err());
}

#[test]
fn test_mark_inheritable_many_options() {
    let mut app = Fli::new("cli", "1.0.0", "CLI app");

    // Add multiple options
    app.add_option(
        "verbose",
        "Enable verbose",
        "-v",
        "--verbose",
        ValueTypes::OptionalSingle(Some(Value::Bool(false))),
    );
    app.add_option(
        "quiet",
        "Suppress output",
        "-q",
        "--quiet",
        ValueTypes::OptionalSingle(Some(Value::Bool(false))),
    );
    app.add_option("color", "Enable colors", "-c", "--color", ValueTypes::OptionalSingle(Some(Value::Bool(false))));

    // Mark them all as inheritable
    let result = app.mark_inheritable_many(&["-v", "-q", "-c"]);
    assert!(result.is_ok());
}

#[test]
fn test_mark_inheritable_many_with_invalid_option() {
    let mut app = Fli::new("cli", "1.0.0", "CLI app");

    // Add only one option
    app.add_option(
        "verbose",
        "Enable verbose",
        "-v",
        "--verbose",
        ValueTypes::OptionalSingle(Some(Value::Bool(false))),
    );

    // Try to mark multiple, including non-existent ones
    let result = app.mark_inheritable_many(&["-v", "-q", "-c"]);
    assert!(result.is_err());
}

#[test]
fn test_subcommand_inherits_options() {
    let mut app = Fli::new("cli", "1.0.0", "CLI app");

    // Add options to root
    app.add_option(
        "verbose",
        "Enable verbose",
        "-v",
        "--verbose",
        ValueTypes::OptionalSingle(Some(Value::Bool(false))),
    );
    app.add_option(
        "quiet",
        "Suppress output",
        "-q",
        "--quiet",
        ValueTypes::OptionalSingle(Some(Value::Bool(false))),
    );

    // Mark them as inheritable
    app.mark_inheritable_many(&["-v", "-q"]).unwrap();

    // Create a subcommand
    let result = app.command("build", "Build project");
    assert!(result.is_ok());

    let cmd = result.unwrap();

    // Check that the subcommand has the inherited options
    assert!(cmd.get_option_parser().has_option("-v"));
    assert!(cmd.get_option_parser().has_option("--verbose"));
    assert!(cmd.get_option_parser().has_option("-q"));
    assert!(cmd.get_option_parser().has_option("--quiet"));
}

#[test]
fn test_multiple_subcommands_inherit_same_options() {
    let mut app = Fli::new("cli", "1.0.0", "CLI app");

    // Add and mark options as inheritable
    app.add_option(
        "verbose",
        "Enable verbose",
        "-v",
        "--verbose",
        ValueTypes::OptionalSingle(Some(Value::Bool(false))),
    );
    app.mark_inheritable("-v").unwrap();

    // Create multiple subcommands
    app.command("build", "Build project").unwrap();
    app.command("test", "Run tests").unwrap();
    app.command("deploy", "Deploy app").unwrap();

    // All should have the verbose option
    assert!(app
        .root_command
        .get_sub_command_mut("build")
        .unwrap()
        .get_option_parser()
        .has_option("-v"));
    assert!(app
        .root_command
        .get_sub_command_mut("test")
        .unwrap()
        .get_option_parser()
        .has_option("-v"));
    assert!(app
        .root_command
        .get_sub_command_mut("deploy")
        .unwrap()
        .get_option_parser()
        .has_option("-v"));
}

#[test]
fn test_inherited_options_dont_affect_parent() {
    let mut app = Fli::new("cli", "1.0.0", "CLI app");

    // Add option to root and mark as inheritable
    app.add_option(
        "verbose",
        "Enable verbose",
        "-v",
        "--verbose",
        ValueTypes::OptionalSingle(Some(Value::Bool(false))),
    );
    app.mark_inheritable("-v").unwrap();

    // Parent should not have the subcommand's option (check before creating subcommand)
    assert!(!app.root_command.get_option_parser().has_option("-r"));

    // Create subcommand
    let subcmd = app.command("build", "Build project").unwrap();

    // Add option only to subcommand
    subcmd.add_option(
        "release",
        "Release build",
        "-r",
        "--release",
        ValueTypes::OptionalSingle(Some(Value::Bool(false))),
    );

    // Subcommand should have both its own and inherited options
    assert!(subcmd.get_option_parser().has_option("-v"));
    assert!(subcmd.get_option_parser().has_option("-r"));
}

#[test]
fn test_mark_inheritable_using_long_flag() {
    let mut app = Fli::new("cli", "1.0.0", "CLI app");

    // Add option
    app.add_option(
        "verbose",
        "Enable verbose",
        "-v",
        "--verbose",
        ValueTypes::OptionalSingle(Some(Value::Bool(false))),
    );

    // Mark using long flag
    let result = app.mark_inheritable("--verbose");
    assert!(result.is_ok());

    // Subcommand should inherit it
    let cmd = app.command("build", "Build project").unwrap();
    assert!(cmd.get_option_parser().has_option("-v"));
    assert!(cmd.get_option_parser().has_option("--verbose"));
}

#[test]
fn test_inheritable_options_with_values() {
    let mut app = Fli::new("cli", "1.0.0", "CLI app");

    // Add option with a default value
    app.add_option(
        "config",
        "Config file",
        "-c",
        "--config",
        ValueTypes::OptionalSingle(Some(Value::Str("config.toml".to_string()))),
    );

    // Mark as inheritable
    app.mark_inheritable("-c").unwrap();

    // Create subcommand
    let cmd = app.command("serve", "Start server").unwrap();

    // Should have the config option with default value
    assert!(cmd.get_option_parser().has_option("-c"));
    assert!(cmd.get_option_parser().has_option("--config"));
}

#[test]
fn test_subcommand_can_override_inherited_option() {
    let mut app = Fli::new("cli", "1.0.0", "CLI app");

    // Add option to root
    app.add_option(
        "port",
        "Port number",
        "-p",
        "--port",
        ValueTypes::OptionalSingle(Some(Value::Int(8080))),
    );
    app.mark_inheritable("-p").unwrap();

    // Create subcommand - it inherits -p
    let cmd = app.command("serve", "Start server").unwrap();

    // Subcommand can still add its own options
    cmd.add_option(
        "host",
        "Host address",
        "-h",
        "--host",
        ValueTypes::OptionalSingle(Some(Value::Str("localhost".to_string()))),
    );

    // Should have both inherited and own options
    assert!(cmd.get_option_parser().has_option("-p"));
    assert!(cmd.get_option_parser().has_option("-h"));
}