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
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
use crate::logger;
use crate::markup::MarkupType;
use crate::Config;
use clap::{App, Arg};
use std::fs;
use std::path::Path;
use std::path::PathBuf;
use std::path::MAIN_SEPARATOR;
use wildmatch::WildMatch;

pub fn parse_args() -> Config {
    let matches = App::new(crate_name!())
        .arg(
            Arg::with_name("directory")
                .help("Check all links in given directory and subdirectory")
                .required(false)
                .index(1),
        )
        .arg(
            Arg::with_name("debug")
                .long("debug")
                .short("d")
                .help("Print debug information to console")
                .required(false),
        )
        .arg(
            Arg::with_name("no_web_links")
                .long("no-web-links")
                .help("Do not check web links")
                .required(false),
        )
        .arg(
            Arg::with_name("match-file-extension")
                .long("match-file-extension")
                .help("Do check for the exact file extension when searching for a file.")
                .required(false),
        )
        .arg(
            Arg::with_name("ignore_path")
                .long("ignore-path")
                .help("List of files and directories which will not be checked")
                .min_values(1)
                .required(false),
        )
        .arg(
            Arg::with_name("ignore_links")
                .long("ignore-links")
                .short("i")
                .help("List of links which will not be checked")
                .min_values(1)
                .required(false),
        )
        .arg(
            Arg::with_name("markup_types")
                .long("markup-types")
                .short("t")
                .help("List of markup types which shall be checked")
                .min_values(1)
                .possible_values(&["md", "html"])
                .required(false),
        )
        .arg(
            Arg::with_name("throttle")
                .long("throttle")
                .help("Wait between http request for a defined number of milliseconds.")
                .required(false)
                .takes_value(true),
        )
        .arg(
            Arg::with_name("root_dir")
                .long("root-dir")
                .takes_value(true)
                .short("r")
                .help("Path to the root folder used to resolve all relative paths")
                .required(false),
        )
        .version(crate_version!())
        .author(crate_authors!())
        .about(crate_description!())
        .get_matches();
    let debug = matches.is_present("debug");

    let throttle = match matches.value_of("throttle") {
        Some(v) => v
            .parse()
            .expect("Integer expected. Throttle time in milliseconds."),
        None => 0,
    };

    let log_level = if debug {
        logger::LogLevel::Debug
    } else {
        logger::LogLevel::Warn
    };
    let directory = matches
        .value_of("directory")
        .unwrap_or("./")
        .parse()
        .unwrap();

    let mut markup_types = vec![MarkupType::Markdown, MarkupType::HTML];
    if let Some(types) = matches.values_of("markup_types") {
        markup_types = types.map(|x| x.parse().unwrap()).collect();
    }

    let no_web_links = matches.is_present("no_web_links");

    let match_file_extension = matches.is_present("match-file-extension");

    let ignore_links: Vec<WildMatch> = matches
        .values_of("ignore_links")
        .unwrap_or_default()
        .map(|x| WildMatch::new(x))
        .collect();

    let ignore_path: Vec<PathBuf> = matches
        .values_of("ignore_path")
        .unwrap_or_default()
        .map(|x| {
            let path = Path::new(x).to_path_buf();
            match fs::canonicalize(&path) {
                Ok(p) => p,
                Err(e) => panic!("Ignore path {:?} not found. {:?}.", &path, e),
            }
        })
        .collect();

    let root_dir = if let Some(root_path) = matches.value_of("root_dir") {
        let root_path = Path::new(
            &root_path
                .replace('/', &MAIN_SEPARATOR.to_string())
                .replace('\\', &MAIN_SEPARATOR.to_string()),
        )
        .to_path_buf();
        if !root_path.is_dir() {
            eprintln!("Root path {:?} must be a directory!", root_path);
            std::process::exit(1);
        }
        Some(root_path)
    } else {
        None
    };

    Config {
        log_level,
        folder: directory,
        markup_types,
        no_web_links,
        match_file_extension,
        ignore_links,
        ignore_path,
        root_dir,
        throttle,
    }
}