Commit 3b131411 authored by Dominik Charousset's avatar Dominik Charousset

Support function objects in extract_opts

parent f97dc341
...@@ -159,6 +159,8 @@ public: ...@@ -159,6 +159,8 @@ public:
/// Stores the name of a command line option ("<long name>[,<short name>]") /// Stores the name of a command line option ("<long name>[,<short name>]")
/// along with a description and a callback. /// along with a description and a callback.
struct cli_arg { struct cli_arg {
/// Returns `true` on a match, `false` otherwise.
using consumer = std::function<bool (const std::string&)>;
/// Full name of this CLI argument using format "<long name>[,<short name>]" /// Full name of this CLI argument using format "<long name>[,<short name>]"
std::string name; std::string name;
...@@ -169,8 +171,8 @@ public: ...@@ -169,8 +171,8 @@ public:
/// Auto-generated helptext for this item. /// Auto-generated helptext for this item.
std::string helptext; std::string helptext;
/// Returns `true` on a match, `false` otherwise. /// Evaluates option arguments.
std::function<bool (const std::string&)> fun; consumer fun;
/// Creates a CLI argument without data. /// Creates a CLI argument without data.
cli_arg(std::string name, std::string text); cli_arg(std::string name, std::string text);
...@@ -181,10 +183,17 @@ public: ...@@ -181,10 +183,17 @@ public:
/// Creates a CLI argument appending matched arguments to `dest`. /// Creates a CLI argument appending matched arguments to `dest`.
cli_arg(std::string name, std::string text, std::vector<std::string>& dest); cli_arg(std::string name, std::string text, std::vector<std::string>& dest);
/// Creates a CLI argument using the function object `f`.
cli_arg(std::string name, std::string text, consumer f);
/// Creates a CLI argument for converting from strings, /// Creates a CLI argument for converting from strings,
/// storing its matched argument in `dest`. /// storing its matched argument in `dest`.
template <class T> template <class T>
cli_arg(std::string name, std::string text, T& dest); cli_arg(typename std::enable_if<
detail::type_nr<T>::value != 0,
std::string
>::type name,
std::string text, T& dest);
/// Creates a CLI argument for converting from strings, /// Creates a CLI argument for converting from strings,
/// appending matched arguments to `dest`. /// appending matched arguments to `dest`.
...@@ -405,7 +414,11 @@ inline message make_message() { ...@@ -405,7 +414,11 @@ inline message make_message() {
******************************************************************************/ ******************************************************************************/
template <class T> template <class T>
message::cli_arg::cli_arg(std::string nstr, std::string tstr, T& arg) message::cli_arg::cli_arg(typename std::enable_if<
detail::type_nr<T>::value != 0,
std::string
>::type
nstr, std::string tstr, T& arg)
: name(std::move(nstr)), : name(std::move(nstr)),
text(std::move(tstr)), text(std::move(tstr)),
fun([&arg](const std::string& str) -> bool { fun([&arg](const std::string& str) -> bool {
......
...@@ -340,6 +340,13 @@ message::cli_arg::cli_arg(std::string nstr, std::string tstr) ...@@ -340,6 +340,13 @@ message::cli_arg::cli_arg(std::string nstr, std::string tstr)
// nop // nop
} }
message::cli_arg::cli_arg(std::string name, std::string text, consumer f)
: name(std::move(name)),
text(std::move(text)),
fun(std::move(f)) {
// nop
}
message::cli_arg::cli_arg(std::string nstr, std::string tstr, std::string& arg) message::cli_arg::cli_arg(std::string nstr, std::string tstr, std::string& arg)
: name(std::move(nstr)), : name(std::move(nstr)),
text(std::move(tstr)), text(std::move(tstr)),
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment