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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
#![warn(missing_docs)]
#![warn(rustdoc::missing_doc_code_examples)]
#![doc = include_str!("../README.md")]

use std::rc::Rc;
use std::str::FromStr;

pub mod params;

mod args;
#[doc(hidden)]
pub mod info;
#[doc(hidden)]
pub mod item;
#[doc(hidden)]
pub mod meta;

use crate::{args::Word, info::Error, item::Item};

#[cfg(test)]
mod tests;
#[doc(inline)]
pub use crate::args::Args;
pub use crate::info::{Info, OptionParser};
pub use crate::meta::Meta;
#[doc(inline)]
pub use crate::params::*;
#[doc(inline)]
#[cfg(feature = "bpaf_derive")]
pub use bpaf_derive::Bpaf;

/// Compose several parsers to produce a single result
///
/// Every parser must succeed in order to produce a result for
/// sequential composition and only one parser needs to succeed
/// for a parallel one (`construct!([a, b, c])`)
///
/// Each parser must be present in a local scope and
/// have the same name as struct field. Alternatively
/// a parser can be present as a function producing a parser
/// bpaf will call this function and use it's result. Later
/// option might be useful when single parser is used in
/// several `construct!` macros
///
/// ```rust
/// # use bpaf::*;
/// struct Res {
///     a: bool,
///     b: u32,
/// }
///
/// // parser defined as a local variable
/// let a: Parser<bool> = short('a').switch();
///
/// // parser defined as a function
/// fn b() -> Parser<u32> { short('b').argument("B").from_str() }
/// let res: Parser<Res> = construct!(Res { a, b() });
/// # drop(res);
/// ```
///
/// `construct!` supports following representations:
///
/// - structs with unnamed fields:
/// ```rust ignore
/// construct!(Res(a, b))
/// ```
/// - structs with named fields:
/// ```ignore
/// construct!(Res {a, b})
/// ```
/// - enums with unnamed fields:
/// ```ignore
/// construct!(Ty::Res(a, b))
/// ```
/// - enums with named fields:
/// ```ignore
/// construct!(Ty::Res {a, b})
/// ```
/// - tuples:
/// ```ignore
/// construct!(a, b)
/// ```
/// - parallel composition, a equivalent of `a.or_else(b).or_else(c)`
/// ```ignore
/// construct!([a, b, c])
/// ```
#[macro_export]
macro_rules! construct {
    // construct!(Enum::Cons { a, b, c })
    ($ns:ident $(:: $con:ident)* { $($tokens:tt)* }) => {{ $crate::construct!(@prepare [named [$ns $(:: $con)*]] [] $($tokens)*) }};
    (:: $ns:ident $(:: $con:ident)* { $($tokens:tt)* }) => {{ $crate::construct!(@prepare [named [:: $ns $(:: $con)*]] [] $($tokens)*) }};
    // construct!(Enum::Cons ( a, b, c ))
    ($ns:ident $(:: $con:ident)* ( $($tokens:tt)* )) => {{ $crate::construct!(@prepare [pos [$ns $(:: $con)*]] [] $($tokens)*) }};
    (:: $ns:ident $(:: $con:ident)* ( $($tokens:tt)* )) => {{ $crate::construct!(@prepare [pos [:: $ns $(:: $con)*]] [] $($tokens)*) }};

    // construct!( a, b, c )
    ($first:ident , $($tokens:tt)*) => {{ $crate::construct!(@prepare [pos] [] $first , $($tokens)*) }};
    ($first:ident (), $($tokens:tt)*) => {{ $crate::construct!(@prepare [pos] [] $first (), $($tokens)*) }};

    // construct![a, b, c]
    ([$first:ident $($tokens:tt)*]) => {{ $crate::construct!(@prepare [alt] [] $first $($tokens)*) }};

    (@prepare $ty:tt [$($fields:tt)*] $field:ident (), $($rest:tt)*) => {{
        let $field = $field();
        $crate::construct!(@prepare $ty [$($fields)* $field] $($rest)*)
    }};
    (@prepare $ty:tt [$($fields:tt)*] $field:ident () $($rest:tt)*) => {{
        let $field = $field();
        $crate::construct!(@prepare $ty [$($fields)* $field] $($rest)*)
    }};
    (@prepare $ty:tt [$($fields:tt)*] $field:ident, $($rest:tt)*) => {{
        $crate::construct!(@prepare $ty [$($fields)* $field] $($rest)*)
    }};
    (@prepare $ty:tt [$($fields:tt)*] $field:ident $($rest:tt)*) => {{
        $crate::construct!(@prepare $ty [$($fields)* $field] $($rest)*)
    }};
    (@prepare [alt] [$first:ident $($fields:ident)*]) => { $first $(.or_else($fields))*  };
    (@prepare $ty:tt [$($fields:tt)*]) => {{
        $crate::Parser {
            parse: ::std::rc::Rc::new(move |args| {
                $(let ($fields , args) = ($fields . parse)(args)?;)*
                ::std::result::Result::Ok(($crate::construct!(@make $ty [$($fields)*]), args))
            }),
            meta: $crate::Meta::And(::std::vec![ $($fields.meta),* ])
        }
    }};

    (@make [named [$($con:tt)+]] [$($fields:ident)*]) => { $($con)+ { $($fields),* } };
    (@make [pos   [$($con:tt)+]] [$($fields:ident)*]) => { $($con)+ ( $($fields),* ) };
    (@make [pos] [$($fields:ident)*]) => { ( $($fields),* ) };
}

#[doc(hidden)]
/// A bit more user friendly alias for parsing function
pub type DynParse<T> = dyn Fn(Args) -> Result<(T, Args), Error>;

/// Simple or composed argument parser
#[derive(Clone)]
pub struct Parser<T> {
    #[doc(hidden)]
    /// Parsing function
    pub parse: Rc<DynParse<T>>,
    #[doc(hidden)]
    /// Included information about the parser
    pub meta: Meta,
}

impl<T> Parser<T> {
    /// Wrap a value into a `Parser`
    ///
    /// Parser will produce `T` without consuming anything from the command line, can be useful
    /// with [`construct!`].
    ///
    /// ```rust
    /// # use bpaf::*;
    /// let a = long("flag-a").switch();
    /// let b = Parser::pure(42u32);
    /// let t: Parser<(bool, u32)> = construct!(a, b);
    /// # drop(t)
    /// ```
    #[must_use]
    pub fn pure(val: T) -> Parser<T>
    where
        T: 'static + Clone,
    {
        let parse = move |i| Ok((val.clone(), i));
        Parser {
            parse: Rc::new(parse),
            meta: Meta::Skip,
        }
    }

    #[doc(hidden)]
    /// <*>
    #[must_use]
    pub fn ap<A, B>(self, other: Parser<A>) -> Parser<B>
    where
        T: Fn(A) -> B + 'static,
        A: 'static,
    {
        let parse = move |i| {
            let (t, rest) = (self.parse)(i)?;
            let (a, rest) = (other.parse)(rest)?;
            Ok((t(a), rest))
        };
        Parser {
            parse: Rc::new(parse),
            meta: Meta::And(vec![self.meta, other.meta]),
        }
    }

    /// If first parser fails - try the second one
    ///
    /// ```rust
    /// # use bpaf::*;
    /// let a = short('a').switch();
    /// let b = short('b').switch();
    ///
    /// // Parser will accept either `-a` or `-b` on a command line but not both at once.
    /// let a_or_b: Parser<bool> = a.or_else(b);
    /// # drop(a_or_b);
    /// ```
    ///
    /// # Performance
    ///
    /// If first parser succeeds - second one will be called anyway to produce a
    /// better error message for combinations of mutually exclusive parsers:
    ///
    /// Suppose program accepts one of two mutually exclusive switches `-a` and `-b`
    /// and both are present error message should point at the second flag
    ///
    /// [`construct!`] can be used to perform a similar task and might generate better code if
    /// combines more than two parsers. Those two invocations are equivalent:
    ///
    /// ```ignore
    /// let abc = a.or_else(b).or_else(c);
    /// ```
    /// ```ignore
    /// let abc = construct!([a, b, c]);
    /// ```
    ///
    #[must_use]
    pub fn or_else(self, other: Parser<T>) -> Parser<T>
    where
        T: 'static,
    {
        let parse = move |mut i: Args| -> Result<(T, Args), Error> {
            i.head = usize::MAX;
            // To generate less confusing error messages give priority to the left most flag/argument
            // from the command line:
            // So if program accepts only one of 3 flags: -a, -b and -c and all 3 are present
            // take the first one and reject the remaining ones.
            let (res, new_args) = match ((self.parse)(i.clone()), (other.parse)(i)) {
                // side channel (--help) reporting takes priority
                (e @ Err(Error::Stdout(_)), _) | (_, e @ Err(Error::Stdout(_))) => e,
                (Ok((r1, a1)), Ok((r2, a2))) => {
                    if a1.head < a2.head {
                        Ok((r1, a1))
                    } else {
                        Ok((r2, a2))
                    }
                }
                (Ok(ok), Err(_)) | (Err(_), Ok(ok)) => Ok(ok),
                (Err(e1), Err(e2)) => Err(e1.combine_with(e2)),
            }?;
            Ok((res, new_args))
        };

        Parser {
            parse: Rc::new(parse),
            meta: self.meta.or(other.meta),
        }
    }

    /// Fail with a fixed error message
    /// ```rust
    /// # use bpaf::*;
    /// let a = short('a').switch();
    /// let no_a = Parser::fail("Custom error message for missing -a");
    ///
    /// // Parser will produce a custom error message if `-a` is not specified
    /// let a_: Parser<bool> = a.or_else(no_a);
    /// # drop(a_);
    /// ```
    #[must_use]
    pub fn fail<M>(msg: M) -> Parser<T>
    where
        String: From<M>,
        M: Clone + 'static,
    {
        Parser {
            meta: Meta::Skip,
            parse: Rc::new(move |_| Err(Error::Stderr(String::from(msg.clone())))),
        }
    }

    /// Consume zero or more items from a command line
    ///
    /// ```rust
    /// # use bpaf::*;
    /// // parser will accept multiple `-n` arguments:
    /// // `-n 1, -n 2, -n 3`
    /// // and return all of them as a vector which can be empty if no `-n` specified
    /// let n: Parser<Vec<u32>> = short('n').argument("NUM").from_str::<u32>().many();
    /// # drop(n);
    /// ```
    ///
    /// # Panics
    /// Panics if parser succeeds without consuming any input: any parser modified with
    /// `many` must consume something,
    #[must_use]
    pub fn many(self) -> Parser<Vec<T>>
    where
        T: 'static,
    {
        let parse = move |mut i: Args| {
            let mut res = Vec::new();
            let mut size = i.len();
            while let Ok((elt, new_i)) = (self.parse)(i.clone()) {
                let new_size = new_i.len();
                #[allow(clippy::panic)]
                if new_size < size {
                    size = new_size;
                } else {
                    panic!("many can't be used with non failing parser")
                }
                i = new_i;
                res.push(elt);
            }
            Ok((res, i))
        };
        Parser {
            parse: Rc::new(parse),
            meta: self.meta.many(),
        }
    }

    /// Validate or fail with a message
    ///
    /// ```rust
    /// # use bpaf::*;
    /// let n = short('n').argument("NUM").from_str::<u32>();
    /// // Parser will reject values greater than 10
    /// let n = n.guard(|v| *v <= 10, "Values greater than 10 are only available in the DLC pack!");
    /// # drop(n);
    /// ```
    #[must_use]
    pub fn guard<F>(self, m: F, message: &'static str) -> Parser<T>
    where
        F: Fn(&T) -> bool + 'static,
        T: 'static,
    {
        let parse = move |i: Args| match (self.parse)(i) {
            Ok((ok, i)) if m(&ok) => Ok((ok, i)),
            Ok(_) => Err(Error::Stderr(message.to_owned())), // TODO - see what exactly we tried to parse
            Err(err) => Err(err),
        };
        Parser {
            parse: Rc::new(parse),
            meta: self.meta,
        }
    }

    /// Consume one or more items from a command line
    ///
    /// Takes a string literal that will be used as an
    /// error message if there's not enough parameters specified
    ///
    /// ```rust
    /// # use bpaf::*;
    /// // parser will accept multiple `-n` arguments:
    /// // `-n 1, -n 2, -n 3`
    /// // and return all of them as a vector. At least one `-n` argument is required.
    /// let n: Parser<Vec<u32>> = short('n').argument("NUM")
    ///     .from_str::<u32>().some("You need to specify at least one number");
    /// # drop(n);
    /// ```
    #[must_use]
    pub fn some(self, msg: &'static str) -> Parser<Vec<T>>
    where
        T: 'static,
    {
        self.many().guard(|x| !x.is_empty(), msg)
    }

    /// Turn a required parser into optional
    ///
    /// ```rust
    /// # use bpaf::*;
    /// let n: Parser<u32> = short('n').argument("NUM").from_str();
    /// // if `-n` is not specified - parser will return `None`
    /// let n: Parser<Option<u32>> = n.optional();
    /// # drop(n);
    /// ```
    pub fn optional(self) -> Parser<Option<T>>
    where
        T: 'static + Clone,
    {
        self.map(Some).fallback(None)
    }

    /// Apply a pure transformation to a contained value
    ///
    /// ```rust
    /// # use bpaf::*;
    /// let n: Parser<u32> = short('n').argument("NUM").from_str();
    /// // produced value is now twice as large
    /// let n = n.map(|v| v * 2);
    /// # drop(n);
    /// ```
    pub fn map<F, B>(self, map: F) -> Parser<B>
    where
        F: Fn(T) -> B + 'static,
        T: 'static,
    {
        let parse = move |args: Args| {
            let (t, args) = (self.parse)(args)?;
            Ok((map(t), args))
        };
        Parser {
            parse: Rc::new(parse),
            meta: self.meta,
        }
    }

    /// Apply a failing transformation
    ///
    /// See also [`from_str`][Parser::from_str]
    /// ```rust
    /// # use bpaf::*;
    /// let s: Parser<String> = short('n').argument("NUM");
    /// // Try to parse String into u32 or fail during the parsing
    /// use std::str::FromStr;
    /// let n = s.map(|s| u32::from_str(&s));
    /// # drop(n);
    /// ```
    pub fn parse<F, B, E>(self, map: F) -> Parser<B>
    where
        F: Fn(T) -> Result<B, E> + 'static,
        T: 'static,
        E: ToString,
    {
        let parse = move |args: Args| {
            let (t, args) = (self.parse)(args)?;

            match map(t) {
                Ok(ok) => Ok((ok, args)),
                Err(e) => Err(Error::Stderr(
                    if let Some(Word { utf8: Some(w), .. }) = args.current {
                        format!("Couldn't parse {:?}: {}", w, e.to_string())
                    } else {
                        format!("Couldn't parse: {}", e.to_string())
                    },
                )),
            }
        };
        Parser {
            parse: Rc::new(parse),
            meta: self.meta,
        }
    }

    /// Use this value as default if value is not present on a command line
    ///
    /// Would still fail if value is present but failure comes from some transformation
    /// ```rust
    /// # use bpaf::*;
    /// let n = short('n').argument("NUM").from_str::<u32>().fallback(42);
    /// # drop(n)
    /// ```
    #[must_use]
    pub fn fallback(self, val: T) -> Parser<T>
    where
        T: Clone + 'static,
    {
        let parse = move |i: Args| match (self.parse)(i.clone()) {
            Ok(ok) => Ok(ok),
            e @ Err(Error::Stderr(_) | Error::Stdout(_)) => e,
            Err(_) => Ok((val.clone(), i)),
        };
        Parser {
            parse: Rc::new(parse),
            meta: Meta::optional(self.meta),
        }
    }

    /// Use value produced by this function as default if value is not present
    ///
    /// Would still fail if value is present but failure comes from some transformation
    /// ```rust
    /// # use bpaf::*;
    /// let n = short('n').argument("NUM").from_str::<u32>();
    /// let n = n.fallback_with(|| Result::<u32, String>::Ok(42));
    /// # drop(n)
    /// ```
    #[must_use]
    pub fn fallback_with<F, E>(self, val: F) -> Parser<T>
    where
        F: Fn() -> Result<T, E> + Clone + 'static,
        E: ToString,
        T: Clone + 'static,
    {
        let parse = move |i: Args| match (self.parse)(i.clone()) {
            Ok(ok) => Ok(ok),
            e @ Err(Error::Stderr(_) | Error::Stdout(_)) => e,
            Err(_) => match val() {
                Ok(ok) => Ok((ok, i)),
                Err(e) => Err(Error::Stderr(e.to_string())),
            },
        };
        Parser {
            parse: Rc::new(parse),
            meta: Meta::optional(self.meta),
        }
    }

    /// Parse `T` or fallback to `T::default()`
    ///
    /// ```rust
    /// # use bpaf::*;
    /// let n = short('n').argument("NUM").from_str::<u32>().default();
    /// # drop(n)
    /// ```
    #[must_use]
    pub fn default(self) -> Parser<T>
    where
        T: Default + 'static + Clone,
    {
        self.fallback(T::default())
    }

    /// Attach help message to a complex parser
    ///
    /// ```rust
    /// # use bpaf::*;
    /// let width = short('w').argument("PX").from_str::<u32>();
    /// let height = short('h').argument("PX").from_str::<u32>();
    /// let rect = construct!(width, height).group_help("take a rectangle");
    /// # drop(rect);
    /// ```
    /// See `examples/rectangle.rs` for a complete example
    #[must_use]
    pub fn group_help(self, msg: &'static str) -> Parser<T> {
        Self {
            parse: self.parse,
            meta: Meta::decorate(self.meta, msg),
        }
    }

    /// Ignore this parser during any sort of help generation
    ///
    /// Best used for optional parsers or parsers with a defined fallback
    ///
    /// ```rust
    /// # use bpaf::*;
    /// // bpaf will accept `-w` but won't show it during help generation
    /// let width = short('w').argument("PX").from_str::<u32>().fallback(10).hide();
    /// let height = short('h').argument("PX").from_str::<u32>();
    /// let rect = construct!(width, height);
    /// # drop(rect);
    /// ```
    ///
    /// See also `examples/cargo-cmd.rs`
    #[must_use]
    pub fn hide(self) -> Parser<T>
    where
        T: 'static,
    {
        Self {
            parse: Rc::new(move |args: Args| {
                (self.parse)(args).map_err(|_| Error::Missing(Vec::new()))
            }),
            meta: Meta::Skip,
        }
    }
}

impl Parser<String> {
    /// Parse stored [`String`] using [`FromStr`] instance
    ///
    /// ```rust
    /// # use bpaf::*;
    /// let speed = short('s').argument("SPEED").from_str::<f64>();
    /// // at this point program would accept things like "-s 3.1415"
    /// // but reject "-s pi"
    /// # drop(speed)
    /// ```
    #[must_use]
    pub fn from_str<T>(self) -> Parser<T>
    where
        T: FromStr,
        <T as FromStr>::Err: std::fmt::Display,
    {
        self.parse(|s| T::from_str(&s))
    }
}

/// Unsuccessful command line parsing outcome
///
/// Useful for unit testing for user parsers, intented to
/// be consumed with [`ParseFailure::unwrap_stdout`] and [`ParseFailure::unwrap_stdout`]
#[derive(Clone, Debug)]
pub enum ParseFailure {
    /// Terminate and print this to stdout
    Stdout(String),
    /// Terminate and print this to stderr
    Stderr(String),
}

impl ParseFailure {
    /// Returns the contained `stderr` values
    ///
    /// Intended to be used with unit tests
    ///
    /// # Panics
    ///
    /// Will panic if failure contains `stdout`
    #[allow(clippy::must_use_candidate)]
    pub fn unwrap_stderr(self) -> String {
        match self {
            Self::Stderr(err) => err,
            Self::Stdout(_) => {
                panic!("not an stderr: {:?}", self)
            }
        }
    }

    /// Returns the contained `stdout` values
    ///
    /// Intended to be used with unit tests
    ///
    /// # Panics
    ///
    /// Will panic if failure contains `stderr`
    #[allow(clippy::must_use_candidate)]
    pub fn unwrap_stdout(self) -> String {
        match self {
            Self::Stdout(err) => err,
            Self::Stderr(_) => {
                panic!("not an stdout: {:?}", self)
            }
        }
    }
}

impl<T> OptionParser<T> {
    /// Execute the [`OptionParser`], extract a parsed value or print some diagnostic and exit
    ///
    /// ```no_run
    /// # use bpaf::*;
    /// let verbose = short('v').req_flag(()).many().map(|xs|xs.len());
    /// let info = Info::default().descr("Takes verbosity flag and does nothing else");
    ///
    /// let opt = info.for_parser(verbose).run();
    /// // At this point `opt` contains number of repetitions of `-v` on a command line
    /// # drop(opt)
    /// ```
    #[must_use]
    pub fn run(self) -> T {
        let mut pos_only = false;
        let mut vec = Vec::new();
        for arg in std::env::args_os().skip(1) {
            args::push_vec(&mut vec, arg, &mut pos_only);
        }

        match self.run_inner(Args::from(vec)) {
            Ok(t) => t,
            Err(ParseFailure::Stdout(msg)) => {
                println!("{}", msg);
                std::process::exit(0);
            }
            Err(ParseFailure::Stderr(msg)) => {
                eprintln!("{}", msg);
                std::process::exit(1);
            }
        }
    }

    /// Execute the [`OptionParser`] and produce a value that can be used in unit tests
    ///
    /// ```
    /// #[test]
    /// fn positional_argument() {
    ///     let p = positional("FILE").help("File to process");
    ///     let parser = Info::default().for_parser(p);
    ///
    ///     let help = parser
    ///         .run_inner(Args::from(&["--help"]))
    ///         .unwrap_err()
    ///         .unwrap_stdout();
    ///     let expected_help = "\
    /// Usage: <FILE>
    ///
    /// Available options:
    ///     -h, --help   Prints help information
    /// ";
    ///     assert_eq!(expected_help, help);
    /// }
    /// ```
    ///
    /// See also [`Args`] and it's `From` impls to produce input and
    /// [`ParseFailure::unwrap_stderr`] / [`ParseFailure::unwrap_stdout`] for processing results.
    ///
    /// # Errors
    ///
    /// If parser can't produce desired outcome `run_inner` will return [`ParseFailure`]
    /// which represents runtime behavior: one branch to print something to stdout and exit with
    /// success and the other branch to print something to stderr and exit with failure.
    ///
    /// Parser is not really capturing anything. If parser detects `--help` or `--version` it will
    /// always produce something that can be consumed with [`ParseFailure::unwrap_stdout`].
    /// Otherwise it will produce [`ParseFailure::unwrap_stderr`]  generated either by the parser
    /// itself in case someone required field is missing or by user's [`Parser::guard`] or
    /// [`Parser::parse`] functions.
    ///
    /// API for those is constructed to only produce a [`String`]. If you try to print something inside
    /// [`Parser::map`] or [`Parser::parse`] - it will not be captured. Depending on a test case
    /// you'll know what to use: `unwrap_stdout` if you want to test generated help or `unwrap_stderr`
    /// if you are testing `parse` / `guard` / missing parameters.
    ///
    /// Exact string reperentations may change between versions including minor releases.
    pub fn run_inner(self, args: Args) -> Result<T, ParseFailure> {
        match (self.parse)(args) {
            Ok((t, rest)) if rest.is_empty() => Ok(t),
            Ok((_, rest)) => Err(ParseFailure::Stderr(format!("unexpected {:?}", rest))),
            Err(Error::Missing(metas)) => Err(ParseFailure::Stderr(format!(
                "Expected {}, pass --help for usage information",
                Meta::Or(metas)
            ))),
            Err(Error::Stdout(stdout)) => Err(ParseFailure::Stdout(stdout)),
            Err(Error::Stderr(stderr)) => Err(ParseFailure::Stderr(stderr)),
        }
    }
}

/// Strip a command name if present at the front when used as a cargo command
///
/// This helper should be used on a top level parser
///
/// ```rust
/// # use bpaf::*;
/// let width = short('w').argument("PX").from_str::<u32>();
/// let height = short('h').argument("PX").from_str::<u32>();
/// let parser: Parser<(u32, u32)> = cargo_helper("cmd", construct!(width, height));
/// # drop(parser);
/// ```
#[must_use]
pub fn cargo_helper<T>(cmd: &'static str, parser: Parser<T>) -> Parser<T>
where
    T: 'static,
{
    let skip = positional_if("", move |s| cmd == s).optional().hide();
    construct!(skip, parser).map(|x| x.1)
}