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
62
63
64
65
66
67
pub mod when;
pub use when::*;

use super::{Param, SyntaxHighLight};
use structopt::*;

#[derive(StructOpt)]
#[structopt(name = "pretty-exec", rename_all = "kebab")]
pub struct Args {
    /// Program to execute
    #[structopt(name = "program")]
    program: String,

    /// Arguments to pass to program
    #[structopt(name = "arguments")]
    arguments: Vec<String>,

    /// Do not execute, print command only
    #[structopt(long)]
    skip_exec: bool,

    /// When to use color
    /// [choices: auto, never, always]
    #[structopt(long, default_value = "auto")]
    color: When,

    /// Enable GitHub Action grouping
    #[structopt(long)]
    github_actions: bool,
}

impl Args {
    pub fn syntax_highlight(&self) -> SyntaxHighLight<String> {
        if self.color == When::Never {
            SyntaxHighLight::default_colorless()
        } else {
            SyntaxHighLight::default_color()
        }
    }

    pub fn param(&'_ self) -> Param<'_> {
        Param {
            program: self.program.as_str(),
            arguments: self.arguments.as_ref(),
            skip_exec: self.skip_exec,
            support_github_action: self.github_actions,
            syntax_highlight: self.syntax_highlight(),
        }
    }

    pub fn get(args: impl Iterator<Item = String>) -> Self {
        use std::process::exit;
        match Self::from_iter_safe(args) as Result<Self, clap::Error> {
            Ok(value) => value,
            Err(clap::Error { kind, message, .. }) => match kind {
                clap::ErrorKind::HelpDisplayed | clap::ErrorKind::VersionDisplayed => {
                    println!("{}", message);
                    exit(0);
                }
                _ => {
                    println!("{}", message);
                    exit(1);
                }
            },
        }
    }
}