Skip to content

Commit fead40f

Browse files
authored
feat: getters of Command (#102)
1 parent 07e9206 commit fead40f

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

src/lib.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,76 @@ impl Command {
862862
self
863863
}
864864

865+
/// Gets an iterator of the arguments that will be passed to the program.
866+
///
867+
/// # Examples
868+
///
869+
/// ```
870+
/// use std::ffi::OsStr;
871+
/// use async_process::Command;
872+
///
873+
/// let mut cmd = Command::new("echo");
874+
/// cmd.arg("first").arg("second");
875+
/// let args: Vec<&OsStr> = cmd.get_args().collect();
876+
/// assert_eq!(args, &["first", "second"]);
877+
/// ```
878+
pub fn get_args(&self) -> std::process::CommandArgs<'_> {
879+
self.inner.get_args()
880+
}
881+
882+
/// Gets an iterator of the environment variables explicitly set for the child process.
883+
///
884+
/// # Examples
885+
///
886+
/// ```
887+
/// use std::ffi::OsStr;
888+
/// use async_process::Command;
889+
///
890+
/// let mut cmd = Command::new("ls");
891+
/// cmd.env("TERM", "dumb").env_remove("TZ");
892+
/// let envs: Vec<(&OsStr, Option<&OsStr>)> = cmd.get_envs().collect();
893+
/// assert_eq!(envs, &[
894+
/// (OsStr::new("TERM"), Some(OsStr::new("dumb"))),
895+
/// (OsStr::new("TZ"), None)
896+
/// ]);
897+
/// ```
898+
pub fn get_envs(&self) -> std::process::CommandEnvs<'_> {
899+
self.inner.get_envs()
900+
}
901+
902+
/// Gets the working directory for the child process.
903+
///
904+
/// This returns [`None`] if the working directory will not be changed.
905+
///
906+
/// # Examples
907+
///
908+
/// ```
909+
/// use std::path::Path;
910+
/// use async_process::Command;
911+
///
912+
/// let mut cmd = Command::new("ls");
913+
/// assert_eq!(cmd.get_current_dir(), None);
914+
/// cmd.current_dir("/bin");
915+
/// assert_eq!(cmd.get_current_dir(), Some(Path::new("/bin")));
916+
/// ```
917+
pub fn get_current_dir(&self) -> Option<&Path> {
918+
self.inner.get_current_dir()
919+
}
920+
921+
/// Gets the path to the program that was given to [`Command::new`].
922+
///
923+
/// # Examples
924+
///
925+
/// ```
926+
/// use async_process::Command;
927+
///
928+
/// let cmd = Command::new("echo");
929+
/// assert_eq!(cmd.get_program(), "echo");
930+
/// ```
931+
pub fn get_program(&self) -> &OsStr {
932+
self.inner.get_program()
933+
}
934+
865935
/// Removes an environment variable mapping.
866936
///
867937
/// # Examples

0 commit comments

Comments
 (0)