Skip to content

Commit ddf4842

Browse files
committed
fix!: with() on windows to be able to lookup program in registry/PATH.
The underlying invocation of `with()` changed slightly on windows to make it more useful as it can now find application names like `chrome` in the registry, but that change may also be breaking for some who previously worked around the previous behaviour. Please let us know if this truly works better, or if more changes are needed to launch something with a program on Windows. See #82 for details.
2 parents 659b8a0 + b5528b6 commit ddf4842

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

src/main.rs

+16-17
Original file line numberDiff line numberDiff line change
@@ -1,24 +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-
let result = match std::env::var("OPEN_WITH").ok() {
13-
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()),
1418
None => open::that(&path_or_url),
15-
};
19+
}?;
1620

17-
match result {
18-
Ok(()) => println!("Opened '{}' successfully.", path_or_url),
19-
Err(err) => {
20-
eprintln!("An error occurred when opening '{}': {}", path_or_url, err);
21-
process::exit(3);
22-
}
23-
}
21+
println!("Opened '{}' successfully.", path_or_url);
22+
Ok(())
2423
}

src/windows.rs

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ pub fn commands<T: AsRef<OsStr>>(path: T) -> Vec<Command> {
2020
pub fn with_command<T: AsRef<OsStr>>(path: T, app: impl Into<String>) -> Command {
2121
let mut cmd = Command::new("cmd");
2222
cmd.arg("/c")
23+
.arg("start")
24+
.raw_arg("\"\"")
2325
.raw_arg(app.into())
2426
.raw_arg(wrap_in_quotes(path))
2527
.creation_flags(CREATE_NO_WINDOW);

0 commit comments

Comments
 (0)