Commit e779d15ef8d3f62a131050b4609b591634000813
1 parent
897cb3e7
Adding complex numbers
Showing
4 changed files
with
61 additions
and
0 deletions
CHANGELOG.md
| ... | ... | @@ -5,6 +5,7 @@ |
| 5 | 5 | * Ini output now includes `=false` when `default_also` is true |
| 6 | 6 | * Ini no longer lists the help pointer |
| 7 | 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 | 10 | ## Version 0.8 |
| 10 | 11 | ... | ... |
README.md
include/CLI/App.hpp
| ... | ... | @@ -399,6 +399,33 @@ public: |
| 399 | 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 | 430 | /// Add a configuration ini file option |
| 404 | 431 | Option* add_config(std::string name="--config", | ... | ... |
tests/NewParseTest.cpp
| ... | ... | @@ -58,3 +58,34 @@ TEST_F(TApp, DefaultComplex) { |
| 58 | 58 | |
| 59 | 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 | +} | ... | ... |