Commit fc6e1c7a43c5dafbc157b1e85592acd95bcada02

Authored by Philip Top
Committed by Henry Schreiner
1 parent 67c441b5

add const version of validator modifiers

include/CLI/Validators.hpp
... ... @@ -43,7 +43,7 @@ class Validator {
43 43 std::function<std::string(std::string &)> func_{[](std::string &) { return std::string{}; }};
44 44 /// The name for search purposes of the Validator
45 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 47 int application_index_ = -1;
48 48 /// Enable for Validator to allow it to be disabled if need be
49 49 bool active_{true};
... ... @@ -54,7 +54,7 @@ class Validator {
54 54 Validator() = default;
55 55 /// Construct a Validator with just the description string
56 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 58 Validator(std::function<std::string(std::string &)> op, std::string validator_desc, std::string validator_name = "")
59 59 : desc_function_([validator_desc]() { return validator_desc; }), func_(std::move(op)),
60 60 name_(std::move(validator_name)) {}
... ... @@ -90,6 +90,12 @@ class Validator {
90 90 desc_function_ = [validator_desc]() { return validator_desc; };
91 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 99 /// Generate type description information for the Validator
94 100 std::string get_description() const {
95 101 if(active_) {
... ... @@ -102,6 +108,12 @@ class Validator {
102 108 name_ = std::move(validator_name);
103 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 117 /// Get the name of the Validator
106 118 const std::string &get_name() const { return name_; }
107 119 /// Specify whether the Validator is active or not
... ... @@ -109,6 +121,12 @@ class Validator {
109 121 active_ = active_val;
110 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 131 /// Specify whether the Validator can be modifying or not
114 132 Validator &non_modifying(bool no_modify = true) {
... ... @@ -120,6 +138,12 @@ class Validator {
120 138 application_index_ = app_index;
121 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 147 /// Get the current value of the application index
124 148 int get_application_index() const { return application_index_; }
125 149 /// Get a boolean if the validator is active
... ...
tests/AppTest.cpp
... ... @@ -1077,8 +1077,9 @@ TEST_F(TApp, PositionalValidation) {
1077 1077 std::string options;
1078 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 1083 app.validate_positionals();
1083 1084 args = {"1", "param1"};
1084 1085 run();
... ... @@ -1087,10 +1088,12 @@ TEST_F(TApp, PositionalValidation) {
1087 1088 EXPECT_EQ(foo, "param1");
1088 1089  
1089 1090 args = {"param1", "1"};
1090   - run();
  1091 + EXPECT_NO_THROW(run());
1091 1092  
1092 1093 EXPECT_EQ(options, "1");
1093 1094 EXPECT_EQ(foo, "param1");
  1095 +
  1096 + EXPECT_NE(app.get_option("bar")->get_validator("valbar"), nullptr);
1094 1097 }
1095 1098  
1096 1099 TEST_F(TApp, PositionalNoSpaceLong) {
... ... @@ -1696,12 +1699,12 @@ TEST_F(TApp, VectorIndexedValidator) {
1696 1699 run();
1697 1700 EXPECT_EQ(4u, app.count("-v"));
1698 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 1703 opt->check((!CLI::PositiveNumber).application_index(1));
1701 1704 EXPECT_NO_THROW(run());
1702 1705 EXPECT_EQ(4u, vvec.size());
1703 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 1708 EXPECT_THROW(run(), CLI::ValidationError);
1706 1709 }
1707 1710  
... ...
tests/HelpTest.cpp
... ... @@ -798,6 +798,16 @@ TEST(THelp, ValidatorsText) {
798 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 811 TEST(THelp, ValidatorsNonPathText) {
802 812 CLI::App app;
803 813  
... ...