click-rs 1.0.1

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
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
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
//! Shell completion support for click-rs.
//!
//! This module provides shell completion functionality for Bash, Zsh, and Fish shells.
//! It generates completion scripts that can be sourced in each shell to provide
//! tab completion for CLI applications built with click-rs.
//!
//! # Reference
//!
//! Based on Python Click's `shell_completion.py`.
//!
//! # Example
//!
//! ```no_run
//! use click::completion::{get_completion_class, shell_complete};
//! use click::command::Command;
//!
//! let cmd = Command::new("myapp").build();
//!
//! // Generate completion script for bash
//! if let Some(completer) = get_completion_class("bash") {
//!     let script = completer.source_template();
//!     println!("{}", script.replace("%(prog_name)s", "myapp"));
//! }
//! ```

use std::env;
use std::io::{self, Write};

use crate::command::Command;
use crate::context::ContextBuilder;
use crate::group::{CommandCollection, CommandLike, Group};
use crate::parameter::Parameter;
use crate::types::CompletionItem;
use crate::utils::split_arg_string;

// =============================================================================
// ShellComplete Trait
// =============================================================================

/// Trait for shell-specific completion implementations.
///
/// Each shell has different completion mechanisms and script formats.
/// Implementations of this trait provide the necessary integration for each shell.
pub trait ShellComplete: Send + Sync {
    /// Returns the name of the shell (e.g., "bash", "zsh", "fish").
    fn name(&self) -> &str;

    /// Returns the shell script template for enabling completions.
    ///
    /// The template can contain placeholders:
    /// - `%(prog_name)s` - The program name
    /// - `%(complete_func)s` - The completion function name
    /// - `%(complete_var)s` - The completion environment variable
    fn source_template(&self) -> &str;

    /// Get completion arguments from the shell environment.
    ///
    /// Parses the completion environment variables set by the shell's completion
    /// system and returns the arguments to complete.
    fn get_completion_args(&self) -> CompletionArgs;

    /// Format a completion item for output to the shell.
    ///
    /// Each shell expects completions in a different format.
    fn format_completion(&self, item: &CompletionItem) -> String;

    /// Get the source script with placeholders replaced.
    fn get_source(&self, prog_name: &str, complete_var: &str) -> String {
        let complete_func = format!("_{}_completion", prog_name.replace('-', "_"));
        self.source_template()
            .replace("%(prog_name)s", prog_name)
            .replace("%(complete_func)s", &complete_func)
            .replace("%(complete_var)s", complete_var)
    }
}

/// Arguments parsed from the shell completion environment.
#[derive(Debug, Clone)]
pub struct CompletionArgs {
    /// The arguments up to the cursor position.
    pub args: Vec<String>,
    /// The incomplete word being typed.
    pub incomplete: String,
}

impl Default for CompletionArgs {
    fn default() -> Self {
        Self {
            args: Vec::new(),
            incomplete: String::new(),
        }
    }
}

// =============================================================================
// BashComplete
// =============================================================================

/// Bash shell completion implementation.
///
/// Uses `COMP_WORDS` and `COMP_CWORD` environment variables set by bash's
/// completion system.
#[derive(Debug, Clone, Default)]
pub struct BashComplete;

impl BashComplete {
    /// The bash completion script template.
    const SOURCE_TEMPLATE: &'static str = r#"
%(complete_func)s() {
    local IFS=$'\n'
    COMPREPLY=( $( env COMP_WORDS="${COMP_WORDS[*]}" \
                   COMP_CWORD=$COMP_CWORD \
                   %(complete_var)s=bash_complete \
                   %(prog_name)s ) )
    return 0
}

%(complete_func)s_setup() {
    complete -o default -F %(complete_func)s %(prog_name)s
}

%(complete_func)s_setup
"#;
}

impl ShellComplete for BashComplete {
    fn name(&self) -> &str {
        "bash"
    }

    fn source_template(&self) -> &str {
        Self::SOURCE_TEMPLATE
    }

    fn get_completion_args(&self) -> CompletionArgs {
        // Match Python Click: parse COMP_WORDS with shell-like splitting.
        let comp_words = env::var("COMP_WORDS").unwrap_or_default();
        let comp_cword: usize = env::var("COMP_CWORD")
            .ok()
            .and_then(|s| s.parse().ok())
            .unwrap_or(0);

        let cwords = split_arg_string(&comp_words);

        let args: Vec<String> = cwords
            .iter()
            .skip(1)
            .take(comp_cword.saturating_sub(1))
            .cloned()
            .collect();

        let incomplete = cwords.get(comp_cword).cloned().unwrap_or_default();

        CompletionArgs { args, incomplete }
    }

    fn format_completion(&self, item: &CompletionItem) -> String {
        // Match Python Click: "type,value"
        format!("{},{}", item.completion_type, item.value)
    }
}

// =============================================================================
// ZshComplete
// =============================================================================

/// Zsh shell completion implementation.
///
/// Uses the `_arguments` style completion with `COMP_WORDS` and `COMP_CWORD`.
#[derive(Debug, Clone, Default)]
pub struct ZshComplete;

impl ZshComplete {
    /// The zsh completion script template.
    const SOURCE_TEMPLATE: &'static str = r#"
#compdef %(prog_name)s

%(complete_func)s() {
    local -a completions
    local -a completions_with_descriptions
    local -a response
    (( ! $+commands[%(prog_name)s] )) && return 1

    response=("${(@f)$(env COMP_WORDS="${words[*]}" COMP_CWORD=$((CURRENT-1)) %(complete_var)s=zsh_complete %(prog_name)s)}")

    for key descr in ${(kv)response}; do
        if [[ "$descr" == "_" ]]; then
            completions+=("$key")
        else
            completions_with_descriptions+=("$key":"$descr")
        fi
    done

    if [ -n "$completions_with_descriptions" ]; then
        _describe -V unsorted completions_with_descriptions -U
    fi

    if [ -n "$completions" ]; then
        compadd -U -V unsorted -a completions
    fi
}

if [[ $zsh_eval_context[-1] == loadautofun ]]; then
    %(complete_func)s "$@"
else
    compdef %(complete_func)s %(prog_name)s
fi
"#;
}

impl ShellComplete for ZshComplete {
    fn name(&self) -> &str {
        "zsh"
    }

    fn source_template(&self) -> &str {
        Self::SOURCE_TEMPLATE
    }

    fn get_completion_args(&self) -> CompletionArgs {
        // Match Python Click: parse COMP_WORDS with shell-like splitting.
        let comp_words = env::var("COMP_WORDS").unwrap_or_default();
        let comp_cword: usize = env::var("COMP_CWORD")
            .ok()
            .and_then(|s| s.parse().ok())
            .unwrap_or(0);

        let cwords = split_arg_string(&comp_words);

        let args: Vec<String> = cwords
            .iter()
            .skip(1)
            .take(comp_cword.saturating_sub(1))
            .cloned()
            .collect();

        let incomplete = cwords.get(comp_cword).cloned().unwrap_or_default();

        CompletionArgs { args, incomplete }
    }

    fn format_completion(&self, item: &CompletionItem) -> String {
        // Match Python Click:
        // - help is "_" when absent
        // - escape ":" in value iff help != "_"
        let help = item.help.as_deref().filter(|h| !h.is_empty()).unwrap_or("_");
        let value = if help != "_" {
            item.value.replace(':', "\\:")
        } else {
            item.value.clone()
        };
        format!("{}\n{}\n{}", item.completion_type, value, help)
    }
}

// =============================================================================
// FishComplete
// =============================================================================

/// Fish shell completion implementation.
///
/// Uses Fish's native completion system with `complete` command.
#[derive(Debug, Clone, Default)]
pub struct FishComplete;

impl FishComplete {
    /// The fish completion script template.
    const SOURCE_TEMPLATE: &'static str = r#"
function %(complete_func)s
    set -l response (env %(complete_var)s=fish_complete COMP_WORDS=(commandline -cp) COMP_CWORD=(commandline -t) %(prog_name)s)

    for completion in $response
        set -l metadata (string split "," -- $completion)

        if [ $metadata[1] = "dir" ]
            __fish_complete_directories $metadata[2]
        else if [ $metadata[1] = "file" ]
            __fish_complete_path $metadata[2]
        else if [ $metadata[1] = "plain" ]
            echo $metadata[2]
        end
    end
end

complete -c %(prog_name)s -f -a "(%(complete_func)s)"
"#;
}

impl ShellComplete for FishComplete {
    fn name(&self) -> &str {
        "fish"
    }

    fn source_template(&self) -> &str {
        Self::SOURCE_TEMPLATE
    }

    fn get_completion_args(&self) -> CompletionArgs {
        // Match Python Click:
        // - COMP_WORDS is split using shell-like rules
        // - COMP_CWORD contains the incomplete word (not an index)
        // - remove incomplete from args if it appears as the last token
        let comp_words = env::var("COMP_WORDS").unwrap_or_default();
        let mut incomplete = env::var("COMP_CWORD").unwrap_or_default();
        if !incomplete.is_empty() {
            incomplete = split_arg_string(&incomplete)
                .into_iter()
                .next()
                .unwrap_or_default();
        }

        let mut args: Vec<String> = split_arg_string(&comp_words).into_iter().skip(1).collect();
        if !incomplete.is_empty() && args.last().is_some_and(|a| a == &incomplete) {
            args.pop();
        }

        CompletionArgs { args, incomplete }
    }

    fn format_completion(&self, item: &CompletionItem) -> String {
        // Match Python Click:
        // - "type,value\\thelp" when help exists
        // - otherwise "type,value"
        if let Some(help) = item.help.as_deref().filter(|h| !h.is_empty()) {
            format!("{},{}\t{}", item.completion_type, item.value, help)
        } else {
            format!("{},{}", item.completion_type, item.value)
        }
    }
}

// =============================================================================
// Shell Registry
// =============================================================================

/// Get a shell completion implementation by name.
///
/// # Arguments
///
/// * `shell` - The shell name ("bash", "zsh", or "fish")
///
/// # Returns
///
/// Returns `Some(Box<dyn ShellComplete>)` if the shell is supported, `None` otherwise.
///
/// # Example
///
/// ```rust
/// use click::completion::get_completion_class;
///
/// if let Some(completer) = get_completion_class("bash") {
///     println!("Shell: {}", completer.name());
/// }
/// ```
pub fn get_completion_class(shell: &str) -> Option<Box<dyn ShellComplete>> {
    match shell.to_lowercase().as_str() {
        "bash" => Some(Box::new(BashComplete)),
        "zsh" => Some(Box::new(ZshComplete)),
        "fish" => Some(Box::new(FishComplete)),
        _ => None,
    }
}

/// Detect the current shell from environment.
///
/// Checks `SHELL` environment variable and tries to determine the shell type.
///
/// # Returns
///
/// Returns the shell name if detected, or `None` if unknown.
pub fn detect_shell() -> Option<String> {
    env::var("SHELL").ok().and_then(|shell| {
        let shell_name = shell.rsplit('/').next()?;
        match shell_name {
            "bash" => Some("bash".to_string()),
            "zsh" => Some("zsh".to_string()),
            "fish" => Some("fish".to_string()),
            _ => None,
        }
    })
}

/// List all supported shell names.
pub fn list_shells() -> Vec<&'static str> {
    vec!["bash", "zsh", "fish"]
}

// =============================================================================
// Completion Functions
// =============================================================================

/// Main shell completion entry point.
///
/// This function should be called when the completion environment variable is set.
/// It parses the completion arguments, generates completions, and outputs them
/// in the appropriate format for the shell.
///
/// # Arguments
///
/// * `cmd` - The root command to complete for
/// * `prog_name` - The program name
/// * `complete_var` - The environment variable name that triggers completion
///
/// # Example
///
/// ```no_run
/// use click::completion::shell_complete;
/// use click::command::Command;
/// use std::env;
///
/// let cmd = Command::new("myapp").build();
///
/// // In your main(), check if completion is requested
/// if let Ok(shell) = env::var("_MYAPP_COMPLETE") {
///     shell_complete(&cmd, "myapp", "_MYAPP_COMPLETE");
///     return;
/// }
/// ```
pub fn shell_complete(cmd: &dyn CommandLike, prog_name: &str, complete_var: &str) {
    // Get the shell type from the completion variable
    let shell_type = match env::var(complete_var) {
        Ok(val) => {
            // The value is typically "bash_complete", "zsh_complete", etc.
            val.split('_').next().unwrap_or("bash").to_string()
        }
        Err(_) => return,
    };

    let completer = match get_completion_class(&shell_type) {
        Some(c) => c,
        None => return,
    };

    // Get completion arguments from environment
    let comp_args = completer.get_completion_args();

    // Generate completions
    let completions = get_completions(cmd, prog_name, &comp_args.args, &comp_args.incomplete);

    // Output completions in shell-specific format
    let stdout = io::stdout();
    let mut handle = stdout.lock();
    for item in completions {
        let _ = writeln!(handle, "{}", completer.format_completion(&item));
    }
}

/// Get completions for a command.
///
/// This is the internal completion generation function that can be used
/// for testing or custom completion implementations.
///
/// # Arguments
///
/// * `cmd` - The command to complete for
/// * `prog_name` - The program name
/// * `args` - The arguments entered so far
/// * `incomplete` - The incomplete word being typed
///
/// # Returns
///
/// A vector of completion items.
pub fn get_completions(
    cmd: &dyn CommandLike,
    prog_name: &str,
    args: &[String],
    incomplete: &str,
) -> Vec<CompletionItem> {
    let mut completions = Vec::new();

    // Create a resilient parsing context
    let ctx = ContextBuilder::new()
        .info_name(prog_name)
        .resilient_parsing(true)
        .build();

    // Check if we're completing a subcommand
    if let Some(group) = cmd.as_any().downcast_ref::<Group>() {
        // First check if the first arg is a valid subcommand - if so, recurse into it
        if !args.is_empty() {
            if let Some(subcmd) = group.get_command(&args[0]) {
                let remaining_args: Vec<String> = args[1..].to_vec();
                return get_completions(subcmd, &args[0], &remaining_args, incomplete);
            }
        }

        // Otherwise, list subcommand completions
        for name in group.list_commands() {
            if name.starts_with(incomplete) {
                let subcmd = group.get_command(name);
                let help = subcmd.map(|c| c.get_short_help());
                let mut item = CompletionItem::new(name);
                if let Some(h) = help {
                    if !h.is_empty() {
                        item = item.with_help(h);
                    }
                }
                completions.push(item);
            }
        }
    }
    if let Some(collection) = cmd.as_any().downcast_ref::<CommandCollection>() {
        if !args.is_empty() {
            if let Some(subcmd) = collection.get_command(&args[0]) {
                let remaining_args: Vec<String> = args[1..].to_vec();
                return get_completions(subcmd, &args[0], &remaining_args, incomplete);
            }
        }

        for name in collection.list_commands() {
            if name.starts_with(incomplete) {
                let subcmd = collection.get_command(&name);
                let help = subcmd.map(|c| c.get_short_help());
                let mut item = CompletionItem::new(&name);
                if let Some(h) = help {
                    if !h.is_empty() {
                        item = item.with_help(h);
                    }
                }
                completions.push(item);
            }
        }
    }

    // Complete options
    if incomplete.starts_with('-') || completions.is_empty() {
        if let Some(command) = cmd.as_any().downcast_ref::<Command>() {
            completions.extend(get_option_completions(command, &ctx, args, incomplete));
        } else if let Some(group) = cmd.as_any().downcast_ref::<Group>() {
            completions.extend(get_option_completions(&group.command, &ctx, args, incomplete));
        } else if let Some(collection) = cmd.as_any().downcast_ref::<CommandCollection>() {
            completions.extend(get_option_completions(
                &collection.base.command,
                &ctx,
                args,
                incomplete,
            ));
        }
    }

    // Complete arguments (if not completing an option)
    if !incomplete.starts_with('-') {
        if let Some(command) = cmd.as_any().downcast_ref::<Command>() {
            completions.extend(get_argument_completions(command, &ctx, args, incomplete));
        } else if let Some(group) = cmd.as_any().downcast_ref::<Group>() {
            completions.extend(get_argument_completions(&group.command, &ctx, args, incomplete));
        } else if let Some(collection) = cmd.as_any().downcast_ref::<CommandCollection>() {
            completions.extend(get_argument_completions(&collection.base.command, &ctx, args, incomplete));
        }
    }

    completions
}

/// Get argument completions for a command.
///
/// Determines which argument is being completed based on the number of
/// positional arguments already provided, then calls the argument's
/// `get_completions` method.
fn get_argument_completions(
    cmd: &Command,
    ctx: &crate::context::Context,
    args: &[String],
    incomplete: &str,
) -> Vec<CompletionItem> {
    // Count how many positional arguments have been consumed
    // (arguments that don't start with '-' and aren't option values)
    let positional_count = args
        .iter()
        .filter(|a| !a.starts_with('-'))
        .count();

    // Find the argument at that position
    if let Some(arg) = cmd.arguments.get(positional_count) {
        return arg.get_completions(ctx, incomplete);
    }

    // If we have variadic arguments, the last argument can complete more values
    if let Some(last_arg) = cmd.arguments.last() {
        if last_arg.multiple() {
            return last_arg.get_completions(ctx, incomplete);
        }
    }

    Vec::new()
}

/// Get option completions for a command.
fn get_option_completions(
    cmd: &Command,
    ctx: &crate::context::Context,
    args: &[String],
    incomplete: &str,
) -> Vec<CompletionItem> {
    // Completing an inline option value: --name=value
    if let Some((opt_name, value_prefix)) = incomplete.split_once('=') {
        if opt_name.starts_with("--") {
            if let Some(opt) = cmd
                .options
                .iter()
                .find(|o| o.long.iter().any(|long| long == opt_name) && option_accepts_value(o))
            {
                return opt
                    .get_completions(ctx, value_prefix)
                    .into_iter()
                    .map(|item| {
                        let mut with_prefix = CompletionItem::new(format!(
                            "{}={}",
                            opt_name, item.value
                        ));
                        if let Some(help) = item.help {
                            with_prefix = with_prefix.with_help(help);
                        }
                        with_prefix
                    })
                    .collect();
            }
        }
    }

    // Completing an option value provided as the next token: --name <TAB>
    if let Some(last_arg) = args.last() {
        if let Some(opt) = cmd.options.iter().find(|o| {
            option_accepts_value(o)
                && (o.long.iter().any(|long| long == last_arg)
                    || o.short.iter().any(|short| short == last_arg))
        }) {
            return opt.get_completions(ctx, incomplete);
        }
    }

    let mut completions = Vec::new();

    for opt in &cmd.options {
        // Add long options
        for long in &opt.long {
            if long.starts_with(incomplete) {
                let mut item = CompletionItem::new(long);
                if let Some(help) = opt.help() {
                    item = item.with_help(help.to_string());
                }
                completions.push(item);
            }
        }

        // Add short options
        for short in &opt.short {
            if short.starts_with(incomplete) {
                let mut item = CompletionItem::new(short);
                if let Some(help) = opt.help() {
                    item = item.with_help(help.to_string());
                }
                completions.push(item);
            }
        }
    }

    // Add help option completion (if enabled).
    if let Some(help_opt) = cmd.get_help_option(ctx) {
        for long in &help_opt.long {
            if long.starts_with(incomplete) {
                let mut item = CompletionItem::new(long);
                if let Some(help) = help_opt.help() {
                    if !help.is_empty() {
                        item = item.with_help(help.to_string());
                    }
                }
                completions.push(item);
            }
        }
        for short in &help_opt.short {
            if short.starts_with(incomplete) {
                let mut item = CompletionItem::new(short);
                if let Some(help) = help_opt.help() {
                    if !help.is_empty() {
                        item = item.with_help(help.to_string());
                    }
                }
                completions.push(item);
            }
        }
    }

    completions
}

fn option_accepts_value(opt: &crate::option::ClickOption) -> bool {
    !opt.is_flag && !opt.count
}

/// Add the shell completion option to a command.
///
/// This returns options that can be added to a command to enable
/// shell completion script generation.
///
/// # Example
///
/// ```
/// use click::completion::make_completion_option;
///
/// let opt = make_completion_option("_MYAPP_COMPLETE");
/// assert!(!opt.is_completion_requested());
/// ```
pub fn make_completion_option(complete_var: &str) -> CompletionOption {
    CompletionOption {
        complete_var: complete_var.to_string(),
    }
}

/// Configuration for the shell completion option.
#[derive(Debug, Clone)]
pub struct CompletionOption {
    /// The environment variable that triggers completion.
    pub complete_var: String,
}

impl CompletionOption {
    /// Check if completion is requested via environment variable.
    pub fn is_completion_requested(&self) -> bool {
        env::var(&self.complete_var).is_ok()
    }

    /// Get the completion shell type if completion is requested.
    pub fn get_completion_shell(&self) -> Option<String> {
        env::var(&self.complete_var).ok().and_then(|val| {
            // Values are like "bash_complete", "bash_source", etc.
            let parts: Vec<&str> = val.split('_').collect();
            if parts.len() >= 2 {
                Some(parts[0].to_string())
            } else {
                None
            }
        })
    }

    /// Check if this is a "source" request (print the completion script).
    pub fn is_source_request(&self) -> bool {
        env::var(&self.complete_var)
            .map(|v| v.ends_with("_source"))
            .unwrap_or(false)
    }

    /// Handle completion if requested.
    ///
    /// Returns `true` if completion was handled and the program should exit.
    pub fn handle_completion(
        &self,
        cmd: &dyn CommandLike,
        prog_name: &str,
    ) -> bool {
        if !self.is_completion_requested() {
            return false;
        }

        if let Some(shell) = self.get_completion_shell() {
            if self.is_source_request() {
                // Print the completion script
                if let Some(completer) = get_completion_class(&shell) {
                    println!("{}", completer.get_source(prog_name, &self.complete_var));
                }
            } else {
                // Generate completions
                shell_complete(cmd, prog_name, &self.complete_var);
            }
            return true;
        }

        false
    }
}

// =============================================================================
// Tests
// =============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_get_completion_class() {
        assert!(get_completion_class("bash").is_some());
        assert!(get_completion_class("zsh").is_some());
        assert!(get_completion_class("fish").is_some());
        assert!(get_completion_class("unknown").is_none());

        // Case insensitive
        assert!(get_completion_class("BASH").is_some());
        assert!(get_completion_class("Zsh").is_some());
    }

    #[test]
    fn test_shell_names() {
        let bash = BashComplete;
        assert_eq!(bash.name(), "bash");

        let zsh = ZshComplete;
        assert_eq!(zsh.name(), "zsh");

        let fish = FishComplete;
        assert_eq!(fish.name(), "fish");
    }

    #[test]
    fn test_list_shells() {
        let shells = list_shells();
        assert!(shells.contains(&"bash"));
        assert!(shells.contains(&"zsh"));
        assert!(shells.contains(&"fish"));
    }

    #[test]
    fn test_bash_source_template() {
        let bash = BashComplete;
        let source = bash.get_source("myapp", "_MYAPP_COMPLETE");

        assert!(source.contains("myapp"));
        assert!(source.contains("_MYAPP_COMPLETE"));
        assert!(source.contains("_myapp_completion"));
        assert!(source.contains("COMP_WORDS"));
        assert!(source.contains("COMP_CWORD"));
    }

    #[test]
    fn test_zsh_source_template() {
        let zsh = ZshComplete;
        let source = zsh.get_source("myapp", "_MYAPP_COMPLETE");

        assert!(source.contains("#compdef myapp"));
        assert!(source.contains("_MYAPP_COMPLETE"));
        assert!(source.contains("_myapp_completion"));
    }

    #[test]
    fn test_fish_source_template() {
        let fish = FishComplete;
        let source = fish.get_source("myapp", "_MYAPP_COMPLETE");

        assert!(source.contains("function _myapp_completion"));
        assert!(source.contains("_MYAPP_COMPLETE"));
        assert!(source.contains("complete -c myapp"));
    }

    #[test]
    fn test_bash_format_completion() {
        let bash = BashComplete;

        let item = CompletionItem::new("--help");
        assert_eq!(bash.format_completion(&item), "plain,--help");

        let item_with_help = CompletionItem::new("--name").with_help("Specify name");
        assert_eq!(bash.format_completion(&item_with_help), "plain,--name");
    }

    #[test]
    fn test_zsh_format_completion() {
        let zsh = ZshComplete;

        let item = CompletionItem::new("--help");
        assert_eq!(zsh.format_completion(&item), "plain\n--help\n_");

        let item_with_help = CompletionItem::new("--name").with_help("Specify name");
        assert_eq!(
            zsh.format_completion(&item_with_help),
            "plain\n--name\nSpecify name"
        );
    }

    #[test]
    fn test_fish_format_completion() {
        let fish = FishComplete;

        let item = CompletionItem::new("--help");
        assert_eq!(fish.format_completion(&item), "plain,--help");

        let item_file = CompletionItem::with_type("path", "file");
        assert_eq!(fish.format_completion(&item_file), "file,path");
    }

    #[test]
    fn test_completion_args_default() {
        let args = CompletionArgs::default();
        assert!(args.args.is_empty());
        assert!(args.incomplete.is_empty());
    }

    #[test]
    fn test_get_completions_empty() {
        let cmd = Command::new("test").build();
        let completions = get_completions(&cmd, "test", &[], "");

        // Should at least have --help
        assert!(completions.iter().any(|c| c.value == "--help"));
    }

    #[test]
    fn test_get_completions_options() {
        let cmd = Command::new("test")
            .option(
                crate::option::ClickOption::new(&["--name", "-n"])
                    .help("The name")
                    .build(),
            )
            .build();

        let completions = get_completions(&cmd, "test", &[], "--");

        assert!(completions.iter().any(|c| c.value == "--name"));
        assert!(completions.iter().any(|c| c.value == "--help"));
    }

    #[test]
    fn test_get_completions_subcommands() {
        let group = Group::new("cli")
            .command(Command::new("init").help("Initialize").build())
            .command(Command::new("build").help("Build").build())
            .build();

        let completions = get_completions(&group, "cli", &[], "");

        assert!(completions.iter().any(|c| c.value == "init"));
        assert!(completions.iter().any(|c| c.value == "build"));
    }

    #[test]
    fn test_get_completions_subcommand_prefix() {
        let group = Group::new("cli")
            .command(Command::new("init").build())
            .command(Command::new("install").build())
            .command(Command::new("build").build())
            .build();

        let completions = get_completions(&group, "cli", &[], "in");

        assert!(completions.iter().any(|c| c.value == "init"));
        assert!(completions.iter().any(|c| c.value == "install"));
        assert!(!completions.iter().any(|c| c.value == "build"));
    }

    #[test]
    fn test_completion_option() {
        let opt = make_completion_option("_TEST_COMPLETE");
        assert_eq!(opt.complete_var, "_TEST_COMPLETE");
    }

    #[test]
    fn test_completion_option_not_requested() {
        // Clear any existing env var
        env::remove_var("_TEST_COMPLETE");

        let opt = make_completion_option("_TEST_COMPLETE");
        assert!(!opt.is_completion_requested());
        assert!(opt.get_completion_shell().is_none());
        assert!(!opt.is_source_request());
    }

    #[test]
    fn test_prog_name_with_dash() {
        let bash = BashComplete;
        let source = bash.get_source("my-app", "_MY_APP_COMPLETE");

        // Function name should have underscore, not dash
        assert!(source.contains("_my_app_completion"));
    }

    // =========================================================================
    // Tests for argument completions
    // =========================================================================

    #[test]
    fn test_get_completions_argument_with_choice_type() {
        use crate::argument::Argument;
        use crate::types::Choice;

        let cmd = Command::new("test")
            .argument(
                Argument::new("format")
                    .type_(Choice::new(["json", "xml", "yaml"]))
                    .build()
            )
            .build();

        let completions = get_completions(&cmd, "test", &[], "j");

        assert_eq!(completions.len(), 1);
        assert_eq!(completions[0].value, "json");
    }

    #[test]
    fn test_get_completions_argument_with_custom_callback() {
        use crate::argument::Argument;

        let cmd = Command::new("test")
            .argument(
                Argument::new("filename")
                    .shell_complete(|_ctx, incomplete| {
                        vec![
                            CompletionItem::new(format!("{}.txt", incomplete)),
                            CompletionItem::new(format!("{}.md", incomplete)),
                        ]
                    })
                    .build()
            )
            .build();

        let completions = get_completions(&cmd, "test", &[], "file");

        assert_eq!(completions.len(), 2);
        assert!(completions.iter().any(|c| c.value == "file.txt"));
        assert!(completions.iter().any(|c| c.value == "file.md"));
    }

    #[test]
    fn test_get_completions_second_argument() {
        use crate::argument::Argument;
        use crate::types::Choice;

        let cmd = Command::new("test")
            .argument(Argument::new("first").build())
            .argument(
                Argument::new("second")
                    .type_(Choice::new(["a", "b", "c"]))
                    .build()
            )
            .build();

        // First argument has no completions (STRING type)
        let completions = get_completions(&cmd, "test", &[], "x");
        assert!(completions.is_empty());

        // Second argument should complete after first is provided
        let completions = get_completions(&cmd, "test", &["value1".to_string()], "a");
        assert_eq!(completions.len(), 1);
        assert_eq!(completions[0].value, "a");
    }

    #[test]
    fn test_get_completions_variadic_argument() {
        use crate::argument::Argument;
        use crate::types::Choice;

        let cmd = Command::new("test")
            .argument(
                Argument::new("files")
                    .multiple()
                    .type_(Choice::new(["foo.txt", "bar.txt", "baz.txt"]))
                    .build()
            )
            .build();

        // First value
        let completions = get_completions(&cmd, "test", &[], "f");
        assert_eq!(completions.len(), 1);
        assert_eq!(completions[0].value, "foo.txt");

        // Second value (variadic continues to complete)
        let completions = get_completions(&cmd, "test", &["foo.txt".to_string()], "b");
        assert_eq!(completions.len(), 2);
        assert!(completions.iter().any(|c| c.value == "bar.txt"));
        assert!(completions.iter().any(|c| c.value == "baz.txt"));
    }

    #[test]
    fn test_get_completions_no_more_arguments() {
        use crate::argument::Argument;

        let cmd = Command::new("test")
            .argument(Argument::new("single").build())
            .build();

        // After the single argument is provided, no more completions
        let completions = get_completions(&cmd, "test", &["value".to_string()], "x");
        // Should be empty (no more arguments to complete)
        // Note: options might still complete if incomplete doesn't start with '-'
        assert!(completions.is_empty());
    }
}