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
//! [reelpath](https://github.com/nickgerace/reelpath) finds the absolute path for a given file or directory.
//! This library exists for the `reelpath` CLI and has not been tested for other use cases.

use std::fs;
use std::io;

/// This function is the primary driver for the `reelpath` CLI. In errorless scenarios, this
/// function will print its results to STDOUT. Otherwise, it will return any error encountered.
pub fn run(s: &[String]) -> io::Result<()> {
    let help = || {
        println!(
            "reelpath {}
https://github.com/nickgerace/reelpath

Find the absolute path of a given file or directory.
To evaluate more than one path, an additional argument per path.
Wildcards can be used in a single argument.

USAGE:
    reelpath [path]...",
            option_env!("CARGO_PKG_VERSION").unwrap_or("v?")
        );
    };

    // I'm not super happy with this code, but it gets the job done.
    let mut print_help = false;
    for i in s {
        match i.as_str() {
            "-h" | "-help" | "--help" => {
                print_help = true;
                break;
            }
            _ => continue,
        }
    }
    if s.is_empty() || print_help {
        help();
    } else {
        for i in s {
            match fs::canonicalize(i) {
                Ok(o) => println!("{}", o.display()),
                Err(e) => return Err(e),
            }
        }
    }
    Ok(())
}