How to add a cli to a rust crate?

I've built a web server with axum and sqlx. For managing the database, creating or removing rows, exporting a json backup and such, i created a small cli using clap. At the moment the binary crate for the cli lives in the bin folder of my server and consumes functions and structs from the crate's lib. I would like to use the cli by just typing cli and not cargo run --bin cli -- , like the sqlx cli, wich can be used just by typing sqlx. What should I do with my bin crate? Is there an accepted/"standard" way for adding a cli to a crate? Thanks

Use cargo install --path

1 Like

How could I install the cli, as it has not a cargo.toml? Should I create a cargo workspace with the cli and the server, add the server library ad a dependecy of the cli and then install the cli in the server crate?

You can install a local binary crate into cargo bins by running

cargo install --path /path-to-crate --bin bin_name

But I don't recommend installing a project-specific binary into cargo, as it may not get updated.

If this is just for convenience, creating a bash script is preferable. So you can run


Do you mean a command line for your crate as is done with


I un - deleted because I am just confused?
There is an sqlx crate and sqlx-cli crate and I think that might follow the same concept. Is this what you mean?

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.