Skip to content

Commit

Permalink
- CMakefile: Use -Wpedantic, -Werror and -Wextra for compilation in gcc
Browse files Browse the repository at this point in the history
- Executed clang format to reformat the code base
  • Loading branch information
BitMaskMixer committed Oct 20, 2023
1 parent 57b63b0 commit 5c78511
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 56 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ project(argparse
)

option(ARGPARSE_INSTALL "Include an install target" ON)
option(ARGPARSE_BUILD_TESTS "Build tests" OFF)
option(ARGPARSE_BUILD_TESTS "Build tests" ON)

include(GNUInstallDirs)
include(CMakePackageConfigHelpers)
Expand Down
38 changes: 17 additions & 21 deletions include/argparse/argparse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ class Argument {
}

template <class F, class... Args>
auto action(F &&callable, Args &&... bound_args)
auto action(F &&callable, Args &&...bound_args)
-> std::enable_if_t<std::is_invocable_v<F, Args..., std::string const>,
Argument &> {
using action_type = std::conditional_t<
Expand Down Expand Up @@ -509,10 +509,12 @@ class Argument {
m_num_args_range = NArgsRange{0, 1};
break;
case nargs_pattern::any:
m_num_args_range = NArgsRange{0, (std::numeric_limits<std::size_t>::max)()};
m_num_args_range =
NArgsRange{0, (std::numeric_limits<std::size_t>::max)()};
break;
case nargs_pattern::at_least_one:
m_num_args_range = NArgsRange{1, (std::numeric_limits<std::size_t>::max)()};
m_num_args_range =
NArgsRange{1, (std::numeric_limits<std::size_t>::max)()};
break;
}
return *this;
Expand Down Expand Up @@ -680,8 +682,8 @@ class Argument {
// align multiline help message
auto stream_width = stream.width();
auto name_padding = std::string(name_stream.str().size(), ' ');
auto pos = 0;
auto prev = 0;
std::string::size_type pos = 0;
std::string::size_type prev = 0;
auto first_line = true;
auto hspace = " "; // minimal space between name and help message
stream << name_stream.str();
Expand Down Expand Up @@ -738,8 +740,7 @@ class Argument {
using ValueType = typename T::value_type;
auto lhs = get<T>();
return std::equal(std::begin(lhs), std::end(lhs), std::begin(rhs),
std::end(rhs),
[](const auto &a, const auto &b) {
std::end(rhs), [](const auto &a, const auto &b) {
return std::any_cast<const ValueType &>(a) == b;
});
}
Expand Down Expand Up @@ -1152,16 +1153,11 @@ class ArgumentParser {
}

explicit operator bool() const {
auto arg_used = std::any_of(m_argument_map.cbegin(),
m_argument_map.cend(),
[](auto &it) {
return it.second->m_is_used;
});
auto subparser_used = std::any_of(m_subparser_used.cbegin(),
m_subparser_used.cend(),
[](auto &it) {
return it.second;
});
auto arg_used = std::any_of(m_argument_map.cbegin(), m_argument_map.cend(),
[](auto &it) { return it.second->m_is_used; });
auto subparser_used =
std::any_of(m_subparser_used.cbegin(), m_subparser_used.cend(),
[](auto &it) { return it.second; });

return m_is_parsed && (arg_used || subparser_used);
}
Expand All @@ -1186,7 +1182,7 @@ class ArgumentParser {
// Parameter packed add_parents method
// Accepts a variadic number of ArgumentParser objects
template <typename... Targs>
ArgumentParser &add_parents(const Targs &... f_args) {
ArgumentParser &add_parents(const Targs &...f_args) {
for (const ArgumentParser &parent_parser : {std::ref(f_args)...}) {
for (const auto &argument : parent_parser.m_positional_arguments) {
auto it = m_positional_arguments.insert(
Expand Down Expand Up @@ -1215,8 +1211,7 @@ class ArgumentParser {
/* Getter for arguments and subparsers.
* @throws std::logic_error in case of an invalid argument or subparser name
*/
template <typename T = Argument>
T& at(std::string_view name) {
template <typename T = Argument> T &at(std::string_view name) {
if constexpr (std::is_same_v<T, Argument>) {
return (*this)[name];
} else {
Expand Down Expand Up @@ -1692,7 +1687,8 @@ class ArgumentParser {
}
std::size_t max_size = 0;
for ([[maybe_unused]] const auto &[unused, argument] : m_argument_map) {
max_size = std::max<std::size_t>(max_size, argument->get_arguments_length());
max_size =
std::max<std::size_t>(max_size, argument->get_arguments_length());
}
for ([[maybe_unused]] const auto &[command, unused] : m_subparser_map) {
max_size = std::max<std::size_t>(max_size, command.size());
Expand Down
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 -Wsign-conversion -Wshadow -Wconversion")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wno-long-long -Wpedantic -Wsign-conversion -Wshadow -Wconversion -Werror -Wextra")
endif()

if(NOT CMAKE_BUILD_TYPE)
Expand Down
2 changes: 1 addition & 1 deletion test/test_append.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import argparse;
#endif
#include <doctest.hpp>

#include <vector>
#include <string>
#include <vector>

using doctest::test_suite;

Expand Down
7 changes: 4 additions & 3 deletions test/test_as_container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ TEST_CASE("Get argument with .at()" * test_suite("as_container")) {

SUBCASE("with unknown argument") {
program.parse_args({"test"});
REQUIRE_THROWS_WITH_AS(program.at("--folder"),
"No such argument: --folder", std::logic_error);
REQUIRE_THROWS_WITH_AS(program.at("--folder"), "No such argument: --folder",
std::logic_error);
}
}

Expand All @@ -44,7 +44,8 @@ TEST_CASE("Get subparser with .at()" * test_suite("as_container")) {
SUBCASE("and its argument") {
program.parse_args({"test", "walk", "4km/h"});
REQUIRE(&(program.at<argparse::ArgumentParser>("walk")) == &walk_cmd);
REQUIRE(&(program.at<argparse::ArgumentParser>("walk").at("speed")) == &speed);
REQUIRE(&(program.at<argparse::ArgumentParser>("walk").at("speed")) ==
&speed);
REQUIRE(program.at<argparse::ArgumentParser>("walk").is_used("speed"));
}

Expand Down
7 changes: 3 additions & 4 deletions test/test_bool_operator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import argparse;

using doctest::test_suite;

TEST_CASE("ArgumentParser in bool context" *
test_suite("argument_parser")) {
TEST_CASE("ArgumentParser in bool context" * test_suite("argument_parser")) {
argparse::ArgumentParser program("test");
program.add_argument("cases").remaining();

Expand Down Expand Up @@ -39,7 +38,7 @@ TEST_CASE("With subparsers in bool context" * test_suite("argument_parser")) {
}

TEST_CASE("Parsers remain false with unknown arguments" *
test_suite("argument_parser")) {
test_suite("argument_parser")) {
argparse::ArgumentParser program("test");

argparse::ArgumentParser cmd_build("build");
Expand All @@ -59,7 +58,7 @@ TEST_CASE("Parsers remain false with unknown arguments" *
}

TEST_CASE("Multi-level parsers match subparser bool" *
test_suite("argument_parser")) {
test_suite("argument_parser")) {
argparse::ArgumentParser program("test");

argparse::ArgumentParser cmd_cook("cook");
Expand Down
4 changes: 2 additions & 2 deletions test/test_default_args.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import argparse;
#endif
#include <doctest.hpp>

#include <iostream>
#include <sstream>
#include <streambuf>
#include <iostream>

using doctest::test_suite;

Expand All @@ -30,7 +30,7 @@ TEST_CASE("Do not exit on default arguments" * test_suite("default_args")) {
argparse::ArgumentParser parser("test", "1.0",
argparse::default_arguments::all, false);
std::stringstream buf;
std::streambuf* saved_cout_buf = std::cout.rdbuf(buf.rdbuf());
std::streambuf *saved_cout_buf = std::cout.rdbuf(buf.rdbuf());
parser.parse_args({"test", "--help"});
std::cout.rdbuf(saved_cout_buf);
REQUIRE(parser.is_used("--help"));
Expand Down
2 changes: 1 addition & 1 deletion test/test_equals_form.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import argparse;
#include <doctest.hpp>

#include <iostream>
#include <vector>
#include <string>
#include <vector>

using doctest::test_suite;

Expand Down
2 changes: 1 addition & 1 deletion test/test_get.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ TEST_CASE("Implicit argument" * test_suite("ArgumentParser::get")) {

TEST_CASE("Mismatched type for argument" * test_suite("ArgumentParser::get")) {
argparse::ArgumentParser program("test");
program.add_argument("-s", "--stuff"); // as default type, a std::string
program.add_argument("-s", "--stuff"); // as default type, a std::string
REQUIRE_NOTHROW(program.parse_args({"test", "-s", "321"}));
REQUIRE_THROWS_AS(program.get<int>("--stuff"), std::bad_any_cast);
}
30 changes: 14 additions & 16 deletions test/test_help.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import argparse;
#endif
#include <doctest.hpp>

#include <sstream>
#include <optional>
#include <sstream>

using doctest::test_suite;

Expand Down Expand Up @@ -82,39 +82,37 @@ TEST_CASE("Users can replace default -h/--help" * test_suite("help")) {

TEST_CASE("Multiline help message alignment") {
// '#' is used at the beginning of each help message line to simplify testing.
// It is important to ensure that this character doesn't appear elsewhere in the test case.
// Default arguments (e.g., -h/--help, -v/--version) are not included in this test.
// It is important to ensure that this character doesn't appear elsewhere in
// the test case. Default arguments (e.g., -h/--help, -v/--version) are not
// included in this test.
argparse::ArgumentParser program("program");
program.add_argument("INPUT1")
.help(
"#This is the first line of help message.\n"
"#And this is the second line of help message."
);
program.add_argument("program_input2")
.help("#There is only one line.");
program.add_argument("INPUT1").help(
"#This is the first line of help message.\n"
"#And this is the second line of help message.");
program.add_argument("program_input2").help("#There is only one line.");
program.add_argument("-p", "--prog_input3")
.help(
R"(#Lorem ipsum dolor sit amet, consectetur adipiscing elit.
R"(#Lorem ipsum dolor sit amet, consectetur adipiscing elit.
#Sed ut perspiciatis unde omnis iste natus error sit voluptatem
#accusantium doloremque laudantium, totam rem aperiam...)"
);
#accusantium doloremque laudantium, totam rem aperiam...)");
program.add_argument("--verbose").default_value(false).implicit_value(true);

std::ostringstream stream;
stream << program;
std::istringstream iss(stream.str());

int help_message_start = -1;
auto help_message_start = std::string::npos;
std::string line;
while (std::getline(iss, line)) {
// Find the position of '#', which indicates the start of the help message line
// Find the position of '#', which indicates the start of the help message
// line
auto pos = line.find('#');

if (pos == std::string::npos) {
continue;
}

if (help_message_start == -1) {
if (help_message_start == std::string::npos) {
help_message_start = pos;
} else {
REQUIRE(pos == help_message_start);
Expand Down
2 changes: 1 addition & 1 deletion test/test_parse_known_args.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import argparse;
#endif
#include <doctest.hpp>

#include <vector>
#include <string>
#include <vector>

using doctest::test_suite;

Expand Down
2 changes: 1 addition & 1 deletion test/test_repr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import argparse.details;
#endif
#include <doctest.hpp>

#include <set>
#include <list>
#include <set>
#include <sstream>

using doctest::test_suite;
Expand Down
6 changes: 3 additions & 3 deletions test/test_subparsers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import argparse;
#include <doctest.hpp>

#include <cmath>
#include <vector>
#include <string>
#include <vector>

using doctest::test_suite;

Expand Down Expand Up @@ -213,8 +213,8 @@ TEST_CASE("Check is_subcommand_used after parse" * test_suite("subparsers")) {

argparse::ArgumentParser command_2("clean");
command_2.add_argument("--fullclean")
.default_value(false)
.implicit_value(true);
.default_value(false)
.implicit_value(true);

argparse::ArgumentParser program("test");
program.add_subparser(command_1);
Expand Down

0 comments on commit 5c78511

Please sign in to comment.