|
1 |
| -use std::{env, process}; |
| 1 | +use std::env; |
2 | 2 |
|
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) { |
5 | 6 | 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()), |
10 | 8 | };
|
11 | 9 |
|
12 |
| - #[cfg(not(windows))] |
13 |
| - let result = match std::env::var("OPEN_WITH").ok() { |
14 |
| - 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()), |
15 | 18 | None => open::that(&path_or_url),
|
16 |
| - }; |
| 19 | + }?; |
17 | 20 |
|
18 |
| - #[cfg(windows)] |
19 |
| - let result = match env::args().nth(2) { |
20 |
| - Some(arg) if arg == "--with" || arg == "-w" => match env::args().nth(3) { |
21 |
| - Some(program) => open::with(&path_or_url, program), |
22 |
| - None => open::that(&path_or_url), |
23 |
| - }, |
24 |
| - _ => open::that(&path_or_url), |
25 |
| - }; |
26 |
| - |
27 |
| - match result { |
28 |
| - Ok(()) => println!("Opened '{}' successfully.", path_or_url), |
29 |
| - Err(err) => { |
30 |
| - eprintln!("An error occurred when opening '{}': {}", path_or_url, err); |
31 |
| - process::exit(3); |
32 |
| - } |
33 |
| - } |
| 21 | + println!("Opened '{}' successfully.", path_or_url); |
| 22 | + Ok(()) |
34 | 23 | }
|
0 commit comments