Skip to content

Commit

Permalink
Merge pull request #202 from p-ranav/bugfix/94_implicit_conversions
Browse files Browse the repository at this point in the history
Closes #94
  • Loading branch information
p-ranav authored Sep 21, 2022
2 parents 4dbc910 + 14287af commit d512563
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
20 changes: 12 additions & 8 deletions include/argparse/argparse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,10 @@ template <typename T> std::string repr(T const &val) {
out << repr(*val.begin());
std::for_each(
std::next(val.begin()),
std::next(val.begin(),
std::min<std::size_t>(size, repr_max_container_size) - 1),
std::next(
val.begin(),
static_cast<typename T::iterator::difference_type>(
std::min<std::size_t>(size, repr_max_container_size) - 1)),
[&out](const auto &v) { out << " " << repr(v); });
if (size <= repr_max_container_size) {
out << " ";
Expand Down Expand Up @@ -507,7 +509,8 @@ class Argument {
if ((dist = static_cast<std::size_t>(std::distance(start, end))) >=
num_args_min) {
if (num_args_max < dist) {
end = std::next(start, num_args_max);
end = std::next(start, static_cast<typename Iterator::difference_type>(
num_args_max));
}
if (!m_accepts_optional_like_value) {
end = std::find_if(start, end, Argument::is_optional);
Expand All @@ -526,7 +529,8 @@ class Argument {
std::for_each(first, last, f);
if (!self.m_default_value.has_value()) {
if (!self.m_accepts_optional_like_value) {
self.m_values.resize(std::distance(first, last));
self.m_values.resize(
static_cast<std::size_t>(std::distance(first, last)));
}
}
}
Expand Down Expand Up @@ -729,7 +733,7 @@ class Argument {
auto consume_digits = [=](std::string_view s) {
// NOLINTNEXTLINE(readability-qualified-auto)
auto it = std::find_if_not(std::begin(s), std::end(s), is_digit);
return s.substr(it - std::begin(s));
return s.substr(static_cast<std::size_t>(it - std::begin(s)));
};

switch (lookahead(s)) {
Expand Down Expand Up @@ -1165,7 +1169,7 @@ class ArgumentParser {
}

for (const auto &argument : parser.m_positional_arguments) {
stream.width(longest_arg_length);
stream.width(static_cast<std::streamsize>(longest_arg_length));
stream << argument;
}

Expand All @@ -1175,7 +1179,7 @@ class ArgumentParser {
}

for (const auto &argument : parser.m_optional_arguments) {
stream.width(longest_arg_length);
stream.width(static_cast<std::streamsize>(longest_arg_length));
stream << argument;
}

Expand All @@ -1185,7 +1189,7 @@ class ArgumentParser {
: "\n")
<< "Subcommands:\n";
for (const auto &[command, subparser] : parser.m_subparser_map) {
stream.width(longest_arg_length);
stream.width(static_cast<std::streamsize>(longest_arg_length));
stream << command << "\t" << subparser->get().m_description << "\n";
}
}
Expand Down
Binary file added include/argparse/test
Binary file not shown.
15 changes: 15 additions & 0 deletions include/argparse/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "argparse.hpp"
#include <cassert>

int main(int argc, char *argv[]) {
argparse::ArgumentParser program("test");
program.add_argument("--foo").implicit_value(true).default_value(false);
program.add_argument("bar");

auto unknown_args =
program.parse_known_args({"test", "--foo", "--badger", "BAR", "spam"});

assert(program.get<bool>("--foo") == true);
assert(program.get<std::string>("bar") == std::string{"BAR"});
assert((unknown_args == std::vector<std::string>{"--badger", "spam"}));
}
2 changes: 1 addition & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ if(MSVC)
endif()
elseif(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX)
# Update if necessary
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wno-long-long -pedantic")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wno-long-long -pedantic -Wsign-conversion")
endif()

if(NOT CMAKE_BUILD_TYPE)
Expand Down

0 comments on commit d512563

Please sign in to comment.