Skip to content

Commit e7f8247

Browse files
coreutils: output proper error for unrecognized options (#9869)
Co-authored-by: Sylvestre Ledru <sylvestre@debian.org>
1 parent c9c78b2 commit e7f8247

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

src/bin/coreutils.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ fn main() {
7575

7676
match util {
7777
"--list" => {
78+
// If --help is also present, show usage instead of list
79+
if args.any(|arg| arg == "--help" || arg == "-h") {
80+
usage(&utils, binary_as_util);
81+
process::exit(0);
82+
}
7883
let mut utils: Vec<_> = utils.keys().collect();
7984
utils.sort();
8085
for util in utils {
@@ -123,6 +128,9 @@ fn main() {
123128
}
124129
usage(&utils, binary_as_util);
125130
process::exit(0);
131+
} else if util.starts_with('-') {
132+
// Argument looks like an option but wasn't recognized
133+
validation::unrecognized_option(binary_as_util, &util_os);
126134
} else {
127135
validation::not_found(&util_os);
128136
}

src/common/validation.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ pub fn not_found(util: &OsStr) -> ! {
2929
process::exit(1);
3030
}
3131

32+
/// Prints an "unrecognized option" error and exits
33+
pub fn unrecognized_option(binary_name: &str, option: &OsStr) -> ! {
34+
eprintln!(
35+
"{}: unrecognized option '{}'",
36+
binary_name,
37+
option.to_string_lossy()
38+
);
39+
process::exit(1);
40+
}
41+
3242
/// Sets up localization for a utility with proper error handling
3343
pub fn setup_localization_or_exit(util_name: &str) {
3444
let util_name = get_canonical_util_name(util_name);

0 commit comments

Comments
 (0)