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
use clap::{App, Arg};
use std::env;

pub fn autoclap() -> clap::App<'static> {
    let repo = env::var("CARGO_PKG_REPOSITORY").unwrap();
    let mut release_tag = "";

    if repo.contains("github") {
        release_tag = "/releases/tag/";
    } else if repo.contains("gitlab") {
        release_tag = "/-/releases/";
    }

    if repo.contains("github") {
        let app_name = format!(
            "{}{}{}{}{}",
            env::var("CARGO_PKG_NAME").unwrap(),
            " ",
            env::var("CARGO_PKG_VERSION").unwrap(),
            " :: ",
            format!(
                "{}{}{}",
                env::var("CARGO_PKG_REPOSITORY").unwrap(),
                release_tag,
                env::var("CARGO_PKG_VERSION").unwrap(),
            )
        );

        return App::new(app_name).arg(
            Arg::new("debug")
                .long("debug")
                .short('D')
                .help("Print raw data used internally."),
        );
    } else {
        panic!("Cannot determine repository from repository URL.");
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_autoclap_name_works_when_typical() {
        let app = autoclap();
        assert_eq!(
            app.get_name(),
            "autoclap 0.1.0 :: https://github.com/mihaigalos/autoclap/releases/tag/0.1.0"
        );
    }
    #[test]
    #[ignore] // Await clap :: author<S: Into<String>>(self, author: S)
    fn test_autoclap_author_works_when_typical() {
        let app = autoclap();
        assert_eq!(
            app.get_about().unwrap(),
            "๐Ÿ‘ Auto-propagate Cargo.toml infos (name, version, author, repo) into app."
        );
    }
}