-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbg_opener-example.rs
executable file
·42 lines (37 loc) · 1.08 KB
/
dbg_opener-example.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env -S cargo +nightly -Zscript
---
package.edition = "2024"
[dependencies]
clap = { version = "4.5", features = ["derive"] }
opener = "0.7.2"
---
//! # Opener example that takes a custom string.
//! example with `BROWSER` env var
//!
//! ## Run:
//! `./opener-example.rs`
//! or
//! `BROWSER='...' ./opener-example.rs`
//!
//! ## Convenience note:
//! `chmod u+x opener-example.rs`
use clap::Parser;
/// Use `BROWSER` envvar to test `opener` crate function.
#[derive(Parser, Debug)]
#[command(version, about)]
struct Args {
/// Optional path to open. (will be prepended with `https://` if not present.)
opt_path: Option<String>,
}
const REQ_PREFIX: &str = "https://";
const DEFAULT_PATH: &str = "https://www.rust-lang.org";
fn main() {
let args = Args::parse();
let mut path = args.opt_path.unwrap_or(DEFAULT_PATH.to_string());
if !path.starts_with(REQ_PREFIX) {
println!("prepending required prefix: {:?}", REQ_PREFIX);
path = format!("{}{}",REQ_PREFIX, path);
}
println!("path to take: {:?}", path);
opener::open_browser(path).unwrap();
}