atcoder_auto_tester/
args.rs

1use std::path;
2
3use crate::error::ErrorHandleable;
4
5#[derive(Debug)]
6pub struct Args {
7    pub should_clean: bool,
8    pub should_login: bool,
9    pub config_file: Box<path::Path>,
10    pub test_directory: Box<path::Path>,
11    pub timeout: u32,
12}
13
14impl Args {
15    pub fn load() -> Args {
16        let matches = clap::App::new("atcoder-auto-tester")
17            .version(env!("CARGO_PKG_VERSION"))
18            .about(env!("CARGO_PKG_DESCRIPTION"))
19            .setting(clap::AppSettings::ColoredHelp)
20            .setting(clap::AppSettings::ArgRequiredElseHelp)
21            .version_short("v")
22            .version_message("Print version information")
23            .help_message("Print help information")
24            .arg(
25                clap::Arg::with_name("clean")
26                    .long("clean")
27                    .help("Remove the test directory")
28                    .display_order(1),
29            )
30            .arg(
31                clap::Arg::with_name("login")
32                    .long("login")
33                    .help("Login to AtCoder")
34                    .conflicts_with("clean")
35                    .display_order(2),
36            )
37            .arg(
38                clap::Arg::with_name("config-file")
39                    .short("f")
40                    .long("config-file")
41                    .value_name("FILE")
42                    .help("Set a config file name")
43                    .default_value(".config.toml"),
44            )
45            .arg(
46                clap::Arg::with_name("test-directory")
47                    .short("d")
48                    .long("test-directory")
49                    .value_name("DIRECTORY")
50                    .help("Set a directory for saving test cases")
51                    .default_value(".test"),
52            )
53            .arg(
54                clap::Arg::with_name("timeout")
55                    .short("t")
56                    .long("timeout")
57                    .value_name("VALUE")
58                    .help("Set a time limit for test execution [unit: seconds]")
59                    .default_value("5"),
60            )
61            .get_matches();
62
63        let clean = matches.is_present("clean");
64        let login = matches.is_present("login");
65        assert!(!(clean && login));
66
67        let config_file = matches.value_of("config-file").unwrap();
68        let test_directory = matches.value_of("test-directory").unwrap();
69        let timeout = matches.value_of("timeout").unwrap();
70
71        Args {
72            should_clean: clean,
73            should_login: login,
74            config_file: Box::from(path::Path::new(config_file)),
75            test_directory: Box::from(path::Path::new(test_directory)),
76            timeout: clap::value_t!(matches.value_of("timeout"), u32).handle_error(&format!(
77                "The argument '{}' isn't a valid value for 'timeout'. Set a non-negative integer.",
78                timeout
79            )),
80        }
81    }
82}