From 6d49d5ee1b454645062b920f017c288d0a9f8e97 Mon Sep 17 00:00:00 2001 From: Pranav Srinivas Kumar Date: Fri, 27 Oct 2023 10:35:04 -0500 Subject: [PATCH] Limiting choices support to string type or integer type --- README.md | 2 +- include/argparse/argparse.hpp | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9b83463..0d9afa0 100644 --- a/README.md +++ b/README.md @@ -1080,7 +1080,7 @@ foo@bar:/home/dev/$ ./main fex Invalid argument "fex" - allowed options: {foo, bar, baz} ``` -Using choices also works in integer types, e.g., +Using choices also works with integer types, e.g., ```cpp argparse::ArgumentParser program("test"); diff --git a/include/argparse/argparse.hpp b/include/argparse/argparse.hpp index 0502894..12e9b07 100644 --- a/include/argparse/argparse.hpp +++ b/include/argparse/argparse.hpp @@ -361,6 +361,15 @@ struct can_invoke_to_string { static constexpr bool value = decltype(test(0))::value; }; +template +struct is_choice_type_supported { + using CleanType = typename std::decay::type; + static const bool value = std::is_integral::value || + std::is_same::value || + std::is_same::value || + std::is_same::value; +}; + } // namespace details enum class nargs_pattern { optional, any, at_least_one }; @@ -546,6 +555,7 @@ public: template void add_choice(T&& choice) { + static_assert(details::is_choice_type_supported::value, "Only string or integer type supported for choice"); static_assert(std::is_convertible_v || details::can_invoke_to_string::value, "Choice is not convertible to string_type"); if (!m_choices.has_value()) { m_choices = std::vector{};