Skip to content

Commit b1dfda2

Browse files
committed
Refactor
1 parent 424d8ab commit b1dfda2

17 files changed

+293
-264
lines changed

.clang-tidy

+1-2
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,8 @@ ImplementationFileExtensions:
4141
- cpp
4242
- cxx
4343
HeaderFilterRegex: ''
44-
AnalyzeTemporaryDtors: false
4544
FormatStyle: file
4645
CheckOptions:
4746
readability-identifier-naming.ClassMethodCase: 'camelBack'
4847

49-
SystemHeaders: false
48+
SystemHeaders: false

Argo/ArgoArg.cc

+14-11
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ struct ParserID {
1616
int idInt = 0;
1717
char idName[N];
1818

19+
// NOLINTNEXTLINE(google-explicit-constructor)
1920
constexpr ParserID(int id) : idInt(id){};
2021

22+
// NOLINTNEXTLINE(google-explicit-constructor)
2123
constexpr ParserID(const char (&id)[N + 1]) {
2224
for (std::size_t i = 0; i < N; i++) {
2325
this->idName[i] = id[i];
@@ -40,6 +42,7 @@ struct ArgName : ArgNameTag {
4042

4143
explicit ArgName() = default;
4244

45+
// NOLINTNEXTLINE(google-explicit-constructor)
4346
constexpr ArgName(const char (&lhs)[N + 1]) {
4447
for (std::size_t i = 0; i < N; i++) {
4548
if (lhs[i] == ',') {
@@ -100,21 +103,21 @@ struct ArgName : ArgNameTag {
100103

101104
if (MV != NV) {
102105
return false;
103-
} else {
104-
for (std::size_t i = 0; i < NV; i++) {
105-
if ((*this)[i] != lhs[i]) {
106-
return false;
107-
}
106+
}
107+
for (std::size_t i = 0; i < NV; i++) {
108+
if ((*this)[i] != lhs[i]) {
109+
return false;
108110
}
109-
return true;
110111
}
112+
return true;
111113
}
112114

115+
// NOLINTNEXTLINE(google-explicit-constructor)
113116
constexpr operator std::string_view() const {
114117
return std::string_view(this->begin(), this->end());
115118
}
116119

117-
constexpr auto containsInvalidChar() const -> bool {
120+
[[nodiscard]] constexpr auto containsInvalidChar() const -> bool {
118121
auto invalid_chars = std::string_view(" \\\"'<>&|$[]");
119122
if (invalid_chars.contains(this->shortName)) {
120123
return true;
@@ -127,7 +130,7 @@ struct ArgName : ArgNameTag {
127130
return false;
128131
}
129132

130-
constexpr auto hasValidNameLength() const -> bool {
133+
[[nodiscard]] constexpr auto hasValidNameLength() const -> bool {
131134
if (this->shortName == '\0') {
132135
return true;
133136
}
@@ -207,7 +210,7 @@ constexpr std::string get_type_name() {
207210
throw std::runtime_error("Error");
208211
}
209212
for (std::size_t i = 0; i < TNArgs.nargs; i++) {
210-
ret = ret + base_type_name;
213+
ret += base_type_name;
211214
ret.push_back(',');
212215
}
213216
ret.pop_back();
@@ -298,7 +301,7 @@ struct FlagArg : FlagArgTag, ArgBase<bool, Name, false, ID> {
298301

299302
inline static constexpr NArgs nargs = NArgs(-1);
300303
inline static std::function<void()> callback = nullptr;
301-
inline static std::string typeName = "";
304+
inline static std::string typeName;
302305
};
303306

304307
struct HelpArgTag {};
@@ -316,7 +319,7 @@ struct HelpArg : HelpArgTag, FlagArgTag, ArgBase<bool, Name, true, ID> {
316319

317320
inline static constexpr NArgs nargs = NArgs(-1);
318321
inline static std::function<void()> callback = nullptr;
319-
inline static std::string typeName = "";
322+
inline static std::string typeName;
320323
};
321324

322325
} // namespace Argo

Argo/ArgoMetaAssigner.cc

+25-31
Original file line numberDiff line numberDiff line change
@@ -53,25 +53,24 @@ constexpr auto tupleAssign(std::tuple<T...>& t, std::span<std::string_view> v,
5353
...);
5454
}
5555

56-
template <class Arguments, class PositionalArgument>
56+
template <class Arguments, class PArg>
5757
struct Assigner {
5858
template <ArgType Head>
5959
static constexpr auto assignOneArg(
6060
std::string_view key, std::span<std::string_view> values) -> bool {
6161
if constexpr (std::derived_from<Head, FlagArgTag>) {
6262
if (!values.empty()) {
63-
if constexpr (std::is_same_v<PositionalArgument, void>) {
63+
if constexpr (std::is_same_v<PArg, std::tuple<>>) {
6464
throw Argo::InvalidArgument(
6565
std::format("Flag {} can not take value", key));
6666
} else {
67-
if (PositionalArgument::assigned) {
67+
if (PArg::assigned) {
6868
throw Argo::InvalidArgument(
6969
std::format("Flag {} can not take value", key));
7070
}
7171
Head::value = true;
7272
Head::assigned = true;
73-
assignOneArg<PositionalArgument>(
74-
std::string_view(PositionalArgument::name), values);
73+
assignOneArg<PArg>(std::string_view(PArg::name), values);
7574
return true;
7675
}
7776
}
@@ -99,21 +98,20 @@ struct Assigner {
9998
}
10099
return true;
101100
}
102-
if constexpr (std::is_same_v<PositionalArgument, void>) {
101+
if constexpr (std::is_same_v<PArg, std::tuple<>>) {
103102
throw Argo::InvalidArgument(
104103
std::format("Argument {} cannot take more than one value got {}",
105104
key,
106105
values.size()));
107106
} else {
108-
if (PositionalArgument::assigned) {
107+
if (PArg::assigned) {
109108
throw Argo::InvalidArgument(std::format(
110109
"Argument {} cannot take more than one value got {}",
111110
key,
112111
values.size()));
113112
}
114113
assignOneArg<Head>(key, values.subspan(0, 1));
115-
assignOneArg<PositionalArgument>(
116-
std::string_view(PositionalArgument::name), values.subspan(1));
114+
assignOneArg<PArg>(std::string_view(PArg::name), values.subspan(1));
117115
return true;
118116
}
119117
} else if constexpr (Head::nargs.getNargsChar() == '*') {
@@ -156,21 +154,20 @@ struct Assigner {
156154
"Argument {} should take exactly one value but zero", key));
157155
}
158156
if (values.size() > 1) {
159-
if constexpr (std::is_same_v<PositionalArgument, void>) {
157+
if constexpr (std::is_same_v<PArg, std::tuple<>>) {
160158
throw Argo::InvalidArgument(
161159
std::format("Argument {} should take exactly one value but {}",
162160
key,
163161
values.size()));
164162
} else {
165-
if (PositionalArgument::assigned) {
163+
if (PArg::assigned) {
166164
throw Argo::InvalidArgument(std::format(
167165
"Argument {} should take exactly one value but {}",
168166
key,
169167
values.size()));
170168
}
171169
assignOneArg<Head>(key, values.subspan(0, 1));
172-
assignOneArg<PositionalArgument>(
173-
std::string_view(PositionalArgument::name), values.subspan(1));
170+
assignOneArg<PArg>(std::string_view(PArg::name), values.subspan(1));
174171
return true;
175172
}
176173
}
@@ -217,27 +214,25 @@ struct Assigner {
217214
key,
218215
Head::nargs.getNargs(),
219216
values.size()));
217+
}
218+
if constexpr (std::is_same_v<PArg, std::tuple<>>) {
219+
throw Argo::InvalidArgument(
220+
std::format("Argument {} should take exactly {} value but {}",
221+
key,
222+
Head::nargs.getNargs(),
223+
values.size()));
220224
} else {
221-
if constexpr (std::is_same_v<PositionalArgument, void>) {
225+
if (PArg::assigned) {
222226
throw Argo::InvalidArgument(
223227
std::format("Argument {} should take exactly {} value but {}",
224228
key,
225229
Head::nargs.getNargs(),
226230
values.size()));
227-
} else {
228-
if (PositionalArgument::assigned) {
229-
throw Argo::InvalidArgument(
230-
std::format("Argument {} should take exactly {} value but {}",
231-
key,
232-
Head::nargs.getNargs(),
233-
values.size()));
234-
}
235-
assignOneArg<Head>(key, values.subspan(0, Head::nargs.getNargs()));
236-
assignOneArg<PositionalArgument>(
237-
std::string_view(PositionalArgument::name),
238-
values.subspan(Head::nargs.getNargs()));
239-
return true;
240231
}
232+
assignOneArg<Head>(key, values.subspan(0, Head::nargs.getNargs()));
233+
assignOneArg<PArg>(std::string_view(PArg::name),
234+
values.subspan(Head::nargs.getNargs()));
235+
return true;
241236
}
242237
}
243238
}
@@ -277,12 +272,11 @@ struct Assigner {
277272

278273
static auto assign(std::string_view key, std::span<std::string_view> values) {
279274
if (key.empty()) {
280-
if constexpr (!std::is_same_v<PositionalArgument, void>) {
281-
if (PositionalArgument::assigned) {
275+
if constexpr (!std::is_same_v<PArg, std::tuple<>>) {
276+
if (PArg::assigned) {
282277
throw InvalidArgument(std::format("Duplicated positional argument"));
283278
}
284-
assignOneArg<PositionalArgument>(
285-
std::string_view(PositionalArgument::name), values);
279+
assignOneArg<PArg>(std::string_view(PArg::name), values);
286280
return;
287281
} else {
288282
throw Argo::InvalidArgument(

Argo/ArgoParser.cc

+15-14
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ export enum class RequiredFlag : bool {
2222
required = true,
2323
};
2424

25-
export using RequiredFlag::required; // NOLINT(misc-unused-using-decls)
26-
export using RequiredFlag::optional; // NOLINT(misc-unused-using-decls)
25+
export using RequiredFlag::required;
26+
export using RequiredFlag::optional;
2727

2828
/*!
2929
* Helper function to create nargs
@@ -46,8 +46,9 @@ struct ParserInfo {
4646
std::optional<std::string_view> positional_argument_help = std::nullopt;
4747
};
4848

49-
export template <ParserID ID = 0, class Args = std::tuple<>, class PArg = void,
50-
class HArg = void, class SubParsers = std::tuple<>>
49+
export template <ParserID ID = 0, class Args = std::tuple<>,
50+
class PArg = std::tuple<>, class HArg = void,
51+
class SubParsers = std::tuple<>>
5152
requires(is_tuple_v<Args> && is_tuple_v<SubParsers>)
5253
class Parser {
5354
private:
@@ -126,13 +127,13 @@ class Parser {
126127
return arg2;
127128
} else {
128129
if constexpr (is_array_v<Type>) {
129-
return NArgs(static_cast<int>(array_len_v<Type>));
130+
return NArgs{static_cast<int>(array_len_v<Type>)};
130131
}
131132
if constexpr (is_vector_v<Type>) {
132-
return NArgs('*');
133+
return NArgs{'*'};
133134
}
134135
if constexpr (is_tuple_v<Type>) {
135-
return NArgs(static_cast<int>(std::tuple_size_v<Type>));
136+
return NArgs{static_cast<int>(std::tuple_size_v<Type>)};
136137
}
137138
return NArgs('?');
138139
}
@@ -154,7 +155,7 @@ class Parser {
154155
return false;
155156
}
156157
}();
157-
if constexpr (!std::is_same_v<PArg, void>) {
158+
if constexpr (!std::is_same_v<PArg, std::tuple<>>) {
158159
static_assert(!(std::string_view(Name) == std::string_view(PArg::name)),
159160
"Duplicated name");
160161
}
@@ -197,7 +198,7 @@ class Parser {
197198
template <ArgName Name, class Type, auto arg1 = Unspecified(),
198199
auto arg2 = Unspecified(), class... T>
199200
auto addPositionalArg(T... args) {
200-
static_assert(std::is_same_v<PArg, void>,
201+
static_assert(std::is_same_v<PArg, std::tuple<>>,
201202
"Positional argument cannot set more than one");
202203
static_assert(Name.shortName == '\0',
203204
"Positional argment cannot have short name");
@@ -208,7 +209,7 @@ class Parser {
208209

209210
template <ArgName Name, class... T>
210211
auto addFlag(T... args) {
211-
if constexpr (!std::is_same_v<PArg, void>) {
212+
if constexpr (!std::is_same_v<PArg, std::tuple<>>) {
212213
static_assert(!(std::string_view(Name) == std::string_view(PArg::name)),
213214
"Duplicated name");
214215
}
@@ -255,7 +256,7 @@ class Parser {
255256
if (!this->parsed_) {
256257
throw ParseError("Parser did not parse argument, call parse first");
257258
}
258-
if constexpr (!std::is_same_v<PArg, void>) {
259+
if constexpr (!std::is_same_v<PArg, std::tuple<>>) {
259260
if constexpr (std::string_view(Name) == std::string_view(PArg::name)) {
260261
return PArg::value;
261262
} else {
@@ -290,7 +291,7 @@ class Parser {
290291
if (!this->parsed_) {
291292
throw ParseError("Parser did not parse argument, call parse first");
292293
}
293-
if constexpr (!std::is_same_v<PArg, void>) {
294+
if constexpr (!std::is_same_v<PArg, std::tuple<>>) {
294295
if constexpr (std::string_view(Name) == std::string_view(PArg::name)) {
295296
return PArg::assigned;
296297
} else {
@@ -343,9 +344,9 @@ class Parser {
343344

344345
public:
345346
auto parse(int argc, char* argv[]) -> void;
346-
std::string formatHelp(bool no_color = false) const;
347+
[[nodiscard]] std::string formatHelp(bool no_color = false) const;
347348

348-
operator bool() const {
349+
explicit operator bool() const {
349350
return this->parsed_;
350351
}
351352
};

0 commit comments

Comments
 (0)