From 5607cd69f4f53aa02c67bccb60f7d4f9be6b74ea Mon Sep 17 00:00:00 2001 From: ZhuXiaojie Date: Sun, 25 Jun 2023 11:02:51 +0800 Subject: [PATCH 1/2] Fix open::with() on Windows. (#80) --- src/main.rs | 10 ++++++++++ src/windows.rs | 2 ++ 2 files changed, 12 insertions(+) diff --git a/src/main.rs b/src/main.rs index 1992ea8..a7eee23 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,11 +9,21 @@ fn main() { } }; + #[cfg(not(windows))] let result = match std::env::var("OPEN_WITH").ok() { Some(program) => open::with(&path_or_url, program), 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) => { 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); From b5528b60d49f4dd48449d9ff5b75edbea71ce248 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Sun, 25 Jun 2023 09:08:25 +0200 Subject: [PATCH 2/2] 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. --- src/main.rs | 43 ++++++++++++++++--------------------------- 1 file changed, 16 insertions(+), 27 deletions(-) diff --git a/src/main.rs b/src/main.rs index a7eee23..245f2db 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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> { + 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()), }; - #[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(()) }