aboutsummaryrefslogtreecommitdiffstats
path: root/src/args.rs
blob: ef119b3833e5a7932a17c735c6566713a99b4bd0 (plain) (blame)
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
use crate::colors;

pub struct CommandLineOptions {
    pub shell: colors::ShellType,
    pub error_code: u8,
}

pub fn parse() -> CommandLineOptions {
    let matches = clap::Command::new("fancy-prompt")
        .about("Prints a fancy prompt")
        .author(clap::crate_authors!())
        .version(clap::crate_version!())
        .arg(
            clap::Arg::new("prompt-escape")
                .long("prompt-escape")
                .value_name("SHELL")
                .help(
                    "Produces escape sequence wrappers for the given shell",
                ),
        )
        .arg(
            clap::Arg::new("error-code")
                .value_name("ERROR_CODE")
                .value_parser(clap::value_parser!(u8))
                .help("The error code of the previously run command"),
        )
        .get_matches();

    let shell = matches
        .get_one::<String>("prompt-escape")
        .map(|s| colors::ShellType::from_str(s))
        .unwrap_or(colors::ShellType::Unknown);
    let error_code =
        matches.get_one::<u8>("error-code").copied().unwrap_or(0);

    CommandLineOptions { shell, error_code }
}