libcaliph/args/
args_conph.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2License, v. 2.0. If a copy of the MPL was not distributed with this
3file, You can obtain one at https://mozilla.org/MPL/2.0/.
4Copyright 2021 Peter Dunne */
5
6///! Read in command line arguments for `conph` using clap
7use clap::{App, Arg};
8
9/// Command line arguments struct, infile, outfile, and silent (i.e. emit to stdout)
10pub struct ConvArgs {
11    /// pH measured
12    pub ph: f64,
13    /// Give custom calibration values insted of reading calibration.ph
14    pub custom: bool,
15
16    pub calibration: Option<[f64; 2]>,
17}
18
19impl ConvArgs {
20    /// Parse command line arguments
21    pub fn parse() -> Self {
22        let matches = App::new("conph")
23            .author("Peter Dunne")
24            .version("0.1.4")
25            .about("Corrects pH measurement with calibration")
26            .arg(
27                Arg::with_name("ph")
28                    .help("pH measured")
29                    .index(1)
30                    .required(true)
31                    .takes_value(true),
32            )
33            .arg(
34                Arg::with_name("temperature")
35                    .help("Temperature of measurement")
36                    .short("t")
37                    .long("temperature")
38                    .takes_value(true),
39            )
40            .arg(
41                Arg::with_name("custom")
42                    .short("c")
43                    .long("custom")
44                    .help("Custom Input")
45                    .requires_all(&["slope", "offset"]),
46            )
47            .arg(
48                Arg::with_name("slope")
49                    .short("s")
50                    .long("slope")
51                    .help("Slope")
52                    .takes_value(true)
53                    .requires_all(&["custom", "offset"]),
54            )
55            .arg(
56                Arg::with_name("offset")
57                    .short("o")
58                    .long("offset")
59                    .help("Offset")
60                    .takes_value(true)
61                    .requires_all(&["custom", "slope"]),
62            )
63            .get_matches();
64
65        let ph = matches
66            .value_of("ph")
67            .unwrap_or_default()
68            .parse::<f64>()
69            .unwrap();
70
71        let custom = matches.is_present("custom");
72
73        let calibration = if custom {
74            let slope = matches
75                .value_of("slope")
76                .unwrap_or_default()
77                .parse::<f64>()
78                .unwrap();
79
80            let offset = matches
81                .value_of("offset")
82                .unwrap_or_default()
83                .parse::<f64>()
84                .unwrap();
85
86            Some([slope, offset])
87        } else {
88            None
89        };
90
91        Self {
92            ph,
93            custom,
94            calibration,
95        }
96    }
97}