Skip to content

Commit 0d22a41

Browse files
committed
Refactor argengine.cpp
1 parent 3eab197 commit 0d22a41

File tree

1 file changed

+16
-24
lines changed

1 file changed

+16
-24
lines changed

src/argengine.cpp

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ class Argengine::Impl
166166
{
167167
}
168168

169-
bool isMatch(OptionVariants variants) const
169+
bool matches(OptionVariants variants) const
170170
{
171171
for (auto && variant : variants) {
172172
if (this->variants.count(variant)) {
@@ -245,7 +245,7 @@ class Argengine::Impl
245245
OptionDefinitionPtr getOptionDefinition(OptionVariants variants) const
246246
{
247247
for (auto && definition : m_optionDefinitions) {
248-
if (definition->isMatch(variants)) {
248+
if (definition->matches(variants)) {
249249
return definition;
250250
}
251251
}
@@ -265,16 +265,14 @@ class Argengine::Impl
265265
std::pair<std::string, std::string> splitAssignmentFormat(std::string arg) const
266266
{
267267
std::string assignmentFormatArg;
268-
const auto pos = arg.find('=');
269-
if (pos != arg.npos) {
268+
if (const auto pos = arg.find('='); pos != arg.npos) {
270269
assignmentFormatArg = arg.substr(0, pos);
271-
const auto match = getOptionDefinition(assignmentFormatArg);
272-
if (match && match->singleStringCallback) {
273-
const auto valueLength = arg.size() - (pos + 1);
274-
if (!valueLength) {
270+
if (const auto match = getOptionDefinition(assignmentFormatArg); match && match->singleStringCallback) {
271+
if (const auto valueLength = arg.size() - (pos + 1); !valueLength) {
275272
return { assignmentFormatArg, "" };
273+
} else {
274+
return { assignmentFormatArg, arg.substr(pos + 1, valueLength) };
276275
}
277-
return { assignmentFormatArg, arg.substr(pos + 1, valueLength) };
278276
}
279277
}
280278
return {};
@@ -291,15 +289,14 @@ class Argengine::Impl
291289
}
292290
}
293291
if (matchingDefinitions.size() == 1) {
294-
const auto match = matchingDefinitions.begin()->first;
295-
if (match && match->singleStringCallback) {
292+
if (const auto match = matchingDefinitions.begin()->first; match && match->singleStringCallback) {
296293
const auto spacelessArg = matchingDefinitions.begin()->second;
297294
const auto pos = spacelessArg.size();
298-
const auto valueLength = arg.size() - pos;
299-
if (!valueLength) {
295+
if (const auto valueLength = arg.size() - pos; !valueLength) {
300296
return { spacelessArg, "" };
297+
} else {
298+
return { spacelessArg, arg.substr(pos, valueLength) };
301299
}
302-
return { spacelessArg, arg.substr(pos, valueLength) };
303300
}
304301
}
305302
return {};
@@ -310,15 +307,13 @@ class Argengine::Impl
310307
ArgumentVector tokens;
311308

312309
for (auto && arg : args) {
313-
const auto assignmentTokens = splitAssignmentFormat(arg);
314-
if (!assignmentTokens.first.empty()) {
310+
if (const auto assignmentTokens = splitAssignmentFormat(arg); !assignmentTokens.first.empty()) {
315311
tokens.push_back(assignmentTokens.first);
316312
if (!assignmentTokens.second.empty()) {
317313
tokens.push_back(assignmentTokens.second);
318314
}
319315
} else {
320-
const auto spacelessTokens = splitSpacelessFormat(arg);
321-
if (!spacelessTokens.first.empty()) {
316+
if (const auto spacelessTokens = splitSpacelessFormat(arg); !spacelessTokens.first.empty()) {
322317
tokens.push_back(spacelessTokens.first);
323318
if (!spacelessTokens.second.empty()) {
324319
tokens.push_back(spacelessTokens.second);
@@ -339,8 +334,7 @@ class Argengine::Impl
339334

340335
// Process help first as it's a special case
341336
for (size_t i = 1; i < tokens.size(); i++) {
342-
const auto arg = tokens.at(i);
343-
if (const auto definition = getOptionDefinition(arg)) {
337+
if (const auto arg = tokens.at(i); const auto definition = getOptionDefinition(arg)) {
344338
if (definition->isHelp) {
345339
processDefinitionMatch(definition, tokens, i, false);
346340
break;
@@ -350,8 +344,7 @@ class Argengine::Impl
350344

351345
// Other arguments
352346
for (size_t i = 1; i < tokens.size(); i++) {
353-
const auto arg = tokens.at(i);
354-
if (const auto definition = getOptionDefinition(arg)) {
347+
if (const auto arg = tokens.at(i); const auto definition = getOptionDefinition(arg)) {
355348
if (!definition->isHelp) {
356349
i = processDefinitionMatch(definition, tokens, i, dryRun);
357350
}
@@ -379,8 +372,7 @@ class Argengine::Impl
379372
} else if (match->singleStringCallback) {
380373
if (++currentIndex < tokens.size()) {
381374
if (!dryRun) {
382-
const auto value = tokens.at(currentIndex);
383-
if (const auto innerMatch = getOptionDefinition(value)) {
375+
if (const auto value = tokens.at(currentIndex); const auto innerMatch = getOptionDefinition(value)) {
384376
throwNoValueError(*match);
385377
}
386378
match->singleStringCallback(tokens.at(currentIndex));

0 commit comments

Comments
 (0)