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> { //use log::{debug, info, trace, warn}; use tracing::{trace, Level}; use std::env; 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("csv_import.ron")); v.add_default("import_file", String::from("allianz.txt")); //v.add_default("username", String::from("nctalkbot")); //v.add_default("password", String::from("botpassword")); v.add_default("verbose", 0); // 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("csv-import") .name(crate_name!()) .version(crate_version!()) .author(crate_authors!()) .about(crate_description!()) .after_help("Test: Versicherungsnummern-Import Allianz DirectCall") .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("Select a config file") .takes_value(true), ) .arg( Arg::with_name("importFile") .short("i") .long("importFile") //.value_name("FILE") .help("Select source file for the csv-import") .takes_value(true), ) // .arg( // Arg::with_name("username") // .short("u") // .long("username") // .help("Sets username") // .takes_value(true), // ) // .arg( // Arg::with_name("password") // .short("P") // .long("password") // .help("Sets password") // .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 ("ADVOTRACKER_") 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")) ); // 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); } // 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("importFile", "import_file"); //v.bond_clap("username", "username"); //v.bond_clap("password", "password"); v.bond_clap("verbose", "verbose"); trace!("verbose {:?}", v.get::("verbose").unwrap()); if v.get::("verbose").unwrap() > 0 { println!( "config_file: {:?}", v.get::("config_file").unwrap_or_default() ); println!( "import_file: {:?}", v.get::("import_file").unwrap_or_default() ); // println!( // "username: {:?}", // v.get::("username").unwrap_or_default() // ); // println!( // "password: {:?}", // v.get::("password").unwrap_or_default() // ); // only for testing now println!( "verbosity level: {:?}", v.get::("verbose").unwrap_or_default() ); } if v.get::("verbose").unwrap() > 1 { println!("\nEnvironment:"); for (key, value) in env::vars() { println!("{}={}", key, value); } } Ok(()) }