1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
//! Snippet from cargo-hackerman crate, shows how to use derive to parse commands and
//! "positional_if" pattern.
//!
//! Command explain takes 3 parameters: required crate name and optional feature and crate version,
//! user is allowed to omitt either field. This example uses simplified is_version, in practice youwould
//! would use semver crate.
//!
//! End user would be able to run commands like
//!
//! ```console
//! $ cargo hackerman explain random 314
//! > krate: "random", feature: None, version: Some(314"),
//! $ cargo hackerman explain serde derive
//! > krate: "serde", feature: Some("derive"), version: None
//! ```
use bpaf::*;
#[derive(Debug, Clone, Bpaf)]
#[bpaf(options("hackerman"))]
pub enum Action {
#[bpaf(command("explain"))]
Explain {
#[bpaf(positional("CRATE"))]
krate: String,
#[bpaf(external(feature_if))]
feature: Option<String>,
#[bpaf(external(version_if))]
version: Option<String>,
},
}
fn feature_if() -> impl Parser<Option<String>> {
positional::<String>("FEATURE")
.guard(move |s| !is_version(s), "")
.optional()
}
fn version_if() -> impl Parser<Option<String>> {
positional::<String>("VERSION")
.guard(move |s| is_version(s), "")
.optional()
}
fn is_version(v: &str) -> bool {
v.chars().all(|c| c.is_numeric())
}
fn main() {
println!("{:?}", action().run());
}