Merge pull request #202 from p-ranav/bugfix/94_implicit_conversions

Closes #94
This commit is contained in:
Pranav 2022-09-21 08:29:14 -05:00 committed by GitHub
commit d512563c45
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 9 deletions

View File

@ -98,8 +98,10 @@ template <typename T> std::string repr(T const &val) {
out << repr(*val.begin()); out << repr(*val.begin());
std::for_each( std::for_each(
std::next(val.begin()), std::next(val.begin()),
std::next(val.begin(), std::next(
std::min<std::size_t>(size, repr_max_container_size) - 1), 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); }); [&out](const auto &v) { out << " " << repr(v); });
if (size <= repr_max_container_size) { if (size <= repr_max_container_size) {
out << " "; out << " ";
@ -507,7 +509,8 @@ public:
if ((dist = static_cast<std::size_t>(std::distance(start, end))) >= if ((dist = static_cast<std::size_t>(std::distance(start, end))) >=
num_args_min) { num_args_min) {
if (num_args_max < dist) { 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) { if (!m_accepts_optional_like_value) {
end = std::find_if(start, end, Argument::is_optional); end = std::find_if(start, end, Argument::is_optional);
@ -526,7 +529,8 @@ public:
std::for_each(first, last, f); std::for_each(first, last, f);
if (!self.m_default_value.has_value()) { if (!self.m_default_value.has_value()) {
if (!self.m_accepts_optional_like_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)));
} }
} }
} }
@ -729,7 +733,7 @@ private:
auto consume_digits = [=](std::string_view s) { auto consume_digits = [=](std::string_view s) {
// NOLINTNEXTLINE(readability-qualified-auto) // NOLINTNEXTLINE(readability-qualified-auto)
auto it = std::find_if_not(std::begin(s), std::end(s), is_digit); 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)) { switch (lookahead(s)) {
@ -1165,7 +1169,7 @@ public:
} }
for (const auto &argument : parser.m_positional_arguments) { for (const auto &argument : parser.m_positional_arguments) {
stream.width(longest_arg_length); stream.width(static_cast<std::streamsize>(longest_arg_length));
stream << argument; stream << argument;
} }
@ -1175,7 +1179,7 @@ public:
} }
for (const auto &argument : parser.m_optional_arguments) { for (const auto &argument : parser.m_optional_arguments) {
stream.width(longest_arg_length); stream.width(static_cast<std::streamsize>(longest_arg_length));
stream << argument; stream << argument;
} }
@ -1185,7 +1189,7 @@ public:
: "\n") : "\n")
<< "Subcommands:\n"; << "Subcommands:\n";
for (const auto &[command, subparser] : parser.m_subparser_map) { 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"; stream << command << "\t" << subparser->get().m_description << "\n";
} }
} }

BIN
include/argparse/test Executable file

Binary file not shown.

15
include/argparse/test.cpp Normal file
View File

@ -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"}));
}

View File

@ -10,7 +10,7 @@ if(MSVC)
endif() endif()
elseif(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX) elseif(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX)
# Update if necessary # 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() endif()
if(NOT CMAKE_BUILD_TYPE) if(NOT CMAKE_BUILD_TYPE)