The world of the terminal is beautiful and I always love dwelling in it. Away from the small old experience of the web which I am finding is quite “routine” now a days, you can build a little corner where you are in total control. So, here is a small tutorial on building your own small CLI in rust. I’ll be going through how to prettify the output, connections and more. So, let’s get started.
This tutorial is by no means to a complete guide. This is just to show you the basic way you can approach making a CLI and how to sort of go about making it. This article also presumes that you have a good enough knowledge of the rust language. If you don’t, I recommend you check out the official rust book. It is a very good resource for learning rust. You can find it here: https://doc.rust-lang.org/book/
What are we building?
So, what we are going to build is actually quite simple and you might actually find it useful. We are going to build a “Key-Value” Store. So, the concept is quite simple. We have 5 commands
- Add a Key-Value [C]
- Read a Value from Key [R]
- Update a Value from Key [U]
- Delete a Value from key [D]
- Search a Key / Value [S]
The classic CRUDS App. So, this is how we are going to do this.
We will be using rust. Rust is a very simple to use memory and type safe language that is excellent for building cool and reliable CLI’s. In fact it has quickly become the number one tool for building CLI’s. I’ll dive into more on why rust CLI’s are good in a future blog post, so stay tuned for that.
So, with that, let’s get our project set up.
Install rust on your machine if you have not already. You can do so by visiting
Rust Programming Language
and just downloading rust for your platform.
Once that is done, just head over to your terminal. The terminal is an excellent way to interact with your computer. If you want to know the basics, there are plenty of resources on YouTube, below is one I recommend from freeCodeCamp.
cd into a new directory and run
We are using
--bin to signify to cargo that we will be using this as a binary rather than a crate, just to make it clear.
Now, open this up in your favourite text editor. I personally use VSCode (I know shocking), but rust has a very good ecosystem for Neovim and Emacs as well.
Now, create all the files shown below
Don’t worry, you don’t need to have SQLITE3 installed to have this work. We will be directly using rust a crate. Let’s just quickly go over the file structure:
We are going to have four files in the
src, including the
- db.rs: Handles database connections and the CRUD operation’s brain
- handler.rs: Essentially acts as a middle ware
- utils.rs: For some cool utility functions like copying, hash generation etc.
- .env file to store our Database address
- main.db, which is our dev database.
Now that you have created these files, it is time to install some crates. Crates are independent rust libraries that provide additional features on top. They are built and maintained by the community and are very useful. It is very easy to install them in your rust project. Just head over to
cargo.toml and under
[dependencies], just paste the dependency name. I’ll be listing what we are using below, however, if you want to find more rust crates, I recommend you check out the GitHub List linked below.
This is going to to our
We will be using quite a bit of dependencies to make a pretty CLI. The
clap dependency is a very simple to use CLI argument parser that uses a predefined struct to manage.
sqlx is a simple SQL binder, not ORM for rust that makes it very easy to execute SQL queries on our database.
serde is a popular JSON library, we use
chrono for managing time related stuff.
clipboard is an intuitive Clipboard abstraction for rust.
rand is random generation,
dotenv for reading from the .env file,
tabled to style a struct into a table and
tokio to make our function asynchronous, so execution will be blocked till our database is queried.
inquire are CLI formatting and prompting libraries that are similar to the
rich library in python
Woah. So we are done with libraries. Be sure to also include the same features I did since we will be using all of them.
Now, we can finally start the actual coding. Open
[main.rs](http://main.rs) file and configure the
clap::Parser to a nice struct.
cmd is a Optional argument and we will prompt the user if it is not entered. We want this CLI for personal use, so no need to worry too much.
We also add all the files to the main file as mods.
Now, in the main function, all the predefined
These will take care of the environment and errors in production. Now let’s parse the arguments and check if the user enter the command. If he has not, let’s ask him
utils::suggester is a simple filter and map function that inquire takes as input, this is the function. You can open up utils and paste this into it
We basically just add a simple rust filter map function.
Let’s configure the database connection now. First, open up db.rs and import the necessary modules and define a struct of how our Database query would look like.
We use serde to Serialize and DeSerialize data, that is derived from sqlx.
Our entry will have a simple id, key, value, hash and timestamp. Quite a simple and easy to work structure.
Now, let’s get the Database connection using
sqlx and return a connection to the table I am calling
As you can see, in the connect function, we just use a SQL query. No ORM needed.
Now, we can use the
connect() function to just return a connection from the db, without having the need to reconnect everytime.
We now define an
add_to_db function, the create part of CRUD
As you can see, we just used simple SQL.
sqlx allows us to bind the
? with a value, which is exactly what we did. We also used the
chrono::Local time in a the ISO format as the timestamp.
We similarly write rest of the RUD:
We now define a middleware like file to handle all the operations and display appropriately to the user. For example, we can ask the user to add like this
Now that we have defined the
add, we can just use a match statement in the main file to call the handler
Like I defined the functions, similarly, you can define your own as well.
Now, this is where I leave you.
The whole point of this article was to help you take your first steps in writing your own CLI using rust and the whole point would be lost if I impose my own ideas on to you. You are now free to create your own CLI.
Format it as you like. Maybe using a little bit of rust magic, you can make a very pretty CLI.
You are free to use
NoobKey for reference, or even build on top of it. It is quite simple and easy to use codebase. This is it’s GitHub repo:
With that being said, thank you for reading. The real reason I wrote this article was to test how code blocks look on my site 😅. You can check that out here: https://noobscience.rocks/blog/tutorials/rust-cli. Thanks for reading, hope you have a great day.