Skip to content

Commit d0553b7

Browse files
committed
echo: print help if not posixly corrent and only argument is --help
1 parent f1436f3 commit d0553b7

File tree

3 files changed

+27
-14
lines changed

3 files changed

+27
-14
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,6 @@ unused_qualifications = "warn"
626626
all = { level = "warn", priority = -1 }
627627
cargo = { level = "warn", priority = -1 }
628628
pedantic = { level = "warn", priority = -1 }
629-
match_bool = "allow" # 8310
630629
cargo_common_metadata = "allow" # 3240
631630
multiple_crate_versions = "allow" # 2314
632631
missing_errors_doc = "allow" # 1504

src/uu/echo/src/echo.rs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ fn is_flag(arg: &OsStr, options: &mut Options) -> bool {
9999
///
100100
/// - Vector of non-flag arguments.
101101
/// - [`Options`], describing how teh arguments should be interpreted.
102-
fn filter_flags(mut args: impl uucore::Args) -> (Vec<OsString>, Options) {
102+
fn filter_flags(mut args: impl Iterator<Item = OsString>) -> (Vec<OsString>, Options) {
103103
let mut arguments = Vec::with_capacity(args.size_hint().0);
104104
let mut options = Options::default();
105105

@@ -124,7 +124,7 @@ fn filter_flags(mut args: impl uucore::Args) -> (Vec<OsString>, Options) {
124124
#[uucore::main]
125125
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
126126
// args[0] is the name of the binary.
127-
let mut args = args.skip(1).peekable();
127+
let args: Vec<OsString> = args.skip(1).collect();
128128

129129
// Check POSIX compatibility mode
130130
//
@@ -139,28 +139,36 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
139139
// > representation. For example, echo -e '\x2dn'.
140140
let is_posixly_correct = env::var_os("POSIXLY_CORRECT").is_some();
141141

142-
let (args, options) = match is_posixly_correct {
143-
// if POSIXLY_CORRECT is not set we filter the flags normally
144-
false => filter_flags(args),
145-
146-
true if args.peek().is_some_and(|arg| arg == "-n") => {
142+
let (args, options) = if is_posixly_correct {
143+
if args.first().is_some_and(|arg| arg == "-n") {
147144
// if POSIXLY_CORRECT is set and the first argument is the "-n" flag
148145
// we filter flags normally but 'escaped' is activated nonetheless.
149-
let (args, _) = filter_flags(args);
146+
let (args, _) = filter_flags(args.into_iter());
150147
(
151148
args,
152149
Options {
153150
trailing_newline: false,
154151
..Options::posixly_correct_default()
155152
},
156153
)
157-
}
158-
159-
true => {
154+
} else {
160155
// if POSIXLY_CORRECT is set and the first argument is not the "-n" flag
161-
// we just collect all arguments as every argument is considered an argument.
162-
(args.collect(), Options::posixly_correct_default())
156+
// we just collect all arguments as no arguments are interpreted as flags.
157+
(args, Options::posixly_correct_default())
163158
}
159+
} else if args.len() == 1 && args[0] == "--help" {
160+
// If POSIXLY_CORRECT is not set and the first argument
161+
// is `--help`, GNU coreutils prints the help message.
162+
//
163+
// Verify this using:
164+
//
165+
// POSIXLY_CORRECT=1 echo --help
166+
// echo --help
167+
uu_app().print_help()?;
168+
return Ok(());
169+
} else {
170+
// if POSIXLY_CORRECT is not set we filter the flags normally
171+
filter_flags(args.into_iter())
164172
};
165173

166174
execute(&mut io::stdout().lock(), args, options)?;

tests/by-util/test_echo.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,12 @@ fn partial_help_argument() {
514514
new_ucmd!().arg("--he").succeeds().stdout_is("--he\n");
515515
}
516516

517+
#[test]
518+
fn only_help_argument_prints_help() {
519+
assert_ne!(new_ucmd!().arg("--help").succeeds().stdout(), b"--help\n");
520+
assert_ne!(new_ucmd!().arg("--help").succeeds().stdout(), b"--help"); // This one is just in case.
521+
}
522+
517523
#[test]
518524
fn multibyte_escape_unicode() {
519525
// spell-checker:disable-next-line

0 commit comments

Comments
 (0)