From 653d7383faeb5c3c92a64001ecfa00ba44825e3a Mon Sep 17 00:00:00 2001 From: Tomi Valkeinen Date: Sun, 25 Oct 2015 21:10:14 +0200 Subject: libkmstest: add Option & OptionSet --- libkmstest/opts.cpp | 117 ++++++++++++++++++++++++++++++++++++++++++++++++++++ libkmstest/opts.h | 38 +++++++++++++++++ 2 files changed, 155 insertions(+) create mode 100644 libkmstest/opts.cpp create mode 100644 libkmstest/opts.h (limited to 'libkmstest') diff --git a/libkmstest/opts.cpp b/libkmstest/opts.cpp new file mode 100644 index 0000000..4ea31f8 --- /dev/null +++ b/libkmstest/opts.cpp @@ -0,0 +1,117 @@ +#include + +#include +#include + +#include "opts.h" + +using namespace std; + +Option::Option(const string& str, function func) + : m_void_func(func) +{ + parse(str); +} + +Option::Option(const string& str, function func) + : m_func(func) +{ + parse(str); +} + +void Option::parse(const string& str) +{ + auto iend = str.end(); + if (*(iend - 1) == '=') { + iend--; + m_has_arg = 1; + } else if (*(iend - 1) == '?') { + iend--; + m_has_arg = 2; + } else { + m_has_arg = 0; + } + + auto isplit = find(str.begin(), iend, '|'); + + if (isplit != str.begin()) + m_short = str[0]; + else + m_short = 0; + + if (isplit != iend) + m_long = string(isplit + 1, iend); +} + +OptionSet::OptionSet(initializer_list