CLI Frameworks for Rust: Overview of the Best Tools for Creating Command-Line Utilities

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

CLI frameworks for Rust: powerful tools for creating console applications



Rust is a systems programming language that is gaining popularity due to its performance, safety, and modern capabilities. One of the most common uses of Rust is creating command-line utilities (CLI). From ripgrep to bat and fd — many well-known tools are written in Rust. In this article, we will look at the main CLI frameworks for Rust that will help you quickly and efficiently develop your own console applications.



Why choose Rust for CLI?

Rust offers several key advantages for CLI development:

  • Performance — compiled code runs with minimal overhead.
  • Memory safety — the ownership system prevents leaks and data races.
  • Cross-platform — a single codebase works on Windows, macOS, and Linux.
  • Ecosystem — a rich set of libraries for argument parsing, colored output, working with configs.


Main CLI frameworks for Rust



1. Clap (Command Line Argument Parser)

Clap is the most popular and feature-rich framework for parsing command-line arguments in Rust. It supports automatic help generation, hints, validation, subcommands, and much more.

use clap::{Parser, Subcommand};

#[derive(Parser)]#[command(name = "git")]#[command(about = "A fictional version of git", long_about = None)]struct Cli { #[command(subcommand)] command: Commands,}

#[derive(Subcommand)]enum Commands { /// Clone a repository Clone { /// URL of the repository url: String, }, /// Push changes Push { /// Remote name remote: String, /// Branch name branch: String, },}

fn main() { let cli = Cli::parse(); match &cli.command { Commands::Clone { url } => println!("Cloning from {}", url), Commands::Push { remote, branch } => println!("Pushing to {} branch {}", remote, branch), }}

Clap provides two approaches: declarative (via derive macros) and functional (via Builder API). For most projects, it is recommended to use the derive approach — it is shorter and more readable.



2. StructOpt

StructOpt is an add-on over Clap that allows you to define arguments through structures. Starting from version 3.0, Clap has built StructOpt functionality directly, so StructOpt is now considered deprecated. However, many older projects still use it.

use structopt::StructOpt;

#[derive(StructOpt)]struct Cli { #[structopt(short = 'n', long = "name")] name: String, #[structopt(short = 'c', long = "count", default_value = "1")] count: u32,}

fn main() { let args = Cli::from_args(); for _ in 0..args.count { println!("Hello, {}!", args.name); }}

Important: for new projects, use Clap with derive macros — this is the modern and supported alternative.



3. Argh

Argh is a lightweight and fast framework that focuses on minimalism. It does not require additional dependencies (other than itself) and compiles very quickly. It is ideal for small utilities.

use argh::FromArgs;

#[derive(FromArgs)]/// A simple greeting toolstruct Greeting { /// name to greet #[argh(option, short = 'n')] name: String,

/// number of times to greet #[argh(option, short = 'c', default = "1")] count: u32,}

fn main() { let greeting: Greeting = argh::from_env(); for _ in 0..greeting.count { println!("Hello, {}!", greet

Recommendations