Commit 6b6af4f56133a920b1b79bb93276c67c5b3241ec

Authored by Jean-Baptiste Bayle
Committed by jarro2783
1 parent 4f3fda4b

Add a method to remove the implicit value of an option (#178)

include/cxxopts.hpp
... ... @@ -314,6 +314,9 @@ namespace cxxopts
314 314 virtual std::shared_ptr<Value>
315 315 implicit_value(const std::string& value) = 0;
316 316  
  317 + virtual std::shared_ptr<Value>
  318 + no_implicit_value() = 0;
  319 +
317 320 virtual bool
318 321 is_boolean() const = 0;
319 322 };
... ... @@ -834,6 +837,13 @@ namespace cxxopts
834 837 return shared_from_this();
835 838 }
836 839  
  840 + std::shared_ptr<Value>
  841 + no_implicit_value()
  842 + {
  843 + m_implicit = false;
  844 + return shared_from_this();
  845 + }
  846 +
837 847 std::string
838 848 get_default_value() const
839 849 {
... ...
test/options.cpp
... ... @@ -250,6 +250,67 @@ TEST_CASE(&quot;Empty with implicit value&quot;, &quot;[implicit]&quot;)
250 250 REQUIRE(result["implicit"].as<std::string>() == "");
251 251 }
252 252  
  253 +TEST_CASE("Boolean without implicit value", "[implicit]")
  254 +{
  255 + cxxopts::Options options("no_implicit", "bool without an implicit value");
  256 + options.add_options()
  257 + ("bool", "Boolean without implicit", cxxopts::value<bool>()
  258 + ->no_implicit_value());
  259 +
  260 + SECTION("When no value provided") {
  261 + Argv av({"no_implicit", "--bool"});
  262 +
  263 + char** argv = av.argv();
  264 + auto argc = av.argc();
  265 +
  266 + CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::missing_argument_exception&);
  267 + }
  268 +
  269 + SECTION("With equal-separated true") {
  270 + Argv av({"no_implicit", "--bool=true"});
  271 +
  272 + char** argv = av.argv();
  273 + auto argc = av.argc();
  274 +
  275 + auto result = options.parse(argc, argv);
  276 + CHECK(result.count("bool") == 1);
  277 + CHECK(result["bool"].as<bool>() == true);
  278 + }
  279 +
  280 + SECTION("With equal-separated false") {
  281 + Argv av({"no_implicit", "--bool=false"});
  282 +
  283 + char** argv = av.argv();
  284 + auto argc = av.argc();
  285 +
  286 + auto result = options.parse(argc, argv);
  287 + CHECK(result.count("bool") == 1);
  288 + CHECK(result["bool"].as<bool>() == false);
  289 + }
  290 +
  291 + SECTION("With space-separated true") {
  292 + Argv av({"no_implicit", "--bool", "true"});
  293 +
  294 + char** argv = av.argv();
  295 + auto argc = av.argc();
  296 +
  297 + auto result = options.parse(argc, argv);
  298 + CHECK(result.count("bool") == 1);
  299 + CHECK(result["bool"].as<bool>() == true);
  300 + }
  301 +
  302 + SECTION("With space-separated false") {
  303 + Argv av({"no_implicit", "--bool", "false"});
  304 +
  305 + char** argv = av.argv();
  306 + auto argc = av.argc();
  307 +
  308 + auto result = options.parse(argc, argv);
  309 + CHECK(result.count("bool") == 1);
  310 + CHECK(result["bool"].as<bool>() == false);
  311 + }
  312 +}
  313 +
253 314 TEST_CASE("Default values", "[default]")
254 315 {
255 316 cxxopts::Options options("defaults", "has defaults");
... ...