Skip to content

Commit 7ff65c1

Browse files
authored
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>
1 parent 38e370f commit 7ff65c1

File tree

3 files changed

+5
-13
lines changed

3 files changed

+5
-13
lines changed

include/CLI/Option.hpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -776,18 +776,6 @@ class Option : public OptionBase<Option> {
776776
_validate_results(results_);
777777
current_option_state_ = old_option_state;
778778
}
779-
} catch(const ValidationError &err) {
780-
// this should be done
781-
results_ = std::move(old_results);
782-
current_option_state_ = old_option_state;
783-
// try an alternate way to convert
784-
std::string alternate = detail::value_string(val);
785-
if(!alternate.empty() && alternate != val_str) {
786-
return default_val(alternate);
787-
}
788-
789-
throw ValidationError(get_name(),
790-
std::string("given default value does not pass validation :") + err.what());
791779
} catch(const ConversionError &err) {
792780
// this should be done
793781
results_ = std::move(old_results);

include/CLI/StringTools.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ namespace enums {
3333
template <typename T, typename = typename std::enable_if<std::is_enum<T>::value>::type>
3434
std::ostream &operator<<(std::ostream &in, const T &item) {
3535
// make sure this is out of the detail namespace otherwise it won't be found when needed
36-
return in << static_cast<typename std::underlying_type<T>::type>(item);
36+
// https://isocpp.org/wiki/faq/input-output#print-char-or-ptr-as-number
37+
return in << +static_cast<typename std::underlying_type<T>::type>(item);
3738
}
3839

3940
} // namespace enums

tests/HelpersTest.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ TEST_CASE("TypeTools: Streaming", "[helpers]") {
3838

3939
CHECK(std::string("string") == CLI::detail::to_string("string"));
4040
CHECK(std::string("string") == CLI::detail::to_string(std::string("string")));
41+
42+
enum class t1 : std::uint8_t { enum1, enum2 };
43+
CHECK(CLI::detail::to_string(t1::enum1) == "0");
4144
}
4245

4346
TEST_CASE("TypeTools: tuple", "[helpers]") {

0 commit comments

Comments
 (0)