diff --git a/src/env/env.rs b/src/env/env.rs index 0203e3ba8..931277bb6 100644 --- a/src/env/env.rs +++ b/src/env/env.rs @@ -47,7 +47,7 @@ pub fn uumain(args: Vec) -> i32 { .optflag( "0", "null", - "end each output line with a 0 byte rather than newline", + "end each output line with a 0 byte rather than newline (only valid when printing the environment)", ) .optopt("u", "unset", "remove variable from the environment", "NAME"); @@ -103,14 +103,14 @@ pub fn uumain(args: Vec) -> i32 { let var = iter.next(); match var { - None => println!("{}: this option requires an argument: {}", NAME, opt), + None => eprintln!("{}: this option requires an argument: {}", NAME, opt), Some(s) => opts.unsets.push(s.to_owned()), } } _ => { - println!("{}: invalid option \"{}\"", NAME, *opt); - println!("Type \"{} --help\" for detailed informations", NAME); + eprintln!("{}: invalid option \"{}\"", NAME, *opt); + eprintln!("Type \"{} --help\" for detailed information", NAME); return 1; } } @@ -134,13 +134,13 @@ pub fn uumain(args: Vec) -> i32 { let var = iter.next(); match var { - None => println!("{}: this option requires an argument: {}", NAME, opt), + None => eprintln!("{}: this option requires an argument: {}", NAME, opt), Some(s) => opts.unsets.push(s.to_owned()), } } _ => { - println!("{}: illegal option -- {}", NAME, c); - println!("Type \"{} --help\" for detailed informations", NAME); + eprintln!("{}: illegal option -- {}", NAME, c); + eprintln!("Type \"{} --help\" for detailed information", NAME); return 1; } } @@ -159,6 +159,11 @@ pub fn uumain(args: Vec) -> i32 { } // no, its a program-like opt _ => { + if opts.null { + eprintln!("{}: cannot specify --null (-0) with command", NAME); + eprintln!("Type \"{} --help\" for detailed information", NAME); + return 1; + } opts.program.push(opt.clone()); break; } @@ -170,6 +175,11 @@ pub fn uumain(args: Vec) -> i32 { // read program arguments for opt in iter { + if opts.null { + eprintln!("{}: cannot specify --null (-0) with command", NAME); + eprintln!("Type \"{} --help\" for detailed information", NAME); + return 1; + } opts.program.push(opt.clone()) } diff --git a/tests/common/util.rs b/tests/common/util.rs index 43fe255f7..38b92fa24 100755 --- a/tests/common/util.rs +++ b/tests/common/util.rs @@ -606,7 +606,7 @@ impl UCommand { } /// Spawns the command, feeds the stdin if any, waits for the result, - /// asserts success, and returns a command result. + /// asserts failure, and returns a command result. pub fn fails(&mut self) -> CmdResult { let cmd_result = self.run(); cmd_result.failure(); diff --git a/tests/test_env.rs b/tests/test_env.rs index 6646d5af7..6b81d1e48 100644 --- a/tests/test_env.rs +++ b/tests/test_env.rs @@ -81,3 +81,9 @@ fn test_unset_variable() { assert_eq!(out.lines().any(|line| line.starts_with("HOME=")), false); } + +#[test] +fn test_fail_null_with_program() { + let out = new_ucmd!().arg("--null").arg("cd").fails().stderr; + assert!(out.contains("cannot specify --null (-0) with command")); +}