Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MISC] argument parser: Move get_help_message to file_validator_base. #1691

Merged
merged 4 commits into from
Apr 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions include/seqan3/argument_parser/validators.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,6 @@ class file_validator_base
}

protected:

/*!\brief Validates the given filename path based on the specified extensions.
* \param path The filename path.
* \throws seqan3::validation_error if the specified extensions don't match the given path, or
Expand Down Expand Up @@ -445,6 +444,15 @@ class file_validator_base
file_guard.remove();
}

//!\brief Returns the information of valid file extensions.
std::string valid_extensions_help_page_message() const
{
if (extensions.empty())
return "";
else
return detail::to_string(" Valid file extensions are: [", extensions | views::join(std::string{", "}), "].");
}

//!\brief Stores the extensions.
std::vector<std::string> extensions{};
};
Expand Down Expand Up @@ -561,9 +569,8 @@ class input_file_validator : public file_validator_base
//!\brief Returns a message that can be appended to the (positional) options help page info.
std::string get_help_page_message() const
{
return detail::to_string("Valid input file formats: [",
smehringer marked this conversation as resolved.
Show resolved Hide resolved
extensions | views::join(std::string{", "}),
"]");
return "The input file must exist and read permissions must be granted." +
valid_extensions_help_page_message();
}
};

Expand Down Expand Up @@ -664,9 +671,8 @@ class output_file_validator : public file_validator_base
//!\brief Returns a message that can be appended to the (positional) options help page info.
std::string get_help_page_message() const
{
return detail::to_string("Valid output file formats: [",
extensions | views::join(std::string{", "}),
"]");
return "The output file must not exist already and write permissions must be granted." +
valid_extensions_help_page_message();
}
};

Expand Down
22 changes: 16 additions & 6 deletions test/unit/argument_parser/format_parse_validators_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ TEST(validator_test, input_file)
"\n"
"POSITIONAL ARGUMENTS\n"
" ARGUMENT-1 (std::filesystem::path)\n"
" desc Valid input file formats: [fa, sam, fasta]\n"
" desc The input file must exist and read permissions must be granted.\n"
" Valid file extensions are: [fa, sam, fasta].\n"
"\n"} +
basic_options_str +
"\n" +
Expand All @@ -190,8 +191,11 @@ TEST(validator_test, input_file_ext_from_file)
{
// Give as a template argument the seqan3 file type to get all valid extensions for this file.
seqan3::input_file_validator<dummy_file> validator{};
EXPECT_EQ(validator.get_help_page_message(), "The input file must exist and read permissions must be granted. "
"Valid file extensions are: [fa, fasta, sam, bam].");

EXPECT_EQ(validator.get_help_page_message(), "Valid input file formats: [fa, fasta, sam, bam]");
seqan3::input_file_validator validator2{};
EXPECT_EQ(validator2.get_help_page_message(), "The input file must exist and read permissions must be granted.");
}

TEST(validator_test, output_file)
Expand Down Expand Up @@ -281,7 +285,8 @@ TEST(validator_test, output_file)
"\n"
"POSITIONAL ARGUMENTS\n"
" ARGUMENT-1 (std::filesystem::path)\n"
" desc Valid output file formats: [fa, sam, fasta]\n"
" desc The output file must not exist already and write permissions\n"
" must be granted. Valid file extensions are: [fa, sam, fasta].\n"
"\n"} +
basic_options_str +
"\n" +
Expand All @@ -294,8 +299,12 @@ TEST(validator_test, output_file_ext_from_file)
{
// Give as a template argument the seqan3 file type to get all valid extensions for this file.
seqan3::output_file_validator<dummy_file> validator{};
EXPECT_EQ(validator.get_help_page_message(), "The output file must not exist already and write permissions must "
"be granted. Valid file extensions are: [fa, fasta, sam, bam].");

EXPECT_EQ(validator.get_help_page_message(), "Valid output file formats: [fa, fasta, sam, bam]");
seqan3::output_file_validator validator2{};
EXPECT_EQ(validator2.get_help_page_message(), "The output file must not exist already and write permissions must "
"be granted.");
}

TEST(validator_test, input_directory)
Expand Down Expand Up @@ -1157,8 +1166,9 @@ TEST(validator_test, chaining_validators)
basic_options_str +
" -s, --string-option (std::string)\n"
" desc Default: . Value must match the pattern '(/[^/]+)+/.*\\.[^/\\.]+$'.\n"
" Valid output file formats: [sa, so] Value must match the pattern\n"
" '.*'.\n"
" The output file must not exist already and write permissions must be\n"
" granted. Valid file extensions are: [sa, so]. Value must match the\n"
" pattern '.*'.\n"
"\n"} +
basic_version_str;
EXPECT_EQ(my_stdout, expected);
Expand Down