aboutsummaryrefslogtreecommitdiff
path: root/tests/issues.rs
blob: 3f9e1af72219752639e1042bb43df27913f0d9fd (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
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
// https://github.com/TeXitoi/structopt/issues/{NUMBER}

mod utils;
use utils::*;

use structopt::StructOpt;

#[test]
fn issue_151() {
    use structopt::{clap::ArgGroup, StructOpt};

    #[derive(StructOpt, Debug)]
    #[structopt(group = ArgGroup::with_name("verb").required(true).multiple(true))]
    struct Opt {
        #[structopt(long, group = "verb")]
        foo: bool,
        #[structopt(long, group = "verb")]
        bar: bool,
    }

    #[derive(Debug, StructOpt)]
    struct Cli {
        #[structopt(flatten)]
        a: Opt,
    }

    assert!(Cli::clap().get_matches_from_safe(&["test"]).is_err());
    assert!(Cli::clap()
        .get_matches_from_safe(&["test", "--foo"])
        .is_ok());
    assert!(Cli::clap()
        .get_matches_from_safe(&["test", "--bar"])
        .is_ok());
    assert!(Cli::clap()
        .get_matches_from_safe(&["test", "--zebra"])
        .is_err());
    assert!(Cli::clap()
        .get_matches_from_safe(&["test", "--foo", "--bar"])
        .is_ok());
}

#[test]
fn issue_289() {
    use structopt::{clap::AppSettings, StructOpt};

    #[derive(StructOpt)]
    #[structopt(setting = AppSettings::InferSubcommands)]
    enum Args {
        SomeCommand(SubSubCommand),
        AnotherCommand,
    }

    #[derive(StructOpt)]
    #[structopt(setting = AppSettings::InferSubcommands)]
    enum SubSubCommand {
        TestCommand,
    }

    assert!(Args::clap()
        .get_matches_from_safe(&["test", "some-command", "test-command"])
        .is_ok());
    assert!(Args::clap()
        .get_matches_from_safe(&["test", "some", "test-command"])
        .is_ok());
    assert!(Args::clap()
        .get_matches_from_safe(&["test", "some-command", "test"])
        .is_ok());
    assert!(Args::clap()
        .get_matches_from_safe(&["test", "some", "test"])
        .is_ok());
}

#[test]
fn issue_324() {
    fn my_version() -> &'static str {
        "MY_VERSION"
    }

    #[derive(StructOpt)]
    #[structopt(version = my_version())]
    struct Opt {
        #[structopt(subcommand)]
        _cmd: Option<SubCommand>,
    }

    #[derive(StructOpt)]
    enum SubCommand {
        Start,
    }

    let help = get_long_help::<Opt>();
    assert!(help.contains("MY_VERSION"));
}

#[test]
fn issue_359() {
    #[derive(Debug, PartialEq, StructOpt)]
    struct Opt {
        #[structopt(subcommand)]
        sub: Subcommands,
    }

    #[derive(Debug, PartialEq, StructOpt)]
    enum Subcommands {
        Add,

        #[structopt(external_subcommand)]
        Other(Vec<String>),
    }

    assert_eq!(
        Opt {
            sub: Subcommands::Other(vec!["only_one_arg".into()])
        },
        Opt::from_iter(&["test", "only_one_arg"])
    );
}

#[test]
fn issue_418() {
    use structopt::StructOpt;

    #[derive(Debug, StructOpt)]
    struct Opts {
        #[structopt(subcommand)]
        /// The command to run
        command: Command,
    }

    #[derive(Debug, StructOpt)]
    enum Command {
        /// Reticulate the splines
        #[structopt(visible_alias = "ret")]
        Reticulate {
            /// How many splines
            num_splines: u8,
        },
        /// Frobnicate the rest
        #[structopt(visible_alias = "frob")]
        Frobnicate,
    }

    let help = get_long_help::<Opts>();
    assert!(help.contains("Reticulate the splines [aliases: ret]"));
}

#[test]
fn issue_490() {
    use std::iter::FromIterator;
    use std::str::FromStr;
    use structopt::StructOpt;

    struct U16ish;
    impl FromStr for U16ish {
        type Err = ();
        fn from_str(_: &str) -> Result<Self, Self::Err> {
            unimplemented!()
        }
    }
    impl<'a> FromIterator<&'a U16ish> for Vec<u16> {
        fn from_iter<T: IntoIterator<Item = &'a U16ish>>(_: T) -> Self {
            unimplemented!()
        }
    }

    #[derive(StructOpt, Debug)]
    struct Opt {
        opt_vec: Vec<u16>,
        #[structopt(long)]
        opt_opt_vec: Option<Vec<u16>>,
    }

    // Assert that it compiles
}