clap copies each global = true flag into every subcommand on each run
FactVerified 5 Nov 2026Holds anywhere
Fact. A statement and the evidence for it.
Statement#
Each run, clap clones every global = true flag (such as --json) into every subcommand. A CLI with about 160 to 250 subcommands pays for this on every start, even for rev-parse. Build only the subcommand that the user typed. In clap derive, #[command(defer = true)] does this. See the clap defer pitfall for its traps.
Evidence#
Source: clap_builder command.rs (line 4740 in the version used). In rgit, building only the typed subcommand saved about 2 MiB of peak memory and 6 to 9 M instructions on every command. stripspace went from 55 M instr and 5.5 MiB to 46 M and 3.5 MiB. A guard test fell from 5075 to about 830 allocations.
Also found by searching
- rust CLI with many subcommands is slow to start
- clap global = true flag cost per subcommand
- why does clap use so much memory at startup