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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80

/// What the user asked at the cli
#[derive(Debug, Clone)]
pub struct Command {
    pub benches: Vec<String>,
    pub graph: Option<String>,
    pub history: Option<String>,
    pub tag: Option<String>,
    pub no_save: bool,
    pub verbose: bool,
}

impl Command {

    /// read std::env::args
    pub fn read() -> Self {
        let mut args = std::env::args()
            .skip(1); // it's the path to the compiled bench in target
        let mut benches = Vec::new();
        let mut graph = None;
        let mut history = None;
        let mut tag = None;
        let mut before_sep = true;
        let mut no_save = false;
        let mut verbose = false;
        while let Some(arg) = args.next() {
            if arg == "--" {
                before_sep = false;
            } else if before_sep {
                if !arg.starts_with("--") {
                    benches.push(arg);
                }
            } else {
                match arg.as_str() {
                    "--no-save" => {
                        no_save = true;
                    }
                    "--verbose" => {
                        verbose = true;
                    }
                    "--graph" => {
                        if let Some(val) = args.next() {
                            graph = Some(val);
                        }
                    }
                    "--history" => {
                        if let Some(val) = args.next() {
                            history = Some(val);
                        }
                    }
                    "--tag" => {
                        if let Some(val) = args.next() {
                            tag = Some(val);
                        }
                    }
                    "--bench" => {
                        // that's how the command given by cargo bench always ends
                    }
                    _ => {
                        println!("ignored bench argument: {:?}", arg);
                    }
                }
            }
        }
        Self {
            benches,
            graph,
            history,
            tag,
            no_save,
            verbose,
        }
    }

    /// tell whether this specific bench should be included
    pub fn include_bench(&self, name: &str) -> bool {
        self.benches.is_empty() || self.benches.iter().any(|g| g==name)
    }

}