From eaf888960119de8ce2190df5ffed7325f540cb8d Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rafa=C5=82=20D=C5=82ugo=C5=82=C4=99cki?= Date: Mon, 8 Feb 2016 20:38:58 +0100 Subject: [PATCH] Possibility to handle class method & example --- include/argument.h | 4 ---- include/option.h | 4 ++-- src/main.cpp | 37 +++++++++++++++++++++++++++++++++---- 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/include/argument.h b/include/argument.h index 2cd3c30..bf5fc33 100644 --- a/include/argument.h +++ b/include/argument.h @@ -32,10 +32,6 @@ namespace command { * @param description Description of current Argument * @param function Function used to handle current Argument. */ - Argument(const std::string & description, void (*function)(ParameterType)) - : Parameter(description), Callable(function) { - } - Argument(const std::string & description, std::function function) : Parameter(description), Callable(function) { } diff --git a/include/option.h b/include/option.h index 7e9917b..d9630a7 100644 --- a/include/option.h +++ b/include/option.h @@ -44,7 +44,7 @@ namespace command { * @param description Description of current Option * @param function Function used to handle current Option. */ - Option(const std::string & name, const std::string & description, void (*function)(ParameterType)) + Option(const std::string & name, const std::string & description, std::function function) : Parameter(description), Callable(function), name(name) { } @@ -158,7 +158,7 @@ namespace command { * @param description Description of current Option * @param function Function used to handle current Option. */ - Option(const std::string & name, const std::string & description, void (*function)(void)) + Option(const std::string & name, const std::string & description, std::function function) : Parameter(description), Callable(function), name(name) { } diff --git a/src/main.cpp b/src/main.cpp index 5ad96a2..ac7a626 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,6 @@ #include #include +#include #include "option.h" #include "argument.h" @@ -22,19 +23,47 @@ void void_function(void) { std::cout << "Void function " << std::endl; } +class ExampleClass { +public: + void _argument(bool a) { + argument_function(a); + } + void _option(std::string a) { + option_function(a); + } + void _void(void) { + void_function(); + } +}; + int main(int argc, char *argv[]) { + Class c; + try { Command command(argc, argv, { new Grouped({ - new Required(new MultiValue("-", new Argument("Input values", argument_function))), - new MultiValue(",", new Option("f", "Optional file", option_function)) + new Required( + new MultiValue("-", + new Argument("Input values", std::bind(&ExampleClass::_argument, &c, std::placeholders::_1)) + ) + ), + new MultiValue(",", + new Option("f", "Optional file", std::bind(&ExampleClass::_option, &c, std::placeholders::_1)) + ) }), - new Option("h", "Help", void_function) + new Option("h", "Help", std::bind(&ExampleClass::_void, &c)), + + // just a pure method calling + new Option("v", "version", void_function) }); + + /* ExampleClass is initialized. + * You can run your main program now + */ } catch(const std::exception & e) { std::cout << e.what() << std::endl; } return 0; -} \ No newline at end of file +} -- 2.30.2