mirror of
https://github.com/CLIUtils/CLI11.git
synced 2025-04-29 12:13:52 +00:00
Adding test and keeping validator API similar
This commit is contained in:
parent
3006f82bf6
commit
038eafed29
@ -220,8 +220,13 @@ class Option : public OptionBase<Option> {
|
||||
}
|
||||
|
||||
/// Adds a validator
|
||||
Option *check(std::function<bool(std::string &)> validator) {
|
||||
Option *check(std::function<bool(const std::string &)> validator) {
|
||||
validators_.emplace_back(validator);
|
||||
return this;
|
||||
}
|
||||
|
||||
/// Adds a validator-like function that can change result
|
||||
Option *transform(std::function<bool(std::string &)> validator) {
|
||||
validators_.push_back(validator);
|
||||
return this;
|
||||
}
|
||||
|
@ -19,7 +19,7 @@ namespace CLI {
|
||||
/// @defgroup validator_group Validators
|
||||
/// @brief Some validators that are provided
|
||||
///
|
||||
/// These are simple `bool(std::string)` validators that are useful.
|
||||
/// These are simple `bool(std::string&)` validators that are useful.
|
||||
/// @{
|
||||
|
||||
/// Check for an existing file
|
||||
|
@ -1171,3 +1171,23 @@ TEST_F(TApp, SetWithDefaultsIC) {
|
||||
|
||||
EXPECT_THROW(run(), CLI::ConversionError);
|
||||
}
|
||||
|
||||
// Added to test defaults on dual method
|
||||
TEST_F(TApp, OrderedModifingValidators) {
|
||||
std::vector<std::string> val;
|
||||
auto m = app.add_option("-m", val);
|
||||
m->transform([](std::string &x) {
|
||||
x += "1";
|
||||
return true;
|
||||
});
|
||||
m->transform([](std::string &x) {
|
||||
x += "2";
|
||||
return true;
|
||||
});
|
||||
|
||||
args = {"-mone", "-mtwo"};
|
||||
|
||||
run();
|
||||
|
||||
EXPECT_EQ(val, std::vector<std::string>({"one12", "two12"}));
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user