Clap command structure
Quick answer
Clap is an external crate for describing and parsing command-line interfaces. Its derive API maps command syntax to Rust types.
Use typed subcommands
In a disposable Cargo project enable the derive feature of Clap 4 (candidate pin 4.5.37). A Parser struct holds top-level options and a Subcommand enum represents exclusive actions. try_parse_from is useful for tests because it returns an error instead of exiting. Keep the application logic outside the parser. Built-in help and errors are useful but still test your own option meanings and defaults.
Manual terminal step; not executed. Expected behavior (environment dependent):
Cargo resolves Clap and updates the disposable project's manifest if compatible.Dependency resolution is not verified here.
use clap::{Parser, Subcommand};#[derive(Parser)]struct Cli { #[command(subcommand)] action: Action }#[derive(Subcommand)]enum Action { Count { #[arg(long)] limit: u32 }, List }fn main() { let c = Cli::try_parse_from(["demo", "count", "--limit", "3"]).unwrap(); match c.action { Action::Count { limit } => println!("{limit}"), Action::List => println!("list") } println!("{}", Cli::try_parse_from(["demo", "count"]).is_err());}Manual local Rust/Cargo candidate; not executed. Expected behavior:
3trueThe enum excludes incompatible simultaneous actions.
Common mistake
Do not place file writes in the argument parser.
Try it yourself
Add a verbose flag and test both the default and explicit cases.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Clap command structure — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
You need separate `add` and `list` CLI actions with different fields. Which Clap model suits this?
Clap command structure — example 1. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```bash cargo add clap@=4.5.37 --features derive ``` Which outcome or interpretation best matches this example?
Clap command structure — example 2. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust use clap::{Parser, Subcommand}; #[derive(Parser)] struct Cli { #[command(subcommand)] action: Action } #[derive(Subcommand)] enum Action { Count { #[arg(long)] limit: u32 }, List } fn main() { let c = Cli::try_parse_from(["demo", "count", "--limit", "3"]).unwrap(); match c.action { Action::Count { limit } => println!("{limit}"), Action::List => println!("list") } println!("{}", Cli::try_parse_from(["demo", "count"]).is_err()); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.