mirror of
https://github.com/CLIUtils/CLI11.git
synced 2025-04-29 12:13:52 +00:00
add const version of validator modifiers
This commit is contained in:
parent
67c441b527
commit
fc6e1c7a43
@ -43,7 +43,7 @@ class Validator {
|
||||
std::function<std::string(std::string &)> func_{[](std::string &) { return std::string{}; }};
|
||||
/// The name for search purposes of the Validator
|
||||
std::string name_;
|
||||
/// A validate will only apply to an indexed value (-1 is all elements)
|
||||
/// A Validator will only apply to an indexed value (-1 is all elements)
|
||||
int application_index_ = -1;
|
||||
/// Enable for Validator to allow it to be disabled if need be
|
||||
bool active_{true};
|
||||
@ -54,7 +54,7 @@ class Validator {
|
||||
Validator() = default;
|
||||
/// Construct a Validator with just the description string
|
||||
explicit Validator(std::string validator_desc) : desc_function_([validator_desc]() { return validator_desc; }) {}
|
||||
// Construct Validator from basic information
|
||||
/// Construct Validator from basic information
|
||||
Validator(std::function<std::string(std::string &)> op, std::string validator_desc, std::string validator_name = "")
|
||||
: desc_function_([validator_desc]() { return validator_desc; }), func_(std::move(op)),
|
||||
name_(std::move(validator_name)) {}
|
||||
@ -90,6 +90,12 @@ class Validator {
|
||||
desc_function_ = [validator_desc]() { return validator_desc; };
|
||||
return *this;
|
||||
}
|
||||
/// Specify the type string
|
||||
Validator description(std::string validator_desc) const {
|
||||
Validator newval(*this);
|
||||
newval.desc_function_ = [validator_desc]() { return validator_desc; };
|
||||
return newval;
|
||||
}
|
||||
/// Generate type description information for the Validator
|
||||
std::string get_description() const {
|
||||
if(active_) {
|
||||
@ -102,6 +108,12 @@ class Validator {
|
||||
name_ = std::move(validator_name);
|
||||
return *this;
|
||||
}
|
||||
/// Specify the type string
|
||||
Validator name(std::string validator_name) const {
|
||||
Validator newval(*this);
|
||||
newval.name_ = std::move(validator_name);
|
||||
return newval;
|
||||
}
|
||||
/// Get the name of the Validator
|
||||
const std::string &get_name() const { return name_; }
|
||||
/// Specify whether the Validator is active or not
|
||||
@ -109,6 +121,12 @@ class Validator {
|
||||
active_ = active_val;
|
||||
return *this;
|
||||
}
|
||||
/// Specify whether the Validator is active or not
|
||||
Validator active(bool active_val = true) const {
|
||||
Validator newval(*this);
|
||||
newval.active_ = active_val;
|
||||
return newval;
|
||||
}
|
||||
|
||||
/// Specify whether the Validator can be modifying or not
|
||||
Validator &non_modifying(bool no_modify = true) {
|
||||
@ -120,6 +138,12 @@ class Validator {
|
||||
application_index_ = app_index;
|
||||
return *this;
|
||||
};
|
||||
/// Specify the application index of a validator
|
||||
Validator application_index(int app_index) const {
|
||||
Validator newval(*this);
|
||||
newval.application_index_ = app_index;
|
||||
return newval;
|
||||
};
|
||||
/// Get the current value of the application index
|
||||
int get_application_index() const { return application_index_; }
|
||||
/// Get a boolean if the validator is active
|
||||
|
@ -1077,8 +1077,9 @@ TEST_F(TApp, PositionalValidation) {
|
||||
std::string options;
|
||||
std::string foo;
|
||||
|
||||
app.add_option("bar", options)->check(CLI::Number);
|
||||
app.add_option("foo", foo);
|
||||
app.add_option("bar", options)->check(CLI::Number.name("valbar"));
|
||||
// disable the check on foo
|
||||
app.add_option("foo", foo)->check(CLI::Number.active(false));
|
||||
app.validate_positionals();
|
||||
args = {"1", "param1"};
|
||||
run();
|
||||
@ -1087,10 +1088,12 @@ TEST_F(TApp, PositionalValidation) {
|
||||
EXPECT_EQ(foo, "param1");
|
||||
|
||||
args = {"param1", "1"};
|
||||
run();
|
||||
EXPECT_NO_THROW(run());
|
||||
|
||||
EXPECT_EQ(options, "1");
|
||||
EXPECT_EQ(foo, "param1");
|
||||
|
||||
EXPECT_NE(app.get_option("bar")->get_validator("valbar"), nullptr);
|
||||
}
|
||||
|
||||
TEST_F(TApp, PositionalNoSpaceLong) {
|
||||
@ -1696,12 +1699,12 @@ TEST_F(TApp, VectorIndexedValidator) {
|
||||
run();
|
||||
EXPECT_EQ(4u, app.count("-v"));
|
||||
EXPECT_EQ(4u, vvec.size());
|
||||
opt->check(CLI::Validator(CLI::PositiveNumber).application_index(0));
|
||||
opt->check(CLI::PositiveNumber.application_index(0));
|
||||
opt->check((!CLI::PositiveNumber).application_index(1));
|
||||
EXPECT_NO_THROW(run());
|
||||
EXPECT_EQ(4u, vvec.size());
|
||||
// v[3] would be negative
|
||||
opt->check(CLI::Validator(CLI::PositiveNumber).application_index(3));
|
||||
opt->check(CLI::PositiveNumber.application_index(3));
|
||||
EXPECT_THROW(run(), CLI::ValidationError);
|
||||
}
|
||||
|
||||
|
@ -798,6 +798,16 @@ TEST(THelp, ValidatorsText) {
|
||||
EXPECT_THAT(help, HasSubstr("UINT:INT in [0 - 12]")); // Loses UINT
|
||||
}
|
||||
|
||||
TEST(THelp, ValidatorsTextCustom) {
|
||||
CLI::App app;
|
||||
|
||||
std::string filename;
|
||||
app.add_option("--f1", filename)->check(CLI::ExistingFile.description("Existing file"));
|
||||
|
||||
std::string help = app.help();
|
||||
EXPECT_THAT(help, HasSubstr("Existing file"));
|
||||
}
|
||||
|
||||
TEST(THelp, ValidatorsNonPathText) {
|
||||
CLI::App app;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user