2020-10-08 22:05:05 +00:00
|
|
|
//! How to use value hints and generate shell completions.
|
|
|
|
//!
|
|
|
|
//! Usage with zsh:
|
2023-03-25 02:29:40 +00:00
|
|
|
//! ```console
|
|
|
|
//! $ cargo run --example completion-derive -- --generate=zsh > /usr/local/share/zsh/site-functions/_completion_derive
|
|
|
|
//! $ compinit
|
|
|
|
//! $ ./target/debug/examples/completion_derive --<TAB>
|
2020-10-08 22:05:05 +00:00
|
|
|
//! ```
|
|
|
|
//! fish:
|
2023-03-25 02:29:40 +00:00
|
|
|
//! ```console
|
|
|
|
//! $ cargo run --example completion-derive -- --generate=fish > completion_derive.fish
|
|
|
|
//! $ . ./completion_derive.fish
|
|
|
|
//! $ ./target/debug/examples/completion_derive --<TAB>
|
2020-10-08 22:05:05 +00:00
|
|
|
//! ```
|
2022-09-28 04:06:41 +00:00
|
|
|
use clap::{Args, Command, CommandFactory, Parser, Subcommand, 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)]
|
2022-09-27 16:13:00 +00:00
|
|
|
#[command(name = "completion-derive")]
|
2020-10-08 22:05:05 +00:00
|
|
|
struct Opt {
|
2022-09-28 04:25:14 +00:00
|
|
|
// If provided, outputs the completion file for given shell
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(long = "generate", value_enum)]
|
2021-08-18 18:00:56 +00:00
|
|
|
generator: Option<Shell>,
|
2023-02-19 00:42:07 +00:00
|
|
|
#[command(subcommand)]
|
2022-09-28 04:06:41 +00:00
|
|
|
command: Option<Commands>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Subcommand, Debug, PartialEq)]
|
|
|
|
enum Commands {
|
2022-09-29 21:11:03 +00:00
|
|
|
#[command(visible_alias = "hint")]
|
2022-09-28 04:06:41 +00:00
|
|
|
ValueHint(ValueHintOpt),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Args, Debug, PartialEq)]
|
|
|
|
struct ValueHintOpt {
|
2020-10-08 22:05:05 +00:00
|
|
|
// Showcasing all possible ValueHints:
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(long, value_hint = ValueHint::Unknown)]
|
2020-10-08 22:05:05 +00:00
|
|
|
unknown: Option<String>,
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(long, value_hint = ValueHint::Other)]
|
2020-10-08 22:05:05 +00:00
|
|
|
other: Option<String>,
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(short, long, value_hint = ValueHint::AnyPath)]
|
2020-10-08 22:05:05 +00:00
|
|
|
path: Option<PathBuf>,
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(short, long, value_hint = ValueHint::FilePath)]
|
2020-10-08 22:05:05 +00:00
|
|
|
file: Option<PathBuf>,
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(short, long, value_hint = ValueHint::DirPath)]
|
2020-10-08 22:05:05 +00:00
|
|
|
dir: Option<PathBuf>,
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(short, long, value_hint = ValueHint::ExecutablePath)]
|
2020-10-08 22:05:05 +00:00
|
|
|
exe: Option<PathBuf>,
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(long, value_hint = ValueHint::CommandName)]
|
2020-10-08 22:05:05 +00:00
|
|
|
cmd_name: Option<OsString>,
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(short, long, value_hint = ValueHint::CommandString)]
|
2020-10-08 22:05:05 +00:00
|
|
|
cmd: Option<String>,
|
2022-09-06 20:41:23 +00:00
|
|
|
// Command::trailing_var_ar is required to use ValueHint::CommandWithArguments
|
|
|
|
#[arg(trailing_var_arg = true, value_hint = ValueHint::CommandWithArguments)]
|
2020-10-08 22:05:05 +00:00
|
|
|
command_with_args: Vec<String>,
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(short, long, value_hint = ValueHint::Username)]
|
2020-10-08 22:05:05 +00:00
|
|
|
user: Option<String>,
|
2022-09-28 19:34:50 +00:00
|
|
|
#[arg(long, value_hint = ValueHint::Hostname)]
|
2020-10-08 22:05:05 +00:00
|
|
|
host: Option<String>,
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(long, value_hint = ValueHint::Url)]
|
2020-10-08 22:05:05 +00:00
|
|
|
url: Option<String>,
|
2022-09-02 20:37:23 +00:00
|
|
|
#[arg(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();
|
2023-01-29 19:14:47 +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 {
|
2023-01-29 19:14:47 +00:00
|
|
|
println!("{opt:#?}");
|
2020-10-08 22:05:05 +00:00
|
|
|
}
|
|
|
|
}
|