Skip to content

Commit 9da44e4

Browse files
author
Jesse
committed
update g++ flags and formatting
1 parent 29d131b commit 9da44e4

File tree

4 files changed

+242
-205
lines changed

4 files changed

+242
-205
lines changed

CMakeLists.txt

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,32 @@
11
cmake_minimum_required(VERSION 3.2 FATAL_ERROR)
22
cmake_policy(VERSION 3.2)
3-
option(ARGPARSE_TEST_ENABLE "Build unit tests" ON)
43

54
set(ARGPARSE_VERSION "0.0.2")
65
project(argparse VERSION ${ARGPARSE_VERSION} LANGUAGES CXX)
6+
7+
set(CMAKE_CXX_STANDARD 11)
8+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
9+
option(ARGPARSE_TEST_ENABLE "Build unit tests" ON)
10+
option(ARGPARSE_BUILD_EXAMPLE "Build example" ON)
11+
if(UNIX)
12+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -pedantic-errors -Wall -Wextra \
13+
-Wcast-align -Wcast-qual -Wctor-dtor-privacy -Wdisabled-optimization -Wformat=2 \
14+
-Winit-self -Wlogical-op -Wmissing-declarations -Wmissing-include-dirs -Wnoexcept \
15+
-Wold-style-cast -Woverloaded-virtual -Wredundant-decls -Wshadow \
16+
-Wsign-conversion -Wsign-promo -Wstrict-null-sentinel -Wstrict-overflow=5 \
17+
-Wswitch-default -Wundef -Werror -Wno-unused -Winline -Wconversion -Wfloat-equal\
18+
-Weffc++")
19+
endif(UNIX)
20+
721
enable_testing()
822

923
add_library(argparse INTERFACE)
1024
target_include_directories(argparse INTERFACE .)
1125

12-
add_executable(example example.cpp)
13-
target_link_libraries(example PRIVATE argparse)
26+
if(ARGPARSE_BUILD_EXAMPLE)
27+
add_executable(example example.cpp)
28+
target_link_libraries(example PRIVATE argparse)
29+
endif(ARGPARSE_BUILD_EXAMPLE)
1430

1531
if(ARGPARSE_TEST_ENABLE)
1632
add_executable(tests tests.cpp)

argparse.h

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,18 @@ class ArgumentParser {
3737
public:
3838
class ArgumentNotFound : public std::runtime_error {
3939
public:
40-
ArgumentNotFound(ArgumentParser::Argument &arg,
40+
ArgumentNotFound(
41+
ArgumentParser::Argument &arg,
4142
std::unordered_map<std::string, std::string> pairs) noexcept
42-
: std::runtime_error(
43-
("Required argument not found: " + arg._name + ((pairs.find(arg._name) == pairs.end())
44-
? "" : " or " + pairs.find(arg._name)->second)).c_str()) {}
43+
: std::runtime_error(("Required argument not found: " + arg._name +
44+
((pairs.find(arg._name) == pairs.end())
45+
? ""
46+
: " or " + pairs.find(arg._name)->second))
47+
.c_str()) {}
4548
};
4649

4750
ArgumentParser(const std::string &desc) : _desc(desc), _help(false) {}
48-
ArgumentParser(const std::string desc, int argc, char *argv[])
51+
ArgumentParser(const std::string desc, int argc, const char *argv[])
4952
: ArgumentParser(desc) {
5053
parse(argc, argv);
5154
}
@@ -75,7 +78,7 @@ class ArgumentParser {
7578
}
7679
}
7780

78-
void parse(int argc, char *argv[]) {
81+
void parse(int argc, const char *argv[]) {
7982
_bin = argv[0];
8083
if (argc > 1) {
8184
std::string name;
@@ -178,18 +181,19 @@ class ArgumentParser {
178181
_help = true;
179182
print_help();
180183
}
181-
_ltrim(name, [](int c) { return c != (int)'-'; });
184+
_ltrim(name, [](int c) { return c != static_cast<int>('-'); });
182185
name = _delimit(name);
183186
if (_pairs.find(name) != _pairs.end()) name = _pairs[name];
184187
_variables[name] = arg_parts;
185188
}
186189
static std::string _delimit(const std::string &name) {
187-
return std::string(std::min(name.size(), (size_t)2), '-').append(name);
190+
return std::string(std::min(name.size(), static_cast<size_t>(2)), '-')
191+
.append(name);
188192
}
189193
static std::string _strip(const std::string &name) {
190194
size_t begin = 0;
191-
begin += name.size() > 0 ? name[0] == '-' : 0;
192-
begin += name.size() > 3 ? name[1] == '-' : 0;
195+
begin += name.size() > 0 ? name[0] == '-' : 0u;
196+
begin += name.size() > 3 ? name[1] == '-' : 0u;
193197
return name.substr(begin);
194198
}
195199
static std::string _upper(const std::string &in) {
@@ -250,11 +254,11 @@ class ArgumentParser {
250254
}
251255

252256
std::string _desc;
253-
std::string _bin;
254-
bool _help;
255-
std::vector<Argument> _arguments;
256-
std::unordered_map<std::string, std::vector<std::string>> _variables;
257-
std::unordered_map<std::string, std::string> _pairs;
257+
std::string _bin{};
258+
bool _help{false};
259+
std::vector<Argument> _arguments{};
260+
std::unordered_map<std::string, std::vector<std::string>> _variables{};
261+
std::unordered_map<std::string, std::string> _pairs{};
258262
};
259263
template <>
260264
inline std::string ArgumentParser::get<std::string>(const std::string &name) {

example.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include <iostream>
2020
#include <iterator>
2121

22-
int main(int argc, char* argv[]) {
22+
int main(int argc, const char* argv[]) {
2323
// run as: [program name] "0 -c" abc -a 1 -sdfl --flag -v 1 2.7 3 4 9 8.12 87
2424
// [program name] -sdfv 1 -o "C:\Users\User Name\Directory - Name\file.dat"
2525
// "C:\Users\User Name 2\Directory 2 - Name 2\file2.dat"

0 commit comments

Comments
 (0)