From a364f3f1e7abeb9c7ebd8492d7f2ca2d97274179 Mon Sep 17 00:00:00 2001 From: Stephan van Veen Date: Sat, 11 May 2019 11:35:32 +0200 Subject: [PATCH] Remove code duplication --- include/argparse.hpp | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/include/argparse.hpp b/include/argparse.hpp index 466e7c0..421d8ea 100644 --- a/include/argparse.hpp +++ b/include/argparse.hpp @@ -634,23 +634,11 @@ class ArgumentParser { } // Used by print_help. - size_t get_length_of_longest_argument() { + size_t get_length_of_longest_argument(const std::vector>& aArguments) { size_t tResult = 0; - for (const auto& mPositionalArgument : mPositionalArguments) { + for (const auto& argument : aArguments) { size_t tCurrentArgumentLength = 0; - auto tNames = mPositionalArgument->mNames; - for (size_t j = 0; j < tNames.size() - 1; j++) { - auto tNameLength = tNames[j].length(); - tCurrentArgumentLength += tNameLength + 2; // +2 for ", " - } - tCurrentArgumentLength += tNames[tNames.size() - 1].length(); - if (tCurrentArgumentLength > tResult) - tResult = tCurrentArgumentLength; - } - - for (const auto& mOptionalArgument : mOptionalArguments) { - size_t tCurrentArgumentLength = 0; - auto tNames = mOptionalArgument->mNames; + auto tNames = argument->mNames; for (size_t j = 0; j < tNames.size() - 1; j++) { auto tNameLength = tNames[j].length(); tCurrentArgumentLength += tNameLength + 2; // +2 for ", " @@ -662,6 +650,14 @@ class ArgumentParser { return tResult; } + // Used by print_help. + size_t get_length_of_longest_argument() { + const auto positionalArgMaxSize = get_length_of_longest_argument(mPositionalArguments); + const auto optionalArgMaxSize = get_length_of_longest_argument(mOptionalArguments); + + return std::max(positionalArgMaxSize, optionalArgMaxSize); + } + std::string mProgramName; std::vector mParentParsers; std::vector> mPositionalArguments;