Rust CLI Dependencies, Features, and Workspaces/Use clap derive for argument parsing

Derive a parser struct for a CLI.

Section: Common CLI crate recipes

Use clap derive for argument parsing

rust
rust
use clap::Parser;

#[derive(Parser, Debug)]
struct Args {
  #[arg(long)]
  verbose: bool,

  #[arg(long, default_value = ".")]
  path: String,
}
Explanation

The `clap` ecosystem is a common choice for Rust CLI argument parsing.

Learn the surrounding workflow

Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.

Related commands

Same sheet · prioritizing Common CLI crate recipes
Return `Result` from main
Use anyhow for ergonomic CLI errors.
OpenIn sheetrustsame section
Set log level through env
Enable application logging with `RUST_LOG`.
OpenIn sheetbashsame section
Add a crates.io dependency
Use a semantic version requirement.
Define package features
Declare named features in the manifest.
Minimal workspace root
Define workspace members from a root manifest.
Use a git dependency
Pin a crate directly from a Git repository.