Commit fc6e1c7a43c5dafbc157b1e85592acd95bcada02
Committed by
Henry Schreiner
1 parent
67c441b5
add const version of validator modifiers
Showing
3 changed files
with
44 additions
and
7 deletions
include/CLI/Validators.hpp
| @@ -43,7 +43,7 @@ class Validator { | @@ -43,7 +43,7 @@ class Validator { | ||
| 43 | std::function<std::string(std::string &)> func_{[](std::string &) { return std::string{}; }}; | 43 | std::function<std::string(std::string &)> func_{[](std::string &) { return std::string{}; }}; |
| 44 | /// The name for search purposes of the Validator | 44 | /// The name for search purposes of the Validator |
| 45 | std::string name_; | 45 | std::string name_; |
| 46 | - /// A validate will only apply to an indexed value (-1 is all elements) | 46 | + /// A Validator will only apply to an indexed value (-1 is all elements) |
| 47 | int application_index_ = -1; | 47 | int application_index_ = -1; |
| 48 | /// Enable for Validator to allow it to be disabled if need be | 48 | /// Enable for Validator to allow it to be disabled if need be |
| 49 | bool active_{true}; | 49 | bool active_{true}; |
| @@ -54,7 +54,7 @@ class Validator { | @@ -54,7 +54,7 @@ class Validator { | ||
| 54 | Validator() = default; | 54 | Validator() = default; |
| 55 | /// Construct a Validator with just the description string | 55 | /// Construct a Validator with just the description string |
| 56 | explicit Validator(std::string validator_desc) : desc_function_([validator_desc]() { return validator_desc; }) {} | 56 | explicit Validator(std::string validator_desc) : desc_function_([validator_desc]() { return validator_desc; }) {} |
| 57 | - // Construct Validator from basic information | 57 | + /// Construct Validator from basic information |
| 58 | Validator(std::function<std::string(std::string &)> op, std::string validator_desc, std::string validator_name = "") | 58 | Validator(std::function<std::string(std::string &)> op, std::string validator_desc, std::string validator_name = "") |
| 59 | : desc_function_([validator_desc]() { return validator_desc; }), func_(std::move(op)), | 59 | : desc_function_([validator_desc]() { return validator_desc; }), func_(std::move(op)), |
| 60 | name_(std::move(validator_name)) {} | 60 | name_(std::move(validator_name)) {} |
| @@ -90,6 +90,12 @@ class Validator { | @@ -90,6 +90,12 @@ class Validator { | ||
| 90 | desc_function_ = [validator_desc]() { return validator_desc; }; | 90 | desc_function_ = [validator_desc]() { return validator_desc; }; |
| 91 | return *this; | 91 | return *this; |
| 92 | } | 92 | } |
| 93 | + /// Specify the type string | ||
| 94 | + Validator description(std::string validator_desc) const { | ||
| 95 | + Validator newval(*this); | ||
| 96 | + newval.desc_function_ = [validator_desc]() { return validator_desc; }; | ||
| 97 | + return newval; | ||
| 98 | + } | ||
| 93 | /// Generate type description information for the Validator | 99 | /// Generate type description information for the Validator |
| 94 | std::string get_description() const { | 100 | std::string get_description() const { |
| 95 | if(active_) { | 101 | if(active_) { |
| @@ -102,6 +108,12 @@ class Validator { | @@ -102,6 +108,12 @@ class Validator { | ||
| 102 | name_ = std::move(validator_name); | 108 | name_ = std::move(validator_name); |
| 103 | return *this; | 109 | return *this; |
| 104 | } | 110 | } |
| 111 | + /// Specify the type string | ||
| 112 | + Validator name(std::string validator_name) const { | ||
| 113 | + Validator newval(*this); | ||
| 114 | + newval.name_ = std::move(validator_name); | ||
| 115 | + return newval; | ||
| 116 | + } | ||
| 105 | /// Get the name of the Validator | 117 | /// Get the name of the Validator |
| 106 | const std::string &get_name() const { return name_; } | 118 | const std::string &get_name() const { return name_; } |
| 107 | /// Specify whether the Validator is active or not | 119 | /// Specify whether the Validator is active or not |
| @@ -109,6 +121,12 @@ class Validator { | @@ -109,6 +121,12 @@ class Validator { | ||
| 109 | active_ = active_val; | 121 | active_ = active_val; |
| 110 | return *this; | 122 | return *this; |
| 111 | } | 123 | } |
| 124 | + /// Specify whether the Validator is active or not | ||
| 125 | + Validator active(bool active_val = true) const { | ||
| 126 | + Validator newval(*this); | ||
| 127 | + newval.active_ = active_val; | ||
| 128 | + return newval; | ||
| 129 | + } | ||
| 112 | 130 | ||
| 113 | /// Specify whether the Validator can be modifying or not | 131 | /// Specify whether the Validator can be modifying or not |
| 114 | Validator &non_modifying(bool no_modify = true) { | 132 | Validator &non_modifying(bool no_modify = true) { |
| @@ -120,6 +138,12 @@ class Validator { | @@ -120,6 +138,12 @@ class Validator { | ||
| 120 | application_index_ = app_index; | 138 | application_index_ = app_index; |
| 121 | return *this; | 139 | return *this; |
| 122 | }; | 140 | }; |
| 141 | + /// Specify the application index of a validator | ||
| 142 | + Validator application_index(int app_index) const { | ||
| 143 | + Validator newval(*this); | ||
| 144 | + newval.application_index_ = app_index; | ||
| 145 | + return newval; | ||
| 146 | + }; | ||
| 123 | /// Get the current value of the application index | 147 | /// Get the current value of the application index |
| 124 | int get_application_index() const { return application_index_; } | 148 | int get_application_index() const { return application_index_; } |
| 125 | /// Get a boolean if the validator is active | 149 | /// Get a boolean if the validator is active |
tests/AppTest.cpp
| @@ -1077,8 +1077,9 @@ TEST_F(TApp, PositionalValidation) { | @@ -1077,8 +1077,9 @@ TEST_F(TApp, PositionalValidation) { | ||
| 1077 | std::string options; | 1077 | std::string options; |
| 1078 | std::string foo; | 1078 | std::string foo; |
| 1079 | 1079 | ||
| 1080 | - app.add_option("bar", options)->check(CLI::Number); | ||
| 1081 | - app.add_option("foo", foo); | 1080 | + app.add_option("bar", options)->check(CLI::Number.name("valbar")); |
| 1081 | + // disable the check on foo | ||
| 1082 | + app.add_option("foo", foo)->check(CLI::Number.active(false)); | ||
| 1082 | app.validate_positionals(); | 1083 | app.validate_positionals(); |
| 1083 | args = {"1", "param1"}; | 1084 | args = {"1", "param1"}; |
| 1084 | run(); | 1085 | run(); |
| @@ -1087,10 +1088,12 @@ TEST_F(TApp, PositionalValidation) { | @@ -1087,10 +1088,12 @@ TEST_F(TApp, PositionalValidation) { | ||
| 1087 | EXPECT_EQ(foo, "param1"); | 1088 | EXPECT_EQ(foo, "param1"); |
| 1088 | 1089 | ||
| 1089 | args = {"param1", "1"}; | 1090 | args = {"param1", "1"}; |
| 1090 | - run(); | 1091 | + EXPECT_NO_THROW(run()); |
| 1091 | 1092 | ||
| 1092 | EXPECT_EQ(options, "1"); | 1093 | EXPECT_EQ(options, "1"); |
| 1093 | EXPECT_EQ(foo, "param1"); | 1094 | EXPECT_EQ(foo, "param1"); |
| 1095 | + | ||
| 1096 | + EXPECT_NE(app.get_option("bar")->get_validator("valbar"), nullptr); | ||
| 1094 | } | 1097 | } |
| 1095 | 1098 | ||
| 1096 | TEST_F(TApp, PositionalNoSpaceLong) { | 1099 | TEST_F(TApp, PositionalNoSpaceLong) { |
| @@ -1696,12 +1699,12 @@ TEST_F(TApp, VectorIndexedValidator) { | @@ -1696,12 +1699,12 @@ TEST_F(TApp, VectorIndexedValidator) { | ||
| 1696 | run(); | 1699 | run(); |
| 1697 | EXPECT_EQ(4u, app.count("-v")); | 1700 | EXPECT_EQ(4u, app.count("-v")); |
| 1698 | EXPECT_EQ(4u, vvec.size()); | 1701 | EXPECT_EQ(4u, vvec.size()); |
| 1699 | - opt->check(CLI::Validator(CLI::PositiveNumber).application_index(0)); | 1702 | + opt->check(CLI::PositiveNumber.application_index(0)); |
| 1700 | opt->check((!CLI::PositiveNumber).application_index(1)); | 1703 | opt->check((!CLI::PositiveNumber).application_index(1)); |
| 1701 | EXPECT_NO_THROW(run()); | 1704 | EXPECT_NO_THROW(run()); |
| 1702 | EXPECT_EQ(4u, vvec.size()); | 1705 | EXPECT_EQ(4u, vvec.size()); |
| 1703 | // v[3] would be negative | 1706 | // v[3] would be negative |
| 1704 | - opt->check(CLI::Validator(CLI::PositiveNumber).application_index(3)); | 1707 | + opt->check(CLI::PositiveNumber.application_index(3)); |
| 1705 | EXPECT_THROW(run(), CLI::ValidationError); | 1708 | EXPECT_THROW(run(), CLI::ValidationError); |
| 1706 | } | 1709 | } |
| 1707 | 1710 |
tests/HelpTest.cpp
| @@ -798,6 +798,16 @@ TEST(THelp, ValidatorsText) { | @@ -798,6 +798,16 @@ TEST(THelp, ValidatorsText) { | ||
| 798 | EXPECT_THAT(help, HasSubstr("UINT:INT in [0 - 12]")); // Loses UINT | 798 | EXPECT_THAT(help, HasSubstr("UINT:INT in [0 - 12]")); // Loses UINT |
| 799 | } | 799 | } |
| 800 | 800 | ||
| 801 | +TEST(THelp, ValidatorsTextCustom) { | ||
| 802 | + CLI::App app; | ||
| 803 | + | ||
| 804 | + std::string filename; | ||
| 805 | + app.add_option("--f1", filename)->check(CLI::ExistingFile.description("Existing file")); | ||
| 806 | + | ||
| 807 | + std::string help = app.help(); | ||
| 808 | + EXPECT_THAT(help, HasSubstr("Existing file")); | ||
| 809 | +} | ||
| 810 | + | ||
| 801 | TEST(THelp, ValidatorsNonPathText) { | 811 | TEST(THelp, ValidatorsNonPathText) { |
| 802 | CLI::App app; | 812 | CLI::App app; |
| 803 | 813 |