1
0
mirror of https://github.com/CLIUtils/CLI11.git synced 2025-04-29 20:23:55 +00:00

fix: promoting uint8_t/int8_t to make it casting result printable (#1132)

The underlying type of uint8_t/int8_t is unsigned char and signed char,
but IOStreams was specified to treat them just like char.

so promoting the casting value to get the expected value.

Signed-off-by: ComixHe <ComixHe1895@outlook.com>
This commit is contained in:
Comix 2025-03-06 12:07:50 +08:00 committed by GitHub
parent 38e370f671
commit 7ff65c16f2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 5 additions and 13 deletions

View File

@ -776,18 +776,6 @@ class Option : public OptionBase<Option> {
_validate_results(results_); _validate_results(results_);
current_option_state_ = old_option_state; current_option_state_ = old_option_state;
} }
} catch(const ValidationError &err) {
// this should be done
results_ = std::move(old_results);
current_option_state_ = old_option_state;
// try an alternate way to convert
std::string alternate = detail::value_string(val);
if(!alternate.empty() && alternate != val_str) {
return default_val(alternate);
}
throw ValidationError(get_name(),
std::string("given default value does not pass validation :") + err.what());
} catch(const ConversionError &err) { } catch(const ConversionError &err) {
// this should be done // this should be done
results_ = std::move(old_results); results_ = std::move(old_results);

View File

@ -33,7 +33,8 @@ namespace enums {
template <typename T, typename = typename std::enable_if<std::is_enum<T>::value>::type> template <typename T, typename = typename std::enable_if<std::is_enum<T>::value>::type>
std::ostream &operator<<(std::ostream &in, const T &item) { std::ostream &operator<<(std::ostream &in, const T &item) {
// make sure this is out of the detail namespace otherwise it won't be found when needed // make sure this is out of the detail namespace otherwise it won't be found when needed
return in << static_cast<typename std::underlying_type<T>::type>(item); // https://isocpp.org/wiki/faq/input-output#print-char-or-ptr-as-number
return in << +static_cast<typename std::underlying_type<T>::type>(item);
} }
} // namespace enums } // namespace enums

View File

@ -38,6 +38,9 @@ TEST_CASE("TypeTools: Streaming", "[helpers]") {
CHECK(std::string("string") == CLI::detail::to_string("string")); CHECK(std::string("string") == CLI::detail::to_string("string"));
CHECK(std::string("string") == CLI::detail::to_string(std::string("string"))); CHECK(std::string("string") == CLI::detail::to_string(std::string("string")));
enum class t1 : std::uint8_t { enum1, enum2 };
CHECK(CLI::detail::to_string(t1::enum1) == "0");
} }
TEST_CASE("TypeTools: tuple", "[helpers]") { TEST_CASE("TypeTools: tuple", "[helpers]") {