From d9bff3dc2b7f06f1054308429253d9dcefbe801d Mon Sep 17 00:00:00 2001 From: Stephan van Veen Date: Wed, 8 May 2019 23:14:27 +0200 Subject: [PATCH] Construct Argument using template pack of names --- include/argparse.hpp | 44 +++++++++++++++----------------------------- 1 file changed, 15 insertions(+), 29 deletions(-) diff --git a/include/argparse.hpp b/include/argparse.hpp index 064d581..551ef64 100644 --- a/include/argparse.hpp +++ b/include/argparse.hpp @@ -89,6 +89,12 @@ class Argument { public: Argument() = default; + template + Argument(Args... args) + : mNames({std::move(args)...}) + , mIsOptional((is_optional(args) || ...)) + {} + Argument& help(const std::string& aHelp) { mHelp = aHelp; return *this; @@ -163,6 +169,10 @@ public: } private: + // If an argument starts with "-" or "--", then it's optional + static bool is_optional(const std::string& aName) { + return (starts_with(aName, "--") || starts_with(aName, "-")); + } // Getter for template types other than std::vector and std::list template @@ -289,17 +299,9 @@ class ArgumentParser { // Parameter packing // Call add_argument with variadic number of string arguments - // TODO: enforce T to be std::string - template - Argument& add_argument(T value, Targs... Fargs) { - std::shared_ptr tArgument = std::make_shared(); - tArgument->mNames.push_back(value); - add_argument_internal(tArgument, Fargs...); - - for (auto& mName : tArgument->mNames) { - if (is_optional(mName)) - tArgument->mIsOptional = true; - } + template + Argument& add_argument(Targs... Fargs) { + std::shared_ptr tArgument = std::make_shared(Fargs...); if (!tArgument->mIsOptional) mPositionalArguments.push_back(tArgument); @@ -470,22 +472,6 @@ class ArgumentParser { } private: - Argument& add_argument_internal(std::shared_ptr aArgument) { - return *aArgument; - } - - template - Argument& add_argument_internal(std::shared_ptr aArgument, T aArgumentName, Targs... Fargs) { - aArgument->mNames.push_back(aArgumentName); - add_argument_internal(aArgument, Fargs...); - return *aArgument; - } - - // If an argument starts with "-" or "--", then it's optional - bool is_optional(const std::string& aName) { - return (starts_with(aName, "--") || starts_with(aName, "-")); - } - // If the argument was defined by the user and can be found in mArgumentMap, then it's valid bool is_valid_argument(const std::string& aName) { std::map>::iterator tIterator = mArgumentMap.find(aName); @@ -546,7 +532,7 @@ class ArgumentParser { } } else { - if (is_optional(argv[i])) { + if (Argument::is_optional(argv[i])) { // This is possibly a compound optional argument // Example: We have three optional arguments -a, -u and -x // The user provides ./main -aux ... @@ -596,7 +582,7 @@ class ArgumentParser { auto tCount = tArgument->mNumArgs - tArgument->mRawValues.size(); while (tCount > 0) { tIterator = mArgumentMap.find(argv[i]); - if (tIterator != mArgumentMap.end() || is_optional(argv[i])) { + if (tIterator != mArgumentMap.end() || Argument::is_optional(argv[i])) { i = i - 1; break; }