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
use crate::logger;
use crate::markup::MarkupType;
use crate::Config;
use clap::{App, Arg};
use wildmatch::WildMatch;
use std::path::Path;


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("ignore_links")
                .long("ignore-links")
                .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("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 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 ignore_links: Vec<WildMatch> = matches
        .values_of("ignore_links")
        .unwrap_or_default()
        .map(|x| WildMatch::new(x))
        .collect();
    
    let root_dir = if let Some(root_path) = matches.value_of("root_dir"){
        let root_path = Path::new(root_path).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,
        ignore_links,
        root_dir,
    }
}