aboutsummaryrefslogtreecommitdiff
path: root/examples/required_if.rs
blob: e3497b27121245b49e83d4c61bcef95441a1d471 (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
//! How to use `required_if` with structopt.
//!
//! Running this example with --help prints this message:
//! -----------------------------------------------------
//! structopt 0.3.25
//!
//! USAGE:
//!     required_if -o <out-type> [FILE]
//!
//! FLAGS:
//!     -h, --help       Prints help information
//!     -V, --version    Prints version information
//!
//! OPTIONS:
//!     -o <out-type>        Where to write the output: to `stdout` or `file`
//!
//! ARGS:
//!     <FILE>    File name: only required when `out-type` is set to `file`
//! -----------------------------------------------------

use structopt::StructOpt;

#[derive(Debug, StructOpt, PartialEq)]
struct Opt {
    /// Where to write the output: to `stdout` or `file`
    #[structopt(short)]
    out_type: String,

    /// File name: only required when `out-type` is set to `file`
    #[structopt(name = "FILE", required_if("out-type", "file"))]
    file_name: Option<String>,
}

fn main() {
    let opt = Opt::from_args();
    println!("{:?}", opt);
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_opt_out_type_file_without_file_name_returns_err() {
        let opt = Opt::from_iter_safe(&["test", "-o", "file"]);
        let err = opt.unwrap_err();
        assert_eq!(err.kind, clap::ErrorKind::MissingRequiredArgument);
    }

    #[test]
    fn test_opt_out_type_file_with_file_name_returns_ok() {
        let opt = Opt::from_iter_safe(&["test", "-o", "file", "filename"]);
        let opt = opt.unwrap();
        assert_eq!(
            opt,
            Opt {
                out_type: "file".into(),
                file_name: Some("filename".into()),
            }
        );
    }
}