|
| 1 | +--- |
| 2 | +# bx-3tpt |
| 3 | +title: Migrate argument parsing to clap |
| 4 | +status: todo |
| 5 | +type: task |
| 6 | +priority: high |
| 7 | +created_at: 2026-01-22T09:25:56Z |
| 8 | +updated_at: 2026-01-22T09:25:56Z |
| 9 | +--- |
| 10 | + |
| 11 | +Replace manual `env::args()` parsing with `clap` derive macros for type-safe argument handling. |
| 12 | + |
| 13 | +## Why |
| 14 | +- Current manual parsing is error-prone and hard to maintain |
| 15 | +- No automatic help generation |
| 16 | +- No argument validation |
| 17 | +- 50% less code with clap |
| 18 | +- Industry standard (95%+ of Rust CLI tools use clap) |
| 19 | + |
| 20 | +## Checklist |
| 21 | +- [ ] Add `clap = { version = "4", features = ["derive"] }` to Cargo.toml |
| 22 | +- [ ] Create src/cli/mod.rs module |
| 23 | +- [ ] Create src/cli/args.rs with Cli struct using `#[derive(Parser)]` |
| 24 | +- [ ] Define Commands enum with subcommands (Init, Cache, Help, Run) |
| 25 | +- [ ] Update src/bin/main.rs to use `Cli::parse()` |
| 26 | +- [ ] Remove manual argument parsing code from main.rs |
| 27 | +- [ ] Update src/lib.rs to export cli module |
| 28 | +- [ ] Ensure backward compatibility with existing flags (-v, -i, -c, -d, -h) |
| 29 | +- [ ] Test all commands work as before |
| 30 | +- [ ] Verify auto-generated --help output is correct |
| 31 | + |
| 32 | +## Cli Structure |
| 33 | +```rust |
| 34 | +use clap::{Parser, Subcommand}; |
| 35 | + |
| 36 | +#[derive(Parser)] |
| 37 | +#[command(name = "bx", version, about = "Simple, cross-platform command aliases")] |
| 38 | +pub struct Cli { |
| 39 | + #[arg(short = 'd', long, global = true)] |
| 40 | + pub debug: bool, |
| 41 | + |
| 42 | + #[command(subcommand)] |
| 43 | + pub command: Option<Commands>, |
| 44 | +} |
| 45 | + |
| 46 | +#[derive(Subcommand)] |
| 47 | +pub enum Commands { |
| 48 | + #[command(name = "-v", alias = "--version")] |
| 49 | + Version, |
| 50 | + #[command(name = "-i", alias = "--init")] |
| 51 | + Init { |
| 52 | + #[arg(short, long)] |
| 53 | + template: Option<String>, |
| 54 | + }, |
| 55 | + #[command(name = "-c", alias = "--cache")] |
| 56 | + Cache, |
| 57 | + #[command(name = "-h", alias = "--help")] |
| 58 | + Help, |
| 59 | + #[command(external_subcommand)] |
| 60 | + Run(Vec<String>), |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +## Notes |
| 65 | +- Keep backward compatibility with current flag-style arguments (-v, -i, etc.) |
| 66 | +- Consider if we want to also support subcommand-style (bx init, bx cache) in addition |
0 commit comments