foo_bar/
foo_bar.rs

1use blarg::{
2    prelude::*, Collection, CommandLineParser, Condition, Nargs, Optional, Parameter, Scalar,
3    Switch,
4};
5use std::collections::HashSet;
6use std::hash::Hash;
7use std::str::FromStr;
8
9#[derive(Debug, PartialEq, Eq, Hash)]
10enum FooBar {
11    Foo,
12    Bar,
13}
14
15impl std::fmt::Display for FooBar {
16    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
17        match self {
18            FooBar::Foo => write!(f, "foo"),
19            FooBar::Bar => write!(f, "bar"),
20        }
21    }
22}
23
24impl FromStr for FooBar {
25    type Err = String;
26
27    fn from_str(value: &str) -> Result<Self, Self::Err> {
28        match value.to_lowercase().as_str() {
29            "foo" => Ok(FooBar::Foo),
30            "bar" => Ok(FooBar::Bar),
31            _ => Err(format!("unknown: {}", value)),
32        }
33    }
34}
35
36#[derive(Debug, PartialEq, Eq, Hash)]
37enum Country {
38    Canada,
39    Pakistan,
40}
41
42impl FromStr for Country {
43    type Err = String;
44
45    fn from_str(value: &str) -> Result<Self, Self::Err> {
46        match value.to_lowercase().as_str() {
47            "canada" => Ok(Country::Canada),
48            "pakistan" => Ok(Country::Pakistan),
49            _ => Err(format!("unknown: {}", value)),
50        }
51    }
52}
53
54fn main() {
55    let mut verbose: bool = false;
56    let mut foo_bar = FooBar::Foo;
57    let mut initial: Option<u32> = None;
58    let mut countries: HashSet<Country> = HashSet::default();
59    let mut items: Vec<u32> = Vec::default();
60
61    let ap = CommandLineParser::new("foo_bar");
62    let parser = ap
63        .add(
64            Parameter::option(Switch::new(&mut verbose, true), "verbose", Some('v'))
65                .help("Do dee doo."),
66        )
67        .branch(
68            Condition::new(Scalar::new(&mut foo_bar), "foo_bar")
69                .choice(FooBar::Foo, "123 abc let's make this one medium long.")
70                .choice(FooBar::Bar, "456 def let's make this one multiple sentences.  We're really stretching here HAAAAAAAA HAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA!")
71                .help("foo'y bar'y stuff")
72                .meta(vec!["a", "b", "c"]),
73        )
74        .command(FooBar::Foo, |sub| {
75            sub.add(Parameter::option(
76                Optional::new(&mut initial),
77                "initial",
78                None,
79            ))
80            .add(
81                Parameter::argument(Collection::new(&mut items, Nargs::Any), "item")
82                    .help("The items."),
83            )
84        })
85        .command(FooBar::Bar, |sub| {
86            sub.add(Parameter::option(
87                Collection::new(&mut countries, Nargs::AtLeastOne),
88                "country",
89                None,
90            ))
91        })
92        .build();
93    parser.parse();
94    println!("Items: {items:?}");
95    execute(verbose, foo_bar, initial, countries, items);
96}
97
98fn execute(
99    _verbose: bool,
100    foo_bar: FooBar,
101    initial: Option<u32>,
102    countries: HashSet<Country>,
103    items: Vec<u32>,
104) {
105    match foo_bar {
106        FooBar::Foo => {
107            println!("Foo: {initial:?} {items:?}");
108        }
109        FooBar::Bar => {
110            println!("Bar: {countries:?}");
111        }
112    };
113}