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