Skip to content

Commit 7e73a57

Browse files
committed
test: add auto synopsis test
1 parent 6de9d23 commit 7e73a57

5 files changed

Lines changed: 79 additions & 4 deletions

File tree

include/sharg/detail/format_base.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ class format_base
6969
return verbose ? "std::string" : "string";
7070
else if constexpr (std::is_same_v<type, std::filesystem::path>)
7171
return verbose ? "std::filesystem::path" : "path";
72+
else if constexpr (!verbose && std::is_enum_v<type>)
73+
return "enum";
7274
else
7375
return sharg::detail::type_name_as_string<value_type>;
7476
}

include/sharg/detail/format_help.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,11 @@ class format_help : public format_help_base<format_help>
195195
std::cout << '\n';
196196
pos = 0;
197197
}
198-
std::fill_n(out, layout.rightColumnTab - pos, ' ');
199-
print_text(desc, layout.rightColumnTab);
198+
if (!desc.empty())
199+
{
200+
std::fill_n(out, layout.rightColumnTab - pos, ' ');
201+
print_text(desc, layout.rightColumnTab);
202+
}
200203

201204
prev_was_paragraph = false;
202205
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
my_program
22
==========
3-
custom_enumeration_snippet [-f|--foo foo::bar]
3+
custom_enumeration_snippet [-f|--foo enum]
44
Try -h or --help for more information.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
my_program
22
==========
3-
custom_parsing_enumeration_snippet [-e|--errc std::__1::errc]
3+
custom_parsing_enumeration_snippet [-e|--errc enum]
44
Try -h or --help for more information.

test/unit/detail/format_help_test.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,3 +565,73 @@ TEST_F(format_help_test, subcommand_parser)
565565
+ basic_options_str + "\n" + version_str();
566566
EXPECT_EQ(get_parse_cout_on_exit(parser), expected);
567567
}
568+
569+
namespace sharg::custom
570+
{
571+
572+
template <>
573+
struct parsing<std::errc>
574+
{
575+
static inline std::unordered_map<std::string_view, std::errc> const enumeration_names{};
576+
};
577+
578+
} // namespace sharg::custom
579+
580+
TEST_F(format_help_test, auto_synopsis)
581+
{
582+
auto parser = get_parser("-h");
583+
parser.info.synopsis.clear();
584+
ASSERT_TRUE(parser.info.synopsis.empty());
585+
586+
foo enum_option_value;
587+
std::errc enum_option_value2;
588+
bool flag{};
589+
uint16_t some_int{};
590+
std::filesystem::path value1{};
591+
std::string value2{};
592+
std::vector<std::filesystem::path> container_value1{};
593+
std::vector<std::string> container_value2{};
594+
595+
parser.add_option(some_int, sharg::config{.short_id = 'a', .long_id = "some_int"});
596+
parser.add_option(value1, sharg::config{.short_id = 'b', .long_id = "value1"});
597+
parser.add_option(value2, sharg::config{.short_id = 'c', .long_id = "value2", .required = true});
598+
parser.add_option(container_value1, sharg::config{.short_id = 'd', .long_id = "container1"});
599+
parser.add_option(container_value2, sharg::config{.short_id = 'e', .long_id = "container2", .required = true});
600+
parser.add_flag(flag, sharg::config{.short_id = 'f', .long_id = "flag"});
601+
parser.add_option(enum_option_value, sharg::config{.short_id = 'g', .long_id = "enum"});
602+
parser.add_option(enum_option_value2, sharg::config{.short_id = 'i', .long_id = "errc"});
603+
parser.add_positional_option(value2, sharg::config{});
604+
parser.add_positional_option(container_value1, sharg::config{});
605+
606+
expected = "test_parser\n"
607+
"===========\n\n"
608+
"SYNOPSIS\n"
609+
" test_parser [-a|--some_int uint16_t] [-b|--value1 path] -c|--value2 string\n"
610+
" [-d|--container1 path]... -e|--container2 string [-e|--container2\n"
611+
" string]... -f|--flag [-g|--enum enum] [-i|--errc enum] [--] string path...\n\n"
612+
"POSITIONAL ARGUMENTS\n"
613+
" ARGUMENT-1 (std::string)\n"
614+
" ARGUMENT-2 (List of std::filesystem::path)\n"
615+
" Default: []\n\n"
616+
"OPTIONS\n"
617+
" -a, --some_int (unsigned 16 bit integer)\n"
618+
" Default: 0\n"
619+
" -b, --value1 (std::filesystem::path)\n"
620+
" Default: \"\"\n"
621+
" -c, --value2 (std::string)\n"
622+
" -d, --container1 (List of std::filesystem::path)\n"
623+
" Default: []\n"
624+
" -e, --container2 (List of std::string)\n"
625+
" -f, --flag\n"
626+
" -g, --enum (foo)\n"
627+
" Default: one\n"
628+
#ifdef _LIBCPP_VERSION
629+
" -i, --errc (std::__1::errc)\n"
630+
#else
631+
" -i, --errc (std::errc)\n"
632+
#endif
633+
" Default: <UNKNOWN_VALUE>\n"
634+
"\n"
635+
+ basic_options_str + "\n" + version_str();
636+
EXPECT_EQ(get_parse_cout_on_exit(parser), expected);
637+
}

0 commit comments

Comments
 (0)