parse_args: commandline parsing module

Signed-off-by: Ralf Zerres <ralf.zerres@networkx.de>
This commit is contained in:
2020-06-09 15:44:06 +02:00
parent ccf276e07a
commit 4094a6a92e

138
src/parse_args.rs Normal file
View File

@@ -0,0 +1,138 @@
use viperus::Viperus;
/// The given Viperus structure will be muted according to the
/// processed default, environment and commandline arguments
pub fn parse_args(v: &mut Viperus) -> Result<(), Box<dyn std::error::Error>> {
//use log::{debug, info, trace, warn};
use log::trace;
use std::env;
if cfg!(feature = "yaml") {
trace!(target:"feature", "Clap feature 'yaml' enabled.");
println!("Using feature yaml.");
}
if cfg!(feature = "fmt-clap") {
trace!(target: "Viperus", "Viperus feature 'fmt-clap' enabled.");
println!("Using feature fmt-clap");
}
// preset default key/value pairs (lowest priority)
v.add_default("config_file", String::from("advotrackerd.yml"));
v.add_default("db_driver", String::from("sqlite"));
v.add_default("verbose", 0);
//if cfg!(feature = "fmt-clap") {
println!("With fmt-clap...");
// parse CLI commandline arguments with clap
use clap::{crate_authors, crate_description, crate_name, crate_version, App, Arg};
// CLI arguments are defined inline
let matches = App::new("advotrackerd")
.name(crate_name!())
.version(crate_version!())
.author(crate_authors!())
.about(crate_description!())
.after_help("in Zusammenarbeit mit Hiedemann Rechtsanwälte <info@hiedemann.de>")
.template(
"\
{bin} v{version}
{about}
{all-args}
(C) 2020 {author}
{after_help}",
)
.arg(
Arg::with_name("configFile")
.short("c")
.long("configFile")
.value_name("FILE")
.help("Sets a custom config file")
.takes_value(true),
)
.arg(
Arg::with_name("dbdriver")
.short("d")
.long("dbdriver")
.value_name("DatabaseDriver")
.help("Driver used to connect to database")
.possible_values(&["mysql", "postgres", "sqlite"])
.takes_value(true),
)
.arg(
Arg::with_name("verbose")
.short("v")
.long("verbose")
.help("Sets verbosity level")
.multiple(true),
)
.get_matches();
//}
if matches.occurrences_of("verbose") > 0 {
// clap is using i64, viperus i32
let n = matches.occurrences_of("verbose") as i32;
v.add("verbose", n);
}
//}
// preset the prefix for relevant environment variables ("ADVOTRACKERD_")
//if cfg!(feature = "fmt-clap") {
let mut env_prefix: String = crate_name!().to_uppercase();
env_prefix.push_str("_");
v.set_env_prefix(&env_prefix);
//}
// respect dotenv environment (e.g for testing)
// -> overwrites the preset default values
println!(
"RUST_LOG={}",
dotenv::var("RUST_LOG").unwrap_or_else(|_| String::from("None"))
);
//v.load_file(".env", v.Format::ENV).unwrap();
// enable caching and automatic update of environment values
if cfg!(feature = "fmt-cache") {
v.cache(true);
}
if cfg!(feature = "fmt-env") {
v.automatic_env(true);
}
//if cfg!(feature = "fmt-clap") {
// load user selected call arguments
// -> overwrites values given via environment variables
v.load_clap(matches)?;
// bond the clap names to camel_case rust variable names
v.bond_clap("configFile", "config_file");
v.bond_clap("dbdriver", "db_driver");
v.bond_clap("verbose", "verbose");
//}
trace!("verbose {:?}", v.get::<i32>("verbose").unwrap());
if v.get::<i32>("verbose").unwrap() > 0 {
println!(
"config_file: {:?}",
v.get::<String>("config_file").unwrap_or_default()
);
println!(
"db_driver: {:?}",
v.get::<String>("db_driver").unwrap_or_default()
);
println!(
"verbosity level: {:?}",
v.get::<i32>("verbose").unwrap_or_default()
);
}
if v.get::<i32>("verbose").unwrap() > 1 {
println!("\nEnvironment:");
for (key, value) in env::vars() {
println!("{}={}", key, value);
}
}
Ok(())
}