1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
use crate::{
PickerArgInfo,
parselib::{MaskedArg, ParserStyle},
};
#[inline(always)]
pub fn build_possible_flags(style: &ParserStyle, arg_info: &PickerArgInfo) -> Vec<String> {
let mut possible_flags = vec![];
if let Some(short) = arg_info.short {
possible_flags.push(style.flag_string(short));
}
if let Some(long) = arg_info.long {
let converted = style.naming_case.convert(long.to_string());
possible_flags.push(style.flag_string(&converted));
}
if let Some(aliases) = &arg_info.alias {
for alias in aliases {
let converted = style.naming_case.convert(alias.to_string());
possible_flags.push(style.flag_string(&converted));
}
}
possible_flags
}
/// Extract a single value from the raw strings tagged by [`SingleMatcher`](crate::parselib::SingleMatcher).
///
/// Returns `None` if no value is available (empty slice),
/// the inline value after the style separator if present (eq mode),
/// or the value directly (positional or flag-following).
///
/// This is the standard `pick` helper for all `Single`-type
/// [`Pickable`](crate::Pickable) implementations.
#[must_use]
pub fn seek_single<'a>(raw_strs: &'a [&'a str]) -> Option<&'a str> {
match raw_strs.len() {
0 => None,
1 => {
let s = raw_strs[0];
let sep = ParserStyle::global_style().value_separator;
if let Some(pos) = s.rfind(sep) {
Some(&s[pos + 1..])
} else {
Some(s)
}
}
_ => Some(raw_strs[1]),
}
}
/// Seeks the index of the end-of-options marker (`--`) in the argument list.
///
/// This function searches for the standard end-of-options separator (`--`)
/// in the given argument list, respecting the parser's style settings
/// (e.g., case sensitivity). The end-of-options marker indicates that all
/// subsequent arguments should be treated as positional arguments, not flags.
#[must_use]
pub fn seek_end_of_options(args: &[MaskedArg], style: &ParserStyle) -> Option<usize> {
args.iter()
.find(|arg| {
if style.case_sensitive {
arg.raw == style.end_of_options
} else {
arg.raw.eq_ignore_ascii_case(style.end_of_options)
}
})
.map(|arg| arg.raw_idx)
}
/// Seeks arguments in `args` that are exactly equal to the given `string`.
///
/// Returns the indices of matching arguments.
#[must_use]
#[inline(always)]
pub fn seek_eq(args: &[MaskedArg], string: &str, case_sensitive: bool) -> Vec<usize> {
args.iter()
.filter(|arg| {
if case_sensitive {
arg.raw == string
} else {
arg.raw.eq_ignore_ascii_case(string)
}
})
.map(|arg| arg.raw_idx)
.collect()
}
/// Seeks arguments in `args` that contain the given `string` as a substring.
///
/// Returns the indices of matching arguments.
#[must_use]
#[inline(always)]
pub fn seek_contains(args: &[MaskedArg], string: &str, case_sensitive: bool) -> Vec<usize> {
args.iter()
.filter(|arg| {
if case_sensitive {
arg.raw.contains(string)
} else {
arg.raw.to_lowercase().contains(&string.to_lowercase())
}
})
.map(|arg| arg.raw_idx)
.collect()
}
/// Seeks arguments in `args` that start with the given `string`.
///
/// Returns the indices of matching arguments.
#[must_use]
#[inline(always)]
pub fn seek_start_with(args: &[MaskedArg], string: &str, case_sensitive: bool) -> Vec<usize> {
args.iter()
.filter(|arg| {
if case_sensitive {
arg.raw.starts_with(string)
} else {
arg.raw.to_lowercase().starts_with(&string.to_lowercase())
}
})
.map(|arg| arg.raw_idx)
.collect()
}
/// Seeks arguments in `args` that end with the given `string`.
///
/// Returns the indices of matching arguments.
#[must_use]
#[inline(always)]
pub fn seek_end_with(args: &[MaskedArg], string: &str, case_sensitive: bool) -> Vec<usize> {
args.iter()
.filter(|arg| {
if case_sensitive {
arg.raw.ends_with(string)
} else {
arg.raw.to_lowercase().ends_with(&string.to_lowercase())
}
})
.map(|arg| arg.raw_idx)
.collect()
}
/// Seeks arguments in `args` that are exactly equal to any of the given `strings`.
///
/// Returns the indices of matching arguments.
#[must_use]
#[inline(always)]
pub fn multi_seek_eq(args: &[MaskedArg], strings: &[&str], case_sensitive: bool) -> Vec<usize> {
args.iter()
.filter(|arg| {
if case_sensitive {
strings.contains(&arg.raw)
} else {
strings.iter().any(|s| arg.raw.eq_ignore_ascii_case(s))
}
})
.map(|arg| arg.raw_idx)
.collect()
}
/// Seeks arguments in `args` that contain any of the given `strings` as a substring.
///
/// Returns the indices of matching arguments.
#[must_use]
#[inline(always)]
pub fn multi_seek_contains(
args: &[MaskedArg],
strings: &[&str],
case_sensitive: bool,
) -> Vec<usize> {
args.iter()
.filter(|arg| {
if case_sensitive {
strings.iter().any(|s| arg.raw.contains(s))
} else {
let lower_raw = arg.raw.to_lowercase();
strings
.iter()
.any(|s| lower_raw.contains(&s.to_lowercase()))
}
})
.map(|arg| arg.raw_idx)
.collect()
}
/// Seeks arguments in `args` that start with any of the given `strings`.
///
/// Returns the indices of matching arguments.
#[must_use]
#[inline(always)]
pub fn multi_seek_start_with(
args: &[MaskedArg],
strings: &[&str],
case_sensitive: bool,
) -> Vec<usize> {
args.iter()
.filter(|arg| {
if case_sensitive {
strings.iter().any(|s| arg.raw.starts_with(s))
} else {
let lower_raw = arg.raw.to_lowercase();
strings
.iter()
.any(|s| lower_raw.starts_with(&s.to_lowercase()))
}
})
.map(|arg| arg.raw_idx)
.collect()
}
/// Seeks arguments in `args` that end with any of the given `strings`.
///
/// Returns the indices of matching arguments.
#[must_use]
#[inline(always)]
pub fn multi_seek_end_with(
args: &[MaskedArg],
strings: &[&str],
case_sensitive: bool,
) -> Vec<usize> {
args.iter()
.filter(|arg| {
if case_sensitive {
strings.iter().any(|s| arg.raw.ends_with(s))
} else {
let lower_raw = arg.raw.to_lowercase();
strings
.iter()
.any(|s| lower_raw.ends_with(&s.to_lowercase()))
}
})
.map(|arg| arg.raw_idx)
.collect()
}
/// Converts a `&Vec<String>` into a `Vec<&str>` by borrowing each string's slice.
///
/// This is useful for converting owned `String` vectors into borrowed `&str` slices
/// for functions that take `&[&str]` or similar parameters.
#[must_use]
#[inline(always)]
#[doc(hidden)]
pub fn vec_string_to_vec_str(input: &[String]) -> Vec<&str> {
input.iter().map(|s| s.as_str()).collect()
}
/// Converts a `&Vec<String>` into a `Vec<&str>` by borrowing each string's slice.
///
/// This is useful for converting owned `String` vectors into borrowed `&str` slices
/// for functions that take `&[&str]` or similar parameters.
#[macro_export]
#[doc(hidden)]
macro_rules! vec_string_slice {
($v:expr) => {
$v.iter()
.map(|s| s.as_str())
.collect::<Vec<&str>>()
.as_slice()
};
}
/// Gets the first element from a vector of seek results, if any.
///
/// Returns `Some(index)` if the vector is non-empty, otherwise `None`.
#[must_use]
#[inline(always)]
pub fn get_seeked_first(seeked: Vec<usize>) -> Option<usize> {
seeked.into_iter().next()
}
|