Skip to content

Commit eec2211

Browse files
committed
refactor the close_match method into 2 different methods
1 parent 7c4a4d5 commit eec2211

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

include/CLI/App.hpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,10 +1235,14 @@ class App {
12351235
/// Get a display name for an app
12361236
CLI11_NODISCARD std::string get_display_name(bool with_aliases = false) const;
12371237

1238+
/// Check the name, case-insensitive and underscore insensitive, and prefix matching if set
1239+
///@return true if matched
1240+
CLI11_NODISCARD bool check_name(std::string name_to_check) const;
1241+
1242+
enum class NameMatch:std::uint8_t{none=0,match=1,prefix=2};
12381243
/// Check the name, case-insensitive and underscore insensitive if set
1239-
/// @return 0 if no match, 1 or higher if there is a match (2 or more is the character difference with prefix
1240-
/// matching enabled)
1241-
CLI11_NODISCARD int check_name(std::string name_to_check) const;
1244+
/// @return NameMatch::none if no match, NameMatch::match if exact NameMatch::prefix if prefix is enabled and a prefix matches
1245+
CLI11_NODISCARD NameMatch check_name_detail(std::string name_to_check) const;
12421246

12431247
/// Get the groups available directly from this option (in order)
12441248
CLI11_NODISCARD std::vector<std::string> get_groups() const;

include/CLI/impl/App_inl.hpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -894,7 +894,12 @@ CLI11_NODISCARD CLI11_INLINE std::string App::get_display_name(bool with_aliases
894894
return dispname;
895895
}
896896

897-
CLI11_NODISCARD CLI11_INLINE int App::check_name(std::string name_to_check) const {
897+
CLI11_NODISCARD CLI11_INLINE bool App::check_name(std::string name_to_check) const {
898+
auto result=check_name_detail(std::move(name_to_check));
899+
return (result!=NameMatch::none);
900+
}
901+
902+
CLI11_NODISCARD CLI11_INLINE App::NameMatch App::check_name_detail(std::string name_to_check) const {
898903
std::string local_name = name_;
899904
if(ignore_underscore_) {
900905
local_name = detail::remove_underscore(name_);
@@ -906,11 +911,11 @@ CLI11_NODISCARD CLI11_INLINE int App::check_name(std::string name_to_check) cons
906911
}
907912

908913
if(local_name == name_to_check) {
909-
return 1;
914+
return App::NameMatch::match;
910915
}
911916
if(allow_prefix_matching_ && name_to_check.size() < local_name.size()) {
912917
if(local_name.compare(0, name_to_check.size(), name_to_check) == 0) {
913-
return static_cast<int>(local_name.size() - name_to_check.size() + 1);
918+
return App::NameMatch::prefix;
914919
}
915920
}
916921
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
921926
les = detail::to_lower(les);
922927
}
923928
if(les == name_to_check) {
924-
return 1;
929+
return App::NameMatch::match;
925930
}
926931
if(allow_prefix_matching_ && name_to_check.size() < les.size()) {
927932
if(les.compare(0, name_to_check.size(), name_to_check) == 0) {
928-
return static_cast<int>(les.size() - name_to_check.size() + 1);
933+
return App::NameMatch::prefix;
929934
}
930935
}
931936
}
932-
return 0;
937+
return App::NameMatch::none;
933938
}
934939

935940
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
18481853
}
18491854
}
18501855
}
1851-
auto res = com->check_name(subc_name);
1852-
if(res != 0) {
1856+
auto res = com->check_name_detail(subc_name);
1857+
if(res != NameMatch::none) {
18531858
if((!*com) || !ignore_used) {
1854-
if(res == 1) {
1859+
if(res == NameMatch::match) {
18551860
return com.get();
18561861
}
18571862
if(bcom != nullptr) {

0 commit comments

Comments
 (0)