Commit e779d15ef8d3f62a131050b4609b591634000813

Authored by Henry Fredrick Schreiner
1 parent 897cb3e7

Adding complex numbers

CHANGELOG.md
@@ -5,6 +5,7 @@ @@ -5,6 +5,7 @@
5 * Ini output now includes `=false` when `default_also` is true 5 * Ini output now includes `=false` when `default_also` is true
6 * Ini no longer lists the help pointer 6 * Ini no longer lists the help pointer
7 * Added test for inclusion in multiple files and linking, fixed issues (rarely needed for CLI, but nice for tools) 7 * Added test for inclusion in multiple files and linking, fixed issues (rarely needed for CLI, but nice for tools)
  8 +* Support for complex numbers
8 9
9 ## Version 0.8 10 ## Version 0.8
10 11
README.md
@@ -101,6 +101,8 @@ app.add_options(option_name, @@ -101,6 +101,8 @@ app.add_options(option_name,
101 help_string="", 101 help_string="",
102 default=false) 102 default=false)
103 103
  104 +app.add_complex(... // Special case: support for complex numbers
  105 +
104 app.add_flag(option_name, 106 app.add_flag(option_name,
105 int_or_bool = nothing, 107 int_or_bool = nothing,
106 help_string="") 108 help_string="")
include/CLI/App.hpp
@@ -399,6 +399,33 @@ public: @@ -399,6 +399,33 @@ public:
399 return opt; 399 return opt;
400 } 400 }
401 401
  402 + /// Add a complex number
  403 + template<typename T>
  404 + Option* add_complex(
  405 + std::string name, T& variable,
  406 + std::string description="", bool defaulted=false,
  407 + std::string label="COMPLEX") {
  408 + CLI::callback_t fun = [&variable](results_t res){
  409 + if(res.size()!=2)
  410 + return false;
  411 + double x,y;
  412 + bool worked = detail::lexical_cast(res[0], x)
  413 + && detail::lexical_cast(res[1], y);
  414 + if(worked)
  415 + variable = T(x,y);
  416 + return worked;
  417 + };
  418 +
  419 + CLI::Option* opt = add_option(name, fun, description, defaulted);
  420 + opt->set_custom_option(label, 2);
  421 + if(defaulted) {
  422 + std::stringstream out;
  423 + out << variable;
  424 + opt->set_default_val(out.str());
  425 + }
  426 + return opt;
  427 + }
  428 +
402 429
403 /// Add a configuration ini file option 430 /// Add a configuration ini file option
404 Option* add_config(std::string name="--config", 431 Option* add_config(std::string name="--config",
tests/NewParseTest.cpp
@@ -58,3 +58,34 @@ TEST_F(TApp, DefaultComplex) { @@ -58,3 +58,34 @@ TEST_F(TApp, DefaultComplex) {
58 58
59 EXPECT_EQ(cx(4,3), comp); 59 EXPECT_EQ(cx(4,3), comp);
60 } 60 }
  61 +
  62 +TEST_F(TApp, BuiltinComplex) {
  63 + cx comp {1, 2};
  64 + app.add_complex("-c,--complex", comp, "", true);
  65 +
  66 + args = {"-c", "4", "3"};
  67 +
  68 + std::string help = app.help();
  69 + EXPECT_THAT(help, HasSubstr("1"));
  70 + EXPECT_THAT(help, HasSubstr("2"));
  71 + EXPECT_THAT(help, HasSubstr("COMPLEX"));
  72 +
  73 + EXPECT_EQ(cx(1,2), comp);
  74 +
  75 + run();
  76 +
  77 + EXPECT_EQ(cx(4,3), comp);
  78 +
  79 +}
  80 +
  81 +
  82 +TEST_F(TApp, BuiltinComplexIgnoreI) {
  83 + cx comp {1, 2};
  84 + app.add_complex("-c,--complex", comp);
  85 +
  86 + args = {"-c", "4", "3i"};
  87 +
  88 + run();
  89 +
  90 + EXPECT_EQ(cx(4,3), comp);
  91 +}