Commit de432f62137e59241fd0eecb6590395d6ec8ac57
1 parent
8218425e
Adding ->parsed
Showing
4 changed files
with
10 additions
and
3 deletions
CHANGELOG.md
| ... | ... | @@ -6,7 +6,7 @@ |
| 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 | 8 | * Support for complex numbers |
| 9 | -* Subcommands now test true/false directly, cleaner parse | |
| 9 | +* Subcommands now test true/false directly or with `->parsed()`, cleaner parse | |
| 10 | 10 | |
| 11 | 11 | ## Version 0.8 |
| 12 | 12 | ... | ... |
README.md
| ... | ... | @@ -168,8 +168,8 @@ Subcommands are supported, and can be nested infinitely. To add a subcommand, ca |
| 168 | 168 | case). |
| 169 | 169 | If you want to require at least one subcommand is given, use `.require_subcommand()` on the parent app. You can optionally give an exact number of subcommands to require, as well. |
| 170 | 170 | |
| 171 | -If an App (main or subcommand) has been parsed on the command line, it's "value" is true, and it is false otherwise. | |
| 172 | -All `App`s have a `get_subcommands()` method, which returns a list of pointers to the subcommands passed on the command line. A `got_subcommand(App_or_name)` method is also provided that will check to see if an App pointer or a string name was collected on the command line. | |
| 171 | +If an `App` (main or subcommand) has been parsed on the command line, `->parsed` will be true (or convert directly to bool). | |
| 172 | +All `App`s have a `get_subcommands()` method, which returns a list of pointers to the subcommands passed on the command line. A `got_subcommand(App_or_name)` method is also provided that will check to see if an `App` pointer or a string name was collected on the command line. | |
| 173 | 173 | |
| 174 | 174 | 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 `.set_callback`. If you throw `CLI::Success`, you can |
| 175 | 175 | even exit the program through the callback. The main `App` has a callback slot, as well, but it is generally not as useful. |
| ... | ... | @@ -184,6 +184,7 @@ There are several options that are supported on the main app and subcommands. Th |
| 184 | 184 | * `.add_subcommand(name, description="")` Add a subcommand, returns a pointer to the internally stored subcommand. |
| 185 | 185 | * `.got_subcommand(App_or_name)`: Check to see if a subcommand was received on the command line |
| 186 | 186 | * `.get_subcommands()`: The list of subcommands given on the command line |
| 187 | +* `.parsed()`: True if this subcommand was given on the command line | |
| 187 | 188 | * `.set_callback(void() function)`: Set the callback that runs at the end of parsing. The options have already run at this point. |
| 188 | 189 | * `.allow_extras()`: Do not throw an error if extra arguments are left over (Only useful on the main `App`, as that's the one that throws errors). |
| 189 | 190 | ... | ... |
include/CLI/App.hpp
| ... | ... | @@ -169,6 +169,10 @@ public: |
| 169 | 169 | } |
| 170 | 170 | |
| 171 | 171 | /// Check to see if this subcommand was parsed, true only if received on command line. |
| 172 | + bool parsed() const {return parsed_;} | |
| 173 | + | |
| 174 | + /// Check to see if this subcommand was parsed, true only if received on command line. | |
| 175 | + /// This allows the subcommand to be directly checked. | |
| 172 | 176 | operator bool () const { return parsed_;} |
| 173 | 177 | |
| 174 | 178 | /// Require a subcommand to be given (does not affect help call) | ... | ... |
tests/SubcommandTest.cpp
| ... | ... | @@ -40,6 +40,7 @@ TEST_F(TApp, MultiSubFallthrough) { |
| 40 | 40 | EXPECT_TRUE(app.got_subcommand("sub1")); |
| 41 | 41 | EXPECT_TRUE(app.got_subcommand(sub1)); |
| 42 | 42 | EXPECT_TRUE(*sub1); |
| 43 | + EXPECT_TRUE(sub1->parsed()); | |
| 43 | 44 | |
| 44 | 45 | EXPECT_TRUE(app.got_subcommand("sub2")); |
| 45 | 46 | EXPECT_TRUE(app.got_subcommand(sub2)); |
| ... | ... | @@ -69,6 +70,7 @@ TEST_F(TApp, MultiSubFallthrough) { |
| 69 | 70 | |
| 70 | 71 | EXPECT_TRUE(*sub1); |
| 71 | 72 | EXPECT_FALSE(*sub2); |
| 73 | + EXPECT_FALSE(sub2->parsed()); | |
| 72 | 74 | |
| 73 | 75 | EXPECT_THROW(app.got_subcommand("sub3"), CLI::OptionNotFound); |
| 74 | 76 | } | ... | ... |