Design command-line interfaces with consistent flag semantics and leverage framework features for validation. Avoid ambiguous syntax that can be interpreted multiple ways, use explicit conflict declarations instead of manual validation, and ensure flag combinations have clear, predictable behavior.
Design command-line interfaces with consistent flag semantics and leverage framework features for validation. Avoid ambiguous syntax that can be interpreted multiple ways, use explicit conflict declarations instead of manual validation, and ensure flag combinations have clear, predictable behavior.
Key principles:
conflicts_with
instead of manual validation: #[arg(long, conflicts_with = "other_flag")]
command: String, args: Vec<String>
over command: Vec<String>
Example of good flag design:
#[derive(clap::Args)]
struct Args {
#[arg(long, conflicts_with = "push")]
fetch: bool,
#[arg(long, conflicts_with = "fetch")]
push: bool,
// Clear semantics: when neither specified, both are true
// When one specified, only that one is true
}
This ensures users can predict behavior and reduces maintenance burden by leveraging framework validation.
Enter the URL of a public GitHub repository