使用Rust在PATH中查找可执行文件

问题描述 投票:4回答:2

在Python中我可以:

from distutils import spawn

cmd = spawn.find_executable("commandname")

我试过类似下面代码的东西,但它假设你在使用/usr/bin/which的unix-like系统(也包括执行我想避免的外部命令):

use std::process::Command;

let output = Command::new("which")
                      .arg("commandname")
                      .unwrap_or_else(|e| /* handle error here */)

在Rust中最简单的方法是什么?

rust
2个回答
5
投票

我可能会抓取环境变量并遍历它,返回第一个匹配路径:

use std::env;
use std::path::{Path, PathBuf};

fn find_it<P>(exe_name: P) -> Option<PathBuf>
    where P: AsRef<Path>,
{
    env::var_os("PATH").and_then(|paths| {
        env::split_paths(&paths).filter_map(|dir| {
            let full_path = dir.join(&exe_name);
            if full_path.is_file() {
                Some(full_path)
            } else {
                None
            }
        }).next()
    })
}

fn main() {
    println!("{:?}", find_it("cat"));
    println!("{:?}", find_it("dog"));
}

这在Windows上可能很难看,因为你必须将.exe附加到可执行文件名。它还应该扩展为仅返回可执行的项,这也是特定于平台的代码。

回顾Python implementation,它们似乎也支持通过的绝对路径。如果函数应该支持,那取决于你。

快速搜索crates.io返回了一个可能有用的箱子:quale,尽管它目前说

目前只适用于类Unix操作系统。

找到其他人并不会让我感到惊讶。


这里有一些丑陋的代码,如果缺少.exe就会添加到最后,但仅限于Windows。

#[cfg(not(target_os = "windows"))]
fn enhance_exe_name(exe_name: &Path) -> Cow<Path> {
    exe_name.into()
}

#[cfg(target_os = "windows")]
fn enhance_exe_name(exe_name: &Path) -> Cow<Path> {
    use std::ffi::OsStr;
    use std::os::windows::ffi::OsStrExt;

    let raw_input: Vec<_> = exe_name.as_os_str().encode_wide().collect();
    let raw_extension: Vec<_> = OsStr::new(".exe").encode_wide().collect();

    if raw_input.ends_with(&raw_extension) {
        exe_name.into()
    } else {
        let mut with_exe = exe_name.as_os_str().to_owned();
        with_exe.push(".exe");
        PathBuf::from(with_exe).into()
    }
}

// At the top of the `find_it` function:
// let exe_name = enhance_exe_name(exe_name.as_ref());

2
投票

我发现了一个解决问题的箱子:which。它包括Windows支持,甚至包括PATHEXT

© www.soinside.com 2019 - 2024. All rights reserved.