Skip to content

Commit

Permalink
[MISC] Add quotes to strings
Browse files Browse the repository at this point in the history
  • Loading branch information
eseiler committed Feb 2, 2024
1 parent 5c99a11 commit 0c74fdf
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
13 changes: 11 additions & 2 deletions include/sharg/detail/format_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<option_type, std::string>)
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();

Expand Down
5 changes: 5 additions & 0 deletions include/sharg/detail/to_string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#pragma once

#include <filesystem>
#include <sstream>

#include <sharg/concept.hpp>
Expand Down Expand Up @@ -63,6 +64,10 @@ std::string to_string(value_types &&... values)
{
stream << static_cast<int16_t>(val);
}
else if constexpr (std::is_same_v<std::remove_cvref_t<decltype(val)>, std::filesystem::path>)
{
stream << val.string();
}
else
{
stream << val;
Expand Down
45 changes: 45 additions & 0 deletions test/unit/detail/format_help_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0c74fdf

Please sign in to comment.