Skip to content

spenserblack/get-executable.rs

get-executable

CI Crates.io Version

Examples

Basic usage

let mut editor = get_executable::editor()
    .expect("Should be able to find an editor")
    .command();
editor.arg("filename.ext");
editor.spawn().expect("Should be able to open the editor");

Advanced usage

use get_executable::{EnvFinder, NameFinder};

let mut editor = EnvFinder::new(["MY_APP_EDITOR", "GIT_EDITOR", "EDITOR"])
    .find()
    .ok()
    .or_else(|| {
        NameFinder::new()
            .split()
            .find_first(["code --wait", "nvim", "nano", "vi"])
        })
    .expect("Should be able to find an editor")
    .command();
editor.arg("filename.ext");
editor.spawn().expect("Should be able to open the editor");

What it does

Kind of like which, this will try to find an appropriate executable. It can take the executable's name or a path to the executable. It can either take this string directly or try to resolve it from environment variables.

Sometimes, executable names include arguments. For example, a common core.editor configuration value for Git is code --wait, which tells VS Code to wait until the user closes the editor to return an exit code. Because of this, this tool will split the executable into its resolved path and any arguments that should be passed to it.

Paths

Examples of absolute paths include /bin/sh and C:\Windows\System32\cmd.exe.

Examples of relative paths include ./scripts/sh-wrapper and .\.venv\Scripts\python.exe.

When a path is used, this tool will simply assert that the path points to a file. On Unix, the file must be executable, and on Windows the file must have an extension that is defined in %PATHEXT%.

Bare filenames

Examples of bare filenames include nano and notepad.exe.

When a bare filename is used, this tool will search through folders defined in the PATH environment variable. It will pick the first executable file with a matching name in one of those folders. Additionally, on Windows, the extension can be ommitted, and the appropriate executable will be guessed based on the extensions in %PATHEXT%.

Resolving to files in the current directory

Some applications, such as cmd.exe on Windows, can resolve a bare filename to a file that exists in the current directory. For example:

@echo off
rem cmd.exe.cmd
echo I am not cmd.exe!
C:> cmd.exe
I am not cmd.exe!

This can introduce security issues if someone drops a malicious file in a project directory. For this reason, this tool will not resolve bare filenames to files in the current directory.

If you need to be able to resolve to files in the current directory, raise an issue so that use cases and potential implementation can be discussed.

Case sensitivity

Executables are case-sensitive on Unix, and case-insensitive on Windows.

About

Find and run a configured executable. Built for running an editor, pager, etc. from your program. Kind of like `which`, but with support for environment variables and `cmd.exe`-style behavior.

Topics

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Contributing

Stars

Watchers

Forks

Contributors

Languages