1
0
mirror of https://github.com/CLIUtils/CLI11.git synced 2025-05-07 15:33:51 +00:00

add some more tests and code cleanup

This commit is contained in:
Philip Top 2025-05-01 05:21:47 -07:00
parent f29420fb82
commit 72c78a2484
3 changed files with 30 additions and 7 deletions

View File

@ -1236,13 +1236,14 @@ class App {
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
/// @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 };
/// @brief enumeration of matching possibilities
enum class NameMatch:std::uint8_t{none=0,exact=1,prefix=2};
/// Check the name, case-insensitive and underscore insensitive if set
/// @return NameMatch::none if no match, NameMatch::match if exact NameMatch::prefix if prefix is enabled and a
/// prefix matches
/// @return NameMatch::none if no match, NameMatch::exact if the match is exact NameMatch::prefix if prefix is enabled and a prefix matches
CLI11_NODISCARD NameMatch check_name_detail(std::string name_to_check) const;
/// Get the groups available directly from this option (in order)

View File

@ -911,7 +911,7 @@ CLI11_NODISCARD CLI11_INLINE App::NameMatch App::check_name_detail(std::string n
}
if(local_name == name_to_check) {
return App::NameMatch::match;
return App::NameMatch::exact;
}
if(allow_prefix_matching_ && name_to_check.size() < local_name.size()) {
if(local_name.compare(0, name_to_check.size(), name_to_check) == 0) {
@ -926,7 +926,7 @@ CLI11_NODISCARD CLI11_INLINE App::NameMatch App::check_name_detail(std::string n
les = detail::to_lower(les);
}
if(les == name_to_check) {
return App::NameMatch::match;
return App::NameMatch::exact;
}
if(allow_prefix_matching_ && name_to_check.size() < les.size()) {
if(les.compare(0, name_to_check.size(), name_to_check) == 0) {
@ -1856,7 +1856,7 @@ App::_find_subcommand(const std::string &subc_name, bool ignore_disabled, bool i
auto res = com->check_name_detail(subc_name);
if(res != NameMatch::none) {
if((!*com) || !ignore_used) {
if(res == NameMatch::match) {
if(res == NameMatch::exact) {
return com.get();
}
if(bcom != nullptr) {

View File

@ -335,6 +335,7 @@ TEST_CASE_METHOD(TApp, "DuplicateSubcommandCallbacksValues", "[subcom]") {
CHECK(36 == vals[2]);
}
TEST_CASE_METHOD(TApp, "Callbacks", "[subcom]") {
auto *sub1 = app.add_subcommand("sub1");
sub1->callback([]() { throw CLI::Success(); });
@ -1871,6 +1872,25 @@ TEST_CASE_METHOD(TApp, "AliasErrors", "[subcom]") {
sub2->ignore_underscore();
CHECK_THROWS_AS(sub2->alias("les3"), CLI::OptionAlreadyAdded);
}
TEST_CASE_METHOD(TApp, "DuplicateErrorsPrefix", "[subcom]") {
app.allow_subcommand_prefix_matching(true);
auto *sub1 = app.add_subcommand("sub_test");
auto *sub2 = app.add_subcommand("sub_deploy");
CHECK_THROWS_AS(app.add_subcommand("sub"), CLI::OptionAlreadyAdded);
// cannot alias to an existing subcommand
CHECK_THROWS_AS(sub2->alias("sub"), CLI::OptionAlreadyAdded);
app.ignore_case();
//this needs to be opposite of the subcommand the alias is being tested on to check for ambiguity
sub2->ignore_case();
CHECK_THROWS_AS(sub1->alias("SUB_"), CLI::OptionAlreadyAdded);
app.ignore_underscore();
sub1->ignore_underscore();
CHECK_THROWS_AS(sub2->alias("su_bt"), CLI::OptionAlreadyAdded);
}
// test adding a subcommand via the pointer
TEST_CASE_METHOD(TApp, "ExistingSubcommandMatch", "[subcom]") {
auto sshared = std::make_shared<CLI::App>("documenting the subcommand", "sub1");
@ -1920,6 +1940,8 @@ TEST_CASE_METHOD(TApp, "AliasErrorsInOptionGroup", "[subcom]") {
CHECK_THROWS_AS(sub2->name("sub1"), CLI::OptionAlreadyAdded);
}
TEST_CASE("SharedSubTests: SharedSubcommand", "[subcom]") {
double val{0.0}, val2{0.0}, val3{0.0}, val4{0.0};
CLI::App app1{"test program1"};