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
#[derive(Debug)]
pub struct Args {
    pub no_build: bool,
    #[cfg(feature = "dracut")]
    pub no_initramfs: bool,
    pub no_modules: bool,
    pub menuconfig: bool,
}

impl Args {
    const HELP: &str = r#"
Kernel Builder
USAGE:
  kernel-builder [OPTIONS]
FLAGS:
  -h, --help            Prints help information
  -v, --version         Print version
OPTIONS:
  --no-build          skip build
  --no-initramfs      skip generating initramfs (only if compiled with dracut feature)
  --no-modules        skip installing kernel modules
  --menuconfig        open menuconfig for kernel configuration
"#;

    #[must_use]
    pub fn parse_args() -> Self {
        let mut pargs = pico_args::Arguments::from_env();

        // Help has a higher priority and should be handled separately.
        if pargs.contains(["-h", "--help"]) {
            print!("{}", Self::HELP);
            std::process::exit(0);
        }

        if pargs.contains(["-v", "--version"]) {
            println!(
                "kernel-builder v{}",
                std::env::var("CARGO_PKG_VERSION").expect("missing package version")
            );
            std::process::exit(0);
        }

        Self {
            no_build: pargs.contains("--no-build"),
            #[cfg(feature = "dracut")]
            no_initramfs: pargs.contains("--no-initramfs"),
            no_modules: pargs.contains("--no-modules"),
            menuconfig: pargs.contains("--menuconfig"),
        }
    }
}