From 508d74fe4afe30ddff2af2f95845f10910ae57f1 Mon Sep 17 00:00:00 2001 From: Devel Date: Tue, 2 Jun 2026 18:37:35 +0300 Subject: [PATCH] Show usage when invoked without argments * exit with code 1 when no arguments * add a test for no arguments. * Update test_sed.rs * cargo fmt * pass -n for missing script arguments test Issue: #274 PR: #409 --- src/sed/mod.rs | 9 +++++++++ tests/by-util/test_sed.rs | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/src/sed/mod.rs b/src/sed/mod.rs index 1206140..7605f52 100644 --- a/src/sed/mod.rs +++ b/src/sed/mod.rs @@ -36,6 +36,15 @@ const USAGE: &str = "sed [OPTION]... [script] [file]..."; #[uucore::main] pub fn uumain(args: impl uucore::Args) -> UResult<()> { let matches = uu_app().try_get_matches_from(args)?; + + // Don't use arg_required_else_help when declaring command + // as it exits with code 2 and we use it to check + // default matches in tests. + if !matches.args_present() { + let _ = uu_app().print_help(); + std::process::exit(1); + } + let (scripts, files) = get_scripts_files(&matches)?; let mut context = build_context(&matches); diff --git a/tests/by-util/test_sed.rs b/tests/by-util/test_sed.rs index fdc13bd..401e887 100644 --- a/tests/by-util/test_sed.rs +++ b/tests/by-util/test_sed.rs @@ -54,11 +54,20 @@ fn test_silent_alias() { #[test] fn test_missing_script_argument() { new_ucmd!() + .args(&["-n"]) .fails() .code_is(1) .stderr_contains("missing script"); } +#[test] +fn test_no_arguments() { + new_ucmd!() + .fails() + .code_is(1) + .stdout_contains("Stream editor for filtering and transforming text"); +} + #[test] fn test_positional_script_ok() { new_ucmd!().arg("l").succeeds().code_is(0);