Skip to content

Commit b5528b6

Browse files
committed
cleanup main program to support --with on all platforms.
Technically this is a breaking change as the way it communicates now looks different, and error handling is different as well.
1 parent 5607cd6 commit b5528b6

File tree

1 file changed

+16
-27
lines changed

1 file changed

+16
-27
lines changed

src/main.rs

+16-27
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,23 @@
1-
use std::{env, process};
1+
use std::env;
22

3-
fn main() {
4-
let path_or_url = match env::args().nth(1) {
3+
fn main() -> Result<(), Box<dyn std::error::Error>> {
4+
let mut args = env::args();
5+
let path_or_url = match args.nth(1) {
56
Some(arg) => arg,
6-
None => {
7-
eprintln!("usage: open <path-or-url>");
8-
process::exit(1);
9-
}
7+
None => return Err("usage: open <path-or-url> [--with|-w program]".into()),
108
};
119

12-
#[cfg(not(windows))]
13-
let result = match std::env::var("OPEN_WITH").ok() {
14-
Some(program) => open::with(&path_or_url, program),
10+
match args.next() {
11+
Some(arg) if arg == "--with" || arg == "-w" => {
12+
let program = args
13+
.next()
14+
.ok_or("--with must be followed by the program to use for opening")?;
15+
open::with(&path_or_url, program)
16+
}
17+
Some(arg) => return Err(format!("Argument '{arg}' is invalid").into()),
1518
None => open::that(&path_or_url),
16-
};
19+
}?;
1720

18-
#[cfg(windows)]
19-
let result = match env::args().nth(2) {
20-
Some(arg) if arg == "--with" || arg == "-w" => match env::args().nth(3) {
21-
Some(program) => open::with(&path_or_url, program),
22-
None => open::that(&path_or_url),
23-
},
24-
_ => open::that(&path_or_url),
25-
};
26-
27-
match result {
28-
Ok(()) => println!("Opened '{}' successfully.", path_or_url),
29-
Err(err) => {
30-
eprintln!("An error occurred when opening '{}': {}", path_or_url, err);
31-
process::exit(3);
32-
}
33-
}
21+
println!("Opened '{}' successfully.", path_or_url);
22+
Ok(())
3423
}

0 commit comments

Comments
 (0)