diff --git a/clap_generate/examples/value_hints.rs b/clap_generate/examples/value_hints.rs index 72a42ea1fc4e..e475d2c7a0c3 100644 --- a/clap_generate/examples/value_hints.rs +++ b/clap_generate/examples/value_hints.rs @@ -6,8 +6,14 @@ //! compinit //! ./target/debug/examples/value_hints -- //! ``` +//! fish: +//! ```sh +//! cargo run --example value_hints -- --generate=fish > value_hints.fish +//! . ./value_hints.fish +//! ./target/debug/examples/value_hints -- +//! ``` use clap::{App, AppSettings, Arg, ValueHint}; -use clap_generate::generators::Zsh; +use clap_generate::generators::{Fish, Zsh}; use clap_generate::{generate, generators::Bash}; use std::io; @@ -20,7 +26,7 @@ fn build_cli() -> App<'static> { .arg( Arg::with_name("generator") .long("generate") - .possible_values(&["zsh", "bash"]), + .possible_values(&["bash", "fish", "zsh"]), ) .arg( Arg::with_name("unknown") @@ -63,9 +69,7 @@ fn build_cli() -> App<'static> { .value_hint(ValueHint::CommandString), ) .arg( - Arg::with_name("command_with_args") - .multiple(true) - .value_hint(ValueHint::CommandWithArguments), + Arg::with_name("command_with_args").multiple(true), // .value_hint(ValueHint::CommandWithArguments), ) .arg( Arg::with_name("user") @@ -94,8 +98,9 @@ fn main() { let mut app = build_cli(); eprintln!("Generating completion file for {}...", generator); match generator { - "zsh" => generate::(&mut app, APPNAME, &mut io::stdout()), "bash" => generate::(&mut app, APPNAME, &mut io::stdout()), + "fish" => generate::(&mut app, APPNAME, &mut io::stdout()), + "zsh" => generate::(&mut app, APPNAME, &mut io::stdout()), _ => panic!("Unknown generator"), } } diff --git a/clap_generate/src/generators/shells/fish.rs b/clap_generate/src/generators/shells/fish.rs index 966f08d0d2d2..6af13a3337c8 100644 --- a/clap_generate/src/generators/shells/fish.rs +++ b/clap_generate/src/generators/shells/fish.rs @@ -71,6 +71,25 @@ fn gen_fish_inner(root_command: &str, app: &App, buffer: &mut String) { if let Some(ref data) = option.possible_vals { template.push_str(format!(" -r -f -a \"{}\"", data.join(" ")).as_str()); + } else { + assert!(option.is_set(ArgSettings::TakesValue)); + let completion = match option.value_hint { + ValueHint::AnyPath | ValueHint::FilePath => "__fish_complete_path", + ValueHint::DirPath => "__fish_complete_directories", + // ValueHint::ExecutablePath => "_absolute_command_paths", + // ValueHint::CommandName => "_command_names -e", + // ValueHint::CommandString => "_cmdstring", + ValueHint::CommandWithArguments => "__fish_complete_subcommand -- -n --interval", + ValueHint::Username => "__fish_complete_users", + ValueHint::Hostname => "__fish_print_hostnames", + // ValueHint::Url => "_urls", + // ValueHint::EmailAddress => "_email_addresses", + _ => "", + }; + + if !completion.is_empty() { + template.push_str(format!(" -r -f -a \"({})\"", completion).as_str()); + } } buffer.push_str(template.as_str()); diff --git a/clap_generate/src/generators/shells/zsh.rs b/clap_generate/src/generators/shells/zsh.rs index 2f8b1b165418..8c068b8d0cd1 100644 --- a/clap_generate/src/generators/shells/zsh.rs +++ b/clap_generate/src/generators/shells/zsh.rs @@ -358,7 +358,7 @@ fn value_completion(arg: &Arg) -> String { ValueHint::Url => "_urls", ValueHint::EmailAddress => "_email_addresses", // zsh default behavior is completing filenames - _ => "_files", + _ => "", } .to_string() }