Building a CLI in Rust
Today I'd like to go over a simple Rust CLI application I made. It is set up with just one simple command, however it is organized to scale to many different commands. Please feel free to use it in your own work. If you have any suggestions, please send me an email - mitchgollub@gmail.com.
The technologies I'm using today are below
Rust CLI Example App Overview
The project is organized into a few sections
.
├── Cargo.lock
├── Cargo.toml
└── src
├── cli.yaml
├── commands
│ ├── js_to_ts.rs
│ └── mod.rs
└── main.rs
Under src
we have the cli.yaml
file. This holds the definition of our application and it's commands. At link below, you'll see a command to change Javascript files to a Typescript file simply by changing the extension. There is an option to specify a directory to search recursively.
main.rs
holds the application orchestration code. You'll see some code that translates string input to a Command. The program first runs some setup code to configure logging and error reporting. After that, it parses the CLI input and runs the appropriate command.
The final file of note is the js_to_ts.rs
file under the commands
folder. This is all the code that runs when the command is selected. The file here shows a simple command that searches a folder recursively and changes any files with a .js
extension to a .ts
extension. It uses tokio
tasks to leverage some system concurrency.
Checkout the full source and give it a ⭐ on Github if you've found it useful. Thanks for reading! 📖