derive_commands/derive_commands.rs
1//! Snippet from cargo-hackerman crate, shows how to use derive to parse commands and
2//! conditional skip for options
3//!
4//! Command explain takes 3 parameters: required crate name and optional feature and crate version,
5//! user is allowed to omit either field. This example uses simplified is_version, in practice you would
6//! would use semver crate.
7//!
8//! End user would be able to run commands like
9//!
10//! ```console
11//! $ cargo hackerman explain random 314
12//! > krate: "random", feature: None, version: Some(314"),
13//! $ cargo hackerman explain serde derive
14//! > krate: "serde", feature: Some("derive"), version: None
15//! ```
16
17use bpaf::*;
18
19#[derive(Debug, Clone, Bpaf)]
20#[bpaf(options("hackerman"))]
21pub enum Action {
22 #[bpaf(command("explain"))]
23 Explain {
24 #[bpaf(positional("CRATE"))]
25 krate: String,
26 #[bpaf(external(feature_if))]
27 feature: Option<String>,
28 #[bpaf(external(version_if))]
29 version: Option<String>,
30 },
31}
32
33fn feature_if() -> impl Parser<Option<String>> {
34 // here feature starts as any string on a command line that does not start with a dash
35 positional::<String>("FEATURE")
36 // guard restricts it such that it can't be a valid version
37 .guard(move |s| !is_version(s), "")
38 // last two steps describe what to do with strings in this position but are actually
39 // versions.
40 // optional allows parser to represent an ignored value with None
41 .optional()
42 // and catch lets optional to handle parse failures coming from guard
43 .catch()
44}
45
46fn version_if() -> impl Parser<Option<String>> {
47 positional::<String>("VERSION")
48 .guard(move |s| is_version(s), "")
49 .optional()
50 .catch()
51}
52
53fn is_version(v: &str) -> bool {
54 v.chars().all(|c| c.is_numeric())
55}
56
57fn main() {
58 println!("{:?}", action().fallback_to_usage().run());
59}