Skip to content

Commit

Permalink
Move checker to macro(better panic message)
Browse files Browse the repository at this point in the history
  • Loading branch information
ldm0 committed Feb 24, 2021
1 parent 4e477f5 commit e186274
Showing 1 changed file with 25 additions and 33 deletions.
58 changes: 25 additions & 33 deletions src/build/arg/debug_asserts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,38 +48,30 @@ pub(crate) fn assert_arg(arg: &Arg) {

fn assert_app_flag(arg: &Arg) {
use ArgSettings::*;
if arg.is_set(AllowEmptyValues) {
assert!(arg.is_set(TakesValue));
}
if arg.is_set(RequireDelimiter) {
assert!(arg.is_set(TakesValue));
assert!(arg.is_set(UseValueDelimiter));
}
if arg.is_set(HidePossibleValues) {
assert!(arg.is_set(TakesValue));
}
if arg.is_set(AllowHyphenValues) {
assert!(arg.is_set(TakesValue));
}
if arg.is_set(RequireEquals) {
assert!(arg.is_set(TakesValue));
}
if arg.is_set(Last) {
assert!(arg.is_set(TakesValue));
}
if arg.is_set(HideDefaultValue) {
assert!(arg.is_set(TakesValue));
}
if arg.is_set(MultipleValues) {
assert!(arg.is_set(TakesValue));
}
if arg.is_set(HideEnv) {
assert!(arg.is_set(TakesValue));
}
if arg.is_set(HideEnvValues) {
assert!(arg.is_set(TakesValue));
}
if arg.is_set(IgnoreCase) {
assert!(arg.is_set(TakesValue));
macro_rules! checker {
($a:ident requires $($b:ident)|+) => {
if arg.is_set($a) {
let mut s = String::new();
$(
if !arg.is_set($b) {
s.push_str(&format!("\nArgSettings::{} is required when ArgSettings::{} is on.\n", std::stringify!($b), std::stringify!($a)));
}
)+
if !s.is_empty() {
panic!("{}", s)
}
}
}
}
checker!(AllowEmptyValues requires TakesValue);
checker!(RequireDelimiter requires TakesValue | UseValueDelimiter);
checker!(HidePossibleValues requires TakesValue);
checker!(AllowHyphenValues requires TakesValue);
checker!(RequireEquals requires TakesValue);
checker!(Last requires TakesValue);
checker!(HideDefaultValue requires TakesValue);
checker!(MultipleValues requires TakesValue);
checker!(HideEnv requires TakesValue);
checker!(HideEnvValues requires TakesValue);
checker!(IgnoreCase requires TakesValue);
}

0 comments on commit e186274

Please sign in to comment.