Skip to content

Commit

Permalink
xargs: print help/version without error prefix
Browse files Browse the repository at this point in the history
xargs, when executed with -h/--help or -V/--version, is printing an
"Error: " prefix before the help and version text. The exit code is
also incorrect (1, instead of 0).

Display the help/version text without the erroneous prefix, and exit
with the correct exit code (success, 0).

Add tests for this bug to prevent regressions.
  • Loading branch information
andrewliebenow committed Sep 22, 2024
1 parent eefc580 commit fecba23
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/xargs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::{
process::{Command, Stdio},
};

use clap::{crate_version, Arg, ArgAction};
use clap::{crate_version, error::ErrorKind, Arg, ArgAction};

mod options {
pub const COMMAND: &str = "COMMAND";
Expand Down Expand Up @@ -954,7 +954,15 @@ fn do_xargs(args: &[&str]) -> Result<CommandResult, XargsError> {

let matches = match matches {
Ok(m) => m,
Err(e) => return Err(XargsError::from(e.to_string())),
Err(e) => match e.kind() {
ErrorKind::DisplayHelp | ErrorKind::DisplayVersion => {
// The help/version text already has a newline, so use `print!` here, not `println!`
print!("{e}");

return Ok(CommandResult::Success);
}
_ => return Err(XargsError::from(e.to_string())),
},
};

let options = Options {
Expand Down
34 changes: 34 additions & 0 deletions tests/xargs_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -564,3 +564,37 @@ fn xargs_replace_multiple_lines() {
.stderr(predicate::str::is_empty())
.stdout(predicate::str::diff("\n\n\n"));
}

#[test]
fn xargs_help() {
for option_style in ["-h", "--help"] {
Command::cargo_bin("xargs")
.expect("found binary")
.args([option_style])
.assert()
.success()
.code(0)
.stderr(predicate::str::is_empty())
.stdout(predicate::str::contains("Usage: xargs "));
}
}

// Do not regress to:
//
// ❯ xargs --version
// Error: xargs 0.7.0
//
// Same for help above
#[test]
fn xargs_version() {
for option_style in ["-V", "--version"] {
Command::cargo_bin("xargs")
.expect("found binary")
.args([option_style])
.assert()
.success()
.code(0)
.stderr(predicate::str::is_empty())
.stdout(predicate::str::starts_with("xargs "));
}
}

0 comments on commit fecba23

Please sign in to comment.