Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons

© 2026 BraveProgrammer. All rights reserved.

  1. Courses
  2. /
  3. Rust Fundamentals

Lesson 31 of 50 · rust

Building a CLI Application

Duration: 25 minutes

Building a CLI Application\n\nUse the clap crate to parse arguments and subcommands.\n\ntoml\n# Cargo.toml\n[dependencies]\nclap = { version = "4.0", features = ["derive"] }\n\n\nrust\nuse clap::{Parser, Subcommand};\n\n#[derive(Parser)]\n#[command(name = \"calc\")]\nstruct Cli {\n #[command(subcommand)]\n command: Commands,\n}\n\n#[derive(Subcommand)]\nenum Commands {\n Add { a: i32, b: i32 },\n Sub { a: i32, b: i32 },\n}\n\nfn main() {\n let cli = Cli::parse();\n match cli.command {\n Commands::Add { a, b } => println!(\"{} + {} = {}\", a, b, a + b),\n Commands::Sub { a, b } => println!(\"{} - {} = {}\", a, b, a - b),\n }\n}\n\n

Previous: Command Line ArgumentsNext: Using External Crates