From b615ac69bd6810ea4d21f690ed8af0e00614b2cd Mon Sep 17 00:00:00 2001 From: arkpar Date: Wed, 18 Jan 2017 12:49:44 +0100 Subject: [PATCH] Add support for optional args with default text --- parity/cli/mod.rs | 11 +++++++---- parity/cli/usage.rs | 22 ++++++++++++++++++++++ parity/configuration.rs | 12 ++++++++---- 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/parity/cli/mod.rs b/parity/cli/mod.rs index 2c008623a85..0802cb67f12 100644 --- a/parity/cli/mod.rs +++ b/parity/cli/mod.rs @@ -91,8 +91,6 @@ usage! { flag_no_download: bool = false, or |c: &Config| otry!(c.parity).no_download.clone(), flag_no_consensus: bool = false, or |c: &Config| otry!(c.parity).no_consensus.clone(), flag_chain: String = "homestead", or |c: &Config| otry!(c.parity).chain.clone(), - flag_base_path: String = dir::default_data_path(), or |c: &Config| otry!(c.parity).base_path.clone(), - flag_db_path: String = dir::CHAINS_PATH, or |c: &Config| otry!(c.parity).db_path.clone(), flag_keys_path: String = "$BASE/keys", or |c: &Config| otry!(c.parity).keys_path.clone(), flag_identity: String = "", or |c: &Config| otry!(c.parity).identity.clone(), @@ -289,6 +287,11 @@ usage! { flag_no_color: bool = false, or |c: &Config| otry!(c.misc).color.map(|c| !c).clone(), } + { + // Values with optional default value. + flag_base_path: Option, display dir::default_data_path(), or |c: &Config| otry!(c.parity).base_path.clone().map(Some), + flag_db_path: Option, display dir::CHAINS_PATH, or |c: &Config| otry!(c.parity).db_path.clone().map(Some), + } } @@ -547,8 +550,8 @@ mod tests { flag_no_download: false, flag_no_consensus: false, flag_chain: "xyz".into(), - flag_base_path: "$HOME/.parity".into(), - flag_db_path: "$HOME/.parity/chains".into(), + flag_base_path: Some("$HOME/.parity".into()), + flag_db_path: Some("$HOME/.parity/chains".into()), flag_keys_path: "$HOME/.parity/keys".into(), flag_identity: "".into(), diff --git a/parity/cli/usage.rs b/parity/cli/usage.rs index 235087168c9..1f9de6f17bd 100644 --- a/parity/cli/usage.rs +++ b/parity/cli/usage.rs @@ -43,6 +43,11 @@ macro_rules! usage { $field:ident : $typ:ty = $default:expr, or $from_config:expr, )* } + { + $( + $field_s:ident : $typ_s:ty, display $default_s:expr, or $from_config_s:expr, + )* + } ) => { use toml; use std::{fs, io, process}; @@ -108,6 +113,10 @@ macro_rules! usage { $( pub $field: $typ, )* + + $( + pub $field_s: $typ_s, + )* } impl Default for Args { @@ -120,6 +129,10 @@ macro_rules! usage { $( $field: $default.into(), )* + + $( + $field_s: Default::default(), + )* } } } @@ -132,6 +145,9 @@ macro_rules! usage { $( $field: Option<$typ>, )* + $( + $field_s: Option<$typ_s>, + )* } impl Args { @@ -206,6 +222,9 @@ macro_rules! usage { $( args.$field = self.$field.or_else(|| $from_config(&config)).unwrap_or_else(|| $default.into()); )* + $( + args.$field_s = self.$field_s.or_else(|| $from_config_s(&config)).unwrap_or(None); + )* args } @@ -222,6 +241,9 @@ macro_rules! usage { // "named argument never used" error // $field = $default, )* + $( + $field_s = $default_s, + )* ) } } diff --git a/parity/configuration.rs b/parity/configuration.rs index e818e3e1c64..9a0b8d831d8 100644 --- a/parity/configuration.rs +++ b/parity/configuration.rs @@ -734,10 +734,14 @@ impl Configuration { use util::path; let local_path = default_local_path(); - let data_path = replace_home("", self.args.flag_datadir.as_ref().unwrap_or(&self.args.flag_base_path)); - - //TODO: refactor this for better detection if base_path is custom. - let base_db_path = if data_path != default_data_path() && self.args.flag_db_path == dir::CHAINS_PATH { "$BASE/chains".to_owned() } else { self.args.flag_db_path.clone() }; + let base_path = self.args.flag_base_path.as_ref().map_or_else(|| default_data_path(), |s| s.clone()); + let data_path = replace_home("", self.args.flag_datadir.as_ref().unwrap_or(&base_path)); + let base_db_path = if self.args.flag_base_path.is_some() && self.args.flag_db_path.is_none() { + // If base_path is set and db_path is not we default to base path subdir instead of LOCAL. + "$BASE/chains" + } else { + self.args.flag_db_path.as_ref().map_or(dir::CHAINS_PATH, |s| &s) + }; let db_path = replace_home_for_db(&data_path, &local_path, &base_db_path); let keys_path = replace_home(&data_path, &self.args.flag_keys_path);