Skip to content

Commit

Permalink
feat: Support hiding flags by default
Browse files Browse the repository at this point in the history
Signed-off-by: Natsuki Ikeguchi <[email protected]>
  • Loading branch information
siketyan committed Jan 4, 2024
1 parent 0021674 commit 6d6ce25
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
7 changes: 7 additions & 0 deletions clap_complete/examples/dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ fn command() -> clap::Command {
.short('i')
.value_hint(clap::ValueHint::FilePath),
)
.arg(
clap::Arg::new("output")
.long("output")
.short('o')
.hide(true)
.value_hint(clap::ValueHint::FilePath),
)
.arg(
clap::Arg::new("format")
.long("format")
Expand Down
18 changes: 10 additions & 8 deletions clap_complete/src/dynamic/completer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ fn complete_arg(
}
} else {
completions.extend(longs_and_visible_aliases(cmd).into_iter().filter_map(
|(f, help)| f.starts_with(flag).then(|| (format!("--{f}").into(), help)),
|(f, a)| {
f.starts_with(flag)
.then(|| (format!("--{f}").into(), a.get_help().cloned()))
},
));
}
}
Expand All @@ -127,7 +130,8 @@ fn complete_arg(
completions.extend(
longs_and_visible_aliases(cmd)
.into_iter()
.map(|(f, help)| (format!("--{f}").into(), help)),
.filter(|(_, a)| !a.is_hide_set())
.map(|(f, a)| (format!("--{f}").into(), a.get_help().cloned())),
);
}

Expand Down Expand Up @@ -292,16 +296,13 @@ fn complete_subcommand(value: &str, cmd: &clap::Command) -> Vec<(OsString, Optio

/// Gets all the long options, their visible aliases and flags of a [`clap::Command`].
/// Includes `help` and `version` depending on the [`clap::Command`] settings.
fn longs_and_visible_aliases(p: &clap::Command) -> Vec<(String, Option<StyledStr>)> {
fn longs_and_visible_aliases(p: &clap::Command) -> Vec<(String, &clap::Arg)> {
debug!("longs: name={}", p.get_name());

p.get_arguments()
.filter_map(|a| {
a.get_long_and_visible_aliases().map(|longs| {
longs
.into_iter()
.map(|s| (s.to_string(), a.get_help().cloned()))
})
a.get_long_and_visible_aliases()
.map(move |longs| longs.into_iter().map(move |s| (s.to_string(), a)))
})
.flatten()
.collect()
Expand All @@ -313,6 +314,7 @@ fn shorts_and_visible_aliases(p: &clap::Command) -> Vec<(char, Option<StyledStr>
debug!("shorts: name={}", p.get_name());

p.get_arguments()
.filter(|a| !a.is_hide_set())
.filter_map(|a| {
a.get_short_and_visible_aliases()
.map(|shorts| shorts.into_iter().map(|s| (s, a.get_help().cloned())))
Expand Down
11 changes: 10 additions & 1 deletion clap_complete/tests/testsuite/dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ fn suggest_long_flag_subset() {
.arg(
clap::Arg::new("hello-moon")
.long("hello-moon")
.action(clap::ArgAction::Count),
.action(clap::ArgAction::Count)
.hide(true),
)
.arg(
clap::Arg::new("goodbye-world")
Expand All @@ -55,6 +56,14 @@ fn suggest_long_flag_subset() {
--help\tPrint help",
complete!(cmd, "--he"),
);

snapbox::assert_eq(
"--hello-world
--goodbye-world
--help\tPrint help
-h\tPrint help",
complete!(cmd, " "),
);
}

#[test]
Expand Down

0 comments on commit 6d6ce25

Please sign in to comment.