From e43a2c6cbff82a0635fb102146f471f872b43be8 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Tue, 4 Jun 2019 10:49:14 +0200 Subject: Add return value to option parsing, to make it possible to bail out if an argument is deemed invalid during parsing. --- getoptpp.hpp | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) (limited to 'getoptpp.hpp') diff --git a/getoptpp.hpp b/getoptpp.hpp index 09323a8..8788dde 100644 --- a/getoptpp.hpp +++ b/getoptpp.hpp @@ -9,7 +9,7 @@ namespace dg { -using Handle = std::function; +using Handle = std::function; class Options { public: @@ -40,7 +40,8 @@ class Options { handles[index] = handle; } - bool process(int argc, char* argv[]) { + int process(int argc, char* argv[]) { + int ret = 0; std::string shortopts; for (auto const & option: options) { if (option.flag != nullptr) { @@ -71,17 +72,22 @@ class Options { if (key == -1) { break; } else if (key == '?') { - return false; + return 1; } else if (key == ':') { - return false; + return 1; } else if (key == 0) { // call flag's handle - handles.at(index)(); + ret = handles.at(index)(); } else { // call option's handle - handles.at(key)(); + ret = handles.at(key)(); } } + + if(ret) { + return ret; + } + for (int i = optind; i < argc; ++i) { args.push_back(argv[i]); } @@ -89,7 +95,7 @@ class Options { // remove terminating option options.pop_back(); assert(options.size() == handles.size()); - return true; + return 0; } const std::vector arguments() const { return args; } -- cgit v1.2.3