From 6cd171ad3f25b1c4b4de0949a2ed77e16ed4dd3f Mon Sep 17 00:00:00 2001 From: Philip Top Date: Tue, 12 Mar 2024 09:45:17 -0700 Subject: [PATCH] get_subcommand_no_throw (#1016) get_subcommand when used for parsing config files, was throwing and catching as part of control flow and expected operation, this resulting in a performance hit in select cases. A get_subcommand_no_throw was added to resolve this issue. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- include/CLI/App.hpp | 11 ++++++++--- include/CLI/impl/App_inl.hpp | 14 +++++++------- tests/SubcommandTest.cpp | 6 ++++-- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/include/CLI/App.hpp b/include/CLI/App.hpp index 5d776160..2d03d85b 100644 --- a/include/CLI/App.hpp +++ b/include/CLI/App.hpp @@ -733,6 +733,10 @@ class App { /// Check to see if a subcommand is part of this command (text version) CLI11_NODISCARD App *get_subcommand(std::string subcom) const; + /// Get a subcommand by name (noexcept non-const version) + /// returns null if subcommand doesn't exist + CLI11_NODISCARD App *get_subcommand_no_throw(std::string subcom) const noexcept; + /// Get a pointer to subcommand by index CLI11_NODISCARD App *get_subcommand(int index = 0) const; @@ -907,8 +911,9 @@ class App { } /// Check with name instead of pointer to see if subcommand was selected - CLI11_NODISCARD bool got_subcommand(std::string subcommand_name) const { - return get_subcommand(subcommand_name)->parsed_ > 0; + CLI11_NODISCARD bool got_subcommand(std::string subcommand_name) const noexcept { + App *sub = get_subcommand_no_throw(subcommand_name); + return (sub != nullptr) ? (sub->parsed_ > 0) : false; } /// Sets excluded options for the subcommand @@ -1038,7 +1043,7 @@ class App { std::vector