diff --git a/src/args/arg_builder/flag.rs b/src/args/arg_builder/flag.rs index d8a08194..73d5b086 100644 --- a/src/args/arg_builder/flag.rs +++ b/src/args/arg_builder/flag.rs @@ -91,54 +91,22 @@ impl<'a, 'b, 'z> From<&'z Arg<'a, 'b>> for FlagBuilder<'a, 'b> { format!("The argument '{}' cannot have a specific value set because it doesn't \ have takes_value(true) set", a.name)); + assert!(!a.is_set(ArgSettings::Required), + format!("The argument '{}' cannot be required because it's a flag, perhaps you forgot \ + takes_value(true)?", a.name)); // No need to check for index() or takes_value() as that is handled above - let mut fb = FlagBuilder { + FlagBuilder { name: a.name, short: a.short, long: a.long, help: a.help, + blacklist: a.blacklist.clone(), + overrides: a.overrides.clone(), + requires: a.requires.clone(), + settings: a.settings.clone(), ..Default::default() - }; - if a.is_set(ArgSettings::Multiple) { - fb.settings.set(ArgSettings::Multiple); } - if a.is_set(ArgSettings::Global) { - fb.settings.set(ArgSettings::Global); - } - if a.is_set(ArgSettings::Hidden) { - fb.settings.set(ArgSettings::Hidden); - } - // Check if there is anything in the blacklist (mutually excludes list) and add - // any - // values - if let Some(ref bl) = a.blacklist { - let mut bhs = vec![]; - // without derefing n = &&str - for n in bl { - bhs.push(*n); - } - fb.blacklist = Some(bhs); - } - // Check if there is anything in the requires list and add any values - if let Some(ref r) = a.requires { - let mut rhs = vec![]; - // without derefing n = &&str - for n in r { - rhs.push(*n); - } - fb.requires = Some(rhs); - } - if let Some(ref or) = a.overrides { - let mut bhs = vec![]; - // without derefing n = &&str - for n in or { - bhs.push(*n); - } - fb.overrides = Some(bhs); - } - - fb } } diff --git a/src/args/arg_builder/option.rs b/src/args/arg_builder/option.rs index 6471df68..cc5a6be5 100644 --- a/src/args/arg_builder/option.rs +++ b/src/args/arg_builder/option.rs @@ -74,72 +74,27 @@ impl<'n, 'e> OptBuilder<'n, 'e> { max_vals: a.max_vals, val_names: a.val_names.clone(), val_delim: a.val_delim, + blacklist: a.blacklist.clone(), + overrides: a.overrides.clone(), + requires: a.requires.clone(), + possible_vals: a.possible_vals.clone(), + settings: a.settings.clone(), ..Default::default() }; - if a.is_set(ArgSettings::Multiple) { - ob.settings.set(ArgSettings::Multiple); - } - if a.is_set(ArgSettings::Required) { - ob.settings.set(ArgSettings::Required); - } - if a.is_set(ArgSettings::Global) { - ob.settings.set(ArgSettings::Global); - } - if !a.is_set(ArgSettings::EmptyValues) { - ob.settings.unset(ArgSettings::Global); - } - if a.is_set(ArgSettings::Hidden) { - ob.settings.set(ArgSettings::Hidden); - } if let Some(ref vec) = ob.val_names { if vec.len() > 1 { ob.num_vals = Some(vec.len() as u8); } } - // Check if there is anything in the blacklist (mutually excludes list) and add - // any - // values - if let Some(ref bl) = a.blacklist { - let mut bhs = vec![]; - // without derefing n = &&str - for n in bl { - bhs.push(*n); - } - ob.blacklist = Some(bhs); - } if let Some(ref p) = a.validator { ob.validator = Some(p.clone()); } - // Check if there is anything in the requires list and add any values - if let Some(ref r) = a.requires { - let mut rhs = vec![]; - // without derefing n = &&str - for n in r { - rhs.push(*n); - if a.is_set(ArgSettings::Required) { - reqs.push(*n); - } + // If the arg is required, add all it's requirements to master required list + if a.is_set(ArgSettings::Required) { + if let Some(ref areqs) = a.requires { + for r in areqs { reqs.push(*r); } } - ob.requires = Some(rhs); } - if let Some(ref or) = a.overrides { - let mut bhs = vec![]; - // without derefing n = &&str - for n in or { - bhs.push(*n); - } - ob.overrides = Some(bhs); - } - // Check if there is anything in the possible values and add those as well - if let Some(ref p) = a.possible_vals { - let mut phs = vec![]; - // without derefing n = &&str - for n in p { - phs.push(*n); - } - ob.possible_vals = Some(phs); - } - ob } diff --git a/src/args/arg_builder/positional.rs b/src/args/arg_builder/positional.rs index b3ee0433..e93d74c5 100644 --- a/src/args/arg_builder/positional.rs +++ b/src/args/arg_builder/positional.rs @@ -73,64 +73,28 @@ impl<'n, 'e> PosBuilder<'n, 'e> { min_vals: a.min_vals, max_vals: a.max_vals, val_names: a.val_names.clone(), + blacklist: a.blacklist.clone(), + overrides: a.overrides.clone(), + requires: a.requires.clone(), + possible_vals: a.possible_vals.clone(), help: a.help, val_delim: a.val_delim, + settings: a.settings.clone(), ..Default::default() }; - if a.is_set(ArgSettings::Multiple) || a.num_vals.is_some() || a.max_vals.is_some() || a.min_vals.is_some() { + if a.max_vals.is_some() + || a.min_vals.is_some() + || (a.num_vals.is_some() && a.num_vals.unwrap() > 1) { pb.settings.set(ArgSettings::Multiple); } - if a.is_set(ArgSettings::Required) { - pb.settings.set(ArgSettings::Required); - } - if a.is_set(ArgSettings::Global) { - pb.settings.set(ArgSettings::Global); - } - if a.is_set(ArgSettings::Hidden) { - pb.settings.set(ArgSettings::Hidden); - } - // Check if there is anything in the blacklist (mutually excludes list) and add - // any - // values - if let Some(ref bl) = a.blacklist { - let mut bhs = vec![]; - // without derefing n = &&str - for n in bl { - bhs.push(*n); - } - pb.blacklist = Some(bhs); - } - if let Some(ref or) = a.overrides { - let mut bhs = vec![]; - // without derefing n = &&str - for n in or { - bhs.push(*n); - } - pb.overrides = Some(bhs); - } - // Check if there is anything in the possible values and add those as well - if let Some(ref p) = a.possible_vals { - let mut phs = vec![]; - // without derefing n = &&str - for n in p { - phs.push(*n); - } - pb.possible_vals = Some(phs); - } if let Some(ref p) = a.validator { pb.validator = Some(p.clone()); } - // Check if there is anything in the requires list and add any values - if let Some(ref r) = a.requires { - let mut rhs: Vec<&'e str> = vec![]; - // without derefing n = &&str - for n in r { - rhs.push(n); - if a.is_set(ArgSettings::Required) { - reqs.push(n); - } + // If the arg is required, add all it's requirements to master required list + if a.is_set(ArgSettings::Required) { + if let Some(ref areqs) = a.requires { + for r in areqs { reqs.push(*r); } } - pb.requires = Some(rhs); } pb }