From 0c74fdfb178d46e80d8c1f17876b724aa1fcd23a Mon Sep 17 00:00:00 2001 From: Enrico Seiler Date: Fri, 2 Feb 2024 19:15:54 +0100 Subject: [PATCH] [MISC] Add quotes to strings --- include/sharg/detail/format_base.hpp | 13 ++++++-- include/sharg/detail/to_string.hpp | 5 +++ test/unit/detail/format_help_test.cpp | 45 +++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 2 deletions(-) diff --git a/include/sharg/detail/format_base.hpp b/include/sharg/detail/format_base.hpp index 6a15b8d3..fa525448 100644 --- a/include/sharg/detail/format_base.hpp +++ b/include/sharg/detail/format_base.hpp @@ -227,10 +227,19 @@ class format_help_base : public format_base { std::string id = prep_id_for_help(config.short_id, config.long_id) + " " + option_type_and_list_info(value); std::string info{config.description}; + + auto quote_string = [&config](auto const & quote_me) -> std::string + { + if constexpr (std::same_as) + return '"' + quote_me + '"'; + else + return detail::to_string(quote_me); + }; + if (config.default_message.empty()) - info += ((config.required) ? std::string{" "} : detail::to_string(" Default: ", value, ". ")); + info += ((config.required) ? std::string{" "} : detail::to_string(" Default: ", quote_string(value), ". ")); else - info += detail::to_string(" Default: ", config.default_message, ". "); + info += detail::to_string(" Default: ", quote_string(config.default_message), ". "); info += config.validator.get_help_page_message(); diff --git a/include/sharg/detail/to_string.hpp b/include/sharg/detail/to_string.hpp index a64817a5..5d313e79 100644 --- a/include/sharg/detail/to_string.hpp +++ b/include/sharg/detail/to_string.hpp @@ -12,6 +12,7 @@ #pragma once +#include #include #include @@ -63,6 +64,10 @@ std::string to_string(value_types &&... values) { stream << static_cast(val); } + else if constexpr (std::is_same_v, std::filesystem::path>) + { + stream << val.string(); + } else { stream << val; diff --git a/test/unit/detail/format_help_test.cpp b/test/unit/detail/format_help_test.cpp index 1324b458..cf87859b 100644 --- a/test/unit/detail/format_help_test.cpp +++ b/test/unit/detail/format_help_test.cpp @@ -109,6 +109,51 @@ TEST(help_page_printing, short_help) check_expected_short_help(argv_with_version_check); } +TEST(help_page_printing, quote_string) +{ + sharg::parser quoted("test_parser", 2, argv_with_h); + sharg::detail::test_accessor::set_terminal_width(quoted, 80); + + std::string string_value1{}; + std::string string_value2{"Some string"}; + std::string string_value3{"Some other string"}; + std::filesystem::path path_value1{}; + std::filesystem::path path_value2{"/some/path"}; + std::filesystem::path path_value3{"/some/other/path"}; + int32_t integer_value{5}; + + quoted.add_option(string_value1, sharg::config{.short_id = 's', .long_id = "string1"}); + quoted.add_option(string_value2, sharg::config{.short_id = 't', .long_id = "string2"}); + quoted.add_option(string_value3, sharg::config{.short_id = 'u', .long_id = "string3", .default_message = "Quoted"}); + quoted.add_option(path_value1, sharg::config{.short_id = 'v', .long_id = "path1"}); + quoted.add_option(path_value2, sharg::config{.short_id = 'w', .long_id = "path2"}); + quoted.add_option(path_value3, sharg::config{.short_id = 'x', .long_id = "path3", .default_message = "/usr/bin/"}); + quoted.add_option(integer_value, sharg::config{.short_id = 'i', .long_id = "integer", .default_message = "Five"}); + + testing::internal::CaptureStdout(); + EXPECT_EXIT(quoted.parse(), ::testing::ExitedWithCode(EXIT_SUCCESS), ""); + std_cout = testing::internal::GetCapturedStdout(); + expected = "test_parser\n" + "===========\n" + "\nOPTIONS\n" + " -s, --string1 (std::string)\n" + " Default: \"\".\n" + " -t, --string2 (std::string)\n" + " Default: \"Some string\".\n" + " -u, --string3 (std::string)\n" + " Default: \"Quoted\".\n" + " -v, --path1 (std::filesystem::path)\n" + " Default: .\n" + " -w, --path2 (std::filesystem::path)\n" + " Default: /some/path.\n" + " -x, --path3 (std::filesystem::path)\n" + " Default: /usr/bin/.\n" + " -i, --integer (signed 32 bit integer)\n" + " Default: Five.\n\n" + + basic_options_str + "\n" + basic_version_str; + EXPECT_EQ(std_cout, expected); +} + TEST(help_page_printing, no_information) { // Empty help call with -h