Commit bf2bc39c30cd7f0208e838eef1b4b0671753c44c
Committed by
Henry Schreiner
1 parent
23cedc12
Rename to set_* on options and add return this
Now use type_name and type_size instead of set_custom_option.
Showing
8 changed files
with
63 additions
and
51 deletions
CHANGELOG.md
| ... | ... | @@ -36,6 +36,7 @@ Validators are now much more powerful [#118], all built in validators upgraded t |
| 36 | 36 | |
| 37 | 37 | Other changes: |
| 38 | 38 | |
| 39 | +* Dropped `set_*` names on options, using `type_name` and `type_size` instead of `set_custom_option`. Methods return this. | |
| 39 | 40 | * Added `->each()` to make adding custom callbacks easier [#126] |
| 40 | 41 | * Added filter argument to `get_subcommands`, `get_options`; use empty filter `{}` to avoid filtering |
| 41 | 42 | * Added `get_groups()` to get groups | ... | ... |
README.md
| ... | ... | @@ -180,7 +180,8 @@ The add commands return a pointer to an internally stored `Option`. If you set t |
| 180 | 180 | |
| 181 | 181 | * `->required()`: The program will quit if this option is not present. This is `mandatory` in Plumbum, but required options seems to be a more standard term. For compatibility, `->mandatory()` also works. |
| 182 | 182 | * `->expected(N)`: Take `N` values instead of as many as possible, only for vector args. If negative, require at least `-N`; end with `--` or another recognized option. |
| 183 | -* `->set_custom_option(typename, N)`: Set the name and (optional) intrinsic size of an option. The parser will require multiples of this number if negative. | |
| 183 | +* `->type_name(typename)`: Set the name of an Option's type (`type_name_fn` allows a function instead) | |
| 184 | +* `->type_size(N)`: Set the intrinsic size of an option. The parser will require multiples of this number if negative. | |
| 184 | 185 | * `->needs(opt)`: This option requires another option to also be present, opt is an `Option` pointer. |
| 185 | 186 | * `->excludes(opt)`: This option cannot be given with `opt` present, opt is an `Option` pointer. |
| 186 | 187 | * `->envname(name)`: Gets the value from the environment if present and not passed on the command line. | ... | ... |
examples/enum.cpp
| ... | ... | @@ -17,7 +17,7 @@ int main(int argc, char **argv) { |
| 17 | 17 | |
| 18 | 18 | Level level; |
| 19 | 19 | app.add_set("-l,--level", level, {Level::High, Level::Medium, Level::Low}, "Level settings") |
| 20 | - ->set_type_name("enum/Level in {High=0, Medium=1, Low=2}"); | |
| 20 | + ->type_name("enum/Level in {High=0, Medium=1, Low=2}"); | |
| 21 | 21 | |
| 22 | 22 | CLI11_PARSE(app, argc, argv); |
| 23 | 23 | ... | ... |
include/CLI/App.hpp
| ... | ... | @@ -331,7 +331,7 @@ class App { |
| 331 | 331 | CLI::callback_t fun = [&variable](CLI::results_t res) { return detail::lexical_cast(res[0], variable); }; |
| 332 | 332 | |
| 333 | 333 | Option *opt = add_option(name, fun, description, false); |
| 334 | - opt->set_custom_option(detail::type_name<T>()); | |
| 334 | + opt->type_name(detail::type_name<T>()); | |
| 335 | 335 | return opt; |
| 336 | 336 | } |
| 337 | 337 | |
| ... | ... | @@ -345,11 +345,11 @@ class App { |
| 345 | 345 | CLI::callback_t fun = [&variable](CLI::results_t res) { return detail::lexical_cast(res[0], variable); }; |
| 346 | 346 | |
| 347 | 347 | Option *opt = add_option(name, fun, description, defaulted); |
| 348 | - opt->set_custom_option(detail::type_name<T>()); | |
| 348 | + opt->type_name(detail::type_name<T>()); | |
| 349 | 349 | if(defaulted) { |
| 350 | 350 | std::stringstream out; |
| 351 | 351 | out << variable; |
| 352 | - opt->set_default_str(out.str()); | |
| 352 | + opt->default_str(out.str()); | |
| 353 | 353 | } |
| 354 | 354 | return opt; |
| 355 | 355 | } |
| ... | ... | @@ -371,7 +371,7 @@ class App { |
| 371 | 371 | }; |
| 372 | 372 | |
| 373 | 373 | Option *opt = add_option(name, fun, description, false); |
| 374 | - opt->set_custom_option(detail::type_name<T>(), -1); | |
| 374 | + opt->type_name(detail::type_name<T>())->type_size(-1); | |
| 375 | 375 | return opt; |
| 376 | 376 | } |
| 377 | 377 | |
| ... | ... | @@ -393,9 +393,9 @@ class App { |
| 393 | 393 | }; |
| 394 | 394 | |
| 395 | 395 | Option *opt = add_option(name, fun, description, defaulted); |
| 396 | - opt->set_custom_option(detail::type_name<T>(), -1); | |
| 396 | + opt->type_name(detail::type_name<T>())->type_size(-1); | |
| 397 | 397 | if(defaulted) |
| 398 | - opt->set_default_str("[" + detail::join(variable) + "]"); | |
| 398 | + opt->default_str("[" + detail::join(variable) + "]"); | |
| 399 | 399 | return opt; |
| 400 | 400 | } |
| 401 | 401 | |
| ... | ... | @@ -440,7 +440,7 @@ class App { |
| 440 | 440 | Option *opt = add_option(name, fun, description, false); |
| 441 | 441 | if(opt->get_positional()) |
| 442 | 442 | throw IncorrectConstruction::PositionalFlag(name); |
| 443 | - opt->set_custom_option("", 0); | |
| 443 | + opt->type_size(0); | |
| 444 | 444 | return opt; |
| 445 | 445 | } |
| 446 | 446 | |
| ... | ... | @@ -460,7 +460,7 @@ class App { |
| 460 | 460 | Option *opt = add_option(name, fun, description, false); |
| 461 | 461 | if(opt->get_positional()) |
| 462 | 462 | throw IncorrectConstruction::PositionalFlag(name); |
| 463 | - opt->set_custom_option("", 0); | |
| 463 | + opt->type_size(0); | |
| 464 | 464 | return opt; |
| 465 | 465 | } |
| 466 | 466 | |
| ... | ... | @@ -480,7 +480,7 @@ class App { |
| 480 | 480 | Option *opt = add_option(name, fun, description, false); |
| 481 | 481 | if(opt->get_positional()) |
| 482 | 482 | throw IncorrectConstruction::PositionalFlag(name); |
| 483 | - opt->set_custom_option("", 0); | |
| 483 | + opt->type_size(0); | |
| 484 | 484 | opt->multi_option_policy(CLI::MultiOptionPolicy::TakeLast); |
| 485 | 485 | return opt; |
| 486 | 486 | } |
| ... | ... | @@ -499,7 +499,7 @@ class App { |
| 499 | 499 | Option *opt = add_option(name, fun, description, false); |
| 500 | 500 | if(opt->get_positional()) |
| 501 | 501 | throw IncorrectConstruction::PositionalFlag(name); |
| 502 | - opt->set_custom_option("", 0); | |
| 502 | + opt->type_size(0); | |
| 503 | 503 | return opt; |
| 504 | 504 | } |
| 505 | 505 | |
| ... | ... | @@ -530,7 +530,7 @@ class App { |
| 530 | 530 | Option *opt = add_option(name, fun, description, false); |
| 531 | 531 | std::string typeval = detail::type_name<T>(); |
| 532 | 532 | typeval += " in {" + detail::join(options) + "}"; |
| 533 | - opt->set_custom_option(typeval); | |
| 533 | + opt->type_name(typeval); | |
| 534 | 534 | return opt; |
| 535 | 535 | } |
| 536 | 536 | |
| ... | ... | @@ -550,7 +550,7 @@ class App { |
| 550 | 550 | }; |
| 551 | 551 | |
| 552 | 552 | Option *opt = add_option(name, fun, description, false); |
| 553 | - opt->set_type_name_fn( | |
| 553 | + opt->type_name_fn( | |
| 554 | 554 | [&options]() { return std::string(detail::type_name<T>()) + " in {" + detail::join(options) + "}"; }); |
| 555 | 555 | |
| 556 | 556 | return opt; |
| ... | ... | @@ -575,11 +575,11 @@ class App { |
| 575 | 575 | Option *opt = add_option(name, fun, description, defaulted); |
| 576 | 576 | std::string typeval = detail::type_name<T>(); |
| 577 | 577 | typeval += " in {" + detail::join(options) + "}"; |
| 578 | - opt->set_custom_option(typeval); | |
| 578 | + opt->type_name(typeval); | |
| 579 | 579 | if(defaulted) { |
| 580 | 580 | std::stringstream out; |
| 581 | 581 | out << member; |
| 582 | - opt->set_default_str(out.str()); | |
| 582 | + opt->default_str(out.str()); | |
| 583 | 583 | } |
| 584 | 584 | return opt; |
| 585 | 585 | } |
| ... | ... | @@ -601,12 +601,12 @@ class App { |
| 601 | 601 | }; |
| 602 | 602 | |
| 603 | 603 | Option *opt = add_option(name, fun, description, defaulted); |
| 604 | - opt->set_type_name_fn( | |
| 604 | + opt->type_name_fn( | |
| 605 | 605 | [&options]() { return std::string(detail::type_name<T>()) + " in {" + detail::join(options) + "}"; }); |
| 606 | 606 | if(defaulted) { |
| 607 | 607 | std::stringstream out; |
| 608 | 608 | out << member; |
| 609 | - opt->set_default_str(out.str()); | |
| 609 | + opt->default_str(out.str()); | |
| 610 | 610 | } |
| 611 | 611 | return opt; |
| 612 | 612 | } |
| ... | ... | @@ -634,7 +634,7 @@ class App { |
| 634 | 634 | Option *opt = add_option(name, fun, description, false); |
| 635 | 635 | std::string typeval = detail::type_name<std::string>(); |
| 636 | 636 | typeval += " in {" + detail::join(options) + "}"; |
| 637 | - opt->set_custom_option(typeval); | |
| 637 | + opt->type_name(typeval); | |
| 638 | 638 | |
| 639 | 639 | return opt; |
| 640 | 640 | } |
| ... | ... | @@ -660,7 +660,7 @@ class App { |
| 660 | 660 | }; |
| 661 | 661 | |
| 662 | 662 | Option *opt = add_option(name, fun, description, false); |
| 663 | - opt->set_type_name_fn([&options]() { | |
| 663 | + opt->type_name_fn([&options]() { | |
| 664 | 664 | return std::string(detail::type_name<std::string>()) + " in {" + detail::join(options) + "}"; |
| 665 | 665 | }); |
| 666 | 666 | |
| ... | ... | @@ -691,9 +691,9 @@ class App { |
| 691 | 691 | Option *opt = add_option(name, fun, description, defaulted); |
| 692 | 692 | std::string typeval = detail::type_name<std::string>(); |
| 693 | 693 | typeval += " in {" + detail::join(options) + "}"; |
| 694 | - opt->set_custom_option(typeval); | |
| 694 | + opt->type_name(typeval); | |
| 695 | 695 | if(defaulted) { |
| 696 | - opt->set_default_str(member); | |
| 696 | + opt->default_str(member); | |
| 697 | 697 | } |
| 698 | 698 | return opt; |
| 699 | 699 | } |
| ... | ... | @@ -720,11 +720,11 @@ class App { |
| 720 | 720 | }; |
| 721 | 721 | |
| 722 | 722 | Option *opt = add_option(name, fun, description, defaulted); |
| 723 | - opt->set_type_name_fn([&options]() { | |
| 723 | + opt->type_name_fn([&options]() { | |
| 724 | 724 | return std::string(detail::type_name<std::string>()) + " in {" + detail::join(options) + "}"; |
| 725 | 725 | }); |
| 726 | 726 | if(defaulted) { |
| 727 | - opt->set_default_str(member); | |
| 727 | + opt->default_str(member); | |
| 728 | 728 | } |
| 729 | 729 | return opt; |
| 730 | 730 | } |
| ... | ... | @@ -749,11 +749,11 @@ class App { |
| 749 | 749 | }; |
| 750 | 750 | |
| 751 | 751 | CLI::Option *opt = add_option(name, fun, description, defaulted); |
| 752 | - opt->set_custom_option(label, 2); | |
| 752 | + opt->type_name(label)->type_size(2); | |
| 753 | 753 | if(defaulted) { |
| 754 | 754 | std::stringstream out; |
| 755 | 755 | out << variable; |
| 756 | - opt->set_default_str(out.str()); | |
| 756 | + opt->default_str(out.str()); | |
| 757 | 757 | } |
| 758 | 758 | return opt; |
| 759 | 759 | } | ... | ... |
include/CLI/Option.hpp
| ... | ... | @@ -174,7 +174,7 @@ class Option : public OptionBase<Option> { |
| 174 | 174 | /// A human readable type value, set when App creates this |
| 175 | 175 | /// |
| 176 | 176 | /// This is a lambda function so "types" can be dynamic, such as when a set prints its contents. |
| 177 | - std::function<std::string()> type_name_; | |
| 177 | + std::function<std::string()> type_name_{[]() { return std::string(); }}; | |
| 178 | 178 | |
| 179 | 179 | /// True if this option has a default |
| 180 | 180 | bool default_{false}; |
| ... | ... | @@ -285,7 +285,7 @@ class Option : public OptionBase<Option> { |
| 285 | 285 | Option *check(const Validator &validator) { |
| 286 | 286 | validators_.emplace_back(validator.func); |
| 287 | 287 | if(!validator.tname.empty()) |
| 288 | - set_type_name(validator.tname); | |
| 288 | + type_name(validator.tname); | |
| 289 | 289 | return this; |
| 290 | 290 | } |
| 291 | 291 | |
| ... | ... | @@ -630,15 +630,17 @@ class Option : public OptionBase<Option> { |
| 630 | 630 | } |
| 631 | 631 | |
| 632 | 632 | /// Puts a result at the end |
| 633 | - void add_result(std::string s) { | |
| 633 | + Option *add_result(std::string s) { | |
| 634 | 634 | results_.push_back(s); |
| 635 | 635 | callback_run_ = false; |
| 636 | + return this; | |
| 636 | 637 | } |
| 637 | 638 | |
| 638 | 639 | /// Set the results vector all at once |
| 639 | - void set_results(std::vector<std::string> results) { | |
| 640 | + Option *set_results(std::vector<std::string> results) { | |
| 640 | 641 | results_ = results; |
| 641 | 642 | callback_run_ = false; |
| 643 | + return this; | |
| 642 | 644 | } |
| 643 | 645 | |
| 644 | 646 | /// Get a copy of the results |
| ... | ... | @@ -651,36 +653,44 @@ class Option : public OptionBase<Option> { |
| 651 | 653 | /// @name Custom options |
| 652 | 654 | ///@{ |
| 653 | 655 | |
| 654 | - /// Set a custom option, typestring, type_size | |
| 655 | - void set_custom_option(std::string typeval, int type_size = 1) { | |
| 656 | - set_type_name(typeval); | |
| 656 | + /// Set the type function to run when displayed on this option | |
| 657 | + Option *type_name_fn(std::function<std::string()> typefun) { | |
| 658 | + type_name_ = typefun; | |
| 659 | + return this; | |
| 660 | + } | |
| 661 | + | |
| 662 | + /// Set a custom option typestring | |
| 663 | + Option *type_name(std::string typeval) { | |
| 664 | + type_name_fn([typeval]() { return typeval; }); | |
| 665 | + return this; | |
| 666 | + } | |
| 667 | + | |
| 668 | + /// Set a custom option size | |
| 669 | + Option *type_size(int type_size) { | |
| 657 | 670 | type_size_ = type_size; |
| 658 | 671 | if(type_size_ == 0) |
| 659 | 672 | required_ = false; |
| 660 | 673 | if(type_size < 0) |
| 661 | 674 | expected_ = -1; |
| 675 | + return this; | |
| 662 | 676 | } |
| 663 | 677 | |
| 664 | 678 | /// Set the default value string representation |
| 665 | - void set_default_str(std::string val) { defaultval_ = val; } | |
| 679 | + Option *default_str(std::string val) { | |
| 680 | + defaultval_ = val; | |
| 681 | + return this; | |
| 682 | + } | |
| 666 | 683 | |
| 667 | 684 | /// Set the default value string representation and evaluate |
| 668 | - void set_default_val(std::string val) { | |
| 669 | - set_default_str(val); | |
| 685 | + Option *default_val(std::string val) { | |
| 686 | + default_str(val); | |
| 670 | 687 | auto old_results = results_; |
| 671 | 688 | results_ = {val}; |
| 672 | 689 | run_callback(); |
| 673 | 690 | results_ = std::move(old_results); |
| 691 | + return this; | |
| 674 | 692 | } |
| 675 | 693 | |
| 676 | - /// Set the type name displayed on this option | |
| 677 | - void set_type_name(std::string typeval) { | |
| 678 | - set_type_name_fn([typeval]() { return typeval; }); | |
| 679 | - } | |
| 680 | - | |
| 681 | - /// Set the type function to run when displayed on this option | |
| 682 | - void set_type_name_fn(std::function<std::string()> typefun) { type_name_ = typefun; } | |
| 683 | - | |
| 684 | 694 | /// Get the typename for this option |
| 685 | 695 | std::string get_type_name() const { return type_name_(); } |
| 686 | 696 | }; | ... | ... |
tests/AppTest.cpp
| ... | ... | @@ -1504,7 +1504,7 @@ TEST_F(TApp, CustomDoubleOption) { |
| 1504 | 1504 | custom_opt = {stol(vals.at(0)), stod(vals.at(1))}; |
| 1505 | 1505 | return true; |
| 1506 | 1506 | }); |
| 1507 | - opt->set_custom_option("INT FLOAT", 2); | |
| 1507 | + opt->type_name("INT FLOAT")->type_size(2); | |
| 1508 | 1508 | |
| 1509 | 1509 | args = {"12", "1.5"}; |
| 1510 | 1510 | ... | ... |
tests/HelpTest.cpp
| ... | ... | @@ -222,8 +222,8 @@ TEST(THelp, ManualSetters) { |
| 222 | 222 | int x = 1; |
| 223 | 223 | |
| 224 | 224 | CLI::Option *op1 = app.add_option("--op", x); |
| 225 | - op1->set_default_str("12"); | |
| 226 | - op1->set_type_name("BIGGLES"); | |
| 225 | + op1->default_str("12"); | |
| 226 | + op1->type_name("BIGGLES"); | |
| 227 | 227 | EXPECT_EQ(x, 1); |
| 228 | 228 | |
| 229 | 229 | std::string help = app.help(); |
| ... | ... | @@ -231,7 +231,7 @@ TEST(THelp, ManualSetters) { |
| 231 | 231 | EXPECT_THAT(help, HasSubstr("=12")); |
| 232 | 232 | EXPECT_THAT(help, HasSubstr("BIGGLES")); |
| 233 | 233 | |
| 234 | - op1->set_default_val("14"); | |
| 234 | + op1->default_val("14"); | |
| 235 | 235 | EXPECT_EQ(x, 14); |
| 236 | 236 | help = app.help(); |
| 237 | 237 | EXPECT_THAT(help, HasSubstr("=14")); |
| ... | ... | @@ -556,7 +556,7 @@ TEST(THelp, CustomDoubleOption) { |
| 556 | 556 | custom_opt = {stol(vals.at(0)), stod(vals.at(1))}; |
| 557 | 557 | return true; |
| 558 | 558 | }); |
| 559 | - opt->set_custom_option("INT FLOAT", 2); | |
| 559 | + opt->type_name("INT FLOAT")->type_size(2); | |
| 560 | 560 | |
| 561 | 561 | EXPECT_THAT(app.help(), Not(HasSubstr("x 2"))); |
| 562 | 562 | } | ... | ... |
tests/NewParseTest.cpp
| ... | ... | @@ -17,11 +17,11 @@ add_option(CLI::App &app, std::string name, cx &variable, std::string descriptio |
| 17 | 17 | }; |
| 18 | 18 | |
| 19 | 19 | CLI::Option *opt = app.add_option(name, fun, description, defaulted); |
| 20 | - opt->set_custom_option("COMPLEX", 2); | |
| 20 | + opt->type_name("COMPLEX")->type_size(2); | |
| 21 | 21 | if(defaulted) { |
| 22 | 22 | std::stringstream out; |
| 23 | 23 | out << variable; |
| 24 | - opt->set_default_str(out.str()); | |
| 24 | + opt->default_str(out.str()); | |
| 25 | 25 | } |
| 26 | 26 | return opt; |
| 27 | 27 | } | ... | ... |