summaryrefslogtreecommitdiffstats
path: root/vendor/xflags-macros/tests/it/repeated_pos.rs
blob: 334af371de6e94d8320d5c753f46e1e1108befb3 (plain)
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
#![allow(unused)]
use std::{ffi::OsString, path::PathBuf};

#[derive(Debug)]
pub struct RepeatedPos {
    pub a: PathBuf,
    pub b: Option<u32>,
    pub c: Option<OsString>,
    pub rest: Vec<OsString>,
}

impl RepeatedPos {
    pub const HELP: &'static str = Self::HELP_;

    #[allow(dead_code)]
    pub fn from_env() -> xflags::Result<Self> {
        Self::from_env_()
    }

    #[allow(dead_code)]
    pub fn from_vec(args: Vec<std::ffi::OsString>) -> xflags::Result<Self> {
        Self::from_vec_(args)
    }
}

impl RepeatedPos {
    fn from_env_() -> xflags::Result<Self> {
        let mut p = xflags::rt::Parser::new_from_env();
        Self::parse_(&mut p)
    }
    fn from_vec_(args: Vec<std::ffi::OsString>) -> xflags::Result<Self> {
        let mut p = xflags::rt::Parser::new(args);
        Self::parse_(&mut p)
    }
}

impl RepeatedPos {
    fn parse_(p_: &mut xflags::rt::Parser) -> xflags::Result<Self> {
        let mut a = (false, Vec::new());
        let mut b = (false, Vec::new());
        let mut c = (false, Vec::new());
        let mut rest = (false, Vec::new());

        while let Some(arg_) = p_.pop_flag() {
            match arg_ {
                Ok(flag_) => match flag_.as_str() {
                    _ => return Err(p_.unexpected_flag(&flag_)),
                },
                Err(arg_) => {
                    if let (done_ @ false, buf_) = &mut a {
                        buf_.push(arg_.into());
                        *done_ = true;
                        continue;
                    }
                    if let (done_ @ false, buf_) = &mut b {
                        buf_.push(p_.value_from_str::<u32>("b", arg_)?);
                        *done_ = true;
                        continue;
                    }
                    if let (done_ @ false, buf_) = &mut c {
                        buf_.push(arg_.into());
                        *done_ = true;
                        continue;
                    }
                    if let (false, buf_) = &mut rest {
                        buf_.push(arg_.into());
                        continue;
                    }
                    return Err(p_.unexpected_arg(arg_));
                }
            }
        }
        Ok(Self {
            a: p_.required("a", a.1)?,
            b: p_.optional("b", b.1)?,
            c: p_.optional("c", c.1)?,
            rest: rest.1,
        })
    }
}
impl RepeatedPos {
    const HELP_: &'static str = "\
RepeatedPos

ARGS:
    <a>

    [b]

    [c]

    <rest>...
";
}