Skip to content

Commit d45107d

Browse files
committed
Improvement with more APIs
1 parent 924f807 commit d45107d

File tree

1 file changed

+50
-6
lines changed

1 file changed

+50
-6
lines changed

wslplugins-rs/src/api/wsl_command.rs

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ impl<'a> WSLCommand<'a> {
6868
///
6969
/// # Parameters
7070
/// - `arg0`: The new value for the first argument.
71-
pub fn arg0(&mut self, arg0: &'a str) -> &mut Self {
72-
self.args[0] = arg0;
71+
pub fn arg0<T: AsRef<str> + ?Sized>(&mut self, arg0: &'a T) -> &mut Self {
72+
self.args[0] = arg0.as_ref();
7373
self
7474
}
7575

@@ -93,17 +93,21 @@ impl<'a> WSLCommand<'a> {
9393
///
9494
/// # Parameters
9595
/// - `arg`: The argument to add.
96-
pub fn arg(&mut self, arg: &'a str) -> &mut Self {
97-
self.args.push(arg);
96+
pub fn arg<T: AsRef<str> + ?Sized>(&mut self, arg: &'a T) -> &mut Self {
97+
self.args.push(arg.as_ref());
9898
self
9999
}
100100

101101
/// Adds multiple arguments to the command.
102102
///
103103
/// # Parameters
104104
/// - `args`: An iterator of arguments to add.
105-
pub fn args<I: IntoIterator<Item = &'a str>>(&mut self, args: I) -> &mut Self {
106-
self.args.extend(args);
105+
pub fn args<I, T>(&mut self, args: I) -> &mut Self
106+
where
107+
I: IntoIterator<Item = &'a T>,
108+
T: 'a + AsRef<str> + ?Sized,
109+
{
110+
self.args.extend(args.into_iter().map(AsRef::as_ref));
107111
self
108112
}
109113

@@ -112,6 +116,46 @@ impl<'a> WSLCommand<'a> {
112116
self.args[1..].iter().copied()
113117
}
114118

119+
/// Clears all arguments except arg0.
120+
///
121+
/// This method removes all additional arguments from the command,
122+
/// effectively resetting the arguments to only include the program path.
123+
///
124+
/// # Returns
125+
/// A mutable reference to the current `WSLCommand` instance.
126+
///
127+
/// # Example
128+
/// ```rust ignore
129+
/// command.arg("Hello").arg("World");
130+
/// command.clear_args(); // Only arg0 ("/bin/echo") remains.
131+
/// assert_eq!(command.get_args().count(), 0)
132+
/// ```
133+
pub fn crear_args(&mut self) -> &mut Self {
134+
self.truncate_args(0);
135+
self
136+
}
137+
138+
/// Truncates the arguments of the command after a specified index.
139+
///
140+
/// This method keeps `arg0` and the first `i` additional arguments, discarding the rest.
141+
///
142+
/// # Parameters
143+
/// - `i`: The index after which arguments will be removed. Note that `i = 0` keeps only `arg0`.
144+
///
145+
/// # Returns
146+
/// A mutable reference to the current `WSLCommand` instance.
147+
///
148+
/// # Example
149+
/// ```rust ignore
150+
/// let mut command = WSLCommand::new(context, session, "/bin/echo");
151+
/// command.arg("Hello").arg("World");
152+
/// command.truncate_args(1); // Keeps only "/bin/echo" and "Hello".
153+
/// ```
154+
pub fn truncate_args(&mut self, i: usize) -> &mut Self {
155+
self.args.truncate(i + 1);
156+
self
157+
}
158+
115159
/// Sets the distribution ID for the command.
116160
///
117161
/// # Parameters

0 commit comments

Comments
 (0)