xorg/
xorg.rs

1/// A way to represent xorg like flags, not a typical usage
2use bpaf::*;
3#[derive(Debug, Clone)]
4#[allow(dead_code)]
5pub struct Options {
6    turbo: bool,
7    backing: bool,
8    xinerama: bool,
9    extensions: Vec<(String, bool)>,
10}
11
12// matches literal name prefixed with - or +
13fn toggle_options(meta: &'static str, name: &'static str, help: &'static str) -> impl Parser<bool> {
14    any(meta, move |s: String| {
15        if let Some(suf) = s.strip_prefix('+') {
16            (suf == name).then_some(true)
17        } else if let Some(suf) = s.strip_prefix('-') {
18            (suf == name).then_some(false)
19        } else {
20            None
21        }
22    })
23    .help(help)
24    .anywhere()
25}
26
27// matches literal +ext and -ext followed by extension name
28fn extension() -> impl Parser<(String, bool)> {
29    let state = any("(+|-)ext", |s: String| match s.as_str() {
30        "-ext" => Some(false),
31        "+ext" => Some(true),
32        _ => None,
33    })
34    .anywhere();
35
36    let name = positional::<String>("EXT")
37        .help("Extension to enable or disable, see documentation for the full list");
38    construct!(state, name).adjacent().map(|(a, b)| (b, a))
39}
40
41pub fn options() -> OptionParser<Options> {
42    let backing = toggle_options("(+|-)backing", "backing", "Set backing status").fallback(false);
43    let xinerama =
44        toggle_options("(+|-)xinerama", "xinerama", "Set Xinerama status").fallback(true);
45    let turbo = short('t')
46        .long("turbo")
47        .help("Engage the turbo mode")
48        .switch();
49    let extensions = extension().many();
50    construct!(Options {
51        turbo,
52        backing,
53        xinerama,
54        extensions,
55    })
56    .to_options()
57}
58
59fn main() {
60    println!("{:#?}", options().run());
61}