clap-v3-to-v4
Automate 80%+ of the Rust clap CLI parser migration from v3 to v4.
What it does
Derive API transforms
#[clap(...)]on structs/enums →#[command(...)]#[clap(...)]on fields →#[arg(...)]arg_enum→value_enumArgEnumderives →ValueEnum- Removes redundant
value_parserandactionattributes (implicit in v4)
Builder API transforms
.takes_value(true)→.num_args(1).multiple_values(true)→.num_args(1..).min_values(N)→.num_args(N..).max_values(N)→.num_args(1..=N).number_of_values(N)→.num_args(N).multiple(true)→.num_args(1..)- Removes
.takes_value(false)(now default) - Removes
.require_value_delimiter(true)(use.value_delimiter(',')) - Removes
.setting(AppSettings::ColoredHelp)(now default) - Removes now-unused
AppSettingsimports whenColoredHelpwas the only use
Error kind renames
ErrorKind::EmptyValue→ErrorKind::InvalidValueErrorKind::UnrecognizedSubcommand→ErrorKind::InvalidSubcommand
Cargo.toml follow-up
- Update
clap = "3.x"→clap = "4" - Update
clap = { version = "3.x", ... }→clap = { version = "4", ... }
Usage
bash
Manual follow-up
After running, you should:
- Review
Cargo.tomlupdates for any uncommon clap dependency formatting not covered by deterministic rewrites - Remove the
deprecatedfeature flag if present - Run
cargo checkto catch remaining compile errors - Verify
num_argsranges match your intended behavior - Check
ArgActionusage — v4 changed default actions for flags - Update custom help templates — format changed in v4
- Remove unused imports (e.g.,
AppSettingsif no longer needed) - Run
cargo testto verify behavior
Development
bash
References
License
MIT