1 // Copyright 2018 Guillaume Pinot (@TeXitoi) <texitoi@texitoi.eu>
2 //
3 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6 // option. This file may not be copied, modified, or distributed
7 // except according to those terms.
8
9 use structopt::clap::AppSettings;
10 use structopt::StructOpt;
11
12 pub const DISPLAY_ORDER: usize = 2;
13
14 // Check if the global settings compile
15 #[derive(StructOpt, Debug, PartialEq, Eq)]
16 #[structopt(global_settings = &[AppSettings::ColoredHelp])]
17 struct Opt {
18 #[structopt(
19 long = "x",
20 display_order = DISPLAY_ORDER,
21 next_line_help = true,
22 default_value = "0",
23 require_equals = true
24 )]
25 x: i32,
26
27 #[structopt(short = "l", long = "level", aliases = &["set-level", "lvl"])]
28 level: String,
29
30 #[structopt(long("values"))]
31 values: Vec<i32>,
32
33 #[structopt(name = "FILE", requires_if("FILE", "values"))]
34 files: Vec<String>,
35 }
36
37 #[test]
test_slice()38 fn test_slice() {
39 assert_eq!(
40 Opt {
41 x: 0,
42 level: "1".to_string(),
43 files: Vec::new(),
44 values: vec![],
45 },
46 Opt::from_clap(&Opt::clap().get_matches_from(&["test", "-l", "1"]))
47 );
48 assert_eq!(
49 Opt {
50 x: 0,
51 level: "1".to_string(),
52 files: Vec::new(),
53 values: vec![],
54 },
55 Opt::from_clap(&Opt::clap().get_matches_from(&["test", "--level", "1"]))
56 );
57 assert_eq!(
58 Opt {
59 x: 0,
60 level: "1".to_string(),
61 files: Vec::new(),
62 values: vec![],
63 },
64 Opt::from_clap(&Opt::clap().get_matches_from(&["test", "--set-level", "1"]))
65 );
66 assert_eq!(
67 Opt {
68 x: 0,
69 level: "1".to_string(),
70 files: Vec::new(),
71 values: vec![],
72 },
73 Opt::from_clap(&Opt::clap().get_matches_from(&["test", "--lvl", "1"]))
74 );
75 }
76
77 #[test]
test_multi_args()78 fn test_multi_args() {
79 assert_eq!(
80 Opt {
81 x: 0,
82 level: "1".to_string(),
83 files: vec!["file".to_string()],
84 values: vec![],
85 },
86 Opt::from_clap(&Opt::clap().get_matches_from(&["test", "-l", "1", "file"]))
87 );
88 assert_eq!(
89 Opt {
90 x: 0,
91 level: "1".to_string(),
92 files: vec!["FILE".to_string()],
93 values: vec![1],
94 },
95 Opt::from_clap(
96 &Opt::clap().get_matches_from(&["test", "-l", "1", "--values", "1", "--", "FILE"]),
97 )
98 );
99 }
100
101 #[test]
test_multi_args_fail()102 fn test_multi_args_fail() {
103 let result = Opt::clap().get_matches_from_safe(&["test", "-l", "1", "--", "FILE"]);
104 assert!(result.is_err());
105 }
106
107 #[test]
test_bool()108 fn test_bool() {
109 assert_eq!(
110 Opt {
111 x: 1,
112 level: "1".to_string(),
113 files: vec![],
114 values: vec![],
115 },
116 Opt::from_clap(&Opt::clap().get_matches_from(&["test", "-l", "1", "--x=1"]))
117 );
118 let result = Opt::clap().get_matches_from_safe(&["test", "-l", "1", "--x", "1"]);
119 assert!(result.is_err());
120 }
121
parse_hex(input: &str) -> Result<u64, std::num::ParseIntError>122 fn parse_hex(input: &str) -> Result<u64, std::num::ParseIntError> {
123 u64::from_str_radix(input, 16)
124 }
125
126 #[derive(StructOpt, PartialEq, Debug)]
127 struct HexOpt {
128 #[structopt(short = "n", parse(try_from_str = parse_hex))]
129 number: u64,
130 }
131
132 #[test]
test_parse_hex_function_path()133 fn test_parse_hex_function_path() {
134 assert_eq!(
135 HexOpt { number: 5 },
136 HexOpt::from_clap(&HexOpt::clap().get_matches_from(&["test", "-n", "5"]))
137 );
138 assert_eq!(
139 HexOpt { number: 0xabcdef },
140 HexOpt::from_clap(&HexOpt::clap().get_matches_from(&["test", "-n", "abcdef"]))
141 );
142
143 let err = HexOpt::clap()
144 .get_matches_from_safe(&["test", "-n", "gg"])
145 .unwrap_err();
146 assert!(err.message.contains("invalid digit found in string"), err);
147 }
148