Skip to content

Commit

Permalink
[MISC] Remove subcommand check
Browse files Browse the repository at this point in the history
  • Loading branch information
eseiler committed Feb 20, 2024
1 parent cf5e34e commit aeed7de
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 30 deletions.
25 changes: 0 additions & 25 deletions include/sharg/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,6 @@ class parser
* \throws sharg::too_many_arguments if the command line call contained more arguments than expected.
* \throws sharg::too_few_arguments if the command line call contained less arguments than expected.
* \throws sharg::validation_error if the argument was not excepted by the provided validator.
* \throws sharg::user_input_error if a subparser was configured at construction but a subcommand is missing.
*
* \details
*
Expand Down Expand Up @@ -428,9 +427,6 @@ class parser
// Determine the format and subcommand.
determine_format_and_subcommand();

// If a subcommand was provided, check that it is valid.
verify_subcommand();

// Apply all defered operations to the parser, e.g., `add_option`, `add_flag`, `add_positional_option`.
for (auto & operation : operations)
operation();
Expand Down Expand Up @@ -1077,27 +1073,6 @@ class parser
}
}

/*!\brief Verifies that the subcommand was correctly specified.
* \throws sharg::user_input_error if a subparser was configured at construction but a subcommand is missing.
*/
inline void verify_subcommand()
{
if (std::holds_alternative<detail::format_parse>(format) && !subcommands.empty() && sub_parser == nullptr)
{
assert(!subcommands.empty());
std::string subcommands_str{"["};
for (std::string const & command : subcommands)
subcommands_str += command + ", ";
subcommands_str.replace(subcommands_str.size() - 2, 2, "]"); // replace last ", " by "]"

throw user_input_error{"You misspelled the subcommand! Please specify which sub-program "
"you want to use: one of "
+ subcommands_str
+ ". Use -h/--help for more "
"information."};
}
}

/*!\brief Parses the command line arguments according to the format.
* \throws sharg::option_declared_multiple_times if an option that is not a list was declared multiple times.
* \throws sharg::user_input_error if an incorrect argument is given as (positional) option value.
Expand Down
12 changes: 7 additions & 5 deletions test/unit/parser/parser_design_error_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,25 +263,27 @@ TEST_F(design_error_test, subcommand_parser_error)
EXPECT_THROW(parser.parse(), sharg::design_error);

// no positional options are allowed
parser = get_subcommand_parser({"foo"}, {"foo"});
parser = get_subcommand_parser({"-f", "foo"}, {"foo"});

EXPECT_THROW(parser.add_positional_option(flag_value, sharg::config{}), sharg::design_error);
EXPECT_NO_THROW(parser.add_option(flag_value, sharg::config{.short_id = 'o'}));
EXPECT_NO_THROW(parser.add_flag(flag_value, sharg::config{.short_id = 'f'}));
EXPECT_EQ(flag_value, false);

EXPECT_THROW(parser.get_sub_parser(), sharg::design_error);
EXPECT_EQ(flag_value, false);
EXPECT_NO_THROW(parser.parse()); // Prints nothing, but sets sub_parser.
EXPECT_NO_THROW(parser.get_sub_parser());
EXPECT_EQ(flag_value, true);

flag_value = false;

// options are allowed
parser = get_subcommand_parser({"-o", "true"}, {"foo"});

EXPECT_THROW(parser.add_positional_option(flag_value, sharg::config{}), sharg::design_error);
EXPECT_NO_THROW(parser.add_option(flag_value, sharg::config{.short_id = 'o'}));
EXPECT_NO_THROW(parser.add_flag(flag_value, sharg::config{.short_id = 'f'}));
EXPECT_EQ(flag_value, false);
EXPECT_THROW(parser.parse(), sharg::user_input_error); // Todo: Subcommand is required?
EXPECT_NO_THROW(parser.parse());
EXPECT_EQ(flag_value, true);
}

TEST_F(design_error_test, not_allowed_after_parse)
Expand Down

0 comments on commit aeed7de

Please sign in to comment.