Skip to content

Commit

Permalink
cleaned up some stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
SpectraL519 committed Dec 10, 2024
1 parent 855015a commit c67599a
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 52 deletions.
45 changes: 19 additions & 26 deletions include/ap/argument_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,11 @@ template <typename T>
concept c_readable = requires(T value, std::istream& input_stream) { input_stream >> value; };

/**
* @brief The concept is satisfied when `T` is c_readable, copy constructible and assignable.
* @brief The concept is satisfied when `T` is c_readable and semiregular.
* @tparam T Type to check.
*/
template <typename T>
concept c_argument_value_type =
c_readable<T> and std::copy_constructible<T> and std::assignable_from<T&, const T&>;

/**
* @brief The concept is satisfied when `T` is comparable using the equality operator `==`.
* @tparam T Type to check.
*/
template <typename T>
concept c_equality_comparable = requires(T lhs, T rhs) {
{ lhs == rhs } -> std::convertible_to<bool>;
};
concept c_argument_value_type = c_readable<T> and std::semiregular<T>;

/**
* @brief Holds the boolean value indicating whether type `T` is the same as one of the `ValidTypes`.
Expand Down Expand Up @@ -694,7 +684,7 @@ class positional_argument : public detail::argument_interface {
* @note Requires T to be equality comparable.
*/
positional_argument& choices(const std::vector<value_type>& choices) noexcept
requires(utility::c_equality_comparable<value_type>)
requires(std::equality_comparable<value_type>)
{
this->_choices = choices;
return *this;
Expand Down Expand Up @@ -976,7 +966,7 @@ class optional_argument : public detail::argument_interface {
* @note Requires T to be equality comparable.
*/
optional_argument& choices(const std::vector<value_type>& choices) noexcept
requires(utility::c_equality_comparable<value_type>)
requires(std::equality_comparable<value_type>)
{
this->_choices = choices;
return *this;
Expand Down Expand Up @@ -1040,7 +1030,7 @@ class optional_argument : public detail::argument_interface {

/// @brief Mark the optional argument as used.
void mark_used() noexcept override {
this->_nused++;
++this->_nused;
}

/// @return True if the optional argument is used, false otherwise.
Expand Down Expand Up @@ -1074,7 +1064,7 @@ class optional_argument : public detail::argument_interface {
if (not (this->_nargs_range or this->_values.empty()))
throw error::value_already_set_error(this->_name);

this->_values.push_back(value);
this->_values.emplace_back(std::move(value));
return *this;
}

Expand Down Expand Up @@ -1269,8 +1259,9 @@ class argument_parser {
if (this->_is_arg_name_used(arg_name))
throw error::argument_name_used_error(arg_name);

this->_positional_args.push_back(std::make_unique<argument::positional_argument<T>>(arg_name
));
this->_positional_args.emplace_back(
std::make_unique<argument::positional_argument<T>>(arg_name)
);
return static_cast<argument::positional_argument<T>&>(*this->_positional_args.back());
}

Expand All @@ -1291,8 +1282,9 @@ class argument_parser {
if (this->_is_arg_name_used(arg_name))
throw error::argument_name_used_error(arg_name);

this->_positional_args.push_back(std::make_unique<argument::positional_argument<T>>(arg_name
));
this->_positional_args.emplace_back(
std::make_unique<argument::positional_argument<T>>(arg_name)
);
return static_cast<argument::positional_argument<T>&>(*this->_positional_args.back());
}

Expand Down Expand Up @@ -1331,7 +1323,8 @@ class argument_parser {
if (this->_is_arg_name_used(arg_name))
throw error::argument_name_used_error(arg_name);

this->_optional_args.push_back(std::make_unique<argument::optional_argument<T>>(arg_name));
this->_optional_args.emplace_back(std::make_unique<argument::optional_argument<T>>(arg_name)
);
return static_cast<argument::optional_argument<T>&>(*this->_optional_args.back());
}

Expand Down Expand Up @@ -1636,14 +1629,14 @@ class argument_parser {
cmd_argument_list args;
args.reserve(argc - 1);

for (int i = 1; i < argc; i++) {
for (int i = 1; i < argc; ++i) {
std::string value = argv[i];
if (this->_is_flag(value)) {
this->_strip_flag_prefix(value);
args.push_back(cmd_argument{cmd_argument::type_discriminator::flag, value});
args.emplace_back(cmd_argument::type_discriminator::flag, std::move(value));
}
else {
args.push_back(cmd_argument{cmd_argument::type_discriminator::value, value});
args.emplace_back(cmd_argument::type_discriminator::value, std::move(value));
}
}

Expand Down Expand Up @@ -1702,7 +1695,7 @@ class argument_parser {
return;

pos_arg->set_value(cmd_it->value);
cmd_it++;
++cmd_it;
}
}

Expand Down Expand Up @@ -1735,7 +1728,7 @@ class argument_parser {
curr_opt_arg->get()->set_value(cmd_it->value);
}

cmd_it++;
++cmd_it;
}
}

Expand Down
14 changes: 7 additions & 7 deletions tests/include/argument_parser_test_fixture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ struct argument_parser_test_fixture {

argv_vec.emplace_back("program");

for (std::size_t i = 0; i < args_split; i++) // positional args
for (std::size_t i = 0; i < args_split; ++i) // positional args
argv_vec.emplace_back(prepare_arg_value(i));

for (std::size_t i = args_split; i < num_args; i++) { // optional args
for (std::size_t i = args_split; i < num_args; ++i) { // optional args
argv_vec.emplace_back(prepare_arg_flag_primary(i));
argv_vec.emplace_back(prepare_arg_value(i));
}
Expand All @@ -78,7 +78,7 @@ struct argument_parser_test_fixture {
}

void free_argv(std::size_t argc, char** argv) const {
for (std::size_t i = 0; i < argc; i++)
for (std::size_t i = 0; i < argc; ++i)
delete[] argv[i];
delete[] argv;
}
Expand All @@ -89,12 +89,12 @@ struct argument_parser_test_fixture {

void add_arguments(ap::argument_parser& parser, std::size_t num_args, std::size_t args_split)
const {
for (std::size_t i = 0; i < args_split; i++) { // positional args
for (std::size_t i = 0; i < args_split; ++i) { // positional args
const auto arg_name = prepare_arg_name(i);
parser.add_positional_argument(arg_name.primary, arg_name.secondary.value());
}

for (std::size_t i = args_split; i < num_args; i++) { // optional args
for (std::size_t i = args_split; i < num_args; ++i) { // optional args
const auto arg_name = prepare_arg_name(i);
parser.add_optional_argument(arg_name.primary, arg_name.secondary.value());
}
Expand All @@ -106,12 +106,12 @@ struct argument_parser_test_fixture {
cmd_argument_list cmd_args;
cmd_args.reserve(get_args_length(num_args, args_split));

for (std::size_t i = 0; i < args_split; i++) { // positional args
for (std::size_t i = 0; i < args_split; ++i) { // positional args
cmd_args.push_back(
cmd_argument{cmd_argument::type_discriminator::value, prepare_arg_value(i)}
);
}
for (std::size_t i = args_split; i < num_args; i++) { // optional args
for (std::size_t i = args_split; i < num_args; ++i) { // optional args
cmd_args.push_back(
cmd_argument{cmd_argument::type_discriminator::flag, prepare_arg_name(i).primary}
);
Expand Down
34 changes: 17 additions & 17 deletions tests/source/test_argument_parser_parse_args.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ TEST_CASE_FIXTURE(

REQUIRE_EQ(cmd_args.size(), get_args_length(non_default_num_args, non_default_args_split));

for (std::size_t i = 0; i < non_default_args_split; i++) { // positional args
for (std::size_t i = 0; i < non_default_args_split; ++i) { // positional args
REQUIRE_EQ(cmd_args.at(i).discriminator, cmd_argument::type_discriminator::value);
CHECK_EQ(cmd_args.at(i).value, prepare_arg_value(i));
}
Expand All @@ -67,7 +67,7 @@ TEST_CASE_FIXTURE(
REQUIRE_EQ(cmd_args.at(i + 1).discriminator, cmd_argument::type_discriminator::value);
CHECK_EQ(cmd_args.at(i + 1).value, prepare_arg_value(opt_arg_idx));

opt_arg_idx++;
++opt_arg_idx;
}

free_argv(argc, argv);
Expand Down Expand Up @@ -115,7 +115,7 @@ TEST_CASE_FIXTURE(
) {
add_arguments(sut, non_default_num_args, non_default_args_split);

for (std::size_t i = 0; i < non_default_num_args; i++) {
for (std::size_t i = 0; i < non_default_num_args; ++i) {
const auto arg_name = prepare_arg_name(i);
CHECK(sut_get_argument(arg_name.primary));
CHECK(sut_get_argument(arg_name.secondary.value()));
Expand Down Expand Up @@ -318,7 +318,7 @@ TEST_CASE_FIXTURE(

REQUIRE_NOTHROW(sut.parse_args(argc, argv));

for (std::size_t i = 0; i < non_default_num_args; i++) {
for (std::size_t i = 0; i < non_default_num_args; ++i) {
const auto arg_name = prepare_arg_name(i);
CHECK(sut.has_value(arg_name.primary));
CHECK(sut.has_value(arg_name.secondary.value()));
Expand All @@ -335,17 +335,17 @@ TEST_CASE_FIXTURE(

REQUIRE_NOTHROW(sut.parse_args(argc, argv));

for (std::size_t i = 0; i < non_default_args_split; i++) {
for (std::size_t i = 0; i < non_default_args_split; ++i) {
const auto arg_name = prepare_arg_name(i);
CHECK(sut.has_value(arg_name.primary));
CHECK(sut.has_value(arg_name.secondary.value()));
}
for (std::size_t i = non_default_args_split; i < non_default_num_args; i++) {
for (std::size_t i = non_default_args_split; i < non_default_num_args; ++i) {
const auto arg_name = prepare_arg_name(i);
CHECK_FALSE(sut.has_value(arg_name.primary));
CHECK_FALSE(sut.has_value(arg_name.secondary.value()));
}
for (std::size_t i = non_default_num_args; i < num_args; i++) {
for (std::size_t i = non_default_num_args; i < num_args; ++i) {
const auto arg_name = prepare_arg_name(i);
CHECK(sut.has_value(arg_name.primary));
CHECK(sut.has_value(arg_name.secondary.value()));
Expand Down Expand Up @@ -380,7 +380,7 @@ TEST_CASE_FIXTURE(
) {
add_arguments(sut, non_default_num_args, non_default_args_split);

for (std::size_t i = 0; i < non_default_num_args; i++) {
for (std::size_t i = 0; i < non_default_num_args; ++i) {
const auto arg_name = prepare_arg_name(i);
CHECK_THROWS_AS(sut.value(arg_name.primary), std::logic_error);
CHECK_THROWS_AS(sut.value(arg_name.secondary.value()), std::logic_error);
Expand Down Expand Up @@ -410,17 +410,17 @@ TEST_CASE_FIXTURE(

REQUIRE_NOTHROW(sut.parse_args(argc, argv));

for (std::size_t i = 0; i < non_default_args_split; i++) {
for (std::size_t i = 0; i < non_default_args_split; ++i) {
const auto arg_name = prepare_arg_name(i);
CHECK_NOTHROW(sut.value(arg_name.primary));
CHECK_NOTHROW(sut.value(arg_name.secondary.value()));
}
for (std::size_t i = non_default_args_split; i < non_default_num_args; i++) {
for (std::size_t i = non_default_args_split; i < non_default_num_args; ++i) {
const auto arg_name = prepare_arg_name(i);
CHECK_THROWS_AS(sut.value(arg_name.primary), std::logic_error);
CHECK_THROWS_AS(sut.value(arg_name.secondary.value()), std::logic_error);
}
for (std::size_t i = non_default_num_args; i < num_args; i++) {
for (std::size_t i = non_default_num_args; i < num_args; ++i) {
const auto arg_name = prepare_arg_name(i);
CHECK_NOTHROW(sut.value(arg_name.primary));
CHECK_NOTHROW(sut.value(arg_name.secondary.value()));
Expand All @@ -444,7 +444,7 @@ TEST_CASE_FIXTURE(

using invalid_value_type = int;

for (std::size_t i = 0; i < non_default_num_args; i++) {
for (std::size_t i = 0; i < non_default_num_args; ++i) {
const auto arg_name = prepare_arg_name(i);

REQUIRE(sut.has_value(arg_name.primary));
Expand Down Expand Up @@ -475,7 +475,7 @@ TEST_CASE_FIXTURE(

REQUIRE_NOTHROW(sut.parse_args(argc, argv));

for (std::size_t i = 0; i < non_default_num_args; i++) {
for (std::size_t i = 0; i < non_default_num_args; ++i) {
const auto arg_name = prepare_arg_name(i);
const auto arg_value = prepare_arg_value(i);

Expand All @@ -494,7 +494,7 @@ TEST_CASE_FIXTURE(
) {
add_arguments(sut, non_default_num_args, non_default_args_split);

for (std::size_t i = 0; i < non_default_num_args; i++) {
for (std::size_t i = 0; i < non_default_num_args; ++i) {
const auto arg_name = prepare_arg_name(i);
CHECK_EQ(sut.count(arg_name.primary), 0u);
CHECK_EQ(sut.count(arg_name.secondary.value()), 0u);
Expand Down Expand Up @@ -611,7 +611,7 @@ TEST_CASE_FIXTURE(
const std::string flag = "--" + optional_primary_name;
argv[1] = new char[flag.length() + 1];
std::strcpy(argv[1], flag.c_str());
for (int i = 2; i < argc; i++) {
for (int i = 2; i < argc; ++i) {
const auto value = prepare_arg_value(i - 1);
argv[i] = new char[value.length() + 1];
std::strcpy(argv[i], value.c_str());
Expand Down Expand Up @@ -703,7 +703,7 @@ TEST_CASE_FIXTURE(

std::vector<std::string> values;

for (int i = 2; i < argc; i++) {
for (int i = 2; i < argc; ++i) {
const auto value = prepare_arg_value(i - 1);
argv[i] = new char[value.length() + 1];
std::strcpy(argv[i], value.c_str());
Expand All @@ -716,7 +716,7 @@ TEST_CASE_FIXTURE(
const auto& stored_values = sut.values(optional_primary_name);

REQUIRE_EQ(stored_values.size(), values.size());
for (std::size_t i = 0; i < stored_values.size(); i++)
for (std::size_t i = 0; i < stored_values.size(); ++i)
CHECK_EQ(stored_values[i], values[i]);

free_argv(argc, argv);
Expand Down
4 changes: 2 additions & 2 deletions tests/source/test_optional_argument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ TEST_CASE_FIXTURE(
auto sut = prepare_argument(primary_name);

constexpr std::size_t nused = 5u;
for (std::size_t n = 0; n < nused; n++)
for (std::size_t n = 0; n < nused; ++n)
sut_set_used(sut);

CHECK_EQ(sut_get_nused(sut), nused);
Expand Down Expand Up @@ -348,7 +348,7 @@ TEST_CASE_FIXTURE(
const auto stored_values = sut_get_values(sut);

REQUIRE_EQ(stored_values.size(), default_choices.size());
for (std::size_t i = 0; i < stored_values.size(); i++) {
for (std::size_t i = 0; i < stored_values.size(); ++i) {
REQUIRE_EQ(std::any_cast<test_value_type>(stored_values[i]), default_choices[i]);
}
}
Expand Down

0 comments on commit c67599a

Please sign in to comment.