0PricingLogin
Learn Rust Coding · Lesson

Subcommands

Structured CLIs.

What are subcommands?

Subcommands let one binary expose several actions, like git commit and git push.

  • Each subcommand has its own arguments
  • The structure scales as your CLI grows

clap models these with nested Command values.

Adding a subcommand

Attach a child command with .subcommand(). The parent becomes a dispatcher.

use clap::Command;

fn main() {
    Command::new("todo")
        .subcommand(Command::new("add"))
        .subcommand(Command::new("list"))
        .get_matches();
}

All lessons in this course

  1. clap Basics
  2. Subcommands
  3. Derive API
  4. Validation and Help
← Back to Learn Rust Coding