Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
Add support for optional args with default text
Browse files Browse the repository at this point in the history
  • Loading branch information
arkpar committed Jan 18, 2017
1 parent cfe5a82 commit b615ac6
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
11 changes: 7 additions & 4 deletions parity/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),

Expand Down Expand Up @@ -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<String>, display dir::default_data_path(), or |c: &Config| otry!(c.parity).base_path.clone().map(Some),
flag_db_path: Option<String>, display dir::CHAINS_PATH, or |c: &Config| otry!(c.parity).db_path.clone().map(Some),
}
}


Expand Down Expand Up @@ -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(),

Expand Down
22 changes: 22 additions & 0 deletions parity/cli/usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -108,6 +113,10 @@ macro_rules! usage {
$(
pub $field: $typ,
)*

$(
pub $field_s: $typ_s,
)*
}

impl Default for Args {
Expand All @@ -120,6 +129,10 @@ macro_rules! usage {
$(
$field: $default.into(),
)*

$(
$field_s: Default::default(),
)*
}
}
}
Expand All @@ -132,6 +145,9 @@ macro_rules! usage {
$(
$field: Option<$typ>,
)*
$(
$field_s: Option<$typ_s>,
)*
}

impl Args {
Expand Down Expand Up @@ -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
}

Expand All @@ -222,6 +241,9 @@ macro_rules! usage {
// "named argument never used" error
// $field = $default,
)*
$(
$field_s = $default_s,
)*
)
}
}
Expand Down
12 changes: 8 additions & 4 deletions parity/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit b615ac6

Please sign in to comment.