Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitec2e85c

Browse files
committed
echo: print help if not posixly corrent and only argument is --help
1 parente2a8afa commitec2e85c

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

‎src/uu/echo/src/echo.rs

Lines changed: 18 additions & 6 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-
fnfilter_flags(mutargs:impluucore::Args) ->(Vec<OsString>,Options){
102+
fnfilter_flags(mutargs:implIterator<Item =OsString>) ->(Vec<OsString>,Options){
103103
letmut arguments =Vec::with_capacity(args.size_hint().0);
104104
letmut options =Options::default();
105105

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

129129
// Check POSIX compatibility mode
130130
//
@@ -140,13 +140,25 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
140140
let is_posixly_correct = env::var_os("POSIXLY_CORRECT").is_some();
141141

142142
let(args, options) =match is_posixly_correct{
143+
// If POSIXLY_CORRECT is not set and the first argument
144+
// is `--help`, GNU coreutils prints the help message.
145+
//
146+
// Verify this using:
147+
//
148+
// POSIXLY_CORRECT=1 echo --help
149+
// echo --help
150+
falseif args.len() ==1 && args[0] =="--help" =>{
151+
uu_app().print_help()?;
152+
returnOk(());
153+
}
154+
143155
// if POSIXLY_CORRECT is not set we filter the flags normally
144-
false =>filter_flags(args),
156+
false =>filter_flags(args.into_iter()),
145157

146-
trueif args.peek().is_some_and(|arg| arg =="-n") =>{
158+
trueif args.first().is_some_and(|arg| arg =="-n") =>{
147159
// if POSIXLY_CORRECT is set and the first argument is the "-n" flag
148160
// we filter flags normally but 'escaped' is activated nonetheless.
149-
let(args, _) =filter_flags(args);
161+
let(args, _) =filter_flags(args.into_iter());
150162
(
151163
args,
152164
Options{
@@ -159,7 +171,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
159171
true =>{
160172
// if POSIXLY_CORRECT is set and the first argument is not the "-n" flag
161173
// we just collect all arguments as every argument is considered an argument.
162-
(args.collect(),Options::posixly_correct_default())
174+
(args,Options::posixly_correct_default())
163175
}
164176
};
165177

‎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+
fnonly_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
fnmultibyte_escape_unicode(){
519525
// spell-checker:disable-next-line

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp