1 use crate::{Arg, ArgSettings, ValueHint};
2 
assert_arg(arg: &Arg)3 pub(crate) fn assert_arg(arg: &Arg) {
4     debug!("Arg::_debug_asserts:{}", arg.name);
5 
6     // Self conflict
7     // TODO: this check should be recursive
8     assert!(
9         !arg.blacklist.iter().any(|x| *x == arg.id),
10         "Argument '{}' cannot conflict with itself",
11         arg.name,
12     );
13 
14     if arg.value_hint != ValueHint::Unknown {
15         assert!(
16             arg.is_set(ArgSettings::TakesValue),
17             "Argument '{}' has value hint but takes no value",
18             arg.name
19         );
20 
21         if arg.value_hint == ValueHint::CommandWithArguments {
22             assert!(
23                 arg.is_set(ArgSettings::MultipleValues),
24                 "Argument '{}' uses hint CommandWithArguments and must accept multiple values",
25                 arg.name
26             )
27         }
28     }
29 
30     if arg.index.is_some() {
31         assert!(
32             arg.is_positional(),
33             "Argument '{}' is a positional argument and can't have short or long name versions",
34             arg.name
35         );
36     }
37 
38     if arg.is_set(ArgSettings::Required) {
39         assert!(
40             arg.default_vals.is_empty(),
41             "Argument '{}' is required and can't have a default value",
42             arg.name
43         );
44     }
45 
46     assert_arg_flags(arg);
47 }
48 
assert_arg_flags(arg: &Arg)49 fn assert_arg_flags(arg: &Arg) {
50     use ArgSettings::*;
51 
52     macro_rules! checker {
53         ($a:ident requires $($b:ident)|+) => {
54             if arg.is_set($a) {
55                 let mut s = String::new();
56 
57                 $(
58                     if !arg.is_set($b) {
59                         s.push_str(&format!("  ArgSettings::{} is required when ArgSettings::{} is set.\n", std::stringify!($b), std::stringify!($a)));
60                     }
61                 )+
62 
63                 if !s.is_empty() {
64                     panic!("Argument {:?}\n{}", arg.get_name(), s)
65                 }
66             }
67         }
68     }
69 
70     checker!(ForbidEmptyValues requires TakesValue);
71     checker!(RequireDelimiter requires TakesValue | UseValueDelimiter);
72     checker!(HidePossibleValues requires TakesValue);
73     checker!(AllowHyphenValues requires TakesValue);
74     checker!(RequireEquals requires TakesValue);
75     checker!(Last requires TakesValue);
76     checker!(HideDefaultValue requires TakesValue);
77     checker!(MultipleValues requires TakesValue);
78     checker!(IgnoreCase requires TakesValue);
79     checker!(AllowInvalidUtf8 requires TakesValue);
80 }
81