File tree Expand file tree Collapse file tree 4 files changed +68
-0
lines changed Expand file tree Collapse file tree 4 files changed +68
-0
lines changed Original file line number Diff line number Diff line change 306
306
#![ feature( min_specialization) ]
307
307
#![ feature( mixed_integer_ops) ]
308
308
#![ feature( must_not_suspend) ]
309
+ #![ feature( mutate_command_args) ]
309
310
#![ feature( needs_panic_runtime) ]
310
311
#![ feature( negative_impls) ]
311
312
#![ feature( never_type) ]
Original file line number Diff line number Diff line change @@ -648,6 +648,38 @@ impl Command {
648
648
self
649
649
}
650
650
651
+ /// Clears the argument array.
652
+ ///
653
+ /// When one wants to restart a Command again with different
654
+ /// arguments the argument list can to be cleared first.
655
+ ///
656
+ /// # Examples
657
+ ///
658
+ /// Basic usage:
659
+ ///
660
+ /// ```no_run
661
+ /// #![feature(mutate_command_args)]
662
+ /// use std::process::Command;
663
+ ///
664
+ /// let mut lsdir = Command::new("ls");
665
+ ///
666
+ /// lsdir
667
+ /// .arg("target")
668
+ /// .spawn()
669
+ /// .expect("ls command failed to start");
670
+ ///
671
+ /// lsdir
672
+ /// .args_clear()
673
+ /// .arg("tests")
674
+ /// .spawn()
675
+ /// .expect("ls command failed to start");
676
+ /// ```
677
+ #[ unstable( feature = "mutate_command_args" , issue = "87379" ) ]
678
+ pub fn args_clear ( & mut self ) -> & mut Command {
679
+ self . inner . args_clear ( ) ;
680
+ self
681
+ }
682
+
651
683
/// Inserts or updates an environment variable mapping.
652
684
///
653
685
/// Note that environment variable names are case-insensitive (but case-preserving) on Windows,
Original file line number Diff line number Diff line change @@ -211,6 +211,32 @@ fn test_wait_with_output_once() {
211
211
assert_eq ! ( stderr, Vec :: new( ) ) ;
212
212
}
213
213
214
+ #[ test]
215
+ fn test_args_clear ( ) {
216
+ let mut prog = Command :: new ( "echo" ) ;
217
+
218
+ if cfg ! ( target_os = "windows" ) {
219
+ prog. args ( & [ "/C" , "echo reset_me" ] )
220
+ } else {
221
+ prog. arg ( "reset_me" )
222
+ } ;
223
+
224
+ prog. args_clear ( ) ;
225
+
226
+ if cfg ! ( target_os = "windows" ) {
227
+ prog. args ( & [ "/C" , "echo hello" ] ) ;
228
+ } else {
229
+ prog. arg ( "hello" ) ;
230
+ } ;
231
+
232
+ let Output { status, stdout, stderr } = prog. output ( ) . unwrap ( ) ;
233
+ let output_str = str:: from_utf8 ( & stdout) . unwrap ( ) ;
234
+
235
+ assert ! ( status. success( ) ) ;
236
+ assert_eq ! ( output_str. trim( ) . to_string( ) , "hello" ) ;
237
+ assert_eq ! ( stderr, Vec :: new( ) ) ;
238
+ }
239
+
214
240
#[ cfg( all( unix, not( target_os = "android" ) ) ) ]
215
241
pub fn env_cmd ( ) -> Command {
216
242
Command :: new ( "env" )
Original file line number Diff line number Diff line change @@ -190,6 +190,15 @@ impl Command {
190
190
self . args . push ( arg) ;
191
191
}
192
192
193
+ pub fn args_clear ( & mut self ) {
194
+ // resize `argv` to 2 (argv[0] and NULL).
195
+ self . argv . 0 . truncate ( 2 ) ;
196
+ self . argv . 0 [ 1 ] = ptr:: null ( ) ;
197
+
198
+ // drop all excess args.
199
+ self . args . truncate ( 1 ) ;
200
+ }
201
+
193
202
pub fn cwd ( & mut self , dir : & OsStr ) {
194
203
self . cwd = Some ( os2c ( dir, & mut self . saw_nul ) ) ;
195
204
}
You can’t perform that action at this time.
0 commit comments