Skip to content

Commit

Permalink
[MISC] Also remove period after default message
Browse files Browse the repository at this point in the history
  • Loading branch information
eseiler committed Feb 5, 2024
1 parent 93b06c0 commit 411384e
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 70 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ OPTIONS
Export the help page information. Value must be one of [html, man,
ctd, cwl].
--version-check (bool)
Whether to check for the newest app version. Default: true.
Whether to check for the newest app version. Default: true
VERSION
Last update:
Expand Down
44 changes: 32 additions & 12 deletions include/sharg/detail/format_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
*/

#pragma once
#include <ranges>

#include <sharg/auxiliary.hpp>
#include <sharg/config.hpp>
Expand Down Expand Up @@ -238,7 +237,6 @@ class format_base
message << detail::to_string(value);
}

message << ". ";
return message.str();
}
};
Expand Down Expand Up @@ -287,11 +285,12 @@ class format_help_base : public format_base
std::string info{config.description};

if (config.default_message.empty())
info += ((config.required) ? std::string{" "} : get_default_message(value, value));
info += ((config.required) ? std::string{} : get_default_message(value, value));
else
info += get_default_message(value, config.default_message);

info += config.validator.get_help_page_message();
if (auto const & validator_message = config.validator.get_help_page_message(); !validator_message.empty())
info += ". " + validator_message;

store_help_page_element(
[this, id, info]()
Expand Down Expand Up @@ -321,20 +320,41 @@ class format_help_base : public format_base
template <typename option_type, typename validator_t>
void add_positional_option(option_type & value, config<validator_t> const & config)
{
// a list at the end may be empty and thus have a default value
auto positional_default_message = [&value]() -> std::string
{
if constexpr (detail::is_container_option<option_type>)
{
return get_default_message(value, value);
}
else
{
(void)value; // Silence unused variable warning.
return {};
}
};

auto positional_validator_message = [&config]() -> std::string
{
if (auto const & validator_message = config.validator.get_help_page_message(); !validator_message.empty())
return ". " + validator_message;
else
return {};
};

positional_option_calls.push_back(
[this, &value, description = config.description, validator = config.validator]()
[this,
&value,
default_message = positional_default_message(),
validator_message = positional_validator_message(),
description = config.description]()
{
++positional_option_count;
derived_t().print_list_item(detail::to_string("\\fBARGUMENT-",
positional_option_count,
"\\fP ",
option_type_and_list_info(value)),
description +
// a list at the end may be empty and thus have a default value
((detail::is_container_option<option_type>)
? get_default_message(value, value)
: std::string{" "})
+ validator.get_help_page_message());
description + default_message + validator_message);
});
}

Expand Down Expand Up @@ -402,7 +422,7 @@ class format_help_base : public format_base
+ detail::supported_exports + ".");
if (version_check_dev_decision == update_notifications::on)
derived_t().print_list_item("\\fB--version-check\\fP (bool)",
"Whether to check for the newest app version. Default: true.");
"Whether to check for the newest app version. Default: true");

if (!meta.examples.empty())
{
Expand Down
45 changes: 35 additions & 10 deletions include/sharg/detail/format_tdl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,14 @@ class format_tdl : format_base
void add_option(option_type & value, config<validator_t> const & config)
{
auto description = config.description;
description += (config.required ? std::string{" "} : detail::to_string(" Default: ", value, ". "));
description += config.validator.get_help_page_message();

if (config.default_message.empty())
description += ((config.required) ? std::string{} : get_default_message(value, value));
else
description += get_default_message(value, config.default_message);

if (auto const & validator_message = config.validator.get_help_page_message(); !validator_message.empty())
description += ". " + validator_message;

auto tags = std::set<std::string>{};
if (config.required)
Expand Down Expand Up @@ -263,19 +269,38 @@ class format_tdl : format_base
template <typename option_type, typename validator_t>
void add_positional_option(option_type & value, config<validator_t> const & config)
{
std::string msg = config.validator.get_help_page_message();
// a list at the end may be empty and thus have a default value
auto positional_default_message = [&value]() -> std::string
{
if constexpr (detail::is_container_option<option_type>)
{
return get_default_message(value, value);
}
else
{
(void)value; // Silence unused variable warning.
return {};
}
};

auto positional_validator_message = [&config]() -> std::string
{
if (auto const & validator_message = config.validator.get_help_page_message(); !validator_message.empty())
return ". " + validator_message;
else
return {};
};

positional_option_calls.push_back(
[this, &value, config, msg](std::string_view)
[this,
&value,
config,
default_message = positional_default_message(),
validator_message = positional_validator_message()](std::string_view)
{
auto id = "positional_" + std::to_string(positional_option_count);
++positional_option_count;
auto description =
config.description +
// a list at the end may be empty and thus have a default value
((detail::is_container_option<option_type>) ? detail::to_string(" Default: ", value, ". ")
: std::string{" "})
+ msg;
auto description = config.description + default_message + validator_message;

parameters.push_back(tdl::Node{
.name = id,
Expand Down
2 changes: 1 addition & 1 deletion test/snippet/readme_sneak_peek.out
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ OPTIONS
Export the help page information. Value must be one of [html, man,
ctd, cwl].
--version-check (bool)
Whether to check for the newest app version. Default: true.
Whether to check for the newest app version. Default: true

VERSION
Last update:
Expand Down
8 changes: 4 additions & 4 deletions test/unit/detail/format_ctd_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ struct format_ctd_test : public ::testing::Test
"\n"
R"del( <PARAMETERS version="1.7.0" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/OpenMS/OpenMS/develop/share/OpenMS/SCHEMAS/Param_1_7_0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">)del"
"\n"
R"del( <ITEM name="positional_0" value="" type="string" description="this is a positional option. " required="false" advanced="false" />)del"
R"del( <ITEM name="positional_0" value="" type="string" description="this is a positional option." required="false" advanced="false" />)del"
"\n"
R"del( <ITEM name="positional_1" value="" type="string" description="this is a positional option. Default: []. " required="false" advanced="false" />)del"
R"del( <ITEM name="positional_1" value="" type="string" description="this is a positional option. Default: []" required="false" advanced="false" />)del"
"\n"
R"del( <ITEM name="int" value="5" type="int" description="this is a int option. Default: 5. " required="false" advanced="false" />)del"
R"del( <ITEM name="int" value="5" type="int" description="this is a int option. Default: 5" required="false" advanced="false" />)del"
"\n"
R"del( <ITEM name="jint" value="5" type="int" description="this is a required int option. " required="true" advanced="false" />)del"
R"del( <ITEM name="jint" value="5" type="int" description="this is a required int option." required="true" advanced="false" />)del"
"\n"
R"del( <ITEM name="flag" value="false" type="bool" description="this is a flag." required="false" advanced="false" />)del"
"\n"
Expand Down
12 changes: 6 additions & 6 deletions test/unit/detail/format_cwl_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ TEST(format_cwl_test, full_information)
"doc: \"description\\ndescription2\\n\"\n"
"inputs:\n"
" int:\n"
" doc: \"this is a int option. Default: 5. \"\n"
" doc: \"this is a int option. Default: 5\"\n"
" type: long?\n"
" inputBinding:\n"
" prefix: --int\n"
" jint:\n"
" doc: \"this is a required int option. \"\n"
" doc: this is a required int option.\n"
" type: long\n"
" inputBinding:\n"
" prefix: --jint\n"
Expand Down Expand Up @@ -203,22 +203,22 @@ TEST(format_cwl_test, subparser)
"doc: \"\"\n"
"inputs:\n"
" int:\n"
" doc: \"this is a int option. Default: 5. \"\n"
" doc: \"this is a int option. Default: 5\"\n"
" type: long?\n"
" inputBinding:\n"
" prefix: --int\n"
" jint:\n"
" doc: \"this is a required int option. \"\n"
" doc: this is a required int option.\n"
" type: long\n"
" inputBinding:\n"
" prefix: --jint\n"
" percent:\n"
" doc: \"this is a required float option. \"\n"
" doc: this is a required float option.\n"
" type: double\n"
" inputBinding:\n"
" prefix: --percent\n"
" string:\n"
" doc: \"this is a string option (advanced). Default: . \"\n"
" doc: \"this is a string option (advanced). Default: \\\"\\\"\"\n"
" type: string?\n"
" inputBinding:\n"
" prefix: --string\n"
Expand Down
34 changes: 17 additions & 17 deletions test/unit/detail/format_help_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ std::string const basic_options_str = " Common options\n"
"[html, man].\n"
#endif
" --version-check (bool)\n"
" Whether to check for the newest app version. Default: true.\n";
" Whether to check for the newest app version. Default: true\n";

std::string const basic_version_str = "VERSION\n"
" Last update:\n"
Expand Down Expand Up @@ -132,18 +132,18 @@ TEST(help_page_printing, quote_strings)
"===========\n\n"
"POSITIONAL ARGUMENTS\n"
" ARGUMENT-1 (List of std::string)\n"
" Default: [\"Some\", \"other\", \"string\"].\n\n"
" Default: [\"Some\", \"other\", \"string\"]\n\n"
"OPTIONS\n"
" -a, --string1 (std::string)\n"
" Default: \"\".\n"
" Default: \"\"\n"
" -b, --string2 (std::string)\n"
" Default: \"Some string\".\n"
" Default: \"Some string\"\n"
" -c, --string3 (std::string)\n"
" Default: \"Quoted\".\n"
" Default: \"Quoted\"\n"
" -d, --string4 (List of std::string)\n"
" Default: [\"Some\", \"other\", \"string\"].\n"
" Default: [\"Some\", \"other\", \"string\"]\n"
" -e, --string5 (List of std::string)\n"
" Default: None.\n\n"
" Default: None\n\n"
+ basic_options_str + "\n" + basic_version_str;
EXPECT_EQ(std_cout, expected);
}
Expand Down Expand Up @@ -171,18 +171,18 @@ TEST(help_page_printing, quote_paths)
"===========\n\n"
"POSITIONAL ARGUMENTS\n"
" ARGUMENT-1 (List of std::filesystem::path)\n"
" Default: [\"/some\", \"/other\", \"/path\"].\n\n"
" Default: [\"/some\", \"/other\", \"/path\"]\n\n"
"OPTIONS\n"
" -a, --path1 (std::filesystem::path)\n"
" Default: \"\".\n"
" Default: \"\"\n"
" -b, --path2 (std::filesystem::path)\n"
" Default: \"/some/path\".\n"
" Default: \"/some/path\"\n"
" -c, --path3 (std::filesystem::path)\n"
" Default: \"/usr/bin/\".\n"
" Default: \"/usr/bin/\"\n"
" -d, --path4 (List of std::filesystem::path)\n"
" Default: [\"/some\", \"/other\", \"/path\"].\n"
" Default: [\"/some\", \"/other\", \"/path\"]\n"
" -e, --path5 (List of std::filesystem::path)\n"
" Default: None.\n\n"
" Default: None\n\n"
+ basic_options_str + "\n" + basic_version_str;
EXPECT_EQ(std_cout, expected);
}
Expand Down Expand Up @@ -452,7 +452,7 @@ TEST(help_page_printing, advanced_options)
"\n"
" advanced subsection\n"
" -j, --jnt (unsigned 8 bit integer)\n"
" this is a int option. Default: 2.\n"
" this is a int option. Default: 2\n"
" -f, --flag\n"
" this is a flag.\n"
" -s, --some\n"
Expand Down Expand Up @@ -532,10 +532,10 @@ TEST(help_page_printing, full_information)
" ARGUMENT-1 (signed 8 bit integer)\n"
" this is not a list.\n"
" ARGUMENT-2 (List of std::string)\n"
" this is a positional option. Default: [].\n"
" this is a positional option. Default: []\n"
"\nOPTIONS\n"
" -i, --int (signed 32 bit integer)\n"
" this is a int option. Default: A number.\n"
" this is a int option. Default: A number\n"
" -e, --enum (foo)\n"
" this is an enum option. Default: one. Value must be one of [three,\n"
" two, one].\n"
Expand Down Expand Up @@ -662,7 +662,7 @@ TEST(parse_test, subcommand_parser)
" subcommand key word is passed on to the corresponding sub-parser.\n"
"\nOPTIONS\n"
" -f, --foo (signed 32 bit integer)\n"
" foo bar. Default: 0.\n"
" foo bar. Default: 0\n"
"\n"
+ basic_options_str + "\n" + basic_version_str;

Expand Down
12 changes: 6 additions & 6 deletions test/unit/detail/format_html_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ TEST(html_format, empty_information)
"[html, man].</dd>\n"
#endif
"<dt><strong>--version-check</strong> (bool)</dt>\n"
"<dd>Whether to check for the newest app version. Default: true.</dd>\n"
"<dd>Whether to check for the newest app version. Default: true</dd>\n"
"</dl>\n"
"<h2>Version</h2>\n"
"<p>\n"
Expand Down Expand Up @@ -143,16 +143,16 @@ TEST(html_format, full_information_information)
"<h2>Positional Arguments</h2>\n"
"<dl>\n"
"<dt><strong>ARGUMENT-1</strong> (<em>signed 8 bit integer</em>)</dt>\n"
"<dd>this is a positional option. </dd>\n"
"<dd>this is a positional option.</dd>\n"
"<dt><strong>ARGUMENT-2</strong> (<em>List</em> of <em>std::string</em>)</dt>\n"
"<dd>this is a positional option. Default: []. </dd>\n"
"<dd>this is a positional option. Default: []</dd>\n"
"</dl>\n"
"<h2>Options</h2>\n"
"<dl>\n"
"<dt><strong>-i</strong>, <strong>--int</strong> (<em>signed 32 bit integer</em>)</dt>\n"
"<dd>this is a int option. Default: A number. </dd>\n"
"<dd>this is a int option. Default: A number</dd>\n"
"<dt><strong>-j</strong>, <strong>--jint</strong> (<em>signed 32 bit integer</em>)</dt>\n"
"<dd>this is a required int option. </dd>\n"
"<dd>this is a required int option.</dd>\n"
"<dt><strong>-f</strong>, <strong>--flag</strong></dt>\n"
"<dd>this is a flag.</dd>\n"
"<dt><strong>-k</strong>, <strong>--kflag</strong></dt>\n"
Expand All @@ -176,7 +176,7 @@ TEST(html_format, full_information_information)
"[html, man].</dd>\n"
#endif
"<dt><strong>--version-check</strong> (bool)</dt>\n"
"<dd>Whether to check for the newest app version. Default: true.</dd>\n"
"<dd>Whether to check for the newest app version. Default: true</dd>\n"
"</dl>\n"
"<h2>Examples</h2>\n"
"<p>\n"
Expand Down
Loading

0 comments on commit 411384e

Please sign in to comment.