I built a simple CLI written in Rust that executes with the command cargo run <ARGUMENTS>. I want to be able to run the CLI from any directory. I used the clap crate and want to be able to call the script with the name passed to clap: brainfast <ARGUMENTS>. I am running on macOS.
Asked
Active
Viewed 1,346 times
0
Shepmaster
- 326,504
- 69
- 892
- 1,159
Nic Bonetto
- 523
- 5
- 20
-
Does this answer your question? [How do I make a Rust program which can be executed without using \`cargo run\`?](https://stackoverflow.com/questions/60944480/how-do-i-make-a-rust-program-which-can-be-executed-without-using-cargo-run) – E_net4 - Krabbe mit Hüten Feb 02 '22 at 13:59
1 Answers
3
This is more like a generic question (and I think a duplicate too, but I can't find any).
You have to copy your executable that is generated by cargo build --release (you can find it in target/release/crate_name) to a folder in your $PATH.
I'm not an expert in macOS, so I can't tell you what is a folder that is included in the $PATH, but you can find that out by yourself by opening a terminal and typing echo $PATH. Use one of the paths and it should be available in your terminal without cargo or using any path.
As an alternative, you can add a folder to your $PATH variable and put it there, e.g.
export PATH /home/foobar/.bin:$PATH
cp target/release/brainfast /home/foobar/.bin
brainfast abc.txt 1 3 99 u
Shepmaster
- 326,504
- 69
- 892
- 1,159
hellow
- 10,920
- 6
- 47
- 69
-
2It's not the best idea to set any random folder to your $PATH (some malicious app could take advantage of it). – Reinstate Monica Aug 10 '18 at 13:36
-
Can you name an example? You can always use a full path as a malicious app, so why rely on $PATH? – hellow Aug 10 '18 at 14:40
-
@hellow Security issues arise if the directory (or any parent) is writable by a different user than the user who adds it to their path. It's easier to keep track of permissions if you limit yourself to a few dedicated directories for this. – CodesInChaos Aug 11 '18 at 07:33