1 use clap::{Arg, ArgAction, Command};
2
3 #[test]
indices_mult_opts()4 fn indices_mult_opts() {
5 let m = Command::new("ind")
6 .args_override_self(true)
7 .arg(
8 Arg::new("exclude")
9 .short('e')
10 .action(ArgAction::Set)
11 .num_args(1..)
12 .action(ArgAction::Append),
13 )
14 .arg(
15 Arg::new("include")
16 .short('i')
17 .action(ArgAction::Set)
18 .num_args(1..),
19 )
20 .try_get_matches_from(vec!["ind", "-e", "A", "B", "-i", "B", "C", "-e", "C"])
21 .unwrap();
22
23 assert_eq!(
24 m.indices_of("exclude").unwrap().collect::<Vec<_>>(),
25 [2, 3, 8]
26 );
27 assert_eq!(m.indices_of("include").unwrap().collect::<Vec<_>>(), [5, 6]);
28 }
29
30 #[test]
index_mult_opts()31 fn index_mult_opts() {
32 let m = Command::new("ind")
33 .args_override_self(true)
34 .arg(
35 Arg::new("exclude")
36 .short('e')
37 .action(ArgAction::Set)
38 .num_args(1..)
39 .action(ArgAction::Append),
40 )
41 .arg(
42 Arg::new("include")
43 .short('i')
44 .action(ArgAction::Set)
45 .num_args(1..),
46 )
47 .try_get_matches_from(vec!["ind", "-e", "A", "B", "-i", "B", "C", "-e", "C"])
48 .unwrap();
49
50 assert_eq!(m.index_of("exclude"), Some(2));
51 assert_eq!(m.index_of("include"), Some(5));
52 }
53
54 #[test]
index_flag()55 fn index_flag() {
56 let m = Command::new("ind")
57 .args_override_self(true)
58 .arg(Arg::new("exclude").short('e').action(ArgAction::SetTrue))
59 .arg(Arg::new("include").short('i').action(ArgAction::SetTrue))
60 .try_get_matches_from(vec!["ind", "-e", "-i"])
61 .unwrap();
62
63 assert_eq!(m.index_of("exclude"), Some(1));
64 assert_eq!(m.index_of("include"), Some(2));
65 }
66
67 #[test]
index_flags()68 fn index_flags() {
69 let m = Command::new("ind")
70 .args_override_self(true)
71 .arg(Arg::new("exclude").short('e').action(ArgAction::SetTrue))
72 .arg(Arg::new("include").short('i').action(ArgAction::SetTrue))
73 .try_get_matches_from(vec!["ind", "-e", "-i", "-e", "-e", "-i"])
74 .unwrap();
75
76 assert_eq!(m.index_of("exclude"), Some(4));
77 assert_eq!(m.index_of("include"), Some(5));
78 }
79
80 #[test]
indices_mult_flags()81 fn indices_mult_flags() {
82 let m = Command::new("ind")
83 .args_override_self(true)
84 .arg(Arg::new("exclude").short('e').action(ArgAction::SetTrue))
85 .arg(Arg::new("include").short('i').action(ArgAction::SetTrue))
86 .try_get_matches_from(vec!["ind", "-e", "-i", "-e", "-e", "-i"])
87 .unwrap();
88
89 assert_eq!(m.indices_of("exclude").unwrap().collect::<Vec<_>>(), [4]);
90 assert_eq!(m.indices_of("include").unwrap().collect::<Vec<_>>(), [5]);
91 }
92
93 #[test]
indices_mult_flags_combined()94 fn indices_mult_flags_combined() {
95 let m = Command::new("ind")
96 .args_override_self(true)
97 .arg(Arg::new("exclude").short('e').action(ArgAction::SetTrue))
98 .arg(Arg::new("include").short('i').action(ArgAction::SetTrue))
99 .try_get_matches_from(vec!["ind", "-eieei"])
100 .unwrap();
101
102 assert_eq!(m.indices_of("exclude").unwrap().collect::<Vec<_>>(), [4]);
103 assert_eq!(m.indices_of("include").unwrap().collect::<Vec<_>>(), [5]);
104 }
105
106 #[test]
indices_mult_flags_opt_combined()107 fn indices_mult_flags_opt_combined() {
108 let m = Command::new("ind")
109 .args_override_self(true)
110 .arg(Arg::new("exclude").short('e').action(ArgAction::SetTrue))
111 .arg(Arg::new("include").short('i').action(ArgAction::SetTrue))
112 .arg(Arg::new("option").short('o').action(ArgAction::Set))
113 .try_get_matches_from(vec!["ind", "-eieeio", "val"])
114 .unwrap();
115
116 assert_eq!(m.indices_of("exclude").unwrap().collect::<Vec<_>>(), [4]);
117 assert_eq!(m.indices_of("include").unwrap().collect::<Vec<_>>(), [5]);
118 assert_eq!(m.indices_of("option").unwrap().collect::<Vec<_>>(), [7]);
119 }
120
121 #[test]
indices_mult_flags_opt_combined_eq()122 fn indices_mult_flags_opt_combined_eq() {
123 let m = Command::new("ind")
124 .args_override_self(true)
125 .arg(Arg::new("exclude").short('e').action(ArgAction::SetTrue))
126 .arg(Arg::new("include").short('i').action(ArgAction::SetTrue))
127 .arg(Arg::new("option").short('o').action(ArgAction::Set))
128 .try_get_matches_from(vec!["ind", "-eieeio=val"])
129 .unwrap();
130
131 assert_eq!(m.indices_of("exclude").unwrap().collect::<Vec<_>>(), [4]);
132 assert_eq!(m.indices_of("include").unwrap().collect::<Vec<_>>(), [5]);
133 assert_eq!(m.indices_of("option").unwrap().collect::<Vec<_>>(), [7]);
134 }
135
136 #[test]
indices_mult_opt_value_delim_eq()137 fn indices_mult_opt_value_delim_eq() {
138 let m = Command::new("myapp")
139 .args_override_self(true)
140 .arg(
141 Arg::new("option")
142 .short('o')
143 .action(ArgAction::Set)
144 .value_delimiter(',')
145 .num_args(1..),
146 )
147 .try_get_matches_from(vec!["myapp", "-o=val1,val2,val3"])
148 .unwrap();
149 assert_eq!(
150 m.indices_of("option").unwrap().collect::<Vec<_>>(),
151 [2, 3, 4]
152 );
153 }
154
155 #[test]
indices_mult_opt_value_no_delim_eq()156 fn indices_mult_opt_value_no_delim_eq() {
157 let m = Command::new("myapp")
158 .args_override_self(true)
159 .arg(
160 Arg::new("option")
161 .short('o')
162 .action(ArgAction::Set)
163 .num_args(1..),
164 )
165 .try_get_matches_from(vec!["myapp", "-o=val1,val2,val3"])
166 .unwrap();
167 assert_eq!(m.indices_of("option").unwrap().collect::<Vec<_>>(), [2]);
168 }
169
170 #[test]
indices_mult_opt_mult_flag()171 fn indices_mult_opt_mult_flag() {
172 let m = Command::new("myapp")
173 .args_override_self(true)
174 .arg(Arg::new("option").short('o').action(ArgAction::Append))
175 .arg(Arg::new("flag").short('f').action(ArgAction::SetTrue))
176 .try_get_matches_from(vec!["myapp", "-o", "val1", "-f", "-o", "val2", "-f"])
177 .unwrap();
178
179 assert_eq!(m.indices_of("option").unwrap().collect::<Vec<_>>(), [2, 5]);
180 assert_eq!(m.indices_of("flag").unwrap().collect::<Vec<_>>(), [6]);
181 }
182