|
| 1 | +extern crate clap; |
| 2 | + |
| 3 | +use std::str; |
| 4 | + |
| 5 | +use clap::{App, Arg, SubCommand, AppSettings}; |
| 6 | + |
| 7 | +fn condense_whitespace(s: &str) -> String { |
| 8 | + s.split_whitespace().collect::<Vec<_>>().join(" ") |
| 9 | +} |
| 10 | + |
| 11 | +fn normalize(s: &str) -> Vec<String> { |
| 12 | + s.lines().map(|l| l.trim()).filter(|l| !l.is_empty()).map(condense_whitespace).collect() |
| 13 | +} |
| 14 | + |
| 15 | +fn get_help(app: App, args: &[&'static str]) -> Vec<String> { |
| 16 | + let res = app.get_matches_from_safe(args.iter().chain(["--help"].iter())); |
| 17 | + normalize(&*res.unwrap_err().message) |
| 18 | +} |
| 19 | + |
| 20 | +#[test] |
| 21 | +fn no_derive_order() { |
| 22 | + let app = App::new("test") |
| 23 | + .args(&[ |
| 24 | + Arg::with_name("flag_b").long("flag_b").help("first flag"), |
| 25 | + Arg::with_name("option_b").long("option_b").takes_value(true).help("first option"), |
| 26 | + Arg::with_name("flag_a").long("flag_a").help("second flag"), |
| 27 | + Arg::with_name("option_a").long("option_a").takes_value(true).help("second option"), |
| 28 | + ]); |
| 29 | + |
| 30 | + assert_eq!(get_help(app, &["test"]), normalize(" |
| 31 | + test |
| 32 | +
|
| 33 | + USAGE: |
| 34 | + test [FLAGS] [OPTIONS] |
| 35 | +
|
| 36 | + FLAGS: |
| 37 | + --flag_a second flag |
| 38 | + --flag_b first flag |
| 39 | + -h, --help Prints help information |
| 40 | + -V, --version Prints version information |
| 41 | +
|
| 42 | + OPTIONS: |
| 43 | + --option_a <option_a> second option |
| 44 | + --option_b <option_b> first option |
| 45 | + ")); |
| 46 | +} |
| 47 | + |
| 48 | +#[test] |
| 49 | +fn derive_order() { |
| 50 | + let app = App::new("test") |
| 51 | + .setting(AppSettings::DeriveDisplayOrder) |
| 52 | + .args(&[ |
| 53 | + Arg::with_name("flag_b").long("flag_b").help("first flag"), |
| 54 | + Arg::with_name("option_b").long("option_b").takes_value(true).help("first option"), |
| 55 | + Arg::with_name("flag_a").long("flag_a").help("second flag"), |
| 56 | + Arg::with_name("option_a").long("option_a").takes_value(true).help("second option"), |
| 57 | + ]); |
| 58 | + |
| 59 | + assert_eq!(get_help(app, &["test"]), normalize(" |
| 60 | + test |
| 61 | +
|
| 62 | + USAGE: |
| 63 | + test [FLAGS] [OPTIONS] |
| 64 | +
|
| 65 | + FLAGS: |
| 66 | + --flag_b first flag |
| 67 | + --flag_a second flag |
| 68 | + -h, --help Prints help information |
| 69 | + -V, --version Prints version information |
| 70 | +
|
| 71 | + OPTIONS: |
| 72 | + --option_b <option_b> first option |
| 73 | + --option_a <option_a> second option |
| 74 | + ")); |
| 75 | +} |
| 76 | + |
| 77 | +#[test] |
| 78 | +fn unified_help() { |
| 79 | + let app = App::new("test") |
| 80 | + .setting(AppSettings::UnifiedHelpMessage) |
| 81 | + .args(&[ |
| 82 | + Arg::with_name("flag_b").long("flag_b").help("first flag"), |
| 83 | + Arg::with_name("option_b").long("option_b").takes_value(true).help("first option"), |
| 84 | + Arg::with_name("flag_a").long("flag_a").help("second flag"), |
| 85 | + Arg::with_name("option_a").long("option_a").takes_value(true).help("second option"), |
| 86 | + ]); |
| 87 | + |
| 88 | + assert_eq!(get_help(app, &["test"]), normalize(" |
| 89 | + test |
| 90 | +
|
| 91 | + USAGE: |
| 92 | + test [OPTIONS] |
| 93 | +
|
| 94 | + OPTIONS: |
| 95 | + --flag_a second flag |
| 96 | + --flag_b first flag |
| 97 | + -h, --help Prints help information |
| 98 | + --option_a <option_a> second option |
| 99 | + --option_b <option_b> first option |
| 100 | + -V, --version Prints version information |
| 101 | + ")); |
| 102 | +} |
| 103 | + |
| 104 | +#[test] |
| 105 | +fn unified_help_and_derive_order() { |
| 106 | + let app = App::new("test") |
| 107 | + .setting(AppSettings::DeriveDisplayOrder) |
| 108 | + .setting(AppSettings::UnifiedHelpMessage) |
| 109 | + .args(&[ |
| 110 | + Arg::with_name("flag_b").long("flag_b").help("first flag"), |
| 111 | + Arg::with_name("option_b").long("option_b").takes_value(true).help("first option"), |
| 112 | + Arg::with_name("flag_a").long("flag_a").help("second flag"), |
| 113 | + Arg::with_name("option_a").long("option_a").takes_value(true).help("second option"), |
| 114 | + ]); |
| 115 | + |
| 116 | + assert_eq!(get_help(app, &["test"]), normalize(" |
| 117 | + test |
| 118 | +
|
| 119 | + USAGE: |
| 120 | + test [OPTIONS] |
| 121 | +
|
| 122 | + OPTIONS: |
| 123 | + --flag_b first flag |
| 124 | + --option_b <option_b> first option |
| 125 | + --flag_a second flag |
| 126 | + --option_a <option_a> second option |
| 127 | + -h, --help Prints help information |
| 128 | + -V, --version Prints version information |
| 129 | + ")); |
| 130 | +} |
| 131 | + |
| 132 | +#[test] |
| 133 | +fn derive_order_subcommand_propagate() { |
| 134 | + let app = App::new("test") |
| 135 | + .global_setting(AppSettings::DeriveDisplayOrder) |
| 136 | + .subcommand(SubCommand::with_name("sub") |
| 137 | + .args(&[ |
| 138 | + Arg::with_name("flag_b").long("flag_b").help("first flag"), |
| 139 | + Arg::with_name("option_b").long("option_b").takes_value(true).help("first option"), |
| 140 | + Arg::with_name("flag_a").long("flag_a").help("second flag"), |
| 141 | + Arg::with_name("option_a").long("option_a").takes_value(true).help("second option"), |
| 142 | + ])); |
| 143 | + |
| 144 | + assert_eq!(get_help(app, &["test", "sub"]), normalize(" |
| 145 | + test-sub |
| 146 | +
|
| 147 | + USAGE: |
| 148 | + test sub [FLAGS] [OPTIONS] |
| 149 | +
|
| 150 | + FLAGS: |
| 151 | + --flag_b first flag |
| 152 | + --flag_a second flag |
| 153 | + -h, --help Prints help information |
| 154 | + -V, --version Prints version information |
| 155 | +
|
| 156 | + OPTIONS: |
| 157 | + --option_b <option_b> first option |
| 158 | + --option_a <option_a> second option |
| 159 | + ")); |
| 160 | +} |
| 161 | + |
| 162 | +#[test] |
| 163 | +fn unified_help_subcommand_propagate() { |
| 164 | + let app = App::new("test") |
| 165 | + .global_setting(AppSettings::UnifiedHelpMessage) |
| 166 | + .subcommand(SubCommand::with_name("sub") |
| 167 | + .args(&[ |
| 168 | + Arg::with_name("flag_b").long("flag_b").help("first flag"), |
| 169 | + Arg::with_name("option_b").long("option_b").takes_value(true).help("first option"), |
| 170 | + Arg::with_name("flag_a").long("flag_a").help("second flag"), |
| 171 | + Arg::with_name("option_a").long("option_a").takes_value(true).help("second option"), |
| 172 | + ])); |
| 173 | + |
| 174 | + assert_eq!(get_help(app, &["test", "sub"]), normalize(" |
| 175 | + test-sub |
| 176 | +
|
| 177 | + USAGE: |
| 178 | + test sub [OPTIONS] |
| 179 | +
|
| 180 | + OPTIONS: |
| 181 | + --flag_a second flag |
| 182 | + --flag_b first flag |
| 183 | + -h, --help Prints help information |
| 184 | + --option_a <option_a> second option |
| 185 | + --option_b <option_b> first option |
| 186 | + -V, --version Prints version information |
| 187 | +
|
| 188 | + ")); |
| 189 | +} |
| 190 | + |
| 191 | +#[test] |
| 192 | +fn unified_help_and_derive_order_subcommand_propagate() { |
| 193 | + let app = App::new("test") |
| 194 | + .global_setting(AppSettings::DeriveDisplayOrder) |
| 195 | + .global_setting(AppSettings::UnifiedHelpMessage) |
| 196 | + .subcommand(SubCommand::with_name("sub") |
| 197 | + .args(&[ |
| 198 | + Arg::with_name("flag_b").long("flag_b").help("first flag"), |
| 199 | + Arg::with_name("option_b").long("option_b").takes_value(true).help("first option"), |
| 200 | + Arg::with_name("flag_a").long("flag_a").help("second flag"), |
| 201 | + Arg::with_name("option_a").long("option_a").takes_value(true).help("second option"), |
| 202 | + ])); |
| 203 | + |
| 204 | + assert_eq!(get_help(app, &["test", "sub"]), normalize(" |
| 205 | + test-sub |
| 206 | +
|
| 207 | + USAGE: |
| 208 | + test sub [OPTIONS] |
| 209 | +
|
| 210 | + OPTIONS: |
| 211 | + --flag_b first flag |
| 212 | + --option_b <option_b> first option |
| 213 | + --flag_a second flag |
| 214 | + --option_a <option_a> second option |
| 215 | + -h, --help Prints help information |
| 216 | + -V, --version Prints version information |
| 217 | +
|
| 218 | + ")); |
| 219 | +} |
| 220 | + |
| 221 | +#[test] |
| 222 | +fn unified_help_and_derive_order_subcommand_propagate_with_explicit_display_order() { |
| 223 | + let app = App::new("test") |
| 224 | + .global_setting(AppSettings::DeriveDisplayOrder) |
| 225 | + .global_setting(AppSettings::UnifiedHelpMessage) |
| 226 | + .subcommand(SubCommand::with_name("sub") |
| 227 | + .args(&[ |
| 228 | + Arg::with_name("flag_b").long("flag_b").help("first flag"), |
| 229 | + Arg::with_name("option_b").long("option_b").takes_value(true).help("first option"), |
| 230 | + Arg::with_name("flag_a").long("flag_a").help("second flag").display_order(0), |
| 231 | + Arg::with_name("option_a").long("option_a").takes_value(true).help("second option"), |
| 232 | + ])); |
| 233 | + |
| 234 | + assert_eq!(get_help(app, &["test", "sub"]), normalize(" |
| 235 | + test-sub |
| 236 | +
|
| 237 | + USAGE: |
| 238 | + test sub [OPTIONS] |
| 239 | +
|
| 240 | + OPTIONS: |
| 241 | + --flag_a second flag |
| 242 | + --flag_b first flag |
| 243 | + --option_b <option_b> first option |
| 244 | + --option_a <option_a> second option |
| 245 | + -h, --help Prints help information |
| 246 | + -V, --version Prints version information |
| 247 | +
|
| 248 | + ")); |
| 249 | +} |
0 commit comments