From d95f9d9f143eddd3cfb37dc1f65a2095c93577ca Mon Sep 17 00:00:00 2001 From: Stephan van Veen Date: Sat, 18 May 2019 11:54:06 +0200 Subject: [PATCH] First check for positional, then optional and compound --- include/argparse.hpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/include/argparse.hpp b/include/argparse.hpp index 30e4c2e..3a8cd5c 100644 --- a/include/argparse.hpp +++ b/include/argparse.hpp @@ -204,6 +204,10 @@ public: return (!aName.empty() && aName[0] == '-'); } + static bool is_positional(const std::string& aName) { + return !is_optional(aName); + } + /* * Getter for template non-container types * @throws std::logic_error in case of incompatible types @@ -436,18 +440,17 @@ class ArgumentParser { if (tCurrentArgument == Argument::mHelpOption || tCurrentArgument == Argument::mHelpOptionLong) { throw std::runtime_error("help called"); } - auto tIterator = mArgumentMap.find(tCurrentArgument); - if (tIterator != mArgumentMap.end()) { - auto tArgument = tIterator->second; - it = tArgument->consume(std::next(it), end, tCurrentArgument); - } - else if (!Argument::is_optional(tCurrentArgument)) { + if (Argument::is_positional(tCurrentArgument)) { if (mNextPositionalArgument >= mPositionalArguments.size()) { throw std::runtime_error("Maximum number of positional arguments exceeded"); } auto tArgument = mPositionalArguments[mNextPositionalArgument++]; it = tArgument->consume(it, end); } + else if (auto tIterator = mArgumentMap.find(tCurrentArgument); tIterator != mArgumentMap.end()) { + auto tArgument = tIterator->second; + it = tArgument->consume(std::next(it), end, tCurrentArgument); + } else { // TODO: compound optional arguments ++it; }