twinleaf_tools/
lib.rs

1use getopts::Options;
2use tio::proto::DeviceRoute;
3use tio::util;
4use twinleaf::tio;
5
6pub fn tio_opts() -> Options {
7    let mut opts = Options::new();
8    opts.optopt(
9        "r",
10        "",
11        &format!("sensor root (default {})", util::default_proxy_url()),
12        "address",
13    );
14    opts.optopt(
15        "s",
16        "",
17        "sensor path in the sensor tree (default /)",
18        "path",
19    );
20    opts
21}
22
23pub fn tio_parseopts(opts: &Options, args: &[String]) -> (getopts::Matches, String, DeviceRoute) {
24    let matches = match opts.parse(args) {
25        Ok(m) => m,
26        Err(f) => {
27            print!("{}", opts.usage("Invalid tool invocation"));
28            panic!("{}", f.to_string())
29        }
30    };
31    let root = if let Some(url) = matches.opt_str("r") {
32        url
33    } else {
34        "tcp://localhost".to_string()
35    };
36    let route = if let Some(path) = matches.opt_str("s") {
37        DeviceRoute::from_str(&path).unwrap()
38    } else {
39        DeviceRoute::root()
40    };
41    (matches, root, route)
42}