Update argument handling and tracing args

Signed-off-by: Ralf Zerres <ralf.zerres@networkx.de>
This commit is contained in:
2021-05-21 12:35:14 +02:00
parent 70de97d761
commit f9eeb4b6bc

View File

@@ -9,18 +9,16 @@ 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 tracing::{trace, Level};
use tracing::trace;
use std::env;
if cfg!(feature = "yaml") {
trace!(target:"feature", "Clap feature 'yaml' enabled.");
println!("Using 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");
trace!(target: "Viperus", "Viperus feature 'fmt-clap' enabled.");
println!("Using feature fmt-clap");
}
// preset default key/value pairs (lowest priority)
@@ -36,51 +34,51 @@ pub fn parse_args(v: &mut Viperus) -> Result<(), Box<dyn std::error::Error>> {
// 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}
.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();
)
.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);
// clap is using i64, viperus i32
let n = matches.occurrences_of("verbose") as i32;
v.add("verbose", n);
}
//}
@@ -94,17 +92,17 @@ pub fn parse_args(v: &mut Viperus) -> Result<(), Box<dyn std::error::Error>> {
// 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"))
"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);
v.cache(true);
}
if cfg!(feature = "fmt-env") {
v.automatic_env(true);
v.automatic_env(true);
}
//if cfg!(feature = "fmt-clap") {
@@ -120,25 +118,25 @@ pub fn parse_args(v: &mut Viperus) -> Result<(), Box<dyn std::error::Error>> {
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()
);
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);
}
println!("\nEnvironment:");
for (key, value) in env::vars() {
println!("{}={}", key, value);
}
}
Ok(())