Skip to content

Commit

Permalink
cleanup main program to support --with on all platforms.
Browse files Browse the repository at this point in the history
Technically this is a breaking change as the way it communicates now looks different,
and error handling is different as well.
  • Loading branch information
Byron committed Jun 25, 2023
1 parent 5607cd6 commit b5528b6
Showing 1 changed file with 16 additions and 27 deletions.
43 changes: 16 additions & 27 deletions src/main.rs
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(())
}

0 comments on commit b5528b6

Please sign in to comment.