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_enum
- ArgEnum derives → ValueEnum
- Removes redundant value_parser and action attributes (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 AppSettings imports when ColoredHelp was the only use
Error kind renames
- ErrorKind::EmptyValue → ErrorKind::InvalidValue
- ErrorKind::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.toml updates for any uncommon clap dependency formatting not covered by deterministic rewrites
- Remove the deprecated feature flag if present
- Run cargo check to catch remaining compile errors
- Verify num_args ranges match your intended behavior
- Check ArgAction usage — v4 changed default actions for flags
- Update custom help templates — format changed in v4
- Remove unused imports (e.g., AppSettings if no longer needed)
- Run cargo test to verify behavior
Development
bash
References
License
MIT