diff --git a/CHANGELOG.md b/CHANGELOG.md index e04ec37b..a9d6babd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## Version 1.4 (in progress) +* Added `get_parent()` to access the parent from a subcommand + ## 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 diff --git a/README.md b/README.md index 6c9dfc28..74a6bdfc 100644 --- a/README.md +++ b/README.md @@ -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. * `.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(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. * `.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_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 * `.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 diff --git a/include/CLI/App.hpp b/include/CLI/App.hpp index 287e3f25..816969df 100644 --- a/include/CLI/App.hpp +++ b/include/CLI/App.hpp @@ -980,6 +980,9 @@ class App { /// Get a pointer to the config option. 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) const Option *get_config_ptr() const { return config_ptr_; } /// Get the name of the current app diff --git a/tests/SubcommandTest.cpp b/tests/SubcommandTest.cpp index 96bad491..5f163f23 100644 --- a/tests/SubcommandTest.cpp +++ b/tests/SubcommandTest.cpp @@ -12,6 +12,8 @@ TEST_F(TApp, BasicSubcommands) { auto sub1 = app.add_subcommand("sub1"); 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_THROW(app.get_subcommand("sub3"), CLI::OptionNotFound);