mirror of
https://github.com/clap-rs/clap
synced 2025-01-05 17:28:42 +00:00
refactor: removes some unnecessary code checks
This commit is contained in:
parent
f1876388a7
commit
e494672ffc
3 changed files with 29 additions and 142 deletions
|
@ -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 \
|
format!("The argument '{}' cannot have a specific value set because it doesn't \
|
||||||
have takes_value(true) set",
|
have takes_value(true) set",
|
||||||
a.name));
|
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
|
// No need to check for index() or takes_value() as that is handled above
|
||||||
|
|
||||||
let mut fb = FlagBuilder {
|
FlagBuilder {
|
||||||
name: a.name,
|
name: a.name,
|
||||||
short: a.short,
|
short: a.short,
|
||||||
long: a.long,
|
long: a.long,
|
||||||
help: a.help,
|
help: a.help,
|
||||||
|
blacklist: a.blacklist.clone(),
|
||||||
|
overrides: a.overrides.clone(),
|
||||||
|
requires: a.requires.clone(),
|
||||||
|
settings: a.settings.clone(),
|
||||||
..Default::default()
|
..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
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -74,72 +74,27 @@ impl<'n, 'e> OptBuilder<'n, 'e> {
|
||||||
max_vals: a.max_vals,
|
max_vals: a.max_vals,
|
||||||
val_names: a.val_names.clone(),
|
val_names: a.val_names.clone(),
|
||||||
val_delim: a.val_delim,
|
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()
|
..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 let Some(ref vec) = ob.val_names {
|
||||||
if vec.len() > 1 {
|
if vec.len() > 1 {
|
||||||
ob.num_vals = Some(vec.len() as u8);
|
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 {
|
if let Some(ref p) = a.validator {
|
||||||
ob.validator = Some(p.clone());
|
ob.validator = Some(p.clone());
|
||||||
}
|
}
|
||||||
// Check if there is anything in the requires list and add any values
|
// If the arg is required, add all it's requirements to master required list
|
||||||
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) {
|
if a.is_set(ArgSettings::Required) {
|
||||||
reqs.push(*n);
|
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
|
ob
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -73,65 +73,29 @@ impl<'n, 'e> PosBuilder<'n, 'e> {
|
||||||
min_vals: a.min_vals,
|
min_vals: a.min_vals,
|
||||||
max_vals: a.max_vals,
|
max_vals: a.max_vals,
|
||||||
val_names: a.val_names.clone(),
|
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,
|
help: a.help,
|
||||||
val_delim: a.val_delim,
|
val_delim: a.val_delim,
|
||||||
|
settings: a.settings.clone(),
|
||||||
..Default::default()
|
..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);
|
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 {
|
if let Some(ref p) = a.validator {
|
||||||
pb.validator = Some(p.clone());
|
pb.validator = Some(p.clone());
|
||||||
}
|
}
|
||||||
// Check if there is anything in the requires list and add any values
|
// If the arg is required, add all it's requirements to master required list
|
||||||
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) {
|
if a.is_set(ArgSettings::Required) {
|
||||||
reqs.push(n);
|
if let Some(ref areqs) = a.requires {
|
||||||
|
for r in areqs { reqs.push(*r); }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pb.requires = Some(rhs);
|
|
||||||
}
|
|
||||||
pb
|
pb
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue