pretty_bytes/
cli.rs

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
use ::converter;

use std::io;
use std::env;
use getopts::Options;
use atty::{self, Stream};

fn print_usage(program: &str, opts: Options) {
    let brief = format!(
r#"Usage:
    $ {} <number>
    $ echo <number> | {}"#, program, program);
    print!("{}", opts.usage(&brief));
}

fn print_version() {
    println!("{}", env!("CARGO_PKG_VERSION"));
}

pub fn run(args: env::Args) -> () {
    let num: f64 = if atty::is(Stream::Stdin) {
        let args: Vec<String> = args.collect();
        let ref program = args[0];

        let mut opts = Options::new();
        opts.optflag("h", "help", "print this help menu");
        opts.optflag("v", "version", "print version");

        let matches = match opts.parse(&args[1..]) {
            Ok(m)  => { m }
            Err(f) => { panic!(f.to_string()) }
        };

        if matches.opt_present("v") {
            return print_version();
        } else if matches.opt_present("h") || args.len() != 2 {
            return print_usage(&program, opts);
        }

        match args[1].parse::<f64>() {
            Ok(n) => n,
            Err(f) => { panic!(f.to_string()) }
        }
    } else {
        let mut input = String::new();
        io::stdin().read_line(&mut input).ok().expect("Unable to read from console!");
        input.trim().parse::<f64>().unwrap()
    };

    println!("{}", converter::convert(num));
}