mirror of
https://github.com/KeqingMoe/argparse.git
synced 2025-07-04 23:24:39 +00:00
Remove code duplication
This commit is contained in:
parent
50f2df36aa
commit
a364f3f1e7
@ -634,23 +634,11 @@ class ArgumentParser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Used by print_help.
|
// Used by print_help.
|
||||||
size_t get_length_of_longest_argument() {
|
size_t get_length_of_longest_argument(const std::vector<std::shared_ptr<Argument>>& aArguments) {
|
||||||
size_t tResult = 0;
|
size_t tResult = 0;
|
||||||
for (const auto& mPositionalArgument : mPositionalArguments) {
|
for (const auto& argument : aArguments) {
|
||||||
size_t tCurrentArgumentLength = 0;
|
size_t tCurrentArgumentLength = 0;
|
||||||
auto tNames = mPositionalArgument->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 ", "
|
|
||||||
}
|
|
||||||
tCurrentArgumentLength += tNames[tNames.size() - 1].length();
|
|
||||||
if (tCurrentArgumentLength > tResult)
|
|
||||||
tResult = tCurrentArgumentLength;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const auto& mOptionalArgument : mOptionalArguments) {
|
|
||||||
size_t tCurrentArgumentLength = 0;
|
|
||||||
auto tNames = mOptionalArgument->mNames;
|
|
||||||
for (size_t j = 0; j < tNames.size() - 1; j++) {
|
for (size_t j = 0; j < tNames.size() - 1; j++) {
|
||||||
auto tNameLength = tNames[j].length();
|
auto tNameLength = tNames[j].length();
|
||||||
tCurrentArgumentLength += tNameLength + 2; // +2 for ", "
|
tCurrentArgumentLength += tNameLength + 2; // +2 for ", "
|
||||||
@ -662,6 +650,14 @@ class ArgumentParser {
|
|||||||
return tResult;
|
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::string mProgramName;
|
||||||
std::vector<ArgumentParser> mParentParsers;
|
std::vector<ArgumentParser> mParentParsers;
|
||||||
std::vector<std::shared_ptr<Argument>> mPositionalArguments;
|
std::vector<std::shared_ptr<Argument>> mPositionalArguments;
|
||||||
|
Loading…
Reference in New Issue
Block a user