blob_dl/
parser.rs

1use clap::{Arg, Command, ArgMatches, ArgAction};
2
3use crate::ui_prompts::*;
4use crate::error::{BlobdlError, BlobResult};
5
6pub fn parse_config() -> BlobResult<CliConfig> {
7    let matches = Command::new("blob-dl")
8        .version("1.0.1")
9        .author("cioccarellimi@gmail.com")
10        .about(SHORT_ABOUT)
11        .long_about(LONG_ABOUT)
12        .arg(
13            Arg::new("verbose")
14                .short('v')
15                .long("verbose")
16                .help("Show all the output produced by yt-dlp")
17                .action(ArgAction::SetTrue),
18        )
19        .arg(
20            Arg::new("quiet")
21                .short('q')
22                .long("quiet")
23                .help("Silence all output except for the final error summary")
24                .action(ArgAction::SetTrue),
25        )
26        .arg(
27            Arg::new("show-command")
28                .help("Print to the console the command generated by blob-dl")
29                .long("show-command")
30                .short('s')
31                .action(ArgAction::SetTrue),
32        )
33        .arg(Arg::new("URL")
34            .help("Link to the youtube video/playlist that you want to download")
35        )
36        .get_matches();
37
38    CliConfig::from(matches)
39}
40
41/// The 3 possible verbosity options for this program
42#[derive(Debug)]
43pub enum Verbosity {
44    Verbose,
45    Default,
46    Quiet,
47}
48
49/// Holds all the information that can be fetched as a command line argument
50#[derive(Debug)]
51pub struct CliConfig {
52    // Refs to this String are stored in other Config objects
53    url: String,
54    verbosity: Verbosity,
55    // Whether to print to the console the final command which is the run by yt-dlp
56    show_command: bool,
57}
58
59impl CliConfig {
60    /// Constructs a CliConfig object based on Clap's output
61    pub fn from(matches: ArgMatches) -> BlobResult<CliConfig> {
62
63        let url = match matches.get_one::<String>("URL") {
64            Some(url) => url.clone(),
65            None => return Err(BlobdlError::MissingArgument),
66        };
67
68        let verbosity = {
69            if matches.get_flag("quiet") {
70                Verbosity::Quiet
71            }
72            else if matches.get_flag("verbose") {
73                Verbosity::Verbose
74            }
75            else {
76                Verbosity::Default
77            }
78        };
79        let show_command = matches.get_flag("show-command");
80
81        Ok(CliConfig {
82            url,
83            verbosity,
84            show_command,
85        })
86    }
87
88    pub fn url(&self) -> &String {
89        &self.url
90    }
91    pub fn verbosity(&self) -> &Verbosity {
92        &self.verbosity
93    }
94    pub fn show_command(&self) -> bool {
95        self.show_command
96    }
97}