Skip to content

Commit f67b0b4

Browse files
committed
run clang-format and minor fixes after merging pull request
1 parent 98687d5 commit f67b0b4

File tree

4 files changed

+139
-132
lines changed

4 files changed

+139
-132
lines changed

include/parser.hpp

Lines changed: 131 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -10,147 +10,155 @@
1010

1111
namespace cmd_line_parser {
1212

13-
struct parser {
14-
inline static const std::string empty;
13+
struct parser {
14+
inline static const std::string empty;
1515

16-
parser(int argc, char **argv)
17-
: m_argc(argc), m_argv(argv), m_required(0) {}
16+
parser(int argc, char** argv)
17+
: m_argc(argc)
18+
, m_argv(argv)
19+
, m_required(0) {}
1820

19-
struct cmd {
20-
std::string shorthand, value, descr;
21-
bool is_boolean;
22-
};
21+
struct cmd {
22+
std::string shorthand, value, descr;
23+
bool is_boolean;
24+
};
2325

24-
bool parse() {
25-
if (m_argc - 1 < m_required) { return abort(); }
26-
for (size_t i{1}, k{0}; i != m_argc; ++i, ++k) {
27-
std::string parsed(m_argv[i]);
28-
if (parsed == "-h" || parsed == "--help") { return abort(); }
29-
size_t id{k};
30-
bool is_optional = id >= m_required;
31-
if (is_optional) {
32-
if (const auto it = m_shorthands.find(parsed);it == m_shorthands.end()) {
33-
std::cerr << "== error: shorthand '" + parsed + "' not found" << std::endl;
34-
return abort();
35-
} else {
36-
id = (*it).second;
37-
}
26+
bool parse() {
27+
assert(m_argc > 0);
28+
if (m_argc - 1 < m_required) return abort();
29+
30+
for (size_t i = 1, k = 0; i != m_argc; ++i, ++k) {
31+
std::string parsed(m_argv[i]);
32+
if (parsed == "-h" || parsed == "--help") {
33+
return abort();
34+
}
35+
size_t id = k;
36+
bool is_optional = id >= m_required;
37+
if (is_optional) {
38+
if (const auto it = m_shorthands.find(parsed); it == m_shorthands.end()) {
39+
std::cerr << "== error: shorthand '" + parsed + "' not found" << std::endl;
40+
return abort();
41+
} else {
42+
id = (*it).second;
3843
}
39-
assert(id < m_names.size());
40-
auto const &name = m_names[id];
41-
auto &c = m_cmds[name];
42-
if (is_optional) {
43-
if (c.is_boolean) {
44-
parsed = "true";
45-
} else {
46-
++i;
47-
if (i == m_argc) { return abort(); }
48-
parsed = m_argv[i];
44+
}
45+
assert(id < m_names.size());
46+
auto const& name = m_names[id];
47+
auto& c = m_cmds[name];
48+
if (is_optional) {
49+
if (c.is_boolean) {
50+
parsed = "true";
51+
} else {
52+
++i;
53+
if (i == m_argc) {
54+
return abort();
4955
}
56+
parsed = m_argv[i];
5057
}
51-
c.value = parsed;
5258
}
53-
return true;
54-
}
55-
56-
void help() const {
57-
std::cerr << "Usage: " << m_argv[0] << " [-h,--help]";
58-
const auto print = [this](bool with_description) {
59-
for (size_t i = 0; i != m_names.size(); ++i) {
60-
auto const &c = m_cmds.at(m_names[i]);
61-
bool is_optional = i >= m_required;
62-
if (is_optional) { std::cerr << " [" << c.shorthand; }
63-
if (!c.is_boolean) { std::cerr << " " << m_names[i]; }
64-
if (is_optional) { std::cerr << "]"; }
65-
if (with_description) { std::cerr << "\n\t" << c.descr << "\n\n"; }
66-
}
67-
};
68-
print(false);
69-
std::cerr << "\n\n";
70-
print(true);
71-
std::cerr << " [-h,--help]\n\tPrint this help text and silently exits." << std::endl;
59+
c.value = parsed;
7260
}
61+
return true;
62+
}
7363

74-
bool add(std::string const &name, std::string const &descr) {
75-
bool ret = m_cmds.emplace(name, cmd{empty, empty, descr, false}).second;
76-
if (ret) {
77-
m_names.push_back(name);
78-
m_required += 1;
64+
void help() const {
65+
std::cerr << "Usage: " << m_argv[0] << " [-h,--help]";
66+
const auto print = [this](bool with_description) {
67+
for (size_t i = 0; i != m_names.size(); ++i) {
68+
auto const& c = m_cmds.at(m_names[i]);
69+
bool is_optional = i >= m_required;
70+
if (is_optional) std::cerr << " [" << c.shorthand;
71+
if (!c.is_boolean) std::cerr << " " << m_names[i];
72+
if (is_optional) std::cerr << "]";
73+
if (with_description) std::cerr << "\n\t" << c.descr << "\n\n";
7974
}
80-
return ret;
81-
}
75+
};
76+
print(false);
77+
std::cerr << "\n\n";
78+
print(true);
79+
std::cerr << " [-h,--help]\n\tPrint this help text and silently exits." << std::endl;
80+
}
8281

83-
bool add(std::string const &name, std::string const &descr, std::string const &shorthand,
84-
bool is_boolean = true) {
85-
bool ret =
86-
m_cmds.emplace(name, cmd{shorthand, is_boolean ? "false" : empty, descr, is_boolean})
87-
.second;
88-
if (ret) {
89-
m_names.push_back(name);
90-
m_shorthands.emplace(shorthand, m_names.size() - 1);
91-
}
92-
return ret;
82+
bool add(std::string const& name, std::string const& descr) {
83+
bool ret = m_cmds.emplace(name, cmd{empty, empty, descr, false}).second;
84+
if (ret) {
85+
m_names.push_back(name);
86+
m_required += 1;
9387
}
88+
return ret;
89+
}
9490

95-
template<typename T>
96-
T get(std::string const &name) const {
97-
auto it = m_cmds.find(name);
98-
if (it == m_cmds.end()) throw std::runtime_error("error: '" + name + "' not found");
99-
auto const &value = (*it).second.value;
100-
return parse < T > (value);
91+
bool add(std::string const& name, std::string const& descr, std::string const& shorthand,
92+
bool is_boolean = true) {
93+
bool ret =
94+
m_cmds.emplace(name, cmd{shorthand, is_boolean ? "false" : empty, descr, is_boolean})
95+
.second;
96+
if (ret) {
97+
m_names.push_back(name);
98+
m_shorthands.emplace(shorthand, m_names.size() - 1);
10199
}
100+
return ret;
101+
}
102102

103-
bool parsed(std::string const &name) const {
104-
auto it = m_cmds.find(name);
105-
if (it == m_cmds.end() || (*it).second.value == empty) { return false; }
106-
return true;
107-
}
103+
template <typename T>
104+
T get(std::string const& name) const {
105+
auto it = m_cmds.find(name);
106+
if (it == m_cmds.end()) throw std::runtime_error("error: '" + name + "' not found");
107+
auto const& value = (*it).second.value;
108+
return parse<T>(value);
109+
}
108110

109-
template<typename T>
110-
T parse(std::string const &value) const {
111-
if constexpr (std::is_same<T, std::string>::value) {
112-
return value;
113-
} else if constexpr (std::is_same<T, char>::value || std::is_same<T, signed char>::value ||
114-
std::is_same<T, unsigned char>::value) {
115-
return value.front();
116-
} else if constexpr (std::is_same<T, unsigned int>::value || std::is_same<T, int>::value ||
117-
std::is_same<T, unsigned short int>::value ||
118-
std::is_same<T, short int>::value) {
119-
return std::strtol(value.c_str(), nullptr, 10);
120-
} else if constexpr (std::is_same<T, unsigned long int>::value ||
121-
std::is_same<T, long int>::value ||
122-
std::is_same<T, unsigned long long int>::value ||
123-
std::is_same<T, long long int>::value) {
124-
return std::strtoll(value.c_str(), nullptr, 10);
125-
} else if constexpr (std::is_same<T, float>::value || std::is_same<T, double>::value ||
126-
std::is_same<T, long double>::value) {
127-
return std::strtod(value.c_str(), nullptr);
128-
} else if constexpr (std::is_same<T, bool>::value) {
129-
std::istringstream stream(value);
130-
bool ret;
131-
if (value == "true" || value == "false") {
132-
stream >> std::boolalpha >> ret;
133-
} else {
134-
stream >> std::noboolalpha >> ret;
135-
}
136-
return ret;
111+
bool parsed(std::string const& name) const {
112+
auto it = m_cmds.find(name);
113+
if (it == m_cmds.end() || (*it).second.value == empty) return false;
114+
return true;
115+
}
116+
117+
template <typename T>
118+
T parse(std::string const& value) const {
119+
if constexpr (std::is_same<T, std::string>::value) {
120+
return value;
121+
} else if constexpr (std::is_same<T, char>::value || std::is_same<T, signed char>::value ||
122+
std::is_same<T, unsigned char>::value) {
123+
return value.front();
124+
} else if constexpr (std::is_same<T, unsigned int>::value || std::is_same<T, int>::value ||
125+
std::is_same<T, unsigned short int>::value ||
126+
std::is_same<T, short int>::value) {
127+
return std::strtol(value.c_str(), nullptr, 10);
128+
} else if constexpr (std::is_same<T, unsigned long int>::value ||
129+
std::is_same<T, long int>::value ||
130+
std::is_same<T, unsigned long long int>::value ||
131+
std::is_same<T, long long int>::value) {
132+
return std::strtoll(value.c_str(), nullptr, 10);
133+
} else if constexpr (std::is_same<T, float>::value || std::is_same<T, double>::value ||
134+
std::is_same<T, long double>::value) {
135+
return std::strtod(value.c_str(), nullptr);
136+
} else if constexpr (std::is_same<T, bool>::value) {
137+
std::istringstream stream(value);
138+
bool ret;
139+
if (value == "true" || value == "false") {
140+
stream >> std::boolalpha >> ret;
141+
} else {
142+
stream >> std::noboolalpha >> ret;
137143
}
138-
assert(false); // should never happen
139-
throw std::runtime_error("unsupported type");
144+
return ret;
140145
}
146+
assert(false); // should never happen
147+
throw std::runtime_error("unsupported type");
148+
}
141149

142-
private:
143-
size_t m_argc;
144-
char **m_argv;
145-
size_t m_required;
146-
std::unordered_map<std::string, cmd> m_cmds;
147-
std::unordered_map<std::string, int> m_shorthands;
148-
std::vector<std::string> m_names;
149-
150-
bool abort() const {
151-
help();
152-
return false;
153-
}
154-
};
150+
private:
151+
size_t m_argc;
152+
char** m_argv;
153+
size_t m_required;
154+
std::unordered_map<std::string, cmd> m_cmds;
155+
std::unordered_map<std::string, int> m_shorthands;
156+
std::vector<std::string> m_names;
157+
158+
bool abort() const {
159+
help();
160+
return false;
161+
}
162+
};
155163

156164
} // namespace cmd_line_parser

src/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ if (UNIX)
2424
endif ()
2525

2626
enable_testing()
27-
add_executable(example ${CMAKE_CURRENT_SOURCE_DIR}/example.cpp)
28-
target_link_libraries(example PUBLIC CMD_LINE_PARSER)
2927

28+
add_executable(example ${CMAKE_CURRENT_SOURCE_DIR}/example.cpp)
3029
add_executable(test_parse ${CMAKE_CURRENT_SOURCE_DIR}/test_parse.cpp)
3130

3231
add_test(test_parse_CTEST ${CMAKE_BINARY_DIR}/test_parse)

src/example.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include <iostream>
22

3-
#include "parser.hpp"
3+
#include "../include/parser.hpp"
44

55
void configure(cmd_line_parser::parser& parser) {
66
// for the following two arguments, we do not specify any shorthand,

src/test_parse.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88

99
#include "../include/parser.hpp"
1010

11-
std::string demangle(std::string const &name) {
11+
std::string demangle(std::string const& name) {
1212
#ifdef __GNUC__
1313
int status = 0;
14-
char *tmp = abi::__cxa_demangle(name.c_str(), 0, 0, &status);
14+
char* tmp = abi::__cxa_demangle(name.c_str(), 0, 0, &status);
1515
std::string demagled(tmp);
1616
free(tmp);
1717
return demagled;
@@ -20,17 +20,17 @@ std::string demangle(std::string const &name) {
2020
#endif
2121
}
2222

23-
template<typename T>
23+
template <typename T>
2424
void info(T val) {
2525
std::cout << "value: " << val << "; type: " << demangle(typeid(val).name()) << std::endl;
2626
}
2727

28-
template<typename T>
29-
void test(cmd_line_parser::parser const &parser, std::string const &value) {
28+
template <typename T>
29+
void test(cmd_line_parser::parser const& parser, std::string const& value) {
3030
info(parser.parse<T>(value));
3131
}
3232

33-
int main(int argc, char **argv) {
33+
int main(int argc, char** argv) {
3434
cmd_line_parser::parser parser(argc, argv);
3535

3636
test<char>(parser, "a");

0 commit comments

Comments
 (0)