From d2b68aef66c53466e167441c7b62652d633718d1 Mon Sep 17 00:00:00 2001 From: Mian Bilawal Date: Mon, 19 Mar 2018 13:43:13 +0500 Subject: [PATCH] Fixed exists() and catching of optional arguments --- argparse.hpp | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/argparse.hpp b/argparse.hpp index e62ea15..42a6ab5 100644 --- a/argparse.hpp +++ b/argparse.hpp @@ -42,7 +42,7 @@ typedef std::map IndexMap; class ArgumentParser { private: class Any; - class Argument; + struct Argument; class PlaceHolder; class Holder; typedef std::string String; @@ -301,6 +301,12 @@ class ArgumentParser { .append(active_name), true); active = arguments_[index_[el]]; + + // if the argument is optional and more arguments are not required + if (active.optional && nrequired == 0) + { + variables_[index_[el]].castTo() = "true"; + } // check if we've satisfied the required arguments if (active.optional && nrequired > 0) argumentError(String("encountered optional argument ") @@ -416,7 +422,17 @@ class ArgumentParser { arguments_.clear(); variables_.clear(); } - bool exists(const String& name) const { return index_.count(delimit(name)) > 0; } + bool exists(const String& name) { + for (size_t i = 0; i < arguments_.size(); ++i) + { + // if the argument name is given and we have setted it to true in parse + if (arguments_[i].name == delimit(name) && variables_[i].castTo() == "true") + return true; + } + + return false; + /*return index_.count(delimit(name)) > 0;*/ + } size_t count(const String& name) { // check if the name is an argument if (index_.count(delimit(name)) == 0) return 0;