X-Git-Url: https://git.dlugolecki.net.pl/?p=command.git;a=blobdiff_plain;f=examples%2Fmethods.cpp;fp=examples%2Fmethods.cpp;h=9a8735f54e6019ed13ece1795bb0688126545694;hp=0000000000000000000000000000000000000000;hb=5d3d8cfdf0048da0f821c055be2565f9c49e3107;hpb=6029eb4be9fbcda03387e9d013e3f026f08a8341 diff --git a/examples/methods.cpp b/examples/methods.cpp new file mode 100644 index 0000000..9a8735f --- /dev/null +++ b/examples/methods.cpp @@ -0,0 +1,58 @@ +#include +#include +#include + +#include "option.h" +#include "argument.h" +#include "required.h" +#include "multiValue.h" +#include "grouped.h" +#include "command.h" + +using namespace command; + +/** + * @file + * @brief Simple example explaining how to bind class methods to command-line parameters + */ + +class ExampleClass { +public: + void _argument(bool a) { + std::cout << "Argument: " << a << std::endl; + } + void _option(std::string a) { + std::cout << "Help function " << a << std::endl; + } + void _void(void) { + std::cout << "Void function " << std::endl; + } +}; + +int main(int argc, char **argv) +{ + ExampleClass c; + + try { + Command command(argc, argv, { + // class method with bool parameter + new Argument("Input values", std::bind(&ExampleClass::_argument, &c, std::placeholders::_1)), + + // class method std::string parameter + new Option("f", "Optional file", std::bind(&ExampleClass::_option, &c, std::placeholders::_1)), + + // class method without parameter + new Option("h", "Help", std::bind(&ExampleClass::_void, &c)) + }); + + /* Code is initialized. + * + * You can run your main program for example here + */ + } + catch(const std::exception & e) { + std::cout << e.what() << std::endl; + } + + return 0; +}