pub struct WSLCommand<'a> { /* private fields */ }Expand description
The WSLCommand struct represents a command that can be executed within the WSL environment.
A prepared command to be executed inside WSL.
WSLCommand is a builder-style abstraction around the WSL Plugin API
execution functions (ExecuteBinary / ExecuteBinaryInDistribution).
Instances of WSLCommand are created through ApiV1::new_command,
ensuring they are always tied to a valid API handle and session.
§Key points
- The program path is a Linux path (UTF-8, Unix-style) represented by
Utf8UnixPath. - Arguments are stored as
Cow<'a, str>to minimize allocations. argv[0]can be overridden; otherwise it defaults to the program path.- The execution target can be the system context or a specific distribution.
§Argument semantics
The underlying WSL Plugin API expects a NULL-terminated argv array:
argv[0] = program name
argv[1..] = user argumentsThis type exposes:
WSLCommand::argvfor iterating over the full argument vector,WSLCommand::arg0/WSLCommand::with_arg0to overrideargv[0].
§Examples
§Basic execution
let stream = api
.new_command(SessionID::from(0), "/bin/cat")
.with_arg("/proc/version")
.execute()?;
§Overriding argv[0]
Some programs inspect argv[0] to determine their behavior
(for example busybox).
let stream = api
.new_command(session_id, "/bin/busybox")
.with_arg0("sh")
.with_args(["-c", "echo hello"])
.execute()?;
§Executing in a user distribution
let distro: UserDistributionID = "3B6F3C1E-9B4A-4F2C-8E7A-2A9C6D4E1F52".parse().unwrap();
let stream = api
.new_command(SessionID::from(0), "/bin/echo")
.with_distribution_id(DistributionID::User(distro))
.with_arg("hello")
.execute()?;§Notes
WSLCommand::executeconsumes the command and returns a connectedTcpStreamto the process stdin/stdout.- stderr is forwarded to
dmesgon the Linux side. - This type performs no validation of the Linux path or arguments beyond UTF-8 handling.
Implementations§
Source§impl<'a> WSLCommand<'a>
impl<'a> WSLCommand<'a>
Sourcepub fn get_path(&self) -> &Utf8UnixPath
pub fn get_path(&self) -> &Utf8UnixPath
Returns the program path as a Utf8UnixPath.
Sourcepub fn get_arg0(&self) -> &str
pub fn get_arg0(&self) -> &str
Returns argv[0].
If arg0 has not been overridden, this returns the program path string.
Sourcepub const fn is_standard_arg_0(&self) -> bool
pub const fn is_standard_arg_0(&self) -> bool
Returns true if argv[0] is the default value (the program path).
Sourcepub fn reset_arg0(&mut self) -> &mut Self
pub fn reset_arg0(&mut self) -> &mut Self
Resets argv[0] to its default value (the program path).
Sourcepub fn arg0<T: Into<Cow<'a, str>>>(&mut self, arg0: T) -> &mut Self
pub fn arg0<T: Into<Cow<'a, str>>>(&mut self, arg0: T) -> &mut Self
Sets argv[0] (builder-style, by mutable reference).
Sourcepub fn with_arg0<T: Into<Cow<'a, str>>>(self, arg0: T) -> Self
pub fn with_arg0<T: Into<Cow<'a, str>>>(self, arg0: T) -> Self
Sets argv[0] (builder-style, by value).
Sourcepub fn arg<T: Into<Cow<'a, str>>>(&mut self, arg: T) -> &mut Self
pub fn arg<T: Into<Cow<'a, str>>>(&mut self, arg: T) -> &mut Self
Pushes one argument (argv[n], n >= 1), builder-style by mutable reference.
Sourcepub fn with_arg<T: Into<Cow<'a, str>>>(self, arg: T) -> Self
pub fn with_arg<T: Into<Cow<'a, str>>>(self, arg: T) -> Self
Pushes one argument (argv[n], n >= 1), builder-style by value.
Sourcepub fn args<I>(&mut self, args: I) -> &mut Self
pub fn args<I>(&mut self, args: I) -> &mut Self
Extends arguments from an iterator, builder-style by mutable reference.
Sourcepub fn with_args<I>(self, args: I) -> Self
pub fn with_args<I>(self, args: I) -> Self
Extends arguments from an iterator, builder-style by value.
Sourcepub fn get_args(&self) -> impl ExactSizeIterator<Item = &str>
pub fn get_args(&self) -> impl ExactSizeIterator<Item = &str>
Returns an iterator over user-provided arguments (argv[1..]).
Sourcepub fn argv(&self) -> impl Iterator<Item = &str>
pub fn argv(&self) -> impl Iterator<Item = &str>
Returns an iterator over the full argv (argv[0] + argv[1..]).
Sourcepub fn clear_args(&mut self) -> &mut Self
pub fn clear_args(&mut self) -> &mut Self
Clears user-provided arguments (argv[1..]).
Sourcepub fn truncate_args(&mut self, i: usize) -> &mut Self
pub fn truncate_args(&mut self, i: usize) -> &mut Self
Truncates user-provided arguments (argv[1..]) to length i.
Sourcepub fn distribution_id(&mut self, distribution_id: DistributionID) -> &mut Self
pub fn distribution_id(&mut self, distribution_id: DistributionID) -> &mut Self
Sets the distribution target (builder-style by mutable reference).
Sourcepub fn with_distribution_id(self, distribution_id: DistributionID) -> Self
pub fn with_distribution_id(self, distribution_id: DistributionID) -> Self
Sets the distribution target (builder-style by value).
Sourcepub fn reset_distribution_id(&mut self) -> &mut Self
pub fn reset_distribution_id(&mut self) -> &mut Self
Resets the distribution target to DistributionID::System.
Sourcepub const fn get_distribution_id(&self) -> DistributionID
pub const fn get_distribution_id(&self) -> DistributionID
Returns the current distribution target.
Sourcepub fn prepare(&self) -> PreparedWSLCommand<'a>
pub fn prepare(&self) -> PreparedWSLCommand<'a>
Prepare a WSLCommand for execution, encoding the path and arguments into C-compatible formats.
Trait Implementations§
Source§impl<'a> Clone for WSLCommand<'a>
impl<'a> Clone for WSLCommand<'a>
Source§fn clone(&self) -> WSLCommand<'a>
fn clone(&self) -> WSLCommand<'a>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more