imp: adds debug_asserts against generating meaningless flags

This commit adds several debug asserts that ensure the user has not
accidentally generated a version flag that has no information. The
exception to this case is when the user wants to generate a version
flag, but then handle the version display manually. I.e. one can still
generate a "meaningless" version flag, but use
`AppSettings::NoAutoVersion` which is allowed.
This commit is contained in:
Kevin K 2021-10-08 13:49:21 -04:00
parent 07aae5ac54
commit bb26ed1c8b
No known key found for this signature in database
GPG key ID: 17218E4B3692F01A

View file

@ -1,4 +1,8 @@
use crate::{build::arg::debug_asserts::assert_arg, App, AppSettings, ArgSettings, ValueHint};
use crate::{
build::arg::{debug_asserts::assert_arg, ArgProvider},
util::Id,
App, AppSettings, ArgSettings, ValueHint,
};
use std::cmp::Ordering;
#[derive(Eq)]
@ -44,6 +48,26 @@ pub(crate) fn assert_app(app: &App) {
let mut short_flags = vec![];
let mut long_flags = vec![];
// Invalid version flag settings
if app.version.is_none() && app.long_version.is_none() {
// PropagateVersion is meaningless if there is no version
assert!(
!app.settings.is_set(AppSettings::PropagateVersion),
"No version information via App::version or App::long_version to propagate"
);
// Used `App::mut_arg("version", ..) but did not provide any version information to display
let has_mutated_version = app
.args
.args()
.any(|x| x.id == Id::version_hash() && x.provider == ArgProvider::GeneratedMutated);
if has_mutated_version {
assert!(app.settings.is_set(AppSettings::NoAutoVersion),
"Used App::mut_arg(\"version\", ..) without providing App::version, App::long_version or using AppSettings::NoAutoVersion");
}
}
for sc in &app.subcommands {
if let Some(s) = sc.short_flag.as_ref() {
short_flags.push(Flag::App(format!("-{}", s), &sc.name));