Skip to content

Commit 155a215

Browse files
committed
coreutils: output proper error for unrecognized options
1 parent efa1aa7 commit 155a215

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/bin/coreutils.rs

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

8080
match util {
8181
"--list" => {
82+
// If --help is also present, show usage instead of list
83+
if args.any(|arg| arg == "--help" || arg == "-h") {
84+
usage(&utils, binary_as_util);
85+
process::exit(0);
86+
}
8287
let mut utils: Vec<_> = utils.keys().collect();
8388
utils.sort();
8489
for util in utils {
@@ -127,6 +132,9 @@ fn main() {
127132
}
128133
usage(&utils, binary_as_util);
129134
process::exit(0);
135+
} else if util.starts_with('-') {
136+
// Argument looks like an option but wasn't recognized
137+
validation::unrecognized_option(binary_as_util, &util_os);
130138
} else {
131139
validation::not_found(&util_os);
132140
}

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)