pareg_core/lib.rs
1mod arg_into;
2mod by_ref;
3pub mod check;
4mod err;
5mod from_arg;
6pub(crate) mod impl_all;
7mod pareg_ref;
8mod parsef;
9mod parsers;
10pub mod proc;
11pub mod reader;
12mod starts;
13
14pub use crate::{
15 arg_into::*,
16 by_ref::*,
17 err::*,
18 from_arg::*,
19 pareg_ref::*,
20 parsef::*,
21 parsers::*,
22 reader::{FromRead, ReadFmt, Reader, ReaderChars, SetFromRead},
23};
24
25use std::{borrow::Cow, cell::Cell, env, ops::Range};
26
27/// Helper for parsing arguments.
28///
29/// The preffered way to use this is to call [`Pareg::ref_mut`] to get
30/// [`ParegRef`] structure, which can be than used to parse the data:
31///
32/// This may be used to own the argument data. You can than get [`ParegRef`]
33/// structure by calling [`Pareg::ref_mut`] to pass around and do the parsing,
34/// because it can be less strict about lifetimes since it refers to the
35/// original pareg structure and so it is more powerful.
36pub struct Pareg {
37 args: Vec<String>,
38 cur: Cell<usize>,
39}
40
41impl From<Vec<String>> for Pareg {
42 fn from(value: Vec<String>) -> Self {
43 Self {
44 args: value,
45 cur: 0.into(),
46 }
47 }
48}
49
50impl Pareg {
51 /// Create [`Pareg`] from vector of arguments. The first argument is NOT
52 /// skipped.
53 #[inline]
54 pub fn new(args: Vec<String>) -> Self {
55 args.into()
56 }
57
58 /// Create [`Pareg`] from [`env::args`], the first argument is skipped.
59 #[inline]
60 pub fn args() -> Self {
61 Self {
62 args: env::args().collect(),
63 cur: 1.into(),
64 }
65 }
66
67 /// DO NOT MAKE THIS PIBLIC. This can be public only if the lifetime
68 /// captured inside [`ParegRef`] borrows the original [`Pareg`] mutably.
69 #[inline(always)]
70 pub(crate) fn inner(&self) -> ParegRef<'_> {
71 ParegRef::new(&self.args, Cow::Borrowed(&self.cur))
72 }
73
74 /// Gets mutable reference to self. Mutating the resulting pareg ref will
75 /// also mutate this pareg.
76 #[inline]
77 pub fn get_mut_ref(&mut self) -> ParegRef<'_> {
78 // It is OK to pass the inner reference out, because this will borrow
79 // [`Pareg`] mutably and so the captured reference in [`ParegRef`]
80 // also borrows [`Pareg`] mutably.
81 self.inner()
82 }
83
84 /// Gets immutable reference to self. Mutating the resulting pareg ref will
85 /// not mutate this pareg.
86 pub fn get_ref(&self) -> ParegRef<'_> {
87 ParegRef::new(&self.args, Cow::Owned(self.cur.clone()))
88 }
89
90 /// Get the next argument
91 // Iterator impl is not possible because the returned values are borrowed.
92 #[allow(clippy::should_implement_trait)]
93 #[inline]
94 pub fn next(&mut self) -> Option<&str> {
95 self.inner().next()
96 }
97
98 /// Equivalent to calling next `cnt` times.
99 #[inline]
100 pub fn skip_args(&mut self, cnt: usize) -> Option<&str> {
101 self.inner().skip_args(cnt)
102 }
103
104 /// Skip all remaining arguments and return the last.
105 #[inline]
106 pub fn skip_all(&mut self) -> Option<&str> {
107 self.inner().skip_all()
108 }
109
110 /// Jump so that the argument at index `idx` is the next argument. Gets the
111 /// argument at `idx - 1`.
112 #[inline]
113 pub fn jump(&mut self, idx: usize) -> Option<&str> {
114 self.inner().jump(idx)
115 }
116
117 /// Jump to the zeroth argument.
118 #[inline]
119 pub fn reset(&mut self) {
120 self.inner().reset()
121 }
122
123 /// Get the last returned argument.
124 #[inline]
125 pub fn cur(&self) -> Option<&str> {
126 self.inner().cur()
127 }
128
129 /// Gets all the arguments (including the first one).
130 #[inline]
131 pub fn all_args(&self) -> &[String] {
132 self.inner().all_args()
133 }
134
135 /// Gets the remaining arguments (not including the current).
136 #[inline]
137 pub fn remaining(&self) -> &[String] {
138 self.inner().remaining()
139 }
140
141 /// Gets the remaining arguments (including the current).
142 #[inline]
143 pub fn cur_remaining(&self) -> &[String] {
144 self.inner().cur_remaining()
145 }
146
147 /// Get value that will be returned with the next call to `next`.
148 #[inline]
149 pub fn peek(&self) -> Option<&str> {
150 self.inner().peek()
151 }
152
153 /// Get the index of the next argument.
154 #[inline]
155 pub fn next_idx(&self) -> Option<usize> {
156 self.inner().next_idx()
157 }
158
159 /// Get index of the current argument.
160 #[inline]
161 pub fn cur_idx(&self) -> Option<usize> {
162 self.inner().cur_idx()
163 }
164
165 /// Get argument at the given index.
166 #[inline]
167 pub fn get(&self, idx: usize) -> Option<&str> {
168 self.inner().get(idx)
169 }
170
171 /// Perform manual parsing on the next argument. This is will make the
172 /// errors have better messages than just doing the parsing without
173 /// [`Pareg`].
174 ///
175 /// `pareg.next_manual(foo)` is equivalent to
176 /// `pareg.map_err(foo(pareg.next()))`, except it has no issues with
177 /// lifetimes.
178 ///
179 /// # Examples
180 /// ```rust
181 /// use pareg_core::{Pareg, key_val_arg};
182 /// let args = ["-D10=0.25"];
183 /// let mut args = Pareg::new(args.iter().map(|a| a.to_string()).collect());
184 ///
185 /// let res: (usize, f32) = args.next_manual(|s| {
186 /// key_val_arg(s.strip_prefix("-D").unwrap(), '=')
187 /// }).unwrap();
188 /// assert_eq!((10, 0.25), res);
189 /// ```
190 #[inline]
191 pub fn next_manual<'a, T: 'a>(
192 &'a mut self,
193 f: impl Fn(&'a str) -> Result<T>,
194 ) -> Result<T> {
195 self.inner().next_manual(f)
196 }
197
198 /// Perform manual parsing on the next argument. This is will make the
199 /// errors have better messages than just doing the parsing without
200 /// [`Pareg`].
201 ///
202 /// `pareg.cur_manual(foo)` is equivalent to
203 /// `pareg.map_err(foo(pareg.cur()))`.
204 ///
205 /// # Examples
206 /// ```rust
207 /// use pareg_core::{Pareg, key_val_arg};
208 /// let args = ["-D10=0.25"];
209 /// let mut args = Pareg::new(args.iter().map(|a| a.to_string()).collect());
210 ///
211 /// args.next();
212 ///
213 /// let res: (usize, f32) = args.cur_manual(|s| {
214 /// key_val_arg(s.strip_prefix("-D").unwrap(), '=')
215 /// }).unwrap();
216 /// assert_eq!((10, 0.25), res);
217 /// ```
218 #[inline]
219 pub fn cur_manual<'a, T: 'a>(
220 &'a self,
221 f: impl Fn(&'a str) -> Result<T>,
222 ) -> Result<T> {
223 self.inner().cur_manual(f)
224 }
225
226 /// Parses the next value in the iterator.
227 ///
228 /// # Examples
229 /// ```rust
230 /// use pareg_core::Pareg;
231 ///
232 /// let args = ["hello", "10", "0.25", "always"];
233 /// let mut args = Pareg::new(args.iter().map(|a| a.to_string()).collect());
234 ///
235 /// assert_eq!("hello", args.next_arg::<&str>().unwrap());
236 /// assert_eq!(10, args.next_arg::<usize>().unwrap());
237 /// assert_eq!(0.25, args.next_arg::<f64>().unwrap());
238 /// ```
239 #[inline]
240 pub fn next_arg<'a, T: FromArg<'a>>(&'a mut self) -> Result<T> {
241 self.inner().next_arg()
242 }
243
244 /// Uses the function [`key_mval_arg`] on the next argument.
245 ///
246 /// If sep was `'='`, parses `"key=value"` into `"key"` and `value` that is
247 /// also parsed to the given type.
248 ///
249 /// In case that there is no `'='`, value is `None`.
250 ///
251 /// # Examples
252 /// ```rust
253 /// use pareg_core::Pareg;
254 ///
255 /// let args = ["key=value", "5:0.25", "only_key"];
256 /// let mut args = Pareg::new(args.iter().map(|a| a.to_string()).collect());
257 ///
258 /// assert_eq!(
259 /// ("key", Some("value")),
260 /// args.next_key_mval::<&str, &str>('=').unwrap()
261 /// );
262 /// assert_eq!(
263 /// (5, Some(0.25)),
264 /// args.next_key_mval::<i32, f64>(':').unwrap()
265 /// );
266 /// assert_eq!(
267 /// ("only_key".to_owned(), None),
268 /// args.next_key_mval::<String, &str>('=').unwrap()
269 /// );
270 /// ```
271 #[inline]
272 pub fn next_key_mval<'a, K: FromArg<'a>, V: FromArg<'a>>(
273 &'a mut self,
274 sep: char,
275 ) -> Result<(K, Option<V>)> {
276 self.inner().next_key_mval(sep)
277 }
278
279 /// Uses the function [`key_val_arg`] on the next value.
280 ///
281 /// If sep was `'='`, parses `"key=value"` into `"key"` and `value` that is
282 /// also parsed to the given type.
283 ///
284 /// In case that there is no `'='`, returns [`ArgError::NoValue`].
285 ///
286 /// # Examples
287 /// ```rust
288 /// use pareg_core::Pareg;
289 ///
290 /// let args = ["key=value", "5:0.25"];
291 /// let mut args = Pareg::new(args.iter().map(|a| a.to_string()).collect());
292 ///
293 /// assert_eq!(
294 /// ("key", "value"),
295 /// args.next_key_val::<&str, &str>('=').unwrap()
296 /// );
297 /// assert_eq!(
298 /// (5, 0.25),
299 /// args.next_key_val::<i32, f64>(':').unwrap()
300 /// );
301 /// ```
302 #[inline]
303 pub fn next_key_val<'a, K: FromArg<'a>, V: FromArg<'a>>(
304 &'a mut self,
305 sep: char,
306 ) -> Result<(K, V)> {
307 self.inner().next_key_val(sep)
308 }
309
310 /// Uses the function [`bool_arg`] on the next value.
311 ///
312 /// Parse bool value in a specific way. If the value of lowercase `arg` is
313 /// equal to `t` returns true, if it is equal to `f` returns false and
314 /// otherwise returns error.
315 ///
316 /// # Examples
317 /// ```rust
318 /// use pareg_core::Pareg;
319 ///
320 /// let args = ["true", "yes", "never"];
321 /// let mut args = Pareg::new(args.iter().map(|a| a.to_string()).collect());
322 ///
323 /// assert_eq!(true, args.next_bool("true", "false").unwrap());
324 /// assert_eq!(true, args.next_bool("yes", "no").unwrap());
325 /// assert_eq!(false, args.next_bool("always", "never").unwrap());
326 /// ```
327 #[inline]
328 pub fn next_bool(&mut self, t: &str, f: &str) -> Result<bool> {
329 self.inner().next_bool(t, f)
330 }
331
332 /// Uses the function [`opt_bool_arg`] on the next argument.
333 ///
334 /// Parse bool value in a specific way. If the value of lowercase `arg` is
335 /// equal to `t` returns true, if it is equal to `f` returns false and
336 /// if it is equal to `n` returns [`None`]. Otherwise returns error.
337 ///
338 /// # Examples
339 /// ```rust
340 /// use pareg_core::Pareg;
341 ///
342 /// let args = ["always", "never", "auto"];
343 /// let mut args = Pareg::new(args.iter().map(|a| a.to_string()).collect());
344 ///
345 /// assert_eq!(
346 /// Some(true),
347 /// args.next_opt_bool("always", "never", "auto").unwrap()
348 /// );
349 /// assert_eq!(
350 /// Some(false),
351 /// args.next_opt_bool("always", "never", "auto").unwrap()
352 /// );
353 /// assert_eq!(
354 /// None,
355 /// args.next_opt_bool("always", "never", "auto").unwrap()
356 /// );
357 /// ```
358 #[inline]
359 pub fn next_opt_bool(
360 &mut self,
361 t: &str,
362 f: &str,
363 n: &str,
364 ) -> Result<Option<bool>> {
365 self.inner().next_opt_bool(t, f, n)
366 }
367
368 /// Uses the function [`key_arg`] on the next value.
369 ///
370 /// If sep was `'='`, parses `"key=value"` into `"key"` and discards `value`.
371 ///
372 /// In case that there is no `'='`, parses the whole input.
373 ///
374 /// # Examples
375 /// ```rust
376 /// use pareg_core::Pareg;
377 ///
378 /// let args = ["key=value", "5:0.25"];
379 /// let mut args = Pareg::new(args.iter().map(|a| a.to_string()).collect());
380 ///
381 /// assert_eq!(
382 /// "key",
383 /// args.next_key::<&str>('=').unwrap()
384 /// );
385 /// assert_eq!(
386 /// 5,
387 /// args.next_key::<i32>(':').unwrap()
388 /// );
389 /// ```
390 #[inline]
391 pub fn next_key<'a, T: FromArg<'a>>(&'a mut self, sep: char) -> Result<T> {
392 self.inner().next_key(sep)
393 }
394
395 /// Uses the function [`val_arg`] on the next value.
396 ///
397 /// If sep was `'='`, parses `"key=value"` into `value` that is parsed to the
398 /// given type.
399 ///
400 /// In case that there is no `'='`, returns [`ArgError::NoValue`].
401 ///
402 /// # Examples
403 /// ```rust
404 /// use pareg_core::Pareg;
405 ///
406 /// let args = ["key=value", "5:0.25"];
407 /// let mut args = Pareg::new(args.iter().map(|a| a.to_string()).collect());
408 ///
409 /// assert_eq!(
410 /// "value",
411 /// args.next_val::<&str>('=').unwrap()
412 /// );
413 /// assert_eq!(
414 /// 0.25,
415 /// args.next_val::<f64>(':').unwrap()
416 /// );
417 /// ```
418 #[inline]
419 pub fn next_val<'a, T: FromArg<'a>>(&'a mut self, sep: char) -> Result<T> {
420 self.inner().next_val(sep)
421 }
422
423 /// Uses the function [`mval_arg`] on the next argument.
424 ///
425 /// If sep was `'='`, parses `"key=value"` into `value` that is parsed to the
426 /// given type.
427 ///
428 /// In case that there is no `'='`, value is `None`.
429 ///
430 /// # Examples
431 /// ```rust
432 /// use pareg_core::Pareg;
433 ///
434 /// let args = ["key=value", "5:0.25", "only_key"];
435 /// let mut args = Pareg::new(args.iter().map(|a| a.to_string()).collect());
436 ///
437 /// assert_eq!(
438 /// Some("value"),
439 /// args.next_mval::<&str>('=').unwrap()
440 /// );
441 /// assert_eq!(
442 /// Some(0.25),
443 /// args.next_mval::<f64>(':').unwrap()
444 /// );
445 /// assert_eq!(
446 /// None,
447 /// args.next_mval::<&str>('=').unwrap()
448 /// );
449 /// ```
450 #[inline]
451 pub fn next_mval<'a, T: FromArg<'a>>(
452 &'a mut self,
453 sep: char,
454 ) -> Result<Option<T>> {
455 self.inner().next_mval(sep)
456 }
457
458 /// Parses the last returned value from the iterator.
459 ///
460 /// # Examples
461 /// ```rust
462 /// use pareg_core::Pareg;
463 ///
464 /// let args = ["hello", "10", "0.25", "always"];
465 /// let mut args = Pareg::new(args.iter().map(|a| a.to_string()).collect());
466 ///
467 /// args.next();
468 /// assert_eq!("hello", args.cur_arg::<&str>().unwrap());
469 /// args.next();
470 /// assert_eq!(10, args.cur_arg::<usize>().unwrap());
471 /// args.next();
472 /// assert_eq!(0.25, args.cur_arg::<f64>().unwrap());
473 /// ```
474 #[inline]
475 pub fn cur_arg<'a, T: FromArg<'a>>(&'a self) -> Result<T> {
476 self.inner().cur_arg()
477 }
478
479 /// Uses the function [`key_mval_arg`] on the last argument. If there is no
480 /// last argument, returns `ArgError::NoLastArgument`.
481 ///
482 /// If sep was `'='`, parses `"key=value"` into `"key"` and `value` that is
483 /// also parsed to the given type.
484 ///
485 /// In case that there is no `'='`, value is `None`.
486 ///
487 /// # Examples
488 /// ```rust
489 /// use pareg_core::Pareg;
490 ///
491 /// let args = ["key=value", "5:0.25", "only_key"];
492 /// let mut args = Pareg::new(args.iter().map(|a| a.to_string()).collect());
493 ///
494 /// args.next();
495 /// assert_eq!(
496 /// ("key", Some("value")),
497 /// args.cur_key_mval::<&str, &str>('=').unwrap()
498 /// );
499 /// args.next();
500 /// assert_eq!(
501 /// (5, Some(0.25)),
502 /// args.cur_key_mval::<i32, f64>(':').unwrap()
503 /// );
504 /// args.next();
505 /// assert_eq!(
506 /// ("only_key".to_owned(), None),
507 /// args.cur_key_mval::<String, &str>('=').unwrap()
508 /// );
509 /// ```
510 #[inline]
511 pub fn cur_key_mval<'a, K: FromArg<'a>, V: FromArg<'a>>(
512 &'a self,
513 sep: char,
514 ) -> Result<(K, Option<V>)> {
515 self.inner().cur_key_mval(sep)
516 }
517
518 /// Uses the function [`key_val_arg`] on the next value. If there is no
519 /// last argument, returns `ArgError::NoLastArgument`.
520 ///
521 /// If sep was `'='`, parses `"key=value"` into `"key"` and `value` that is
522 /// also parsed to the given type.
523 ///
524 /// In case that there is no `'='`, returns [`ArgError::NoValue`].
525 ///
526 /// # Examples
527 /// ```rust
528 /// use pareg_core::Pareg;
529 ///
530 /// let args = ["key=value", "5:0.25"];
531 /// let mut args = Pareg::new(args.iter().map(|a| a.to_string()).collect());
532 ///
533 /// args.next();
534 /// assert_eq!(
535 /// ("key", "value"),
536 /// args.cur_key_val::<&str, &str>('=').unwrap()
537 /// );
538 /// args.next();
539 /// assert_eq!(
540 /// (5, 0.25),
541 /// args.cur_key_val::<i32, f64>(':').unwrap()
542 /// );
543 /// ```
544 #[inline]
545 pub fn cur_key_val<'a, K: FromArg<'a>, V: FromArg<'a>>(
546 &'a self,
547 sep: char,
548 ) -> Result<(K, V)> {
549 self.inner().cur_key_val(sep)
550 }
551
552 /// Uses the function [`bool_arg`] on the next value. If there is no last
553 /// argument, returns `ArgError::NoLastArgument`.
554 ///
555 /// Parse bool value in a specific way. If the value of lowercase `arg` is
556 /// equal to `t` returns true, if it is equal to `f` returns false and
557 /// otherwise returns error.
558 ///
559 /// # Examples
560 /// ```rust
561 /// use pareg_core::Pareg;
562 ///
563 /// let args = ["true", "yes", "never"];
564 /// let mut args = Pareg::new(args.iter().map(|a| a.to_string()).collect());
565 ///
566 /// args.next();
567 /// assert_eq!(true, args.cur_bool("true", "false").unwrap());
568 /// args.next();
569 /// assert_eq!(true, args.cur_bool("yes", "no").unwrap());
570 /// args.next();
571 /// assert_eq!(false, args.cur_bool("always", "never").unwrap());
572 /// ```
573 #[inline]
574 pub fn cur_bool(&self, t: &str, f: &str) -> Result<bool> {
575 self.inner().cur_bool(t, f)
576 }
577
578 /// Uses the function [`opt_bool_arg`] on the next argument. If there is no
579 /// last argument, returns `ArgError::NoLastArgument`.
580 ///
581 /// Parse bool value in a specific way. If the value of lowercase `arg` is
582 /// equal to `t` returns true, if it is equal to `f` returns false and
583 /// if it is equal to `n` returns [`None`]. Otherwise returns error.
584 ///
585 /// # Examples
586 /// ```rust
587 /// use pareg_core::Pareg;
588 ///
589 /// let args = ["always", "never", "auto"];
590 /// let mut args = Pareg::new(args.iter().map(|a| a.to_string()).collect());
591 ///
592 /// args.next();
593 /// assert_eq!(
594 /// Some(true),
595 /// args.cur_opt_bool("always", "never", "auto").unwrap()
596 /// );
597 /// args.next();
598 /// assert_eq!(
599 /// Some(false),
600 /// args.cur_opt_bool("always", "never", "auto").unwrap()
601 /// );
602 /// args.next();
603 /// assert_eq!(
604 /// None,
605 /// args.cur_opt_bool("always", "never", "auto").unwrap()
606 /// );
607 /// ```
608 #[inline]
609 pub fn cur_opt_bool(
610 &self,
611 t: &str,
612 f: &str,
613 n: &str,
614 ) -> Result<Option<bool>> {
615 self.inner().cur_opt_bool(t, f, n)
616 }
617
618 /// Uses the function [`key_arg`] on the next argument. If there is no
619 /// last argument, returns `ArgError::NoLastArgument`.
620 ///
621 /// If sep was `'='`, parses `"key=value"` into `"key"` and discards `value`.
622 ///
623 /// In case that there is no `'='`, parses the whole input.
624 ///
625 /// # Examples
626 /// ```rust
627 /// use pareg_core::Pareg;
628 ///
629 /// let args = ["key=value", "5:0.25"];
630 /// let mut args = Pareg::new(args.iter().map(|a| a.to_string()).collect());
631 ///
632 /// args.next();
633 /// assert_eq!(
634 /// "key",
635 /// args.cur_key::<&str>('=').unwrap()
636 /// );
637 /// args.next();
638 /// assert_eq!(
639 /// 5,
640 /// args.cur_key::<i32>(':').unwrap()
641 /// );
642 /// ```
643 #[inline]
644 pub fn cur_key<'a, T: FromArg<'a>>(&'a self, sep: char) -> Result<T> {
645 self.inner().cur_key(sep)
646 }
647
648 /// Uses the function [`val_arg`] on the next argument. If there is no
649 /// last argument, returns `ArgError::NoLastArgument`.
650 ///
651 /// If sep was `'='`, parses `"key=value"` into `value` that is parsed to the
652 /// given type.
653 ///
654 /// In case that there is no `'='`, returns [`ArgError::NoValue`].
655 ///
656 /// # Examples
657 /// ```rust
658 /// use pareg_core::Pareg;
659 ///
660 /// let args = ["key=value", "5:0.25"];
661 /// let mut args = Pareg::new(args.iter().map(|a| a.to_string()).collect());
662 ///
663 /// args.next();
664 /// assert_eq!(
665 /// "value",
666 /// args.cur_val::<&str>('=').unwrap()
667 /// );
668 /// args.next();
669 /// assert_eq!(
670 /// 0.25,
671 /// args.cur_val::<f64>(':').unwrap()
672 /// );
673 /// ```
674 #[inline]
675 pub fn cur_val<'a, T: FromArg<'a>>(&'a self, sep: char) -> Result<T> {
676 self.inner().cur_val(sep)
677 }
678
679 /// Uses the function [`mval_arg`] on the next argument. If there is no
680 /// last argument, returns `ArgError::NoLastArgument`.
681 ///
682 /// If sep was `'='`, parses `"key=value"` into `value` that is parsed to the
683 /// given type.
684 ///
685 /// In case that there is no `'='`, value is `None`.
686 ///
687 /// # Examples
688 /// ```rust
689 /// use pareg_core::Pareg;
690 ///
691 /// let args = ["key=value", "5:0.25", "only_key"];
692 /// let mut args = Pareg::new(args.iter().map(|a| a.to_string()).collect());
693 ///
694 /// args.next();
695 /// assert_eq!(
696 /// Some("value"),
697 /// args.cur_mval::<&str>('=').unwrap()
698 /// );
699 /// args.next();
700 /// assert_eq!(
701 /// Some(0.25),
702 /// args.cur_mval::<f64>(':').unwrap()
703 /// );
704 /// args.next();
705 /// assert_eq!(
706 /// None,
707 /// args.cur_mval::<&str>('=').unwrap()
708 /// );
709 /// ```
710 #[inline]
711 pub fn cur_mval<'a, T: FromArg<'a>>(
712 &'a self,
713 sep: char,
714 ) -> Result<Option<T>> {
715 self.inner().cur_mval(sep)
716 }
717
718 /// Split the current argument by the given separator and return the parsed
719 /// value after the separator or if there is no such separator, parse the
720 /// next argument.
721 ///
722 /// # Examples
723 /// ```rust
724 /// use pareg_core::Pareg;
725 ///
726 /// let args = ["--cnt", "20", "--cnt=10"];
727 /// let mut args = Pareg::new(args.iter().map(|a| a.to_string()).collect());
728 ///
729 /// args.next();
730 /// assert_eq!(
731 /// 20,
732 /// args.cur_val_or_next::<u32>('=').unwrap()
733 /// );
734 /// args.next();
735 /// assert_eq!(
736 /// 10,
737 /// args.cur_val_or_next::<u32>('=').unwrap()
738 /// );
739 /// ```
740 #[inline]
741 pub fn cur_val_or_next<'a, T: FromArg<'a>>(
742 &'a mut self,
743 sep: char,
744 ) -> Result<T> {
745 self.inner().cur_val_or_next(sep)
746 }
747
748 /// Tries to set the value of `res` to some if it is none. Throws error if it
749 /// is some.
750 #[inline]
751 pub fn try_set_cur_with<'a, T>(
752 &'a self,
753 res: &mut Option<T>,
754 f: impl FnOnce(&'a str) -> Result<T>,
755 ) -> Result<()> {
756 self.inner().try_set_cur_with(res, f)
757 }
758
759 /// Tries to set the value of `res` to some if it is none. Throws error if it
760 /// is some.
761 #[inline]
762 pub fn try_set_next_with<'a, T>(
763 &'a mut self,
764 res: &mut Option<T>,
765 f: impl FnOnce(&'a str) -> Result<T>,
766 ) -> Result<()> {
767 self.inner().try_set_next_with(res, f)
768 }
769
770 /// Tries to set the value of `res` to some if it is none. Throws error if it
771 /// is some.
772 #[inline]
773 pub fn try_set_cur<'a, T: FromArg<'a>>(
774 &'a mut self,
775 res: &mut Option<T>,
776 ) -> Result<()> {
777 self.inner().try_set_cur(res)
778 }
779
780 /// Tries to set the value of `res` to some if it is none. Throws error if it
781 /// is some.
782 #[inline]
783 pub fn try_set_next<'a, T: FromArg<'a>>(
784 &'a mut self,
785 res: &mut Option<T>,
786 ) -> Result<()> {
787 self.inner().try_set_next(res)
788 }
789
790 /// Splits last argument by separator `sep` and parses each word into a
791 /// resulting vector.
792 ///
793 /// Difference from [`Pareg::cur_list`] is that this will first to split
794 /// and than try to parse.
795 #[inline]
796 pub fn split_cur<'a, T: FromArg<'a>>(
797 &'a self,
798 sep: &str,
799 ) -> Result<Vec<T>> {
800 self.inner().split_cur(sep)
801 }
802
803 /// Parses multiple values in last argument separated by `sep`.
804 ///
805 /// Unlike [`Pareg::split_cur`], this will first try to parse and than
806 /// check if the separator is present. So valid values may contain contents
807 /// of `sep`, and it will properly parse the vales, whereas
808 /// [`Pareg::split_cur`] would split `arg` and than try to parse.
809 #[inline]
810 pub fn cur_list<T: FromRead>(&self, sep: &str) -> Result<Vec<T>> {
811 self.inner().cur_list(sep)
812 }
813
814 /// Splits next argument by separator `sep` and parses each word into a
815 /// resulting vector.
816 ///
817 /// Difference from [`Pareg::next_list`] is that this will first to split
818 /// and than try to parse.
819 #[inline]
820 pub fn split_next<'a, T: FromArg<'a>>(
821 &'a mut self,
822 sep: &str,
823 ) -> Result<Vec<T>> {
824 self.inner().split_next(sep)
825 }
826
827 /// Parses multiple values in next argument separated by `sep`.
828 ///
829 /// Unlike [`Pareg::split_next`], this will first try to parse and than
830 /// check if the separator is present. So valid values may contain contents
831 /// of `sep`, and it will properly parse the vales, whereas
832 /// [`Pareg::split_next`] would split `arg` and than try to parse.
833 #[inline]
834 pub fn next_list<T: FromRead>(&mut self, sep: &str) -> Result<Vec<T>> {
835 self.inner().next_list(sep)
836 }
837
838 /// Creates pretty error that the last argument (cur) is unknown.
839 #[inline]
840 pub fn err_unknown_argument(&self) -> ArgError {
841 self.inner().err_unknown_argument()
842 }
843
844 /// Creates pretty error that there should be more arguments but there are
845 /// no more arguments.
846 #[inline]
847 pub fn err_no_more_arguments(&self) -> ArgError {
848 self.inner().err_no_more_arguments()
849 }
850
851 /// Creates error that says that the current argument has invalid value.
852 #[inline]
853 pub fn err_invalid(&self) -> ArgError {
854 self.inner().err_invalid()
855 }
856
857 /// Creates error that says that the given part of the current argument has
858 /// invalid value.
859 #[inline]
860 pub fn err_invalid_value(&self, value: String) -> ArgError {
861 self.inner().err_invalid_value(value)
862 }
863
864 /// Creates error that says that the given part of the current argument has
865 /// invalid value.
866 #[inline]
867 pub fn err_invalid_span(&self, span: Range<usize>) -> ArgError {
868 self.inner().err_invalid_span(span)
869 }
870
871 /// Adds additional information to error so that it has better error
872 /// message. Consider using [`ParegRef::cur_manual`] or
873 /// [`ParegRef::next_manual`] instead.
874 #[inline]
875 pub fn map_err(&self, err: ArgError) -> ArgError {
876 self.inner().map_err(err)
877 }
878
879 /// Adds additional information to error so that it has better error
880 /// message. Consider using [`Pareg::cur_manual`] or [`Pareg::next_manual`]
881 /// instead.
882 ///
883 /// # Examples
884 /// ```rust
885 /// use pareg_core::{Pareg, key_val_arg};
886 /// let args = ["-D10=0.25"];
887 /// let mut args = Pareg::new(args.iter().map(|a| a.to_string()).collect());
888 ///
889 /// args.next();
890 /// let arg: &str = args.cur_arg().unwrap();
891 /// let arg = arg.strip_prefix("-D").unwrap();
892 ///
893 /// let res: (usize, f32) = args.map_res(key_val_arg(arg, '=')).unwrap();
894 /// assert_eq!((10, 0.25), res);
895 /// ```
896 #[inline]
897 pub fn map_res<T>(&self, res: Result<T>) -> Result<T> {
898 self.inner().map_res(res)
899 }
900}