Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fish/complete: Generate helper functions to check for subcommands #5568

Merged
merged 9 commits into from
Jul 11, 2024
17 changes: 5 additions & 12 deletions clap_complete/src/shells/fish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,15 @@ impl Generator for Fish {
}

fn generate(&self, cmd: &Command, buf: &mut dyn Write) {
use clap::ArgAction::{Help, HelpLong, HelpShort, Version};
tesuji marked this conversation as resolved.
Show resolved Hide resolved
let bin_name = cmd
.get_bin_name()
.expect("crate::generate should have set the bin_name");

let has_global_flags = {
let n_args = cmd.get_arguments().filter(|a| !a.is_positional()).count();
// `-h` and `-v` behaves as command, not regular flags/options that modifies subcommand behaviors.
match (
cmd.is_disable_help_flag_set(),
cmd.is_disable_version_flag_set(),
) {
(false, false) => n_args > 2,
(true, true) => n_args > 0,
_ => n_args > 1,
}
};
// `-h` and `-v` behaves as command, not regular flags/options that modifies subcommand behaviors.
let has_global_flags = cmd.get_arguments().any(|a| {
!a.is_positional() && !matches!(a.get_action(), Help | HelpShort | HelpLong | Version)
epage marked this conversation as resolved.
Show resolved Hide resolved
});

let name = escape_name(bin_name);
let mut needs_fn_name = &format!("__fish_{name}_needs_command")[..];
Expand Down
Loading