use clap::{Arg, Command};
use crate::app::args::Args;
use crate::f::from_list_str_into_map_string_string::*;
use crate::types::*;
pub fn app() -> Command<'static> {
trace!("clap::app");
Command::new("Sita")
.version("1.0.0")
.author("Joel Parker Henderson <joel@joelparkerhenderson.com>")
.arg(Arg::new("input")
.short('i')
.long("input")
.alias("inputs")
.value_name("FILE | DIRECTORY | GLOB")
.takes_value(true)
.multiple_occurrences(true)
.multiple_values(true)
.help("An input path string.\nExample file: --input \"example.html\" …\nExample directory: --input \"examples/\" …\nExample glob: --input \"examples/**/*\" …"))
.arg(Arg::new("output")
.short('o')
.long("output")
.alias("outputs")
.value_name("FILE | DIRECTORY | GLOB")
.takes_value(true)
.multiple_occurrences(true)
.multiple_values(true)
.help("An output path string.\nExample file: --output \"example.html\" …\nExample directory: --output \"examples/\" …\nExample glob: --output \"examples/**/*\" …"))
.arg(Arg::new("output_file_name_extension")
.long("output-extension")
.value_name("EXTENSION")
.takes_value(true)
.help("The output file name extension.\nDefault: \"html\".\nExample: --output-extension \"html\""))
.arg(Arg::new("template")
.short('t')
.long("template")
.alias("templates")
.value_name("FILE | DIRECTORY | GLOB")
.takes_value(true)
.multiple_occurrences(true)
.multiple_values(true)
.help("A template path string.\nExample file: --template \"example.html\" …\nExample directory: --template \"examples/\" …\nExample glob: --template \"examples/**/*\" …"))
.arg(Arg::new("helper")
.short('h')
.long("helper")
.alias("helpers")
.value_name("FILE | DIRECTORY | GLOB")
.takes_value(true)
.multiple_occurrences(true)
.multiple_values(true)
.help("A helper path string.\nExample file: --helper \"example.rhai\" …\nExample directory: --helper \"helpers/\" …\nExample glob: --helper \"helpers/**/*\" …"))
.arg(Arg::new("test")
.long("test")
.takes_value(false)
.help("Print test output for debugging, verifying, tracing, and the like.\nExample: --test"))
.arg(Arg::new("set")
.short('s')
.long("set")
.value_names(&["NAME", "VALUE"])
.takes_value(true)
.multiple_occurrences(true)
.multiple_values(true)
.help("Set a variable name to a value.\nExample: --set pi 3.1415 …"))
.arg(Arg::new("verbose")
.short('v')
.long("verbose")
.takes_value(false)
.multiple_occurrences(true)
.help("Set the verbosity level: 0=none, 1=error, 2=warn, 3=info, 4=debug, 5=trace.\nExample: --verbose …"))
}
pub fn args() -> Args {
trace!("clap::args");
let matches = app().get_matches();
trace!("clap::args matches: {:?}", matches);
let input_list_pathable_string = match matches.values_of("input") {
Some(x) => Some(x.map(|x| PathableString::from(x)).collect::<List<PathableString>>()),
_ => None,
};
let output_list_pathable_string = match matches.values_of("output") {
Some(x) => Some(x.map(|x| PathableString::from(x)).collect::<List<PathableString>>()),
_ => None,
};
let output_file_name_extension = match matches.value_of("output_file_name_extension") {
Some(x) => Some(String::from(x)),
_ => None,
};
let helper_list_pathable_string = match matches.values_of("helper") {
Some(x) => Some(x.map(|x| PathableString::from(x)).collect::<List<PathableString>>()),
_ => None,
};
let settings = match matches.values_of("set") {
Some(x) => {
let list_str = x.into_iter().collect::<List<&str>>();
Some(from_list_str_into_map_string_string(&list_str))
},
_ => None,
};
let template_list_pathable_string = match matches.values_of("template") {
Some(x) => Some(x.map(|x| PathableString::from(x)).collect::<List<PathableString>>()),
_ => None,
};
let test = matches.is_present("test");
let log_level = match matches.occurrences_of("verbose") {
0 => None,
1 => Some(::log::Level::Error),
2 => Some(::log::Level::Warn),
3 => Some(::log::Level::Info),
4 => Some(::log::Level::Debug),
5 => Some(::log::Level::Trace),
_ => Some(::log::Level::Trace),
};
let args = Args {
input_list_pathable_string: input_list_pathable_string,
log_level: log_level,
output_list_pathable_string: output_list_pathable_string,
output_file_name_extension: output_file_name_extension,
settings: settings,
template_list_pathable_string: template_list_pathable_string,
helper_list_pathable_string: helper_list_pathable_string,
test: test,
};
trace!("clap::args -> {:?}", args);
args
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test::*;
use assertables::*;
#[test]
fn test_test() {
let mut command = ::std::process::Command::new(&*COMMAND_OS);
command.args(&["--test"]);
let target = r#"Args { "#;
assert_command_stdout_contains!(command, &target);
}
#[test]
fn test_verbose() {
assert_program_args_stdout_contains!(&*COMMAND_OS, &["--test"], r#" log_level: None"#);
assert_program_args_stdout_contains!(&*COMMAND_OS, &["--test", "-v"], r#" log_level: Some(Error)"#);
assert_program_args_stdout_contains!(&*COMMAND_OS, &["--test", "-vv"], r#" log_level: Some(Warn)"#);
assert_program_args_stdout_contains!(&*COMMAND_OS, &["--test", "-vvv"], r#" log_level: Some(Info)"#);
assert_program_args_stdout_contains!(&*COMMAND_OS, &["--test", "-vvvv"], r#" log_level: Some(Debug)"#);
assert_program_args_stdout_contains!(&*COMMAND_OS, &["--test", "-vvvvv"], r#" log_level: Some(Trace)"#);
assert_program_args_stdout_contains!(&*COMMAND_OS, &["--test", "--verbose"], r#" log_level: Some(Error)"#);
assert_program_args_stdout_contains!(&*COMMAND_OS, &["--test", "--verbose", "--verbose"], r#" log_level: Some(Warn)"#);
assert_program_args_stdout_contains!(&*COMMAND_OS, &["--test", "--verbose", "--verbose", "--verbose"], r#" log_level: Some(Info)"#);
assert_program_args_stdout_contains!(&*COMMAND_OS, &["--test", "--verbose", "--verbose", "--verbose", "--verbose"], r#" log_level: Some(Debug)"#);
assert_program_args_stdout_contains!(&*COMMAND_OS, &["--test", "--verbose", "--verbose", "--verbose", "--verbose", "--verbose"], r#" log_level: Some(Trace)"#);
}
#[test]
fn test_input() {
let s1 = "alpha";
let s2 = "bravo";
let s3 = "charlie";
let s4 = "delta";
let target = format!(" input_list_pathable_string: Some([\"{}\", \"{}\", \"{}\", \"{}\"])", &s1, &s2, &s3, &s4);
let mut command = ::std::process::Command::new(&*COMMAND_OS);
command.args(&["--test", "-i", &s1, &s2, "-i", &s3, &s4]);
assert_command_stdout_contains!(command, &target);
let mut command = ::std::process::Command::new(&*COMMAND_OS);
command.args(&["--test", "--input", &s1, &s2, "--input", &s3, &s4]);
assert_command_stdout_contains!(command, &target);
let mut command = ::std::process::Command::new(&*COMMAND_OS);
command.args(&["--test", "--inputs", &s1, &s2, "--inputs", &s3, &s4]);
assert_command_stdout_contains!(command, &target);
}
#[test]
fn test_output() {
let s1 = "alpha";
let s2 = "bravo";
let s3 = "charlie";
let s4 = "delta";
let target = format!(" output_list_pathable_string: Some([\"{}\", \"{}\", \"{}\", \"{}\"])", &s1, &s2, &s3, &s4);
let mut command = ::std::process::Command::new(&*COMMAND_OS);
command.args(&["--test", "-o", &s1, &s2, "-o", &s3, &s4]);
assert_command_stdout_contains!(command, &target);
let mut command = ::std::process::Command::new(&*COMMAND_OS);
command.args(&["--test", "--output", &s1, &s2, "--output", &s3, &s4]);
assert_command_stdout_contains!(command, &target);
let mut command = ::std::process::Command::new(&*COMMAND_OS);
command.args(&["--test", "--outputs", &s1, &s2, "--outputs", &s3, &s4]);
assert_command_stdout_contains!(command, &target);
}
#[test]
fn test_clap_output_file_name_extension() {
let mut command = ::std::process::Command::new(&*COMMAND_OS);
command.args(&["--test", "--output-extension", "alpha"]);
let target = r#" output_file_name_extension: Some("alpha")"#;
assert_command_stdout_contains!(command, &target);
}
#[test]
fn test_template() {
let glob_dir = "template_list_pathable_string";
let glob1 = format!("{}/{}", &glob_dir, "a/**/*");
let glob2 = format!("{}/{}", &glob_dir, "b/**/*");
let glob3 = format!("{}/{}", &glob_dir, "c/**/*");
let glob4 = format!("{}/{}", &glob_dir, "d/**/*");
let target = format!(" template_list_pathable_string: Some([\"{}\", \"{}\", \"{}\", \"{}\"])", &glob1, &glob2, &glob3, &glob4);
let mut command = ::std::process::Command::new(&*COMMAND_OS);
command.args(&["--test", "-t", &glob1, &glob2, "-t", &glob3, &glob4]);
assert_command_stdout_contains!(command, &target);
let mut command = ::std::process::Command::new(&*COMMAND_OS);
command.args(&["--test", "--template", &glob1, &glob2, "--template", &glob3, &glob4]);
assert_command_stdout_contains!(command, &target);
let mut command = ::std::process::Command::new(&*COMMAND_OS);
command.args(&["--test", "--templates", &glob1, &glob2, "--templates", &glob3, &glob4]);
assert_command_stdout_contains!(command, &target);
}
}