Repository: clap-rs/clap
Pull request #5695 (2024-08-22 17:29:59)

fix(complete)!: Be consistent in value language
<!--
Thanks for helping out!

Please link the appropriate issue from your PR.

If you don't have an issue, we'd recommend starting with one first so the PR can focus on the
implementation (unless its an obvious bug or documentation fix that will have
little conversation).
-->



Patch:
diff --git a/clap_complete/src/command/shells.rs b/clap_complete/src/command/shells.rs
index acadb16d..cf1cbdd8 100644
--- a/clap_complete/src/command/shells.rs
+++ b/clap_complete/src/command/shells.rs
@@ -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(())
     }
diff --git a/clap_complete/src/engine/candidate.rs b/clap_complete/src/engine/candidate.rs
index ed0d0df1..c75b46eb 100644
--- a/clap_complete/src/engine/candidate.rs
+++ b/clap_complete/src/engine/candidate.rs
@@ -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
diff --git a/clap_complete/src/engine/complete.rs b/clap_complete/src/engine/complete.rs
index 24fc9cac..b9bb2522 100644
--- a/clap_complete/src/engine/complete.rs
+++ b/clap_complete/src/engine/complete.rs
@@ -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();
diff --git a/clap_complete/src/env/shells.rs b/clap_complete/src/env/shells.rs
index eee9eee4..2c527d94 100644
--- a/clap_complete/src/env/shells.rs
+++ b/clap_complete/src/env/shells.rs
@@ -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(())
     }


Test patch:
diff --git a/clap_complete/tests/testsuite/engine.rs b/clap_complete/tests/testsuite/engine.rs
index a4211183..5acb77bc 100644
--- a/clap_complete/tests/testsuite/engine.rs
+++ b/clap_complete/tests/testsuite/engine.rs
@@ -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 {