@@ -3,6 +3,7 @@ use std::ffi::OsStr;
3
3
use std:: sync:: Arc ;
4
4
5
5
use clap:: builder:: ArgExt ;
6
+ use clap:: builder:: CommandExt ;
6
7
use clap_lex:: OsStrExt as _;
7
8
8
9
use super :: CompletionCandidate ;
@@ -52,8 +53,54 @@ impl std::fmt::Debug for ArgValueCandidates {
52
53
53
54
impl ArgExt for ArgValueCandidates { }
54
55
56
+ /// Extend [`Command`][clap::Command] with a [`ValueCandidates`]
57
+ ///
58
+ /// # Example
59
+ /// ```rust
60
+ /// use clap::Parser;
61
+ /// use clap_complete::engine::{SubcommandCandidates, CompletionCandidate};
62
+ /// #[derive(Debug, Parser)]
63
+ /// #[clap(name = "cli", add = SubcommandCandidates::new(|| { vec![
64
+ /// CompletionCandidate::new("foo"),
65
+ /// CompletionCandidate::new("bar"),
66
+ /// CompletionCandidate::new("baz")] }))]
67
+ /// struct Cli {
68
+ /// #[arg(long)]
69
+ /// input: Option<String>,
70
+ /// }
71
+ /// ```
72
+ #[ derive( Clone ) ]
73
+ pub struct SubcommandCandidates ( Arc < dyn ValueCandidates > ) ;
74
+
75
+ impl SubcommandCandidates {
76
+ /// Create a new `SubcommandCandidates` with a custom completer
77
+ pub fn new < C > ( completer : C ) -> Self
78
+ where
79
+ C : ValueCandidates + ' static ,
80
+ {
81
+ Self ( Arc :: new ( completer) )
82
+ }
83
+
84
+ /// All potential candidates for an external subcommand.
85
+ ///
86
+ /// See [`CompletionCandidate`] for more information.
87
+ pub fn candidates ( & self ) -> Vec < CompletionCandidate > {
88
+ self . 0 . candidates ( )
89
+ }
90
+ }
91
+
92
+ impl std:: fmt:: Debug for SubcommandCandidates {
93
+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
94
+ f. write_str ( type_name :: < Self > ( ) )
95
+ }
96
+ }
97
+
98
+ impl CommandExt for SubcommandCandidates { }
99
+
55
100
/// User-provided completion candidates for an [`Arg`][clap::Arg], see [`ArgValueCandidates`]
56
101
///
102
+ /// User-provided completion candidates for an [`Subcommand`][clap::Subcommand], see [`SubcommandCandidates`]
103
+ ///
57
104
/// This is useful when predefined value hints are not enough.
58
105
pub trait ValueCandidates : Send + Sync {
59
106
/// All potential candidates for an argument.
0 commit comments