summaryrefslogtreecommitdiffstats
path: root/vendor/clap_complete/examples
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:19:03 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:19:03 +0000
commit64d98f8ee037282c35007b64c2649055c56af1db (patch)
tree5492bcf97fce41ee1c0b1cc2add283f3e66cdab0 /vendor/clap_complete/examples
parentAdding debian version 1.67.1+dfsg1-1. (diff)
downloadrustc-64d98f8ee037282c35007b64c2649055c56af1db.tar.xz
rustc-64d98f8ee037282c35007b64c2649055c56af1db.zip
Merging upstream version 1.68.2+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/clap_complete/examples')
-rw-r--r--vendor/clap_complete/examples/completion-derive.rs83
-rw-r--r--vendor/clap_complete/examples/completion.rs109
-rw-r--r--vendor/clap_complete/examples/dynamic.rs37
3 files changed, 229 insertions, 0 deletions
diff --git a/vendor/clap_complete/examples/completion-derive.rs b/vendor/clap_complete/examples/completion-derive.rs
new file mode 100644
index 000000000..8eeebf89d
--- /dev/null
+++ b/vendor/clap_complete/examples/completion-derive.rs
@@ -0,0 +1,83 @@
+//! 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>
+//! ```
+use clap::{Args, Command, CommandFactory, Parser, Subcommand, ValueHint};
+use clap_complete::{generate, Generator, Shell};
+use std::ffi::OsString;
+use std::io;
+use std::path::PathBuf;
+
+#[derive(Parser, Debug, PartialEq)]
+#[command(name = "completion-derive")]
+struct Opt {
+ // If provided, outputs the completion file for given shell
+ #[arg(long = "generate", value_enum)]
+ generator: Option<Shell>,
+ #[clap(subcommand)]
+ command: Option<Commands>,
+}
+
+#[derive(Subcommand, Debug, PartialEq)]
+enum Commands {
+ #[command(visible_alias = "hint")]
+ ValueHint(ValueHintOpt),
+}
+
+#[derive(Args, Debug, PartialEq)]
+struct ValueHintOpt {
+ // Showcasing all possible ValueHints:
+ #[arg(long, value_hint = ValueHint::Unknown)]
+ unknown: Option<String>,
+ #[arg(long, value_hint = ValueHint::Other)]
+ other: Option<String>,
+ #[arg(short, long, value_hint = ValueHint::AnyPath)]
+ path: Option<PathBuf>,
+ #[arg(short, long, value_hint = ValueHint::FilePath)]
+ file: Option<PathBuf>,
+ #[arg(short, long, value_hint = ValueHint::DirPath)]
+ dir: Option<PathBuf>,
+ #[arg(short, long, value_hint = ValueHint::ExecutablePath)]
+ exe: Option<PathBuf>,
+ #[arg(long, value_hint = ValueHint::CommandName)]
+ cmd_name: Option<OsString>,
+ #[arg(short, long, value_hint = ValueHint::CommandString)]
+ cmd: Option<String>,
+ // Command::trailing_var_ar is required to use ValueHint::CommandWithArguments
+ #[arg(trailing_var_arg = true, value_hint = ValueHint::CommandWithArguments)]
+ command_with_args: Vec<String>,
+ #[arg(short, long, value_hint = ValueHint::Username)]
+ user: Option<String>,
+ #[arg(long, value_hint = ValueHint::Hostname)]
+ host: Option<String>,
+ #[arg(long, value_hint = ValueHint::Url)]
+ url: Option<String>,
+ #[arg(long, value_hint = ValueHint::EmailAddress)]
+ email: Option<String>,
+}
+
+fn print_completions<G: Generator>(gen: G, cmd: &mut Command) {
+ generate(gen, cmd, cmd.get_name().to_string(), &mut io::stdout());
+}
+
+fn main() {
+ let opt = Opt::parse();
+
+ if let Some(generator) = opt.generator {
+ let mut cmd = Opt::command();
+ eprintln!("Generating completion file for {:?}...", generator);
+ print_completions(generator, &mut cmd);
+ } else {
+ println!("{:#?}", opt);
+ }
+}
diff --git a/vendor/clap_complete/examples/completion.rs b/vendor/clap_complete/examples/completion.rs
new file mode 100644
index 000000000..0455d4f9c
--- /dev/null
+++ b/vendor/clap_complete/examples/completion.rs
@@ -0,0 +1,109 @@
+//! Example to test arguments with different ValueHint values.
+//!
+//! Usage with zsh:
+//! ```sh
+//! cargo run --example value_hints -- --generate=zsh > /usr/local/share/zsh/site-functions/_value_hints
+//! compinit
+//! ./target/debug/examples/value_hints --<TAB>
+//! ```
+//! fish:
+//! ```sh
+//! cargo run --example value_hints -- --generate=fish > value_hints.fish
+//! . ./value_hints.fish
+//! ./target/debug/examples/value_hints --<TAB>
+//! ```
+use clap::{value_parser, Arg, Command, ValueHint};
+use clap_complete::{generate, Generator, Shell};
+use std::io;
+
+fn build_cli() -> Command {
+ let value_hint_command = Command::new("value-hint")
+ .visible_alias("hint")
+ .arg(
+ Arg::new("unknown")
+ .long("unknown")
+ .value_hint(ValueHint::Unknown),
+ )
+ .arg(Arg::new("other").long("other").value_hint(ValueHint::Other))
+ .arg(
+ Arg::new("path")
+ .long("path")
+ .short('p')
+ .value_hint(ValueHint::AnyPath),
+ )
+ .arg(
+ Arg::new("file")
+ .long("file")
+ .short('f')
+ .value_hint(ValueHint::FilePath),
+ )
+ .arg(
+ Arg::new("dir")
+ .long("dir")
+ .short('d')
+ .value_hint(ValueHint::DirPath),
+ )
+ .arg(
+ Arg::new("exe")
+ .long("exe")
+ .short('e')
+ .value_hint(ValueHint::ExecutablePath),
+ )
+ .arg(
+ Arg::new("cmd_name")
+ .long("cmd-name")
+ .value_hint(ValueHint::CommandName),
+ )
+ .arg(
+ Arg::new("cmd")
+ .long("cmd")
+ .short('c')
+ .value_hint(ValueHint::CommandString),
+ )
+ .arg(
+ Arg::new("command_with_args")
+ .num_args(1..)
+ // AppSettings::TrailingVarArg is required to use ValueHint::CommandWithArguments
+ .trailing_var_arg(true)
+ .value_hint(ValueHint::CommandWithArguments),
+ )
+ .arg(
+ Arg::new("user")
+ .short('u')
+ .long("user")
+ .value_hint(ValueHint::Username),
+ )
+ .arg(
+ Arg::new("host")
+ .long("host")
+ .value_hint(ValueHint::Hostname),
+ )
+ .arg(Arg::new("url").long("url").value_hint(ValueHint::Url))
+ .arg(
+ Arg::new("email")
+ .long("email")
+ .value_hint(ValueHint::EmailAddress),
+ );
+
+ Command::new("completion")
+ .arg(
+ Arg::new("generator")
+ .long("generate")
+ .value_parser(value_parser!(Shell)),
+ )
+ .subcommand(value_hint_command)
+}
+
+fn print_completions<G: Generator>(gen: G, cmd: &mut Command) {
+ generate(gen, cmd, cmd.get_name().to_string(), &mut io::stdout());
+}
+
+fn main() {
+ let matches = build_cli().get_matches();
+
+ if let Some(generator) = matches.get_one::<Shell>("generator") {
+ let mut cmd = build_cli();
+ eprintln!("Generating completion file for {}...", generator);
+ print_completions(*generator, &mut cmd);
+ }
+}
diff --git a/vendor/clap_complete/examples/dynamic.rs b/vendor/clap_complete/examples/dynamic.rs
new file mode 100644
index 000000000..5abf2de1f
--- /dev/null
+++ b/vendor/clap_complete/examples/dynamic.rs
@@ -0,0 +1,37 @@
+use clap::FromArgMatches;
+use clap::Subcommand;
+
+fn command() -> clap::Command {
+ let cmd = clap::Command::new("dynamic")
+ .arg(
+ clap::Arg::new("input")
+ .long("input")
+ .short('i')
+ .value_hint(clap::ValueHint::FilePath),
+ )
+ .arg(
+ clap::Arg::new("format")
+ .long("format")
+ .short('F')
+ .value_parser(["json", "yaml", "toml"]),
+ )
+ .args_conflicts_with_subcommands(true);
+ clap_complete::dynamic::bash::CompleteCommand::augment_subcommands(cmd)
+}
+
+fn main() {
+ let cmd = command();
+ let matches = cmd.get_matches();
+ if let Ok(completions) =
+ clap_complete::dynamic::bash::CompleteCommand::from_arg_matches(&matches)
+ {
+ completions.complete(&mut command());
+ } else {
+ println!("{:#?}", matches);
+ }
+}
+
+#[test]
+fn verify_cli() {
+ command().debug_assert();
+}