that/
lib.rs

1use clap::Parser;
2use nothing::Probably;
3use std::env;
4use std::path::{Path, PathBuf};
5
6/// find binary for command
7///
8/// credits to <https://stackoverflow.com/questions/37498864/finding-executable-in-path-with-rust>
9pub fn locate<P>(exe_name: P) -> Option<PathBuf>
10where
11    P: AsRef<Path>,
12{
13    env::var_os("PATH").and_then(|paths| {
14        env::split_paths(&paths)
15            .filter_map(|dir| {
16                let full_path = dir.join(&exe_name);
17                if full_path.is_file() {
18                    Some(full_path)
19                } else {
20                    None
21                }
22            })
23            .next()
24    })
25}
26
27/// clap::Parser struct
28#[derive(Parser)]
29#[clap(author, version, about, long_about = None)]
30pub struct Args {
31    /// command name
32    pub command: String,
33}
34
35impl Args {
36    /// find command path
37    pub fn locate(self) -> Probably<PathBuf> {
38        locate(self.command).into()
39    }
40}