diff --git a/src/main.rs b/src/main.rs index 1992ea8..245f2db 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,24 +1,23 @@ -use std::{env, process}; +use std::env; -fn main() { - let path_or_url = match env::args().nth(1) { +fn main() -> Result<(), Box> { + let mut args = env::args(); + let path_or_url = match args.nth(1) { Some(arg) => arg, - None => { - eprintln!("usage: open "); - process::exit(1); - } + None => return Err("usage: open [--with|-w program]".into()), }; - 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), - }; + }?; - 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(()) } diff --git a/src/windows.rs b/src/windows.rs index 1fe7441..e9f9aee 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -20,6 +20,8 @@ pub fn commands>(path: T) -> Vec { pub fn with_command>(path: T, app: impl Into) -> Command { let mut cmd = Command::new("cmd"); cmd.arg("/c") + .arg("start") + .raw_arg("\"\"") .raw_arg(app.into()) .raw_arg(wrap_in_quotes(path)) .creation_flags(CREATE_NO_WINDOW);