pipev/
args.rs

1use clap::{Arg, ArgAction, Command};
2use std::env;
3
4pub struct Args {
5    pub infile: String,
6    pub outfile: String,
7    pub silent: bool,
8}
9
10impl Args {
11    pub fn parse() -> Self {
12        let matches = Command::new("Pipe Viewer")
13            .version("1.0.0")
14            .arg(
15                Arg::new("infile")
16                    .short('i')
17                    .long("infile")
18                    .default_value("")
19                    .help("Read from file instead of stdin"),
20            )
21            .arg(
22                Arg::new("outfile")
23                    .short('o')
24                    .long("outfile")
25                    .default_value("")
26                    .help("Write to file instead of stdout"),
27            )
28            .arg(
29                Arg::new("silent")
30                    .short('s')
31                    .action(ArgAction::SetTrue)
32                    .long("silent"),
33            )
34            .get_matches();
35
36        let infile = matches
37            .get_one::<String>("infile")
38            .expect("invalid input file")
39            .to_string();
40
41        let outfile = matches
42            .get_one::<String>("outfile")
43            .expect("invalid output file")
44            .to_string();
45
46        let silent = if matches.get_flag("silent") {
47            true
48        } else {
49            !env::var("PV_SILENT").unwrap_or_default().is_empty()
50        };
51
52        Self {
53            infile,
54            outfile,
55            silent,
56        }
57    }
58}