mirror of
https://github.com/clap-rs/clap
synced 2024-11-10 06:44:16 +00:00
Merge pull request #5695 from epage/value
fix(complete)!: Be consistent in value language
This commit is contained in:
commit
f9721f1435
5 changed files with 27 additions and 31 deletions
|
@ -247,7 +247,7 @@ fi
|
|||
if i != 0 {
|
||||
write!(buf, "{}", ifs.as_deref().unwrap_or("\n"))?;
|
||||
}
|
||||
write!(buf, "{}", candidate.get_content().to_string_lossy())?;
|
||||
write!(buf, "{}", candidate.get_value().to_string_lossy())?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
@ -341,7 +341,7 @@ set edit:completion:arg-completer[BIN] = { |@words|
|
|||
if i != 0 {
|
||||
write!(buf, "{}", ifs.as_deref().unwrap_or("\n"))?;
|
||||
}
|
||||
write!(buf, "{}", candidate.get_content().to_string_lossy())?;
|
||||
write!(buf, "{}", candidate.get_value().to_string_lossy())?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
@ -379,7 +379,7 @@ impl CommandCompleter for Fish {
|
|||
let completions = crate::engine::complete(cmd, args, index, current_dir)?;
|
||||
|
||||
for candidate in completions {
|
||||
write!(buf, "{}", candidate.get_content().to_string_lossy())?;
|
||||
write!(buf, "{}", candidate.get_value().to_string_lossy())?;
|
||||
if let Some(help) = candidate.get_help() {
|
||||
write!(
|
||||
buf,
|
||||
|
@ -445,7 +445,7 @@ Register-ArgumentCompleter -Native -CommandName {bin} -ScriptBlock {{
|
|||
let completions = crate::engine::complete(cmd, args, index, current_dir)?;
|
||||
|
||||
for candidate in completions {
|
||||
write!(buf, "{}", candidate.get_content().to_string_lossy())?;
|
||||
write!(buf, "{}", candidate.get_value().to_string_lossy())?;
|
||||
if let Some(help) = candidate.get_help() {
|
||||
write!(
|
||||
buf,
|
||||
|
@ -522,7 +522,7 @@ compdef _clap_dynamic_completer BIN"#
|
|||
if i != 0 {
|
||||
write!(buf, "{}", ifs.as_deref().unwrap_or("\n"))?;
|
||||
}
|
||||
write!(buf, "{}", candidate.get_content().to_string_lossy())?;
|
||||
write!(buf, "{}", candidate.get_value().to_string_lossy())?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -6,17 +6,17 @@ use clap::builder::StyledStr;
|
|||
/// A shell-agnostic completion candidate
|
||||
#[derive(Default, Debug, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub struct CompletionCandidate {
|
||||
content: OsString,
|
||||
value: OsString,
|
||||
help: Option<StyledStr>,
|
||||
hidden: bool,
|
||||
}
|
||||
|
||||
impl CompletionCandidate {
|
||||
/// Create a new completion candidate
|
||||
pub fn new(content: impl Into<OsString>) -> Self {
|
||||
let content = content.into();
|
||||
pub fn new(value: impl Into<OsString>) -> Self {
|
||||
let value = value.into();
|
||||
Self {
|
||||
content,
|
||||
value,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
@ -35,24 +35,24 @@ impl CompletionCandidate {
|
|||
self
|
||||
}
|
||||
|
||||
/// Add a prefix to the content of completion candidate
|
||||
/// Add a prefix to the value of completion candidate
|
||||
///
|
||||
/// This is generally used for post-process by [`complete`][crate::engine::complete()] for
|
||||
/// things like pre-pending flags, merging delimiter-separated values, etc.
|
||||
pub fn add_prefix(mut self, prefix: impl Into<OsString>) -> Self {
|
||||
let suffix = self.content;
|
||||
let mut content = prefix.into();
|
||||
content.push(&suffix);
|
||||
self.content = content;
|
||||
let suffix = self.value;
|
||||
let mut value = prefix.into();
|
||||
value.push(&suffix);
|
||||
self.value = value;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
/// Reflection API
|
||||
impl CompletionCandidate {
|
||||
/// Get the content of the completion candidate
|
||||
pub fn get_content(&self) -> &OsStr {
|
||||
&self.content
|
||||
/// Get the literal value being proposed for completion
|
||||
pub fn get_value(&self) -> &OsStr {
|
||||
&self.value
|
||||
}
|
||||
|
||||
/// Get the help message of the completion candidate
|
||||
|
|
|
@ -150,15 +150,11 @@ fn complete_arg(
|
|||
}
|
||||
} else {
|
||||
completions.extend(longs_and_visible_aliases(cmd).into_iter().filter(
|
||||
|comp| {
|
||||
comp.get_content()
|
||||
.starts_with(format!("--{}", flag).as_str())
|
||||
},
|
||||
|comp| comp.get_value().starts_with(format!("--{}", flag).as_str()),
|
||||
));
|
||||
|
||||
completions.extend(hidden_longs_aliases(cmd).into_iter().filter(|comp| {
|
||||
comp.get_content()
|
||||
.starts_with(format!("--{}", flag).as_str())
|
||||
comp.get_value().starts_with(format!("--{}", flag).as_str())
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
@ -352,7 +348,7 @@ fn complete_custom_arg_value(
|
|||
debug!("complete_custom_arg_value: completer={completer:?}, value={value:?}");
|
||||
|
||||
let mut values = completer.candidates();
|
||||
values.retain(|comp| comp.get_content().starts_with(&value.to_string_lossy()));
|
||||
values.retain(|comp| comp.get_value().starts_with(&value.to_string_lossy()));
|
||||
values
|
||||
}
|
||||
|
||||
|
@ -365,7 +361,7 @@ fn complete_subcommand(value: &str, cmd: &clap::Command) -> Vec<CompletionCandid
|
|||
|
||||
let mut scs = subcommands(cmd)
|
||||
.into_iter()
|
||||
.filter(|x| x.get_content().starts_with(value))
|
||||
.filter(|x| x.get_value().starts_with(value))
|
||||
.collect::<Vec<_>>();
|
||||
scs.sort();
|
||||
scs.dedup();
|
||||
|
|
10
clap_complete/src/env/shells.rs
vendored
10
clap_complete/src/env/shells.rs
vendored
|
@ -92,7 +92,7 @@ fi
|
|||
if i != 0 {
|
||||
write!(buf, "{}", ifs.as_deref().unwrap_or("\n"))?;
|
||||
}
|
||||
write!(buf, "{}", candidate.get_content().to_string_lossy())?;
|
||||
write!(buf, "{}", candidate.get_value().to_string_lossy())?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
@ -195,7 +195,7 @@ set edit:completion:arg-completer[BIN] = { |@words|
|
|||
if i != 0 {
|
||||
write!(buf, "{}", ifs.as_deref().unwrap_or("\n"))?;
|
||||
}
|
||||
write!(buf, "{}", candidate.get_content().to_string_lossy())?;
|
||||
write!(buf, "{}", candidate.get_value().to_string_lossy())?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
@ -240,7 +240,7 @@ impl EnvCompleter for Fish {
|
|||
let completions = crate::engine::complete(cmd, args, index, current_dir)?;
|
||||
|
||||
for candidate in completions {
|
||||
write!(buf, "{}", candidate.get_content().to_string_lossy())?;
|
||||
write!(buf, "{}", candidate.get_value().to_string_lossy())?;
|
||||
if let Some(help) = candidate.get_help() {
|
||||
write!(
|
||||
buf,
|
||||
|
@ -313,7 +313,7 @@ Register-ArgumentCompleter -Native -CommandName {bin} -ScriptBlock {{
|
|||
let completions = crate::engine::complete(cmd, args, index, current_dir)?;
|
||||
|
||||
for candidate in completions {
|
||||
write!(buf, "{}", candidate.get_content().to_string_lossy())?;
|
||||
write!(buf, "{}", candidate.get_value().to_string_lossy())?;
|
||||
if let Some(help) = candidate.get_help() {
|
||||
write!(
|
||||
buf,
|
||||
|
@ -399,7 +399,7 @@ compdef _clap_dynamic_completer BIN"#
|
|||
if i != 0 {
|
||||
write!(buf, "{}", ifs.as_deref().unwrap_or("\n"))?;
|
||||
}
|
||||
write!(buf, "{}", candidate.get_content().to_string_lossy())?;
|
||||
write!(buf, "{}", candidate.get_value().to_string_lossy())?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -1006,7 +1006,7 @@ fn complete(cmd: &mut Command, args: impl AsRef<str>, current_dir: Option<&Path>
|
|||
.unwrap()
|
||||
.into_iter()
|
||||
.map(|candidate| {
|
||||
let compl = candidate.get_content().to_str().unwrap();
|
||||
let compl = candidate.get_value().to_str().unwrap();
|
||||
if let Some(help) = candidate.get_help() {
|
||||
format!("{compl}\t{help}")
|
||||
} else {
|
||||
|
|
Loading…
Reference in a new issue