extern crate clap;
extern crate num_cpus;
use core::config;
use self::clap::{App, Arg, Error};
pub use self::clap::{crate_authors, crate_description, crate_version};
use std::env;
use std::path::PathBuf;
pub fn parse_args() -> Result<config::Configuration, Error> {
let default_threads = format!("{}", config::default_threads());
let default_temp_dir = format!("{:?}", env::temp_dir());
let matches = App::new("Agni")
.version(crate_version!())
.author(crate_authors!())
.about(crate_description!())
.arg(Arg::with_name("inputs")
.help("The input files to render")
.required(true)
.multiple(true)
.takes_value(true))
.arg(Arg::with_name("outfile")
.short("o")
.long("outfile")
.help("The output file to write to")
.takes_value(true)
.default_value("render.png"))
.arg(Arg::with_name("threads")
.help("Use specified number of threads for rendering")
.short("t")
.long("threads")
.default_value(&*default_threads)
.takes_value(true))
.arg(Arg::with_name("crop")
.help("Specify an image crop window. x0 x1 y0 y1")
.short("c")
.long("cropwindow")
.hidden(true)
.number_of_values(4))
.arg(Arg::with_name("quick")
.hidden(true)
.help("Automatically reduce a number of quality settings to render faster")
.long("quick"))
.arg(Arg::with_name("quiet")
.help("Suppress all text output other than error messages")
.long("quiet"))
.arg(Arg::with_name("logdir")
.hidden(true)
.help("Specify directory to write log files to")
.takes_value(true)
.long("logdir")
.default_value(&*default_temp_dir))
.arg(Arg::with_name("logtostderr")
.hidden(true)
.long("logtostderr")
.help("Print all logging messages to stderr."))
.arg(Arg::with_name("loglevel")
.hidden(true)
.long("loglevel")
.short("l")
.help("The minimum logging level to use.")
.takes_value(true))
.arg(Arg::with_name("verbose")
.hidden(true)
.long("verbose")
.short("v")
.help("Quick toggle to enable verbose logging"))
.arg(Arg::with_name("print")
.hidden(true)
.long("print")
.short("p")
.help("Print the internal representation of the input file to standard output"))
.arg(Arg::with_name("toply")
.hidden(true)
.long("toply")
.help("Converts all triangle meshes to the PLY format"))
.get_matches();
let mut configuration = config::Configuration::new();
if let Some(output) = matches.value_of("outfile") {
configuration.set_output(String::from(output));
}
if let Some(inputs) = matches.values_of("inputs") {
for input in inputs {
let file = PathBuf::from(input);
if !file.exists() {
println!("{} could not be found.", input);
} else {
configuration.add_input(file);
}
}
}
if let Some(threads) = matches.value_of("threads") {
configuration.set_threads(threads.parse::<u8>().unwrap_or(config::default_threads()));
}
if matches.is_present("quiet") {
configuration.set_quiet(true);
}
if !configuration.quiet {
println!("### Agni version {}. -- Using {} (out of {}) threads.",
crate_version!(), configuration.number_of_threads, num_cpus::get());
println!("### Copyright (c)2018 {}", crate_authors!());
println!("### {}", crate_description!());
}
Ok(configuration)
}