1

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.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Nic Bonetto
  • 543
  • 1
  • 8
  • 20

1 Answers1

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
  • 388,571
  • 95
  • 1,107
  • 1,366
hellow
  • 12,430
  • 7
  • 56
  • 79
  • 2
    It's not the best idea to set any random folder to your $PATH (some malicious app could take advantage of it). – RaidenF 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