| Crates.io | setenv |
| lib.rs | setenv |
| version | 0.1.2 |
| created_at | 2016-02-24 03:49:17.885611+00 |
| updated_at | 2018-05-19 19:46:28.012962+00 |
| description | Helpers for writing tools that will be eval'd by shells |
| homepage | https://github.com/eminence/setenv |
| repository | https://github.com/eminence/setenv |
| max_upload_size | |
| id | 4262 |
| size | 13,397 |
It's well known that a program cannot modify the environment of its parent shell. But this is useful to do, and we can use some tricks to do this. Almost all shells support some way to evaluate the output of programs (even Windows), so by returning the right commands to be eval'd by the parent shell, we can apply these changes.
Add the following to your Cargo.toml file:
[dependencies]
setenv = "0.1"
This library provides two things:
At the moment, the only two commands supported are cd for changing directories, and
setenv for setting environment variables.
Two other functions are also provided as a convienence: split_env which is just a wrapper
around std::env::split_paths, and set_env_list which is a wrapper around
std::env::join_paths.
To make use of all this, each executable using setenv should be wrapped in an
alias/function/bat file. Here are some examples:
for /f "tokens=*" %%I in ('d:\target\debug\myapp.exe %*') do (
%%I
)
function dothing() {
eval `/target/debug/myapp "$@"`
}
dothing() {
eval `/target/debug/myapp "$@"`
}
function dothing() {
eval `/target/debug/myapp $*`
}
alias dothing 'eval `/target/debug/myapp \!*`'
Since all text send to stdout is eval'd by the shell, great care must be taken to control what is printed to stdout. All user-facing messages should go to stderr instead.

To the extent possible under law, Andrew Chin has waived all copyright and related or neighboring rights to setenv. This work is published from: United States.