2020-10-08 22:05:05 +00:00
|
|
|
//! How to use value hints and generate shell completions.
|
|
|
|
//!
|
|
|
|
//! Usage with zsh:
|
|
|
|
//! ```sh
|
|
|
|
//! cargo run --example value_hints_derive -- --generate=zsh > /usr/local/share/zsh/site-functions/_value_hints_derive
|
|
|
|
//! compinit
|
|
|
|
//! ./target/debug/examples/value_hints_derive --<TAB>
|
|
|
|
//! ```
|
|
|
|
//! fish:
|
|
|
|
//! ```sh
|
|
|
|
//! cargo run --example value_hints_derive -- --generate=fish > value_hints_derive.fish
|
|
|
|
//! . ./value_hints_derive.fish
|
|
|
|
//! ./target/debug/examples/value_hints_derive --<TAB>
|
|
|
|
//! ```
|
2022-02-14 22:09:38 +00:00
|
|
|
use clap::{Command, CommandFactory, Parser, ValueHint};
|
2021-12-31 17:59:15 +00:00
|
|
|
use clap_complete::{generate, Generator, Shell};
|
2020-10-08 22:05:05 +00:00
|
|
|
use std::ffi::OsString;
|
|
|
|
use std::io;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2021-07-13 17:50:55 +00:00
|
|
|
#[derive(Parser, Debug, PartialEq)]
|
2020-10-08 22:05:05 +00:00
|
|
|
#[clap(
|
|
|
|
name = "value_hints_derive",
|
2022-02-12 03:48:29 +00:00
|
|
|
// Command::trailing_var_ar is required to use ValueHint::CommandWithArguments
|
2022-02-10 17:51:40 +00:00
|
|
|
trailing_var_arg = true,
|
2020-10-08 22:05:05 +00:00
|
|
|
)]
|
|
|
|
struct Opt {
|
|
|
|
/// If provided, outputs the completion file for given shell
|
2022-08-26 18:29:10 +00:00
|
|
|
#[clap(long = "generate", value_enum)]
|
2021-08-18 18:00:56 +00:00
|
|
|
generator: Option<Shell>,
|
2020-10-08 22:05:05 +00:00
|
|
|
// Showcasing all possible ValueHints:
|
2022-07-23 00:36:26 +00:00
|
|
|
#[clap(long, value_hint = ValueHint::Unknown)]
|
2020-10-08 22:05:05 +00:00
|
|
|
unknown: Option<String>,
|
2022-07-23 00:36:26 +00:00
|
|
|
#[clap(long, value_hint = ValueHint::Other)]
|
2020-10-08 22:05:05 +00:00
|
|
|
other: Option<String>,
|
2022-07-23 00:36:26 +00:00
|
|
|
#[clap(short, long, value_hint = ValueHint::AnyPath)]
|
2020-10-08 22:05:05 +00:00
|
|
|
path: Option<PathBuf>,
|
2022-07-23 00:36:26 +00:00
|
|
|
#[clap(short, long, value_hint = ValueHint::FilePath)]
|
2020-10-08 22:05:05 +00:00
|
|
|
file: Option<PathBuf>,
|
2022-07-23 00:36:26 +00:00
|
|
|
#[clap(short, long, value_hint = ValueHint::DirPath)]
|
2020-10-08 22:05:05 +00:00
|
|
|
dir: Option<PathBuf>,
|
2022-07-23 00:36:26 +00:00
|
|
|
#[clap(short, long, value_hint = ValueHint::ExecutablePath)]
|
2020-10-08 22:05:05 +00:00
|
|
|
exe: Option<PathBuf>,
|
2022-07-23 00:36:26 +00:00
|
|
|
#[clap(long, value_hint = ValueHint::CommandName)]
|
2020-10-08 22:05:05 +00:00
|
|
|
cmd_name: Option<OsString>,
|
2022-07-23 00:36:26 +00:00
|
|
|
#[clap(short, long, value_hint = ValueHint::CommandString)]
|
2020-10-08 22:05:05 +00:00
|
|
|
cmd: Option<String>,
|
2022-07-23 00:36:26 +00:00
|
|
|
#[clap(value_hint = ValueHint::CommandWithArguments)]
|
2020-10-08 22:05:05 +00:00
|
|
|
command_with_args: Vec<String>,
|
2022-07-23 00:36:26 +00:00
|
|
|
#[clap(short, long, value_hint = ValueHint::Username)]
|
2020-10-08 22:05:05 +00:00
|
|
|
user: Option<String>,
|
2022-07-23 00:36:26 +00:00
|
|
|
#[clap(short, long, value_hint = ValueHint::Hostname)]
|
2020-10-08 22:05:05 +00:00
|
|
|
host: Option<String>,
|
2022-07-23 00:36:26 +00:00
|
|
|
#[clap(long, value_hint = ValueHint::Url)]
|
2020-10-08 22:05:05 +00:00
|
|
|
url: Option<String>,
|
2022-07-23 00:36:26 +00:00
|
|
|
#[clap(long, value_hint = ValueHint::EmailAddress)]
|
2020-10-08 22:05:05 +00:00
|
|
|
email: Option<String>,
|
|
|
|
}
|
|
|
|
|
2022-02-14 21:47:20 +00:00
|
|
|
fn print_completions<G: Generator>(gen: G, cmd: &mut Command) {
|
|
|
|
generate(gen, cmd, cmd.get_name().to_string(), &mut io::stdout());
|
2020-10-08 22:05:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let opt = Opt::parse();
|
|
|
|
|
|
|
|
if let Some(generator) = opt.generator {
|
2022-02-14 22:04:07 +00:00
|
|
|
let mut cmd = Opt::command();
|
2020-10-08 22:05:05 +00:00
|
|
|
eprintln!("Generating completion file for {:?}...", generator);
|
2022-02-14 21:47:20 +00:00
|
|
|
print_completions(generator, &mut cmd);
|
2020-10-08 22:05:05 +00:00
|
|
|
} else {
|
|
|
|
println!("{:#?}", opt);
|
|
|
|
}
|
|
|
|
}
|