1
0
mirror of https://github.com/CLIUtils/CLI11.git synced 2025-05-04 14:23:51 +00:00

refactor the close_match method into 2 different methods

This commit is contained in:
Philip Top 2025-04-30 08:00:27 -07:00
parent 7c4a4d555f
commit eec2211b0f
2 changed files with 21 additions and 12 deletions

View File

@ -1235,10 +1235,14 @@ class App {
/// Get a display name for an app /// Get a display name for an app
CLI11_NODISCARD std::string get_display_name(bool with_aliases = false) const; CLI11_NODISCARD std::string get_display_name(bool with_aliases = false) const;
/// Check the name, case-insensitive and underscore insensitive, and prefix matching if set
///@return true if matched
CLI11_NODISCARD bool check_name(std::string name_to_check) const;
enum class NameMatch:std::uint8_t{none=0,match=1,prefix=2};
/// Check the name, case-insensitive and underscore insensitive if set /// Check the name, case-insensitive and underscore insensitive if set
/// @return 0 if no match, 1 or higher if there is a match (2 or more is the character difference with prefix /// @return NameMatch::none if no match, NameMatch::match if exact NameMatch::prefix if prefix is enabled and a prefix matches
/// matching enabled) CLI11_NODISCARD NameMatch check_name_detail(std::string name_to_check) const;
CLI11_NODISCARD int check_name(std::string name_to_check) const;
/// Get the groups available directly from this option (in order) /// Get the groups available directly from this option (in order)
CLI11_NODISCARD std::vector<std::string> get_groups() const; CLI11_NODISCARD std::vector<std::string> get_groups() const;

View File

@ -894,7 +894,12 @@ CLI11_NODISCARD CLI11_INLINE std::string App::get_display_name(bool with_aliases
return dispname; return dispname;
} }
CLI11_NODISCARD CLI11_INLINE int App::check_name(std::string name_to_check) const { CLI11_NODISCARD CLI11_INLINE bool App::check_name(std::string name_to_check) const {
auto result=check_name_detail(std::move(name_to_check));
return (result!=NameMatch::none);
}
CLI11_NODISCARD CLI11_INLINE App::NameMatch App::check_name_detail(std::string name_to_check) const {
std::string local_name = name_; std::string local_name = name_;
if(ignore_underscore_) { if(ignore_underscore_) {
local_name = detail::remove_underscore(name_); local_name = detail::remove_underscore(name_);
@ -906,11 +911,11 @@ CLI11_NODISCARD CLI11_INLINE int App::check_name(std::string name_to_check) cons
} }
if(local_name == name_to_check) { if(local_name == name_to_check) {
return 1; return App::NameMatch::match;
} }
if(allow_prefix_matching_ && name_to_check.size() < local_name.size()) { if(allow_prefix_matching_ && name_to_check.size() < local_name.size()) {
if(local_name.compare(0, name_to_check.size(), name_to_check) == 0) { if(local_name.compare(0, name_to_check.size(), name_to_check) == 0) {
return static_cast<int>(local_name.size() - name_to_check.size() + 1); return App::NameMatch::prefix;
} }
} }
for(std::string les : aliases_) { // NOLINT(performance-for-range-copy) for(std::string les : aliases_) { // NOLINT(performance-for-range-copy)
@ -921,15 +926,15 @@ CLI11_NODISCARD CLI11_INLINE int App::check_name(std::string name_to_check) cons
les = detail::to_lower(les); les = detail::to_lower(les);
} }
if(les == name_to_check) { if(les == name_to_check) {
return 1; return App::NameMatch::match;
} }
if(allow_prefix_matching_ && name_to_check.size() < les.size()) { if(allow_prefix_matching_ && name_to_check.size() < les.size()) {
if(les.compare(0, name_to_check.size(), name_to_check) == 0) { if(les.compare(0, name_to_check.size(), name_to_check) == 0) {
return static_cast<int>(les.size() - name_to_check.size() + 1); return App::NameMatch::prefix;
} }
} }
} }
return 0; return App::NameMatch::none;
} }
CLI11_NODISCARD CLI11_INLINE std::vector<std::string> App::get_groups() const { CLI11_NODISCARD CLI11_INLINE std::vector<std::string> App::get_groups() const {
@ -1848,10 +1853,10 @@ App::_find_subcommand(const std::string &subc_name, bool ignore_disabled, bool i
} }
} }
} }
auto res = com->check_name(subc_name); auto res = com->check_name_detail(subc_name);
if(res != 0) { if(res != NameMatch::none) {
if((!*com) || !ignore_used) { if((!*com) || !ignore_used) {
if(res == 1) { if(res == NameMatch::match) {
return com.get(); return com.get();
} }
if(bcom != nullptr) { if(bcom != nullptr) {