Introduction
Rust has become the de facto language for building command-line tools. Its performance, safety guarantees, and excellent cross-compilation support make it ideal for everything from simple scripts to complex system utilities. But before you can handle any actual logic, your tool needs to parse its command-line arguments. In the Rust ecosystem, three libraries dominate this space: Clap (the feature-rich veteran), argh (the minimalist derive-based parser from Google), and bpaf (the combinator-based parser with applicative interfaces).
Each library represents a different design philosophy. Clap gives you every feature imaginable with both builder and derive APIs. argh strips away complexity for a dead-simple derive-only experience. bpaf offers a unique combinator-based approach that composes parsers from small building blocks. Let’s compare all three.
Quick Comparison Table
| Feature | Clap (16,620 ⭐) | argh (1,944 ⭐) | bpaf (451 ⭐) |
|---|---|---|---|
| API Styles | Builder + Derive | Derive-only | Combinator + Derive |
| Binary Size Impact | ~400KB (with features) | ~60KB | ~80KB |
| Compile Time | Moderate | Fast | Fast |
| Error Messages | Rich, colored, suggestions | Basic but clear | Customizable |
| Subcommands | First-class support | Supported | Supported |
| Custom Validators | Extensive | Limited | Via combinators |
| Shell Completions | Built-in generation | Not included | Not included |
| Last Updated | August 2026 | May 2026 | July 2026 |
| Async Support | No (parse first, then async) | No | No |
Clap: The Feature-Rich Powerhouse
Clap (Command Line Argument Parser) is the most popular Rust CLI library by a wide margin — 16,620 stars and used by tools like cargo, ripgrep, and fd. It supports two API styles: a builder pattern for dynamic argument configuration, and a derive macro for declarative struct-based parsing.
Derive API (recommended for most use cases):
| |
Clap’s derive macros generate all the parsing logic, help text, and error messages from your struct definition. The #[arg(short, long)] attributes are intuitive and well-documented. Clap also supports subcommands, argument groups, conflicts, and custom validators:
| |
Best for: Any Rust CLI project — Clap is the safe default. Its rich feature set (shell completions, man page generation, colored error messages) makes it the best choice for tools with complex argument structures or public-facing CLIs.
argh: Minimalist Derive Parser from Google
argh (pronounced “arg”) is Google’s minimalist alternative to Clap. It strips out every feature that isn’t essential for basic argument parsing, resulting in a significantly smaller binary footprint and faster compile times. If you’re building a simple internal tool and don’t need shell completions or custom validators, argh keeps things lean:
| |
argh’s API is deliberately simpler than Clap’s: no builder pattern, no argument groups, no custom validators. If your argument parsing needs fit into a flat struct with positional args, options, and switches, argh does exactly what you need with zero ceremony.
Best for: Internal tools, simple utilities, and projects where binary size matters (e.g., embedded devices, container images). If your CLI has exactly one level of commands and doesn’t need rich error messages, argh cuts significant cruft from both your binary and your compilation time.
bpaf: Combinator-Based Parsing
bpaf (Bread’s Parser for Arguments and Flags) takes a fundamentally different approach: instead of deriving from macros, you compose parsers from small combinator functions. This functional style is familiar to developers who have used parser combinator libraries like nom or combine:
| |
bpaf’s combinator-based approach lets you compose parsers like lego blocks. The construct! macro wires individual argument parsers into a struct constructor. This composability makes bpaf ideal for libraries that want to expose argument parsing as part of their public API — consumers can add their own arguments on top of the library’s parser.
bpaf also supports a derive API (added in recent versions), but its combinator API remains the most expressive:
| |
Best for: Library authors who want to expose composable CLI parsing, developers who prefer functional composition over derive macros, and projects that need custom parsing logic that doesn’t fit cleanly into Clap’s validation framework.
Binary Size and Compile Time
Rust CLI tools often ship as static binaries, so the parser library’s contribution to binary size matters:
| |
Clap adds about 370-500KB to a release binary (depending on features enabled), while argh and bpaf add 35-60KB. For most desktop/server tools, this difference is negligible. But for embedded systems, WebAssembly targets, or container images where every kilobyte counts, argh’s minimal footprint is a significant advantage.
Compile times follow the same pattern: Clap’s derive macros generate substantial code and require more type-checking. argh’s simpler macros compile faster. bpaf sits in between.
Error Handling and User Experience
Clap generates the richest error messages:
| |
argh’s error messages are functional but basic:
| |
bpaf lets you customize error formatting through its parser combinators, but requires more effort to match Clap’s built-in quality.
Why Choose the Right CLI Parser
The CLI parser you choose shapes your tool’s user experience from the first --help invocation. A well-crafted CLI with clear argument names, helpful error messages, and intelligent suggestions makes your tool feel professional. Conversely, cryptic errors and missing --help output frustrate users and generate unnecessary support requests.
For the broader Rust ecosystem, see our Rust configuration libraries guide — CLI arguments often pair with config files for a complete configuration story. If you’re building data processing tools that need both CLI parsing and high performance, our Rust benchmarking comparison covers performance measurement patterns. For web-focused Rust projects, see our Rust web frameworks article.
FAQ
Should I always use Clap for new projects?
For most projects, yes. Clap is the safe default — it’s well-maintained, battle-tested, and has the richest feature set. The only reasons to choose argh or bpaf are (1) you need minimal binary size, (2) you want faster compile times for rapid iteration, or (3) you prefer a combinator-based API over derive macros.
How do I add shell completions?
Clap has built-in support for generating shell completion scripts for bash, zsh, fish, PowerShell, and elvish. Add #[command(disable_help_subcommand = true)] and use the clap_complete crate to generate completions at build time. argh and bpaf don’t include completion generation — you’d need to implement it manually or use a separate tool.
Can I mix derive with builder patterns in Clap?
Yes. Clap supports hybrid usage where you define the type structure with derive macros and then call builder methods on the Command to add runtime configuration like version numbers, author info, or custom help templates.
Is argh actively maintained?
argh received updates as recently as May 2026 and continues to be maintained by Google engineers. While its feature set is intentionally limited compared to Clap, the features it provides are stable and well-tested in Google’s internal infrastructure.
When would I use bpaf over Clap or argh?
Choose bpaf when you need composable argument parsing — for example, if you’re building a library that provides a base CLI parser and allows downstream consumers to extend it with additional arguments. bpaf’s combinator-based design makes this natural, whereas Clap and argh require workarounds.
💰 想测试你的市场判断力?我用 Polymarket 做预测市场交易——这是全球最大的预测市场平台,从大选结果到技术监管时间线,什么都可以押注。和赌博不同,这是真正的信息市场:你懂的信息越多,胜率越高。我靠预测技术相关事件的走向已经赚了不少。用我的邀请链接注册:Polymarket.com