mirror of
https://github.com/clap-rs/clap
synced 2024-12-14 14:52:33 +00:00
88a335ff97
`clap_generate` originally intended to be "generate anything". With `fig`, we already broke one part out. With #3174's man support, we are also looking at keeping it separate: - More freedom to iterate on the API - Uniqueness (and potential weight) of its dependencies - man generation is normally more for distribution while completions are a mix of being distributed with the app or the app generating the completions (which will be exacerbated if we move most completion parsing logic to be in Rust) So `clap_generate` is having a lot more limited of a role than the original name conveys. I worry the generic name will be a hindrance to people discovering and using it (yes, documentation can help but there are limits). I hesitated because we are on the verge of releasing 3.0. However, doing it even later will be even more disruptive because more people will be using it (crates.io lists ~70 people using `clap_generate`). To ease things, we are still releasing `clap_generate` as a wrapper around `clap_complete`.
107 lines
3.2 KiB
Rust
107 lines
3.2 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_complete::{generate, Generator, Shell};
|
|
use std::io;
|
|
|
|
fn build_cli() -> App<'static> {
|
|
App::new("value_hints")
|
|
// AppSettings::TrailingVarArg is required to use ValueHint::CommandWithArguments
|
|
.setting(AppSettings::TrailingVarArg)
|
|
.arg(
|
|
Arg::new("generator")
|
|
.long("generate")
|
|
.possible_values(Shell::possible_values()),
|
|
)
|
|
.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>(gen: G, app: &mut App) {
|
|
generate(gen, app, app.get_name().to_string(), &mut io::stdout());
|
|
}
|
|
|
|
fn main() {
|
|
let matches = build_cli().get_matches();
|
|
|
|
if let Ok(generator) = matches.value_of_t::<Shell>("generator") {
|
|
let mut app = build_cli();
|
|
eprintln!("Generating completion file for {}...", generator);
|
|
print_completions(generator, &mut app);
|
|
}
|
|
}
|