From fc6e1c7a43c5dafbc157b1e85592acd95bcada02 Mon Sep 17 00:00:00 2001 From: Philip Top Date: Tue, 20 Aug 2019 05:54:31 -0700 Subject: [PATCH] add const version of validator modifiers --- include/CLI/Validators.hpp | 28 ++++++++++++++++++++++++++-- tests/AppTest.cpp | 13 ++++++++----- tests/HelpTest.cpp | 10 ++++++++++ 3 files changed, 44 insertions(+), 7 deletions(-) diff --git a/include/CLI/Validators.hpp b/include/CLI/Validators.hpp index adb3d375..e3736845 100644 --- a/include/CLI/Validators.hpp +++ b/include/CLI/Validators.hpp @@ -43,7 +43,7 @@ class Validator { std::function 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 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 diff --git a/tests/AppTest.cpp b/tests/AppTest.cpp index 186a8294..8037cae2 100644 --- a/tests/AppTest.cpp +++ b/tests/AppTest.cpp @@ -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); } diff --git a/tests/HelpTest.cpp b/tests/HelpTest.cpp index 2ca7655a..229d441b 100644 --- a/tests/HelpTest.cpp +++ b/tests/HelpTest.cpp @@ -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;