@@ -110,6 +110,8 @@ use crate::path::Path;
110
110
use crate :: str;
111
111
use crate :: sys:: pipe:: { read2, AnonPipe } ;
112
112
use crate :: sys:: process as imp;
113
+ #[ unstable( feature = "command_access" , issue = "44434" ) ]
114
+ pub use crate :: sys_common:: process:: CommandEnvs ;
113
115
use crate :: sys_common:: { AsInner , AsInnerMut , FromInner , IntoInner } ;
114
116
115
117
/// Representation of a running or exited child process.
@@ -894,6 +896,98 @@ impl Command {
894
896
. map ( Child :: from_inner)
895
897
. and_then ( |mut p| p. wait ( ) )
896
898
}
899
+
900
+ /// Returns the path to the program that was given to [`Command::new`].
901
+ ///
902
+ /// # Examples
903
+ ///
904
+ /// ```
905
+ /// # #![feature(command_access)]
906
+ /// use std::process::Command;
907
+ ///
908
+ /// let cmd = Command::new("echo");
909
+ /// assert_eq!(cmd.get_program(), "echo");
910
+ /// ```
911
+ #[ unstable( feature = "command_access" , issue = "44434" ) ]
912
+ pub fn get_program ( & self ) -> & OsStr {
913
+ self . inner . get_program ( )
914
+ }
915
+
916
+ /// Returns an iterator of the arguments that will be passed to the program.
917
+ ///
918
+ /// This does not include the path to the program as the first argument;
919
+ /// it only includes the arguments specified with [`Command::arg`] and
920
+ /// [`Command::args`].
921
+ ///
922
+ /// # Examples
923
+ ///
924
+ /// ```
925
+ /// # #![feature(command_access)]
926
+ /// use std::ffi::OsStr;
927
+ /// use std::process::Command;
928
+ ///
929
+ /// let mut cmd = Command::new("echo");
930
+ /// cmd.arg("first").arg("second");
931
+ /// let args: Vec<&OsStr> = cmd.get_args().collect();
932
+ /// assert_eq!(args, &["first", "second"]);
933
+ /// ```
934
+ #[ unstable( feature = "command_access" , issue = "44434" ) ]
935
+ pub fn get_args ( & self ) -> CommandArgs < ' _ > {
936
+ CommandArgs { inner : self . inner . get_args ( ) }
937
+ }
938
+
939
+ /// Returns an iterator of the environment variables that will be set when
940
+ /// the process is spawned.
941
+ ///
942
+ /// Each element is a tuple `(&OsStr, Option<&OsStr>)`, where the first
943
+ /// value is the key, and the second is the value, which is [`None`] if
944
+ /// the environment variable is to be explicitly removed.
945
+ ///
946
+ /// This only includes environment variables explicitly set with
947
+ /// [`Command::env`], [`Command::envs`], and [`Command::env_remove`]. It
948
+ /// does not include environment variables that will be inherited by the
949
+ /// child process.
950
+ ///
951
+ /// # Examples
952
+ ///
953
+ /// ```
954
+ /// # #![feature(command_access)]
955
+ /// use std::ffi::OsStr;
956
+ /// use std::process::Command;
957
+ ///
958
+ /// let mut cmd = Command::new("ls");
959
+ /// cmd.env("TERM", "dumb").env_remove("TZ");
960
+ /// let envs: Vec<(&OsStr, Option<&OsStr>)> = cmd.get_envs().collect();
961
+ /// assert_eq!(envs, &[
962
+ /// (OsStr::new("TERM"), Some(OsStr::new("dumb"))),
963
+ /// (OsStr::new("TZ"), None)
964
+ /// ]);
965
+ /// ```
966
+ #[ unstable( feature = "command_access" , issue = "44434" ) ]
967
+ pub fn get_envs ( & self ) -> CommandEnvs < ' _ > {
968
+ self . inner . get_envs ( )
969
+ }
970
+
971
+ /// Returns the working directory for the child process.
972
+ ///
973
+ /// This returns [`None`] if the working directory will not be changed.
974
+ ///
975
+ /// # Examples
976
+ ///
977
+ /// ```
978
+ /// # #![feature(command_access)]
979
+ /// use std::path::Path;
980
+ /// use std::process::Command;
981
+ ///
982
+ /// let mut cmd = Command::new("ls");
983
+ /// assert_eq!(cmd.get_current_dir(), None);
984
+ /// cmd.current_dir("/bin");
985
+ /// assert_eq!(cmd.get_current_dir(), Some(Path::new("/bin")));
986
+ /// ```
987
+ #[ unstable( feature = "command_access" , issue = "44434" ) ]
988
+ pub fn get_current_dir ( & self ) -> Option < & Path > {
989
+ self . inner . get_current_dir ( )
990
+ }
897
991
}
898
992
899
993
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
@@ -918,6 +1012,37 @@ impl AsInnerMut<imp::Command> for Command {
918
1012
}
919
1013
}
920
1014
1015
+ /// An iterator over the command arguments.
1016
+ ///
1017
+ /// This struct is created by [`Command::get_args`]. See its documentation for
1018
+ /// more.
1019
+ #[ unstable( feature = "command_access" , issue = "44434" ) ]
1020
+ #[ derive( Debug ) ]
1021
+ pub struct CommandArgs < ' a > {
1022
+ inner : imp:: CommandArgs < ' a > ,
1023
+ }
1024
+
1025
+ #[ unstable( feature = "command_access" , issue = "44434" ) ]
1026
+ impl < ' a > Iterator for CommandArgs < ' a > {
1027
+ type Item = & ' a OsStr ;
1028
+ fn next ( & mut self ) -> Option < & ' a OsStr > {
1029
+ self . inner . next ( )
1030
+ }
1031
+ fn size_hint ( & self ) -> ( usize , Option < usize > ) {
1032
+ self . inner . size_hint ( )
1033
+ }
1034
+ }
1035
+
1036
+ #[ unstable( feature = "command_access" , issue = "44434" ) ]
1037
+ impl < ' a > ExactSizeIterator for CommandArgs < ' a > {
1038
+ fn len ( & self ) -> usize {
1039
+ self . inner . len ( )
1040
+ }
1041
+ fn is_empty ( & self ) -> bool {
1042
+ self . inner . is_empty ( )
1043
+ }
1044
+ }
1045
+
921
1046
/// The output of a finished process.
922
1047
///
923
1048
/// This is returned in a Result by either the [`output`] method of a
0 commit comments