diff --git a/CMakeLists.txt b/CMakeLists.txt index f49d90d..2d87cc7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.20) include(cmake/color.cmake) -set(BLT_VERSION 3.0.8) +set(BLT_VERSION 4.0.0) set(BLT_TARGET BLT) diff --git a/include/blt/parse/argparse_v2.h b/include/blt/parse/argparse_v2.h index 82ff1f0..1914870 100644 --- a/include/blt/parse/argparse_v2.h +++ b/include/blt/parse/argparse_v2.h @@ -76,8 +76,6 @@ namespace blt::argparse { if (input == nullptr) throw detail::bad_flag("Argument cannot be null!"); - if (m_argument.size() == 1) - throw detail::bad_flag("Argument cannot be a single character!"); } [[nodiscard]] std::string_view get_flag() const @@ -114,11 +112,14 @@ namespace blt::argparse private: void process_argument() const { - size_t start = 0; - for (start = 0; start < m_argument.size(); ++start) + size_t start = m_argument.size(); + for (auto [i, c] : enumerate(m_argument)) { - if (std::isalnum(m_argument[start])) + if (std::isalnum(c)) + { + start = i; break; + } } m_is_flag = (start != 0); flag_section = {m_argument.data(), start}; @@ -126,7 +127,8 @@ namespace blt::argparse if (!flag_section->empty() && !detail::allowed_flag_prefixes.contains(*flag_section)) throw detail::bad_flag( - "Invalid flag detected, flag is not in allowed list of flags! Must be one of " + detail::flag_prefix_list_string); + "Invalid flag " + std::string(*flag_section) + " detected, flag is not in allowed list of flags! Must be one of " + + detail::flag_prefix_list_string); } std::string_view m_argument; diff --git a/src/blt/parse/argparse_v2.cpp b/src/blt/parse/argparse_v2.cpp index fcb89a0..2d9cc8d 100644 --- a/src/blt/parse/argparse_v2.cpp +++ b/src/blt/parse/argparse_v2.cpp @@ -52,7 +52,7 @@ namespace blt::argparse { const argument_string_t arg("-f"); BLT_ASSERT(arg.is_flag() && "Expected argument to be identified as a flag."); - BLT_ASSERT(arg.value() == "-f" && "Flag value should match the input string."); + BLT_ASSERT(arg.value() == "f" && "Flag value should match the input string."); } // Test Case 2: Ensure the constructor handles long flags correctly @@ -60,7 +60,7 @@ namespace blt::argparse { const argument_string_t arg("--file"); BLT_ASSERT(arg.is_flag() && "Expected argument to be identified as a flag."); - BLT_ASSERT(arg.value() == "--file" && "Long flag value should match the input string."); + BLT_ASSERT(arg.value() == "file" && "Long flag value should match the input string."); } // Test Case 3: Ensure positional arguments are correctly identified @@ -71,15 +71,6 @@ namespace blt::argparse BLT_ASSERT(arg.value() == "filename.txt" && "Positional argument value should match the input string."); } - // Test Case 4: Test for an edge case where the string starts like a flag but isn't - void test_argument_string_t_invalid_flag() - { - const argument_string_t arg("-notFlagBecauseItIsPositional"); - BLT_ASSERT(!arg.is_flag() && "Expected argument to be identified as positional."); - BLT_ASSERT(arg.value() == "-notFlagBecauseItIsPositional" - && "Argument value should match the input string."); - } - // Test Case 5: Handle an empty string void test_argument_string_t_empty_input() { @@ -93,15 +84,8 @@ namespace blt::argparse { const argument_string_t arg("-"); BLT_ASSERT(arg.is_flag() && "Expected single hyphen (`-`) to be treated as a flag."); - BLT_ASSERT(arg.value() == "-" && "Single hyphen flag should match the input string."); - } - - // Test Case 7: Handle arguments with mixed cases - void test_argument_string_t_mixed_case() - { - const argument_string_t arg("-FlagWithMixedCASE"); - BLT_ASSERT(arg.is_flag() && "Expected argument to be identified as a flag."); - BLT_ASSERT(arg.value() == "-FlagWithMixedCASE" && "Mixed case flag value should match the input string."); + BLT_ASSERT(arg.value().empty() && "Single hyphen flag should have empty value."); + BLT_ASSERT(arg.get_flag() == "-" && "Single hyphen flag should match the input string."); } // Test Case 8: Handle arguments with prefix only (like "--") @@ -109,15 +93,23 @@ namespace blt::argparse { const argument_string_t arg("--"); BLT_ASSERT(arg.is_flag() && "Double hyphen ('--') should be treated as a flag."); - BLT_ASSERT(arg.value() == "--" && "Double hyphen value should match the input string."); + BLT_ASSERT(arg.value().empty() && "Double hyphen flag should have empty value."); + BLT_ASSERT(arg.get_flag() == "--" && "Double hyphen value should match the input string."); } // Test Case 9: Validate edge case of an argument with spaces void test_argument_string_t_with_spaces() { - const argument_string_t arg(" "); - BLT_ASSERT(!arg.is_flag() && "Arguments with spaces should not be treated as flags."); - BLT_ASSERT(arg.value() == " " && "Arguments with spaces should match the input string."); + try + { + const argument_string_t arg(" "); + BLT_ASSERT(!arg.is_flag() && "Arguments with spaces should not be treated as flags."); + BLT_ASSERT(arg.value() == " " && "Arguments with spaces should match the input string."); + } catch (bad_flag&) + { + return; + } + BLT_ASSERT(false && "Expected an exception to be thrown for arguments with spaces."); } // Test Case 10: Validate arguments with numeric characters @@ -125,7 +117,7 @@ namespace blt::argparse { const argument_string_t arg("-123"); BLT_ASSERT(arg.is_flag() && "Numeric flags should still be treated as flags."); - BLT_ASSERT(arg.value() == "-123" && "Numeric flag value should match the input string."); + BLT_ASSERT(arg.value() == "123" && "Numeric flag value should match the input string."); } void run_all_tests_argument_string_t() @@ -133,10 +125,8 @@ namespace blt::argparse test_argument_string_t_flag_basic(); test_argument_string_t_long_flag(); test_argument_string_t_positional_argument(); - test_argument_string_t_invalid_flag(); test_argument_string_t_empty_input(); test_argument_string_t_single_hyphen(); - test_argument_string_t_mixed_case(); test_argument_string_t_double_hyphen(); test_argument_string_t_with_spaces(); test_argument_string_t_numeric_flag();