Commit e328364ae71a23c081f66c5df8424c77d208a919
1 parent
cfc389d4
Adding test for extras access
Showing
4 changed files
with
38 additions
and
8 deletions
CHANGELOG.md
| 1 | ## Version 0.5 (in progress) | 1 | ## Version 0.5 (in progress) |
| 2 | 2 | ||
| 3 | * `->ignore_case()` added to subcommands, options, and `add_set_ignore_case`. Subcommand inherit setting from parent App on creation. | 3 | * `->ignore_case()` added to subcommands, options, and `add_set_ignore_case`. Subcommand inherit setting from parent App on creation. |
| 4 | -* Subcommands now can be "chained", that is, left over arguments can now include subcommands that then get parsed. Subcommands are now a list (`get_subcommands`). Added `got_subcommand(App_or_name)` to check for subcommands. (untested) | ||
| 5 | -* Added `.allow_extras()` to disable error on failure. Parse returns a vector of leftover options. Renamed error to `ExtrasError`, and now triggers on extra options too. (untested) | ||
| 6 | -* Added `require_subcommand` to `App`, to simplify forcing subcommands. Do not "chain" with `add_subcommand`, since that is the subcommand, not the master `App`. | 4 | +* Subcommands now can be "chained", that is, left over arguments can now include subcommands that then get parsed. Subcommands are now a list (`get_subcommands`). Added `got_subcommand(App_or_name)` to check for subcommands. |
| 5 | +* Added `.allow_extras()` to disable error on failure. Parse returns a vector of leftover options. Renamed error to `ExtrasError`, and now triggers on extra options too. | ||
| 6 | +* Added `require_subcommand` to `App`, to simplify forcing subcommands. Do **not** do `add_subcommand()->require_subcommand`, since that is the subcommand, not the master `App`. | ||
| 7 | * Added printout of ini file text given parsed options, skips flags. | 7 | * Added printout of ini file text given parsed options, skips flags. |
| 8 | * Support for quotes and spaces in ini files | 8 | * Support for quotes and spaces in ini files |
| 9 | * Fixes to allow support for Windows (added Appveyor) (Use `-`, not `/` syntax) | 9 | * Fixes to allow support for Windows (added Appveyor) (Use `-`, not `/` syntax) |
README.md
| @@ -41,8 +41,6 @@ This library was built to supply the Application object for the GooFit CUDA/OMP | @@ -41,8 +41,6 @@ This library was built to supply the Application object for the GooFit CUDA/OMP | ||
| 41 | * Collect user feedback | 41 | * Collect user feedback |
| 42 | * Ini configuration support is basic (long options only, no vector support), is more needed? | 42 | * Ini configuration support is basic (long options only, no vector support), is more needed? |
| 43 | * Evaluate compatibility with [ROOT](https://root.cern.ch)'s TApplication object. | 43 | * Evaluate compatibility with [ROOT](https://root.cern.ch)'s TApplication object. |
| 44 | - * Add tests: Add way for subclasses to return remaining options rather than throwing error | ||
| 45 | -* Chained subcommands are not tested, once a subcommand is given the rest of the options go to that subcommand, rather than allowing multiple subcommands. This is currently intentional behavior, but multiple base level subcommands, like [`Click`](http://click.pocoo.org) supports, might be considered in the future. | ||
| 46 | * Throw error if `ignore_case` causes non-unique matches | 44 | * Throw error if `ignore_case` causes non-unique matches |
| 47 | 45 | ||
| 48 | See the [changelog](./CHANGELOG.md) or [GitHub releases](https://github.com/henryiii/CLI11/releases) for details. | 46 | See the [changelog](./CHANGELOG.md) or [GitHub releases](https://github.com/henryiii/CLI11/releases) for details. |
| @@ -164,7 +162,7 @@ Subcommands are naturally supported, with an infinite depth. To add a subcommand | @@ -164,7 +162,7 @@ Subcommands are naturally supported, with an infinite depth. To add a subcommand | ||
| 164 | 162 | ||
| 165 | All `App`s have a `get_subcommands()` method, which returns a list of pointers to the subcommand passed on the command line. A simple compare of these pointers to each subcommand allows choosing based on subcommand, facilitated by a `got_subcommand(App_or_name) method that will check the list for you. For many cases, however, using an app's callback may be easier. Every app executes a callback function after it parses; just use a lambda function (with capture to get parsed values) to `.add_callback`. If you throw `CLI::Success`, you can | 163 | All `App`s have a `get_subcommands()` method, which returns a list of pointers to the subcommand passed on the command line. A simple compare of these pointers to each subcommand allows choosing based on subcommand, facilitated by a `got_subcommand(App_or_name) method that will check the list for you. For many cases, however, using an app's callback may be easier. Every app executes a callback function after it parses; just use a lambda function (with capture to get parsed values) to `.add_callback`. If you throw `CLI::Success`, you can |
| 166 | even exit the program through the callback. The main `App` has a callback slot, as well, but it is generally not as useful. | 164 | even exit the program through the callback. The main `App` has a callback slot, as well, but it is generally not as useful. |
| 167 | - | 165 | +Multiple subcommands are allowed, to allow [`Click`](http://click.pocoo.org) like series of commands (order is preserved). If you want only one, throw `CLI::Success` or only process one. |
| 168 | 166 | ||
| 169 | ## Subclassing | 167 | ## Subclassing |
| 170 | 168 |
tests/AppTest.cpp
| @@ -599,3 +599,35 @@ TEST_F(TApp, RangeDouble) { | @@ -599,3 +599,35 @@ TEST_F(TApp, RangeDouble) { | ||
| 599 | EXPECT_NO_THROW(run()); | 599 | EXPECT_NO_THROW(run()); |
| 600 | } | 600 | } |
| 601 | 601 | ||
| 602 | +// Check to make sure progromatic access to left over is available | ||
| 603 | +TEST_F(TApp, AllowExtras) { | ||
| 604 | + | ||
| 605 | + app.allow_extras(); | ||
| 606 | + | ||
| 607 | + bool val = true; | ||
| 608 | + app.add_flag("-f", val); | ||
| 609 | + EXPECT_FALSE(val); | ||
| 610 | + | ||
| 611 | + args = {"-x", "-f"}; | ||
| 612 | + std::vector<std::string> left_over; | ||
| 613 | + EXPECT_NO_THROW({left_over = run();}); | ||
| 614 | + EXPECT_TRUE(val); | ||
| 615 | + EXPECT_EQ(std::vector<std::string>({"-x"}), left_over); | ||
| 616 | + | ||
| 617 | +} | ||
| 618 | + | ||
| 619 | +TEST_F(TApp, AllowExtrasOrder) { | ||
| 620 | + | ||
| 621 | + app.allow_extras(); | ||
| 622 | + | ||
| 623 | + args = {"-x", "-f"}; | ||
| 624 | + std::vector<std::string> left_over; | ||
| 625 | + EXPECT_NO_THROW({left_over = run();}); | ||
| 626 | + EXPECT_EQ(std::vector<std::string>({"-f", "-x"}), left_over); | ||
| 627 | + app.reset(); | ||
| 628 | + | ||
| 629 | + std::vector<std::string> left_over_2; | ||
| 630 | + left_over_2 = app.parse(left_over); | ||
| 631 | + EXPECT_EQ(left_over, left_over_2); | ||
| 632 | + | ||
| 633 | +} |
tests/app_helper.hpp
| @@ -15,10 +15,10 @@ struct TApp : public ::testing::Test { | @@ -15,10 +15,10 @@ struct TApp : public ::testing::Test { | ||
| 15 | CLI::App app{"My Test Program"}; | 15 | CLI::App app{"My Test Program"}; |
| 16 | input_t args; | 16 | input_t args; |
| 17 | 17 | ||
| 18 | - void run() { | 18 | + std::vector<std::string> run() { |
| 19 | input_t newargs = args; | 19 | input_t newargs = args; |
| 20 | std::reverse(std::begin(newargs), std::end(newargs)); | 20 | std::reverse(std::begin(newargs), std::end(newargs)); |
| 21 | - app.parse(newargs); | 21 | + return app.parse(newargs); |
| 22 | } | 22 | } |
| 23 | 23 | ||
| 24 | }; | 24 | }; |