224 lines
7.3 KiB
Rust
224 lines
7.3 KiB
Rust
// SPDX-License-Identifier: (0BSD or MIT)
|
|
/*
|
|
* advotracker - Hotline tackingtool for Advocats
|
|
*
|
|
* Copyright 2020 Ralf Zerres <ralf.zerres@networkx.de>
|
|
*/
|
|
|
|
// parse CLI commandline arguments with clap
|
|
use clap::{crate_authors, crate_description, crate_name, crate_version, App, Arg};
|
|
|
|
//use log::{debug, info, trace, warn};
|
|
use std::env;
|
|
use tracing::trace;
|
|
use viperus::Viperus;
|
|
|
|
/// Parse the commandline arguments and preset default values
|
|
/// Precedence: defaults -> config-file -> environment -> commandline
|
|
pub fn parse_args(v: &mut Viperus) -> Result<(), Box<dyn std::error::Error>> {
|
|
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("POLLFNR_WOECHENTLICH.txt"));
|
|
v.add_default("export_file", String::from(""));
|
|
v.add_default("test_policy_number", String::from("9999999992"));
|
|
v.add_default(
|
|
"to_email_address_file",
|
|
String::from("Allianz RA-Hotline <smr-rahotline@allianz.de>"),
|
|
);
|
|
v.add_default(
|
|
"from_email_address_file",
|
|
String::from("Allianz-Hotline RA-Hiedemann <azhotline@hiedemann.de>"),
|
|
);
|
|
//v.add_default("username", String::from("nctalkbot"));
|
|
//v.add_default("password", String::from("botpassword"));
|
|
v.add_default("verbose", 0);
|
|
|
|
// CLI arguments are defined inline
|
|
let matches = App::new("csv-test")
|
|
.name(crate_name!())
|
|
.version(crate_version!())
|
|
.author(crate_authors!())
|
|
.about(crate_description!())
|
|
.after_help(
|
|
"
|
|
Testprogramm: Allianz Online-Beratung Im/Export CSV-Daten
|
|
Direct-Call via IVR-System (Interactive Voice Response)
|
|
SMR Deckungssummen-Prüfung: 089 92529 60211
|
|
SMR Unerledigt: 089 92529 60222",
|
|
)
|
|
.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")
|
|
.default_value("csv_import.ron")
|
|
.takes_value(true),
|
|
)
|
|
.arg(
|
|
Arg::with_name("importFile")
|
|
.short("i")
|
|
.long("importFile")
|
|
.help("Select source file for the csv-import")
|
|
.default_value("POLLFNR_WOECHENTLICH.txt")
|
|
//.default_value("POLLFNR_MINI.txt")
|
|
.takes_value(true),
|
|
)
|
|
.arg(
|
|
Arg::with_name("exportFile")
|
|
.short("e")
|
|
.long("exportFile")
|
|
.help("Select target file for the csv-export")
|
|
.default_value("RA-Hiedemann_DirectCall.txt")
|
|
.takes_value(true),
|
|
)
|
|
.arg(
|
|
Arg::with_name("fromEmailAddress")
|
|
.short("f")
|
|
.long("fromEmailAddress")
|
|
.help("Select the sender email-address (From:)")
|
|
.default_value("Allianz-Hotline RA-Hiedemann <azhotline@hiedemann.de>")
|
|
.takes_value(true),
|
|
)
|
|
.arg(
|
|
Arg::with_name("toEmailAddress")
|
|
.short("t")
|
|
.long("toEmailAddress")
|
|
.help("Select the target email-address (To:)")
|
|
.default_value("Allianz RA-Hotline <smr-rahotline@allianz.de>")
|
|
.takes_value(true),
|
|
)
|
|
.arg(
|
|
Arg::with_name("testPolicyNumber")
|
|
.short("p")
|
|
.long("testPolicyNumber")
|
|
.help("Test validity of given policy number")
|
|
//.default_value("")
|
|
.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
|
|
v.cache(true);
|
|
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("exportFile", "export_file");
|
|
v.bond_clap("toEmailAddress", "to_email_address");
|
|
v.bond_clap("fromEmailAddress", "from_email_address");
|
|
v.bond_clap("testPolicyNumber", "test_policy_number");
|
|
//v.bond_clap("username", "username");
|
|
//v.bond_clap("password", "password");
|
|
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!(
|
|
"import_file: {:?}",
|
|
v.get::<String>("import_file").unwrap_or_default()
|
|
);
|
|
println!(
|
|
"export_file: {:?}",
|
|
v.get::<String>("export_file").unwrap_or_default()
|
|
);
|
|
println!(
|
|
"to_email_address: {:?}",
|
|
v.get::<String>("to_email_address").unwrap_or_default()
|
|
);
|
|
println!(
|
|
"from_email_address: {:?}",
|
|
v.get::<String>("from_email_address").unwrap_or_default()
|
|
);
|
|
println!(
|
|
"test_policy_number: {:?}",
|
|
v.get::<String>("test_policy_number").unwrap_or_default()
|
|
);
|
|
// println!(
|
|
// "username: {:?}",
|
|
// v.get::<String>("username").unwrap_or_default()
|
|
// );
|
|
// println!(
|
|
// "password: {:?}",
|
|
// v.get::<String>("password").unwrap_or_default()
|
|
// ); // only for testing now
|
|
|
|
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(())
|
|
}
|