click-rs 1.0.2

A Rust port of Python's Click library for creating command-line interfaces
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
//! Integration tests for the derive macros.

use click::CommandLike;
use click_derive::Command;
use std::sync::atomic::{AtomicBool, Ordering};

/// A simple greeter command
#[derive(Command)]
#[command(name = "greet")]
struct Greet {
    /// Name to greet
    #[argument]
    name: String,

    /// Number of times to greet
    #[option(short, long, default = 1)]
    count: i32,

    /// Be verbose
    #[option(short, long)]
    verbose: bool,
}

#[test]
fn test_command_generation() {
    let cmd = Greet::command();

    // Verify command name
    assert_eq!(cmd.name.as_deref(), Some("greet"));

    // Verify we have arguments and options
    assert!(!cmd.arguments.is_empty());
    assert!(!cmd.options.is_empty());
}

#[test]
fn test_command_help() {
    let cmd = Greet::command();
    let ctx = click::ContextBuilder::new().info_name("greet").build();
    let help = cmd.get_help(&ctx);

    // Help should contain relevant information
    assert!(help.contains("greet"), "Help should contain command name");
    assert!(
        help.contains("NAME") || help.contains("name"),
        "Help should contain argument"
    );
    assert!(
        help.contains("--count") || help.contains("-c"),
        "Help should contain count option"
    );
    assert!(
        help.contains("--verbose") || help.contains("-v"),
        "Help should contain verbose option"
    );
}

#[test]
fn test_command_with_callback() {
    use std::sync::{Arc, Mutex};

    let output = Arc::new(Mutex::new(Vec::new()));
    let output_clone = output.clone();

    let cmd = Greet::command_with_run(move |greet, _ctx| {
        for _ in 0..greet.count {
            output_clone
                .lock()
                .unwrap()
                .push(format!("Hello, {}!", greet.name));
        }
        if greet.verbose {
            output_clone
                .lock()
                .unwrap()
                .push("(verbose mode)".to_string());
        }
        Ok(())
    });

    // Test with default count
    cmd.main(vec!["World".to_string()]).unwrap();
    let lines = output.lock().unwrap();
    assert_eq!(lines.len(), 1);
    assert_eq!(lines[0], "Hello, World!");
    drop(lines);

    // Reset and test with count
    output.lock().unwrap().clear();
    let output_clone = output.clone();
    let cmd = Greet::command_with_run(move |greet, _ctx| {
        for _ in 0..greet.count {
            output_clone
                .lock()
                .unwrap()
                .push(format!("Hello, {}!", greet.name));
        }
        Ok(())
    });

    cmd.main(vec![
        "--count".to_string(),
        "3".to_string(),
        "Alice".to_string(),
    ])
    .unwrap();
    let lines = output.lock().unwrap();
    assert_eq!(lines.len(), 3);
    for line in lines.iter() {
        assert_eq!(line, "Hello, Alice!");
    }
}

#[test]
fn test_command_from_context() {
    let cmd = Greet::command();
    let ctx = cmd
        .make_context(
            "greet",
            vec!["--count".to_string(), "2".to_string(), "Bob".to_string()],
            None,
        )
        .unwrap();

    let greet = Greet::from_context(&ctx).unwrap();
    assert_eq!(greet.name, "Bob");
    assert_eq!(greet.count, 2);
    assert!(!greet.verbose);
}

/// Test command with optional argument
#[derive(Command)]
#[command(name = "optional")]
struct OptionalArgs {
    /// Optional filename
    #[argument(required = false)]
    filename: Option<String>,

    /// Optional output
    #[option(short, long)]
    output: Option<String>,
}

#[test]
fn test_optional_argument() {
    let cmd = OptionalArgs::command();

    // Without argument
    let ctx = cmd.make_context("optional", vec![], None).unwrap();
    let args = OptionalArgs::from_context(&ctx).unwrap();
    assert!(args.filename.is_none());
    assert!(args.output.is_none());

    // With argument
    let ctx = cmd
        .make_context("optional", vec!["test.txt".to_string()], None)
        .unwrap();
    let args = OptionalArgs::from_context(&ctx).unwrap();
    assert_eq!(args.filename, Some("test.txt".to_string()));
}

/// Test command with multiple values
#[derive(Command)]
#[command(name = "multi")]
struct MultiValues {
    /// Input files
    #[argument(multiple)]
    files: Vec<String>,

    /// Tags
    #[option(short, long, multiple)]
    tags: Vec<String>,
}

#[test]
fn test_multiple_values() {
    let cmd = MultiValues::command();

    let ctx = cmd
        .make_context(
            "multi",
            vec![
                "-t".to_string(),
                "tag1".to_string(),
                "-t".to_string(),
                "tag2".to_string(),
                "file1.txt".to_string(),
                "file2.txt".to_string(),
            ],
            None,
        )
        .unwrap();

    let args = MultiValues::from_context(&ctx).unwrap();
    assert_eq!(args.files, vec!["file1.txt", "file2.txt"]);
    assert_eq!(args.tags, vec!["tag1", "tag2"]);
}

/// Test command with count option
#[derive(Command)]
#[command(name = "verbose")]
struct VerboseCmd {
    /// Verbosity level
    #[option(short, long, count)]
    verbose: usize,
}

#[test]
fn test_count_option() {
    let cmd = VerboseCmd::command();

    // No -v flags
    let ctx = cmd.make_context("verbose", vec![], None).unwrap();
    let args = VerboseCmd::from_context(&ctx).unwrap();
    assert_eq!(args.verbose, 0);

    // Multiple -v flags
    let ctx = cmd
        .make_context(
            "verbose",
            vec!["-v".to_string(), "-v".to_string(), "-v".to_string()],
            None,
        )
        .unwrap();
    let args = VerboseCmd::from_context(&ctx).unwrap();
    assert_eq!(args.verbose, 3);
}

/// Test command with doc comment as help
#[derive(Command)]
#[command(name = "documented")]
/// This is a documented command.
///
/// It has multiple lines of documentation.
struct DocumentedCmd {
    /// This argument has help from doc comment
    #[argument]
    #[allow(dead_code)]
    input: String,
}

#[test]
fn test_doc_comment_help() {
    let cmd = DocumentedCmd::command();
    let ctx = click::ContextBuilder::new().info_name("documented").build();
    let help = cmd.get_help(&ctx);

    assert!(
        help.contains("documented"),
        "Help should contain command name"
    );
}

/// Test command with hidden option
#[derive(Command)]
#[command(name = "hidden")]
struct HiddenCmd {
    /// A visible option
    #[option(long)]
    #[allow(dead_code)]
    visible: String,

    /// A hidden option
    #[option(long, hidden)]
    #[allow(dead_code)]
    secret: String,
}

#[test]
fn test_hidden_option() {
    let cmd = HiddenCmd::command();
    let ctx = click::ContextBuilder::new().info_name("hidden").build();
    let help = cmd.get_help(&ctx);

    assert!(
        help.contains("--visible"),
        "Help should contain visible option"
    );
    assert!(
        !help.contains("--secret"),
        "Help should not contain hidden option"
    );
}

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

#[derive(Command)]
#[command(name = "auto-run", run)]
struct AutoRunCmd {
    #[argument(required = false)]
    #[allow(dead_code)]
    input: Option<String>,
}

impl AutoRunCmd {
    fn run(&self, _ctx: &click::Context) -> click::Result<()> {
        AUTO_RUN_CALLED.store(true, Ordering::SeqCst);
        Ok(())
    }
}

#[test]
fn test_command_run_attribute_wires_callback() {
    AUTO_RUN_CALLED.store(false, Ordering::SeqCst);
    let cmd = AutoRunCmd::command();
    assert!(cmd.callback.is_some());
    cmd.main(vec![]).unwrap();
    assert!(AUTO_RUN_CALLED.load(Ordering::SeqCst));
}

#[click::command(name = "fn-greet", help = "Function macro test command")]
fn fn_greet(
    #[argument] name: String,
    #[option(short, long, default = 1)] count: i32,
) -> click::Result<()> {
    for _ in 0..count {
        let _ = &name;
    }
    Ok(())
}

#[test]
fn test_function_command_attribute_macro() {
    let cmd = fn_greet_command();
    assert_eq!(cmd.name.as_deref(), Some("fn-greet"));
    assert!(cmd.main(vec![]).is_err());
    assert!(cmd
        .main(vec![
            "--count".to_string(),
            "2".to_string(),
            "Bob".to_string()
        ])
        .is_ok());
}

#[click::command(name = "leaf-one")]
fn leaf_one() -> click::Result<()> {
    Ok(())
}

#[click::command(name = "leaf-two")]
fn leaf_two() -> click::Result<()> {
    Ok(())
}

#[click::command(name = "nested-leaf")]
fn nested_leaf() -> click::Result<()> {
    Ok(())
}

#[click::group(name = "nested", commands = [nested_leaf])]
fn nested() -> click::Result<()> {
    Ok(())
}

#[click::group(name = "root", commands = [leaf_one, leaf_two], groups = [nested])]
fn root(#[option(short, long)] verbose: bool) -> click::Result<()> {
    let _ = verbose;
    Ok(())
}

#[test]
fn test_function_group_attribute_macro_with_attachments() {
    let group = root_group();
    assert_eq!(group.command.name.as_deref(), Some("root"));
    let names = group.list_commands();
    assert_eq!(names, vec!["leaf-one", "leaf-two", "nested"]);
    assert!(CommandLike::main(&group, vec!["leaf-one".to_string()]).is_ok());
    assert!(group
        .main(vec!["nested".to_string(), "nested-leaf".to_string()])
        .is_ok());
}

// Note: Environment variable support in options is defined at the struct level
// but full envvar parsing integration requires additional work in the parser.
// The derive macro sets up the envvar attribute correctly; the parser needs
// to be enhanced to read from environment variables when CLI args are missing.

fn complete_names(_ctx: &click::Context, incomplete: &str) -> Vec<click::CompletionItem> {
    ["alice", "bob", "charlie"]
        .into_iter()
        .filter(|name| name.starts_with(incomplete))
        .map(click::CompletionItem::new)
        .collect()
}

fn complete_envs(_ctx: &click::Context, incomplete: &str) -> Vec<click::CompletionItem> {
    ["HOME", "HOSTNAME", "PATH"]
        .into_iter()
        .filter(|name| name.starts_with(incomplete))
        .map(click::CompletionItem::new)
        .collect()
}

#[derive(Command)]
#[command(name = "complete-me")]
struct DerivedShellComplete {
    #[argument(shell_complete = complete_names)]
    #[allow(dead_code)]
    name: String,

    #[option(long, shell_complete = complete_envs)]
    #[allow(dead_code)]
    env: Option<String>,
}

#[test]
fn test_derive_shell_complete_attributes() {
    let cmd = DerivedShellComplete::command();

    let arg_completions = click::completion::get_completions(&cmd, "complete-me", &[], "a");
    assert!(arg_completions.iter().any(|c| c.value == "alice"));

    let opt_value_completions =
        click::completion::get_completions(&cmd, "complete-me", &["--env".to_string()], "HO");
    assert!(opt_value_completions.iter().any(|c| c.value == "HOME"));
    assert!(opt_value_completions.iter().any(|c| c.value == "HOSTNAME"));
}

#[derive(Command)]
#[command(name = "dest-opt")]
struct DestOptionCmd {
    #[option(long = "actual-name", dest = "alias_name")]
    alias_name: String,
}

#[test]
fn test_derive_option_dest_attribute() {
    let cmd = DestOptionCmd::command();
    let ctx = cmd
        .make_context(
            "dest-opt",
            vec!["--actual-name".to_string(), "value".to_string()],
            None,
        )
        .unwrap();

    let parsed = DestOptionCmd::from_context(&ctx).unwrap();
    assert_eq!(parsed.alias_name, "value");
}

#[derive(Command)]
#[command(name = "typed-adapters")]
struct TypedAdaptersCmd {
    #[argument(nargs = -1, type = click::INT)]
    nums: Vec<i64>,

    #[option(long, type = click::INT)]
    limit: i64,
}

#[test]
fn test_derive_type_and_nargs_adapters() {
    let cmd = TypedAdaptersCmd::command();
    let ctx = cmd
        .make_context(
            "typed-adapters",
            vec![
                "--limit".to_string(),
                "5".to_string(),
                "10".to_string(),
                "20".to_string(),
                "30".to_string(),
            ],
            None,
        )
        .unwrap();

    let parsed = TypedAdaptersCmd::from_context(&ctx).unwrap();
    assert_eq!(parsed.limit, 5);
    assert_eq!(parsed.nums, vec![10, 20, 30]);
}

fn validate_even_count(value: &i64) -> std::result::Result<(), String> {
    if *value > 0 && *value % 2 == 0 {
        Ok(())
    } else {
        Err("Should be a positive, even integer.".to_string())
    }
}

fn validate_nonempty_name(value: &String) -> std::result::Result<(), String> {
    if value.trim().is_empty() {
        Err("Name cannot be empty".to_string())
    } else {
        Ok(())
    }
}

#[derive(Command)]
#[command(name = "validated")]
struct ValidatedCmd {
    #[option(long, type = click::INT, validate = validate_even_count)]
    count: i64,

    #[argument(validate = validate_nonempty_name)]
    name: String,
}

#[test]
fn test_declarative_validators_success() {
    let cmd = ValidatedCmd::command();
    let ctx = cmd
        .make_context(
            "validated",
            vec!["--count".to_string(), "4".to_string(), "alice".to_string()],
            None,
        )
        .unwrap();
    let parsed = ValidatedCmd::from_context(&ctx).unwrap();
    assert_eq!(parsed.count, 4);
    assert_eq!(parsed.name, "alice");
}

#[test]
fn test_declarative_validators_failure() {
    let cmd = ValidatedCmd::command();
    let err = cmd
        .make_context(
            "validated",
            vec!["--count".to_string(), "3".to_string(), "alice".to_string()],
            None,
        )
        .unwrap_err();
    assert!(err.to_string().contains("positive, even integer"));
}