From 4094a6a92e490e97ad61591f0afe373303c33057 Mon Sep 17 00:00:00 2001 From: Ralf Zerres Date: Tue, 9 Jun 2020 15:44:06 +0200 Subject: [PATCH] parse_args: commandline parsing module Signed-off-by: Ralf Zerres --- src/parse_args.rs | 138 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 src/parse_args.rs diff --git a/src/parse_args.rs b/src/parse_args.rs new file mode 100644 index 0000000..8cab549 --- /dev/null +++ b/src/parse_args.rs @@ -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> { + //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 ") + .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::("verbose").unwrap()); + if v.get::("verbose").unwrap() > 0 { + println!( + "config_file: {:?}", + v.get::("config_file").unwrap_or_default() + ); + println!( + "db_driver: {:?}", + v.get::("db_driver").unwrap_or_default() + ); + 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(()) +}