How to Build a Basic CLI App in Rust
CLI (Command Line Interfaces) play a crucial role in software development and system administration. They provide a streamlined way to interact with computers, making them favorable for developers and power users for automation, scripting, and remote administration. Rust, a programming language gaining popularity, offers powerful features for building CLI applications.
To get started building CLI apps with Rust, there are several third-party packages available, such as Clap, StructOpt, and Termion crates, that provide the necessary features. Additionally, Rust’s standard library provides the std::env crate for working with environment variables and command-line arguments in a platform-independent manner.
To create a new Rust project for your CLI app using Cargo (Rust’s package management tool), run the command cargo new crypto_cli. This will set up the basic structure for your project.
One common use case for CLI apps is making API calls. In this example, we’ll use the Reqwest crate to make HTTP requests to Coinmarketcap’s API, which provides cryptocurrency data. To use Reqwest, add the reqwest and tokio crates to your project’s dependencies in the Cargo.toml file.
Next, create two files in the src folder: api.rs and cli.rs. These files will contain the implementation of the API call and CLI functionalities, respectively.
In the api.rs file, you can define the structs that represent the data you want to extract from the API response. Use the serde crate to specify the attributes and deserialize the JSON data into the structs for easier manipulation. The structs should include the necessary fields for the data you need.
To make the API request, create a new client using the Client::new() method from Reqwest. Specify the API endpoint URL and any required parameters. Then, send the request using the client’s get() method and provide the necessary headers and query parameters.
Deserialize the JSON response using the serde_json crate and print the result to the console. This will give you access to the desired data retrieved from the API.
In the cli.rs file, import the crypto function from the api.rs file. Then, retrieve the command-line arguments using the std::env crate’s args() method. If the user specifies crypto as an argument, call the crypto function. Otherwise, print an error message.
Finally, in the main.rs file, call the cli function within the async main function. Add the #[tokio::main] attribute to enable asynchronous execution.
When you run the project using the cargo run command and specify crypto as an argument, the CLI app will make the API request and display the retrieved data on the console.
Rust’s powerful features and the availability of third-party packages make it an excellent choice for building CLI apps. With Rust’s fine-grained control over memory allocation, thread safety, and concurrency, you can create robust and efficient CLI tools. Additionally, Rust’s thriving ecosystem of libraries and frameworks tailored for CLI applications makes development easier and more efficient.
By following the steps outlined above, you can quickly build a basic CLI app in Rust and start leveraging its benefits. Whether you’re automating tasks, scripting, or performing remote administration, Rust has the tools and capabilities to meet your needs.