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
//! Parses command line arguments
//!
//! # Args
//!
//! Parses the command line arguments
//!

use clap::Parser;
use std::env;

#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
pub struct Args {
    /// Read from a file instead of stdin
    #[clap(short, long)]
    pub infile: Option<String>,

    /// Write to a file instead of stdout
    #[clap(short, long)]
    pub outfile: Option<String>,

    /// Display no output
    #[clap(short, long)]
    pub silent: bool,
}

/// Implementations for Args
impl Args {
    /// Parses the command line arguments
    pub fn parse() -> Self {
        let args = Args::try_parse().unwrap_or_else(|e| e.exit());

        let infile = args.infile;

        let outfile = args.outfile;

        // If the environment variable PV_SILENT has been set, no matter its content, set silent to true
        let silent = args.silent || !env::var("PV_SILENT").unwrap_or_default().is_empty();

        Self {
            infile,
            outfile,
            silent,
        }
    }
}