From c60b6b2857c039d68208741dbe13bca0c5b719f6 Mon Sep 17 00:00:00 2001 From: Philip Top Date: Mon, 11 Mar 2024 09:21:39 -0700 Subject: [PATCH 1/2] 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. --- 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 5d7761608..8a810b495 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