Debian Codegraph @

examples/completion-derive.rs

raw symbols SCIP
1 //! How to use value hints and generate shell completions.
2 //!
3 //! Usage with zsh:
4 //! ```console
5 //! $ cargo run --example completion-derive -- --generate=zsh > /usr/local/share/zsh/site-functions/_completion_derive
6 //! $ compinit
7 //! $ ./target/debug/examples/completion_derive --<TAB>
8 //! ```
9 //! fish:
10 //! ```console
11 //! $ cargo run --example completion-derive -- --generate=fish > completion_derive.fish
12 //! $ . ./completion_derive.fish
13 //! $ ./target/debug/examples/completion_derive --<TAB>
14 //! ```
15 use claprust-analyzer cargo clap 4.6.6 crate/::{Argsrust-analyzer cargo clap_builder 4.6.6 derive/Args#, Commandrust-analyzer cargo clap_builder 4.6.6 builder/command/Command#, CommandFactoryrust-analyzer cargo clap_builder 4.6.6 derive/CommandFactory#, Parserrust-analyzer cargo clap_builder 4.6.6 derive/Parser#, Subcommandrust-analyzer cargo clap_builder 4.6.6 derive/Subcommand#, ValueHintrust-analyzer cargo clap_builder 4.6.6 builder/value_hint/ValueHint#};
16 use clap_completerust-analyzer cargo clap_complete 4.6.8 crate/::{Generatorrust-analyzer cargo clap_complete 4.6.8 aot/generator/Generator#, Shellrust-analyzer cargo clap_complete 4.6.8 aot/shells/shell/Shell#, generaterust-analyzer cargo clap_complete 4.6.8 aot/generator/generate().};
17 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ffirust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/::OsStringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsString#;
18 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/;
19 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#;
20
21 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Parserrust-analyzer cargo clap_derive 4.6.4 Parser:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:)]
22 #[command(name = "completion-derive")]
23 struct Optrust-analyzer cargo clap_complete 4.6.8 Opt# {
24 // If provided, outputs the completion file for given shell
25 #[argrust-analyzer cargo clap_derive 4.6.4 arg:(longrust-analyzer cargo clap_builder 4.6.6 builder/arg/impl#[Arg]long(). = "generate", value_enum)]
26 generatorrust-analyzer cargo clap_complete 4.6.8 Opt#generator.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Shellrust-analyzer cargo clap_complete 4.6.8 aot/shells/shell/Shell#>,
27 #[commandrust-analyzer cargo clap_derive 4.6.4 command:(subcommand)]
28 commandrust-analyzer cargo clap_complete 4.6.8 Opt#command.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Commandsrust-analyzer cargo clap_complete 4.6.8 Commands#>,
29 }
30
31 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Subcommandrust-analyzer cargo clap_derive 4.6.4 Subcommand:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:)]
32 enum Commandsrust-analyzer cargo clap_complete 4.6.8 Commands# {
33 #[commandrust-analyzer cargo clap_derive 4.6.4 command:(visible_aliasrust-analyzer cargo clap_builder 4.6.6 builder/command/impl#[Command]visible_alias(). = "hint")]
34 ValueHintrust-analyzer cargo clap_complete 4.6.8 Commands#ValueHint#(ValueHintOptrust-analyzer cargo clap_complete 4.6.8 ValueHintOpt#),
35 }
36
37 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Argsrust-analyzer cargo clap_derive 4.6.4 Args:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:)]
38 struct ValueHintOptrust-analyzer cargo clap_complete 4.6.8 ValueHintOpt# {
39 // Showcasing all possible ValueHints:
40 #[argrust-analyzer cargo clap_derive 4.6.4 arg:(longrust-analyzer cargo clap_builder 4.6.6 builder/arg/impl#[Arg]long()., value_hintrust-analyzer cargo clap_builder 4.6.6 builder/arg/impl#[Arg]value_hint(). = ValueHintrust-analyzer cargo clap_builder 4.6.6 builder/value_hint/ValueHint#::Unknownrust-analyzer cargo clap_builder 4.6.6 builder/value_hint/ValueHint#Unknown#)]
41 unknownrust-analyzer cargo clap_complete 4.6.8 ValueHintOpt#unknown.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>,
42 #[argrust-analyzer cargo clap_derive 4.6.4 arg:(longrust-analyzer cargo clap_builder 4.6.6 builder/arg/impl#[Arg]long()., value_hintrust-analyzer cargo clap_builder 4.6.6 builder/arg/impl#[Arg]value_hint(). = ValueHintrust-analyzer cargo clap_builder 4.6.6 builder/value_hint/ValueHint#::Otherrust-analyzer cargo clap_builder 4.6.6 builder/value_hint/ValueHint#Other#)]
43 otherrust-analyzer cargo clap_complete 4.6.8 ValueHintOpt#other.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>,
44 #[argrust-analyzer cargo clap_derive 4.6.4 arg:(shortrust-analyzer cargo clap_builder 4.6.6 builder/arg/impl#[Arg]short()., longrust-analyzer cargo clap_builder 4.6.6 builder/arg/impl#[Arg]long()., value_hintrust-analyzer cargo clap_builder 4.6.6 builder/arg/impl#[Arg]value_hint(). = ValueHintrust-analyzer cargo clap_builder 4.6.6 builder/value_hint/ValueHint#::AnyPathrust-analyzer cargo clap_builder 4.6.6 builder/value_hint/ValueHint#AnyPath#)]
45 pathrust-analyzer cargo clap_complete 4.6.8 ValueHintOpt#path.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#>,
46 #[argrust-analyzer cargo clap_derive 4.6.4 arg:(shortrust-analyzer cargo clap_builder 4.6.6 builder/arg/impl#[Arg]short()., longrust-analyzer cargo clap_builder 4.6.6 builder/arg/impl#[Arg]long()., value_hintrust-analyzer cargo clap_builder 4.6.6 builder/arg/impl#[Arg]value_hint(). = ValueHintrust-analyzer cargo clap_builder 4.6.6 builder/value_hint/ValueHint#::FilePathrust-analyzer cargo clap_builder 4.6.6 builder/value_hint/ValueHint#FilePath#)]
47 filerust-analyzer cargo clap_complete 4.6.8 ValueHintOpt#file.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#>,
48 #[argrust-analyzer cargo clap_derive 4.6.4 arg:(shortrust-analyzer cargo clap_builder 4.6.6 builder/arg/impl#[Arg]short()., longrust-analyzer cargo clap_builder 4.6.6 builder/arg/impl#[Arg]long()., value_hintrust-analyzer cargo clap_builder 4.6.6 builder/arg/impl#[Arg]value_hint(). = ValueHintrust-analyzer cargo clap_builder 4.6.6 builder/value_hint/ValueHint#::DirPathrust-analyzer cargo clap_builder 4.6.6 builder/value_hint/ValueHint#DirPath#)]
49 dirrust-analyzer cargo clap_complete 4.6.8 ValueHintOpt#dir.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#>,
50 #[argrust-analyzer cargo clap_derive 4.6.4 arg:(shortrust-analyzer cargo clap_builder 4.6.6 builder/arg/impl#[Arg]short()., longrust-analyzer cargo clap_builder 4.6.6 builder/arg/impl#[Arg]long()., value_hintrust-analyzer cargo clap_builder 4.6.6 builder/arg/impl#[Arg]value_hint(). = ValueHintrust-analyzer cargo clap_builder 4.6.6 builder/value_hint/ValueHint#::ExecutablePathrust-analyzer cargo clap_builder 4.6.6 builder/value_hint/ValueHint#ExecutablePath#)]
51 exerust-analyzer cargo clap_complete 4.6.8 ValueHintOpt#exe.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#>,
52 #[argrust-analyzer cargo clap_derive 4.6.4 arg:(longrust-analyzer cargo clap_builder 4.6.6 builder/arg/impl#[Arg]long()., value_hintrust-analyzer cargo clap_builder 4.6.6 builder/arg/impl#[Arg]value_hint(). = ValueHintrust-analyzer cargo clap_builder 4.6.6 builder/value_hint/ValueHint#::CommandNamerust-analyzer cargo clap_builder 4.6.6 builder/value_hint/ValueHint#CommandName#)]
53 cmd_namerust-analyzer cargo clap_complete 4.6.8 ValueHintOpt#cmd_name.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<OsStringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsString#>,
54 #[argrust-analyzer cargo clap_derive 4.6.4 arg:(shortrust-analyzer cargo clap_builder 4.6.6 builder/arg/impl#[Arg]short()., longrust-analyzer cargo clap_builder 4.6.6 builder/arg/impl#[Arg]long()., value_hintrust-analyzer cargo clap_builder 4.6.6 builder/arg/impl#[Arg]value_hint(). = ValueHintrust-analyzer cargo clap_builder 4.6.6 builder/value_hint/ValueHint#::CommandStringrust-analyzer cargo clap_builder 4.6.6 builder/value_hint/ValueHint#CommandString#)]
55 cmdrust-analyzer cargo clap_complete 4.6.8 ValueHintOpt#cmd.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>,
56 // Command::trailing_var_ar is required to use ValueHint::CommandWithArguments
57 #[argrust-analyzer cargo clap_derive 4.6.4 arg:(trailing_var_argrust-analyzer cargo clap_builder 4.6.6 builder/arg/impl#[Arg]trailing_var_arg(). = true, value_hintrust-analyzer cargo clap_builder 4.6.6 builder/arg/impl#[Arg]value_hint(). = ValueHintrust-analyzer cargo clap_builder 4.6.6 builder/value_hint/ValueHint#::CommandWithArgumentsrust-analyzer cargo clap_builder 4.6.6 builder/value_hint/ValueHint#CommandWithArguments#)]
58 command_with_argsrust-analyzer cargo clap_complete 4.6.8 ValueHintOpt#command_with_args.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>,
59 #[argrust-analyzer cargo clap_derive 4.6.4 arg:(shortrust-analyzer cargo clap_builder 4.6.6 builder/arg/impl#[Arg]short()., longrust-analyzer cargo clap_builder 4.6.6 builder/arg/impl#[Arg]long()., value_hintrust-analyzer cargo clap_builder 4.6.6 builder/arg/impl#[Arg]value_hint(). = ValueHintrust-analyzer cargo clap_builder 4.6.6 builder/value_hint/ValueHint#::Usernamerust-analyzer cargo clap_builder 4.6.6 builder/value_hint/ValueHint#Username#)]
60 userrust-analyzer cargo clap_complete 4.6.8 ValueHintOpt#user.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>,
61 #[argrust-analyzer cargo clap_derive 4.6.4 arg:(longrust-analyzer cargo clap_builder 4.6.6 builder/arg/impl#[Arg]long()., value_hintrust-analyzer cargo clap_builder 4.6.6 builder/arg/impl#[Arg]value_hint(). = ValueHintrust-analyzer cargo clap_builder 4.6.6 builder/value_hint/ValueHint#::Hostnamerust-analyzer cargo clap_builder 4.6.6 builder/value_hint/ValueHint#Hostname#)]
62 hostrust-analyzer cargo clap_complete 4.6.8 ValueHintOpt#host.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>,
63 #[argrust-analyzer cargo clap_derive 4.6.4 arg:(longrust-analyzer cargo clap_builder 4.6.6 builder/arg/impl#[Arg]long()., value_hintrust-analyzer cargo clap_builder 4.6.6 builder/arg/impl#[Arg]value_hint(). = ValueHintrust-analyzer cargo clap_builder 4.6.6 builder/value_hint/ValueHint#::Urlrust-analyzer cargo clap_builder 4.6.6 builder/value_hint/ValueHint#Url#)]
64 urlrust-analyzer cargo clap_complete 4.6.8 ValueHintOpt#url.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>,
65 #[argrust-analyzer cargo clap_derive 4.6.4 arg:(longrust-analyzer cargo clap_builder 4.6.6 builder/arg/impl#[Arg]long()., value_hintrust-analyzer cargo clap_builder 4.6.6 builder/arg/impl#[Arg]value_hint(). = ValueHintrust-analyzer cargo clap_builder 4.6.6 builder/value_hint/ValueHint#::EmailAddressrust-analyzer cargo clap_builder 4.6.6 builder/value_hint/ValueHint#EmailAddress#)]
66 emailrust-analyzer cargo clap_complete 4.6.8 ValueHintOpt#email.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>,
67 }
68
69 fn print_completionsrust-analyzer cargo clap_complete 4.6.8 print_completions().<G: Generatorrust-analyzer cargo clap_complete 4.6.8 aot/generator/Generator#>(generator: G, cmd: &mut Commandrust-analyzer cargo clap_builder 4.6.6 builder/command/Command#) {
70 generaterust-analyzer cargo clap_complete 4.6.8 aot/generator/generate().(generator, cmd, cmd.get_namerust-analyzer cargo clap_builder 4.6.6 builder/command/impl#[Command]get_name().().to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().(), &mut iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::stdoutrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/stdio/stdout().());
71 }
72
73 fn mainrust-analyzer cargo clap_complete 4.6.8 main().() {
74 let opt = Optrust-analyzer cargo clap_complete 4.6.8 Opt#::parserust-analyzer cargo clap_builder 4.6.6 derive/Parser#parse().();
75
76 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(generator) = opt.generatorrust-analyzer cargo clap_complete 4.6.8 Opt#generator. {
77 let mut cmd = Optrust-analyzer cargo clap_complete 4.6.8 Opt#::commandrust-analyzer cargo clap_complete 4.6.8 impl#[Opt][CommandFactory]command().();
78 eprintlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/eprintln!!("Generating completion file for {generator:?}...");
79 print_completionsrust-analyzer cargo clap_complete 4.6.8 print_completions().(generator, &mut cmd);
80 } else {
81 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("{opt:#?}");
82 }
83 }