@@ -848,7 +848,7 @@ CLI11_INLINE std::vector<const Option *> App::get_options(const std::function<bo
848848 options.insert (options.end (), subcopts.begin (), subcopts.end ());
849849 }
850850 }
851- if (fallthrough_ && parent_ != nullptr ) {
851+ if (fallthrough_ && parent_ != nullptr && !name_. empty () ) {
852852 const auto *fallthrough_parent = _get_fallthrough_parent ();
853853 std::vector<const Option *> subcopts = fallthrough_parent->get_options (filter);
854854 for (const auto *opt : subcopts) {
@@ -879,7 +879,7 @@ CLI11_INLINE std::vector<Option *> App::get_options(const std::function<bool(Opt
879879 options.insert (options.end (), subcopts.begin (), subcopts.end ());
880880 }
881881 }
882- if (fallthrough_ && parent_ != nullptr ) {
882+ if (fallthrough_ && parent_ != nullptr && !name_. empty () ) {
883883 auto *fallthrough_parent = _get_fallthrough_parent ();
884884 std::vector<Option *> subcopts = fallthrough_parent->get_options (filter);
885885 for (auto *opt : subcopts) {
@@ -893,6 +893,36 @@ CLI11_INLINE std::vector<Option *> App::get_options(const std::function<bool(Opt
893893 return options;
894894}
895895
896+ // / Get an option by name
897+ CLI11_NODISCARD CLI11_INLINE const Option *App::get_option (std::string option_name) const {
898+ const auto *opt = get_option_no_throw (option_name);
899+ if (opt == nullptr ) {
900+ if (fallthrough_ && parent_ != nullptr && name_.empty ()) {
901+ // as a special case option groups with fallthrough enabled can also check the parent for options if the
902+ // option is not found in the group this will not recurse as the internal call is to the no_throw version
903+ // which will not check the parent again for option groups even with fallthrough enabled
904+ return _get_fallthrough_parent ()->get_option (option_name);
905+ }
906+ throw OptionNotFound (option_name);
907+ }
908+ return opt;
909+ }
910+
911+ // / Get an option by name (non-const version)
912+ CLI11_NODISCARD CLI11_INLINE Option *App::get_option (std::string option_name) {
913+ auto *opt = get_option_no_throw (option_name);
914+ if (opt == nullptr ) {
915+ if (fallthrough_ && parent_ != nullptr && name_.empty ()) {
916+ // as a special case option groups with fallthrough enabled can also check the parent for options if the
917+ // option is not found in the group this will not recurse as the internal call is to the no_throw version
918+ // which will not check the parent again for option groups even with fallthrough enabled
919+ return _get_fallthrough_parent ()->get_option (option_name);
920+ }
921+ throw OptionNotFound (option_name);
922+ }
923+ return opt;
924+ }
925+
896926CLI11_NODISCARD CLI11_INLINE Option *App::get_option_no_throw (std::string option_name) noexcept {
897927 for (Option_p &opt : options_) {
898928 if (opt->check_name (option_name)) {
@@ -908,7 +938,9 @@ CLI11_NODISCARD CLI11_INLINE Option *App::get_option_no_throw(std::string option
908938 }
909939 }
910940 }
911- if (fallthrough_ && parent_ != nullptr ) {
941+ if (fallthrough_ && parent_ != nullptr && !name_.empty ()) {
942+ // if there is fallthrough and a parent and this is not an option_group then also check the parent for the
943+ // option
912944 return _get_fallthrough_parent ()->get_option_no_throw (option_name);
913945 }
914946 return nullptr ;
@@ -929,7 +961,7 @@ CLI11_NODISCARD CLI11_INLINE const Option *App::get_option_no_throw(std::string
929961 }
930962 }
931963 }
932- if (fallthrough_ && parent_ != nullptr ) {
964+ if (fallthrough_ && parent_ != nullptr && !name_. empty () ) {
933965 return _get_fallthrough_parent ()->get_option_no_throw (option_name);
934966 }
935967 return nullptr ;
0 commit comments