Skip to content

Commit

Permalink
chore: Use get_action to filter out help/version flags
Browse files Browse the repository at this point in the history
  • Loading branch information
tesuji committed Jul 6, 2024
1 parent 2167fee commit 6c706d5
Showing 1 changed file with 5 additions and 12 deletions.
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};
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)
});

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

0 comments on commit 6c706d5

Please sign in to comment.