1
0
mirror of https://github.com/CLIUtils/CLI11.git synced 2025-04-29 20:23:55 +00:00

Adding get_parent()

This commit is contained in:
Henry Fredrick Schreiner 2017-12-12 15:14:24 -06:00
parent 97b6265cee
commit 67cd2e6345
4 changed files with 10 additions and 1 deletions

View File

@ -1,3 +1,6 @@
## Version 1.4 (in progress)
* Added `get_parent()` to access the parent from a subcommand
## Version 1.3: Refactor ## Version 1.3: Refactor
This version focused on refactoring several key systems to ensure correct behavior in the interaction of different settings. Most caveats about This version focused on refactoring several key systems to ensure correct behavior in the interaction of different settings. Most caveats about

View File

@ -230,10 +230,11 @@ There are several options that are supported on the main app and subcommands. Th
* `.fallthrough()`: Allow extra unmatched options and positionals to "fall through" and be matched on a parent command. Subcommands always are allowed to fall through. * `.fallthrough()`: Allow extra unmatched options and positionals to "fall through" and be matched on a parent command. Subcommands always are allowed to fall through.
* `.require_subcommand()`: Require 1 or more subcommands. * `.require_subcommand()`: Require 1 or more subcommands.
* `.require_subcommand(N)`: Require `N` subcommands if `N>0`, or up to `N` if `N<0`. `N=0` resets to the default 0 or more. * `.require_subcommand(N)`: Require `N` subcommands if `N>0`, or up to `N` if `N<0`. `N=0` resets to the default 0 or more.
* `.require_subcommand(min, max)`: Explicilty set min and max allowed subcommands. Setting `max` to 0 is unlimited. * `.require_subcommand(min, max)`: Explicitly set min and max allowed subcommands. Setting `max` to 0 is unlimited.
* `.add_subcommand(name, description="")` Add a subcommand, returns a pointer to the internally stored subcommand. * `.add_subcommand(name, description="")` Add a subcommand, returns a pointer to the internally stored subcommand.
* `.got_subcommand(App_or_name)`: Check to see if a subcommand was received on the command line * `.got_subcommand(App_or_name)`: Check to see if a subcommand was received on the command line
* `.get_subcommands()`: The list of subcommands given on the command line * `.get_subcommands()`: The list of subcommands given on the command line
* `.get_parent()`: Get the parent App or nullptr if called on master App **Coming in version 1.4**
* `.parsed()`: True if this subcommand was given on the command line * `.parsed()`: True if this subcommand was given on the command line
* `.set_callback(void() function)`: Set the callback that runs at the end of parsing. The options have already run at this point. * `.set_callback(void() function)`: Set the callback that runs at the end of parsing. The options have already run at this point.
* `.allow_extras()`: Do not throw an error if extra arguments are left over * `.allow_extras()`: Do not throw an error if extra arguments are left over

View File

@ -980,6 +980,9 @@ class App {
/// Get a pointer to the config option. /// Get a pointer to the config option.
Option *get_config_ptr() { return config_ptr_; } Option *get_config_ptr() { return config_ptr_; }
/// Get the parent of this subcommand (or nullptr if master app)
App *get_parent() { return parent_; }
/// Get a pointer to the config option. (const) /// Get a pointer to the config option. (const)
const Option *get_config_ptr() const { return config_ptr_; } const Option *get_config_ptr() const { return config_ptr_; }
/// Get the name of the current app /// Get the name of the current app

View File

@ -12,6 +12,8 @@ TEST_F(TApp, BasicSubcommands) {
auto sub1 = app.add_subcommand("sub1"); auto sub1 = app.add_subcommand("sub1");
auto sub2 = app.add_subcommand("sub2"); auto sub2 = app.add_subcommand("sub2");
EXPECT_EQ(sub1->get_parent(), &app);
EXPECT_EQ(sub1, app.get_subcommand(sub1)); EXPECT_EQ(sub1, app.get_subcommand(sub1));
EXPECT_EQ(sub1, app.get_subcommand("sub1")); EXPECT_EQ(sub1, app.get_subcommand("sub1"));
EXPECT_THROW(app.get_subcommand("sub3"), CLI::OptionNotFound); EXPECT_THROW(app.get_subcommand("sub3"), CLI::OptionNotFound);