Skip to content

Commit

Permalink
Make is_option_set only work after calling parse()
Browse files Browse the repository at this point in the history
  • Loading branch information
smehringer committed Oct 9, 2020
1 parent 378feef commit bd7aa56
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
8 changes: 8 additions & 0 deletions include/seqan3/argument_parser/argument_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,11 +422,15 @@ class argument_parser
*/
bool is_option_set(char const id)
{
if (!parse_was_called)
throw design_error("You can only asked which options have been set after calling the function `parse()`.");

auto constexpr allowed = is_alnum || is_char<'_'> || is_char<'@'>;

if (!allowed(id))
throw design_error("Short option identifiers may only contain alphanumeric characters, '_', or '@'.");

// we only need to search for an option before the `end_of_options_indentifier` (`--`)
auto end_of_options = std::find(cmd_arguments.begin(), cmd_arguments.end(), end_of_options_indentifier);
return detail::format_parse::find_option_id(cmd_arguments.begin(), end_of_options, id) != end_of_options;
}
Expand All @@ -435,6 +439,9 @@ class argument_parser
//!\overload
bool is_option_set(std::string const & id)
{
if (!parse_was_called)
throw design_error("You can only asked which options have been set after calling the function `parse()`.");

if (id.size() <= 1)
throw design_error("Long IDs must be longer than one character.");
else if (is_char<'-'>(id[0]))
Expand All @@ -447,6 +454,7 @@ class argument_parser
throw design_error("Long identifiers may only contain alphanumeric characters, '_', '-', or '@'.");
});

// we only need to search for an option before the `end_of_options_indentifier` (`--`)
auto end_of_options = std::find(cmd_arguments.begin(), cmd_arguments.end(), end_of_options_indentifier);
return detail::format_parse::find_option_id(cmd_arguments.begin(), end_of_options, id) != end_of_options;
}
Expand Down
16 changes: 16 additions & 0 deletions test/unit/argument_parser/format_parse_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -972,13 +972,29 @@ TEST(parse_test, issue1544)

TEST(parse_test, is_option_set)
{
std::string option_value{};
const char * argv[] = {"./argument_parser_test", "-l", "hallo", "--foobar", "ballo", "--", "--loo"};
seqan3::argument_parser parser{"test_parser", 5, argv, false};
parser.add_option(option_value, 'l', "loo", "this is a option.");
parser.add_option(option_value, 'f', "foobar", "this is a option.");

EXPECT_THROW(parser.is_option_set("foo"), seqan3::design_error); // you cannot call option_is_set before parse()

EXPECT_NO_THROW(parser.parse());

EXPECT_TRUE(parser.is_option_set('l'));
EXPECT_TRUE(parser.is_option_set("foobar"));

EXPECT_FALSE(parser.is_option_set("foo"));
EXPECT_FALSE(parser.is_option_set('f'));
EXPECT_FALSE(parser.is_option_set("loo"));

// errors:
EXPECT_THROW(parser.is_option_set(""), seqan3::design_error); // empty option
EXPECT_THROW(parser.is_option_set("--foo"), seqan3::design_error); // no dash at the beginning
EXPECT_THROW(parser.is_option_set("f"), seqan3::design_error); // long id must be greater than 1 character
EXPECT_THROW(parser.is_option_set("f!o"), seqan3::design_error); // no weird character

EXPECT_THROW(parser.is_option_set('!'), seqan3::design_error); // no weird character
EXPECT_THROW(parser.is_option_set('-'), seqan3::design_error); // no weird character
}

0 comments on commit bd7aa56

Please sign in to comment.