clap/clap_generate/examples/value_hints.rs
Roland Fredenhagen 5580e8c465
ArgValue builder (#2758)
* feat(arg_value): ArgValue can be used for possible_values

Through the ArgValue it is possible:

* `hide` possible_values from showing in completion, help and validation
* add `about` to possible_values in completion

* Resolved a few change-requests by epage

* make clippy happy

* add ArgValue::get_visible_value

* remove verbose destructering

* rename ArgValue::get_hidden to ArgValue::is_hidden

* add test for help output of hidden ArgValues

* Documentation for ArgValue

There is an issue that required to implement From<&ArgValue> for
ArgValue. We should probably find a solution without that.

* fix requested changes by epage

* fix formatting

* add deref in possible_values call to remove From<&&str>

* make clippy happy

* use copied() instad of map(|v|*v)

* Finishing up for merge, hopefully

* changes requested by pksunkara
2021-09-19 10:29:09 +00:00

118 lines
3.6 KiB
Rust

//! 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::{App, AppSettings, Arg, ValueHint};
use clap_generate::generators::{Bash, Elvish, Fish, PowerShell, Zsh};
use clap_generate::{generate, Generator};
use std::io;
fn build_cli() -> App<'static> {
App::new("value_hints")
.setting(AppSettings::DisableVersionFlag)
// AppSettings::TrailingVarArg is required to use ValueHint::CommandWithArguments
.setting(AppSettings::TrailingVarArg)
.arg(Arg::new("generator").long("generate").possible_values([
"bash",
"elvish",
"fish",
"powershell",
"zsh",
]))
.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")
.takes_value(true)
.multiple_values(true)
.value_hint(ValueHint::CommandWithArguments),
)
.arg(
Arg::new("user")
.short('u')
.long("user")
.value_hint(ValueHint::Username),
)
.arg(
Arg::new("host")
.short('h')
.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),
)
}
fn print_completions<G: Generator>(app: &mut App) {
generate::<G, _>(app, app.get_name().to_string(), &mut io::stdout());
}
fn main() {
let matches = build_cli().get_matches();
if let Some(generator) = matches.value_of("generator") {
let mut app = build_cli();
eprintln!("Generating completion file for {}...", generator);
match generator {
"bash" => print_completions::<Bash>(&mut app),
"elvish" => print_completions::<Elvish>(&mut app),
"fish" => print_completions::<Fish>(&mut app),
"powershell" => print_completions::<PowerShell>(&mut app),
"zsh" => print_completions::<Zsh>(&mut app),
_ => panic!("Unknown generator"),
}
}
}