-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
1 changed file
with
16 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,23 @@ | ||
use std::{env, process}; | ||
use std::env; | ||
|
||
fn main() { | ||
let path_or_url = match env::args().nth(1) { | ||
fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
let mut args = env::args(); | ||
let path_or_url = match args.nth(1) { | ||
Some(arg) => arg, | ||
None => { | ||
eprintln!("usage: open <path-or-url>"); | ||
process::exit(1); | ||
} | ||
None => return Err("usage: open <path-or-url> [--with|-w program]".into()), | ||
}; | ||
|
||
#[cfg(not(windows))] | ||
let result = match std::env::var("OPEN_WITH").ok() { | ||
Some(program) => open::with(&path_or_url, program), | ||
match args.next() { | ||
Some(arg) if arg == "--with" || arg == "-w" => { | ||
let program = args | ||
.next() | ||
.ok_or("--with must be followed by the program to use for opening")?; | ||
open::with(&path_or_url, program) | ||
} | ||
Some(arg) => return Err(format!("Argument '{arg}' is invalid").into()), | ||
None => open::that(&path_or_url), | ||
}; | ||
}?; | ||
|
||
#[cfg(windows)] | ||
let result = match env::args().nth(2) { | ||
Some(arg) if arg == "--with" || arg == "-w" => match env::args().nth(3) { | ||
Some(program) => open::with(&path_or_url, program), | ||
None => open::that(&path_or_url), | ||
}, | ||
_ => open::that(&path_or_url), | ||
}; | ||
|
||
match result { | ||
Ok(()) => println!("Opened '{}' successfully.", path_or_url), | ||
Err(err) => { | ||
eprintln!("An error occurred when opening '{}': {}", path_or_url, err); | ||
process::exit(3); | ||
} | ||
} | ||
println!("Opened '{}' successfully.", path_or_url); | ||
Ok(()) | ||
} |