~elchenberg/not-envsubst

ref: f4ab9be4f33746392bb68bbc614cd1283a7dc474 not-envsubst/src/cli.rs -rw-r--r-- 1.3 KiB
f4ab9be4elchenberg Initial commit 7 months ago
                                                                                
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
61
use std::env::args;
use std::process::exit;

use gumdrop::Options;

#[derive(Options)]
pub struct Args {
    #[options(help = "print this help message")]
    pub help: bool,

    #[options(help = "print the version number", short = "V")]
    pub version: bool,
}

pub fn get_args() -> Args {
    let env_args = args().collect::<Vec<_>>();
    match Args::parse_args_default(&env_args[1..]) {
        Ok(args) => args,
        Err(error) => {
            eprintln!("ERROR: {}", error);
            eprintln!();
            eprintln!("{}", get_help());
            exit(1);
        }
    }
}

pub fn print_help_and_exit() {
    print_help();
    exit(0);
}

pub fn print_version_and_exit() {
    print_version();
    exit(0);
}

fn get_help() -> String {
    format!(
        "{name} v{version}\n\n{description}\n\nUsage:\n  {bin_name} [OPTIONS]\n\n{usage}",
        name = crate::pkg::get_name(),
        version = crate::pkg::get_version(),
        description = crate::pkg::get_description(),
        bin_name = args()
            .next()
            .unwrap_or_else(|| crate::pkg::get_bin_name().to_string()),
        usage = get_usage(),
    )
}

fn print_version() {
    println!("{}", crate::pkg::get_version());
}

fn print_help() {
    println!("{}", get_help())
}

fn get_usage() -> &'static str {
    Args::usage()
}