mirror of
https://github.com/clap-rs/clap
synced 2024-11-14 00:27:13 +00:00
edd0124af0
* Check for `DisableHelpFlag` along with `NoAutoHelp` Also applies for `DisableHelpSubcommand` and `DisableVersionFlag` with `NoAutoVersion` * Add tests for overriding help and version, and disabling version. These override by disabling the default and adding a new one. * Don't use `default_missing_value` for `override_version_using_short` * Check errors by the API. This changes the `disabled_version_long` and `disabled_version_short` tests. This is opposed to comparing the stderr output.
313 lines
7.6 KiB
Rust
313 lines
7.6 KiB
Rust
mod utils;
|
|
|
|
use std::str;
|
|
|
|
use clap::{App, AppSettings, Arg, ErrorKind};
|
|
|
|
static VERSION: &str = "clap-test v1.4.8\n";
|
|
|
|
#[test]
|
|
fn version_short() {
|
|
let m = App::new("test")
|
|
.author("Kevin K.")
|
|
.about("tests stuff")
|
|
.version("1.3")
|
|
.long_version("1.3 (abcdef12)")
|
|
.try_get_matches_from(vec!["myprog", "-V"]);
|
|
|
|
assert!(m.is_err());
|
|
let err = m.unwrap_err();
|
|
assert_eq!(err.kind, ErrorKind::DisplayVersion);
|
|
assert_eq!(err.to_string(), "test 1.3\n");
|
|
}
|
|
|
|
#[test]
|
|
fn version_long() {
|
|
let m = App::new("test")
|
|
.author("Kevin K.")
|
|
.about("tests stuff")
|
|
.version("1.3")
|
|
.long_version("1.3 (abcdef12)")
|
|
.try_get_matches_from(vec!["myprog", "--version"]);
|
|
|
|
assert!(m.is_err());
|
|
let err = m.unwrap_err();
|
|
assert_eq!(err.kind, ErrorKind::DisplayVersion);
|
|
assert_eq!(err.to_string(), "test 1.3 (abcdef12)\n");
|
|
}
|
|
|
|
#[test]
|
|
fn complex_version_output() {
|
|
let mut a = App::new("clap-test").version("v1.4.8");
|
|
let _ = a.try_get_matches_from_mut(vec![""]);
|
|
|
|
// Now we check the output of print_version()
|
|
assert_eq!(a.render_version(), VERSION);
|
|
}
|
|
|
|
fn prefer_user_app() -> App<'static> {
|
|
App::new("test")
|
|
.version("1.3")
|
|
.arg(
|
|
Arg::new("version1")
|
|
.long("version")
|
|
.short('V')
|
|
.about("some version"),
|
|
)
|
|
.subcommand(
|
|
App::new("foo").arg(
|
|
Arg::new("version1")
|
|
.long("version")
|
|
.short('V')
|
|
.about("some version"),
|
|
),
|
|
)
|
|
}
|
|
|
|
#[test]
|
|
fn prefer_user_version_long() {
|
|
let m = prefer_user_app().try_get_matches_from(vec!["test", "--version"]);
|
|
|
|
assert!(m.is_ok());
|
|
assert!(m.unwrap().is_present("version1"));
|
|
}
|
|
|
|
#[test]
|
|
fn prefer_user_version_short() {
|
|
let m = prefer_user_app().try_get_matches_from(vec!["test", "-V"]);
|
|
|
|
assert!(m.is_ok());
|
|
assert!(m.unwrap().is_present("version1"));
|
|
}
|
|
|
|
#[test]
|
|
fn prefer_user_subcmd_version_long() {
|
|
let m = prefer_user_app().try_get_matches_from(vec!["test", "foo", "--version"]);
|
|
|
|
assert!(m.is_ok());
|
|
assert!(m
|
|
.unwrap()
|
|
.subcommand_matches("foo")
|
|
.unwrap()
|
|
.is_present("version1"));
|
|
}
|
|
|
|
#[test]
|
|
fn prefer_user_subcmd_version_short() {
|
|
let m = prefer_user_app().try_get_matches_from(vec!["test", "foo", "-V"]);
|
|
|
|
assert!(m.is_ok());
|
|
assert!(m
|
|
.unwrap()
|
|
.subcommand_matches("foo")
|
|
.unwrap()
|
|
.is_present("version1"));
|
|
}
|
|
|
|
static OVERRIDE_VERSION_SHORT: &str = "test 1.3
|
|
|
|
USAGE:
|
|
test
|
|
|
|
FLAGS:
|
|
-h, --help Print help information
|
|
-v, --version Print version information
|
|
";
|
|
|
|
#[test]
|
|
fn override_version_short() {
|
|
let app = App::new("test")
|
|
.version("1.3")
|
|
.mut_arg("version", |a| a.short('v'));
|
|
|
|
let m = app.clone().try_get_matches_from(vec!["test", "-v"]);
|
|
|
|
assert!(m.is_err());
|
|
let err = m.unwrap_err();
|
|
assert_eq!(err.kind, ErrorKind::DisplayVersion);
|
|
assert_eq!(err.to_string(), "test 1.3\n");
|
|
|
|
let m = app.clone().try_get_matches_from(vec!["test", "--version"]);
|
|
|
|
assert!(m.is_err());
|
|
let err = m.unwrap_err();
|
|
assert_eq!(err.kind, ErrorKind::DisplayVersion);
|
|
assert_eq!(err.to_string(), "test 1.3\n");
|
|
|
|
assert!(utils::compare_output(
|
|
app,
|
|
"test -h",
|
|
OVERRIDE_VERSION_SHORT,
|
|
false
|
|
));
|
|
}
|
|
|
|
static OVERRIDE_VERSION_LONG: &str = "test 1.3
|
|
|
|
USAGE:
|
|
test [FLAGS]
|
|
|
|
FLAGS:
|
|
-h, --help Print help information
|
|
-V, --vers Print version information
|
|
";
|
|
|
|
#[test]
|
|
fn override_version_long() {
|
|
let app = App::new("test")
|
|
.version("1.3")
|
|
.mut_arg("version", |a| a.long("vers"));
|
|
|
|
let m = app.clone().try_get_matches_from(vec!["test", "-V"]);
|
|
|
|
assert!(m.is_err());
|
|
let err = m.unwrap_err();
|
|
assert_eq!(err.kind, ErrorKind::DisplayVersion);
|
|
assert_eq!(err.to_string(), "test 1.3\n");
|
|
|
|
let m = app.clone().try_get_matches_from(vec!["test", "--vers"]);
|
|
|
|
assert!(m.is_err());
|
|
let err = m.unwrap_err();
|
|
assert_eq!(err.kind, ErrorKind::DisplayVersion);
|
|
assert_eq!(err.to_string(), "test 1.3\n");
|
|
|
|
assert!(utils::compare_output(
|
|
app,
|
|
"test -h",
|
|
OVERRIDE_VERSION_LONG,
|
|
false
|
|
));
|
|
}
|
|
|
|
static OVERRIDE_VERSION_ABOUT: &str = "test 1.3
|
|
|
|
USAGE:
|
|
test
|
|
|
|
FLAGS:
|
|
-h, --help Print help information
|
|
-V, --version version info
|
|
";
|
|
|
|
#[test]
|
|
fn override_version_about() {
|
|
let app = App::new("test")
|
|
.version("1.3")
|
|
.mut_arg("version", |a| a.about("version info"));
|
|
|
|
assert!(utils::compare_output(
|
|
app.clone(),
|
|
"test -h",
|
|
OVERRIDE_VERSION_ABOUT,
|
|
false
|
|
));
|
|
assert!(utils::compare_output(
|
|
app,
|
|
"test --help",
|
|
OVERRIDE_VERSION_ABOUT,
|
|
false
|
|
));
|
|
}
|
|
|
|
static VERSION_ABOUT_MULTI_SC: &str = "myapp-subcmd-multi 1.0
|
|
|
|
USAGE:
|
|
myapp subcmd multi
|
|
|
|
FLAGS:
|
|
-h, --help Print help information
|
|
-V, --version Print custom version about text
|
|
";
|
|
|
|
#[test]
|
|
fn version_about_multi_subcmd() {
|
|
let app = App::new("myapp")
|
|
.mut_arg("version", |a| a.about("Print custom version about text"))
|
|
.subcommand(App::new("subcmd").subcommand(App::new("multi").version("1.0")));
|
|
|
|
assert!(utils::compare_output(
|
|
app.clone(),
|
|
"myapp help subcmd multi",
|
|
VERSION_ABOUT_MULTI_SC,
|
|
false
|
|
));
|
|
assert!(utils::compare_output(
|
|
app.clone(),
|
|
"myapp subcmd multi -h",
|
|
VERSION_ABOUT_MULTI_SC,
|
|
false
|
|
));
|
|
assert!(utils::compare_output(
|
|
app,
|
|
"myapp subcmd multi --help",
|
|
VERSION_ABOUT_MULTI_SC,
|
|
false
|
|
));
|
|
}
|
|
|
|
static VERSION_ABOUT_MULTI_SC_OVERRIDE: &str = "myapp-subcmd-multi 1.0
|
|
|
|
USAGE:
|
|
myapp subcmd multi
|
|
|
|
FLAGS:
|
|
-h, --help Print help information
|
|
-V, --version Print custom version about text from multi
|
|
";
|
|
|
|
#[test]
|
|
fn version_about_multi_subcmd_override() {
|
|
let app = App::new("myapp")
|
|
.mut_arg("version", |a| a.about("Print custom version about text"))
|
|
.subcommand(App::new("subcmd").subcommand(
|
|
App::new("multi").version("1.0").mut_arg("version", |a| {
|
|
a.about("Print custom version about text from multi")
|
|
}),
|
|
));
|
|
|
|
assert!(utils::compare_output(
|
|
app,
|
|
"myapp help subcmd multi",
|
|
VERSION_ABOUT_MULTI_SC_OVERRIDE,
|
|
false
|
|
));
|
|
}
|
|
|
|
#[test]
|
|
fn disabled_version_long() {
|
|
let app = App::new("foo").setting(AppSettings::DisableVersionFlag);
|
|
let res = app.try_get_matches_from(&["foo", "--version"]);
|
|
assert!(res.is_err());
|
|
let err = res.unwrap_err();
|
|
assert_eq!(err.kind, clap::ErrorKind::UnknownArgument);
|
|
assert_eq!(err.info, ["--version"]);
|
|
}
|
|
|
|
#[test]
|
|
fn disabled_version_short() {
|
|
let app = App::new("foo").setting(AppSettings::DisableVersionFlag);
|
|
let res = app.try_get_matches_from(&["foo", "-V"]);
|
|
assert!(res.is_err());
|
|
let err = res.unwrap_err();
|
|
assert_eq!(err.kind, clap::ErrorKind::UnknownArgument);
|
|
assert_eq!(err.info, ["-V"]);
|
|
}
|
|
|
|
#[test]
|
|
fn override_version_using_long() {
|
|
let app = App::new("foo")
|
|
.setting(AppSettings::DisableVersionFlag)
|
|
.arg(Arg::new("version").long("version"));
|
|
let matches = app.get_matches_from(&["foo", "--version"]);
|
|
assert!(matches.is_present("version"));
|
|
}
|
|
|
|
#[test]
|
|
fn override_version_using_short() {
|
|
let app = App::new("foo")
|
|
.arg(Arg::new("version").short('V'))
|
|
.setting(AppSettings::DisableVersionFlag);
|
|
let matches = app.get_matches_from(&["foo", "-V"]);
|
|
assert!(matches.is_present("version"));
|
|
}
|