X-Git-Url: https://git.dlugolecki.net.pl/?p=command.git;a=blobdiff_plain;f=examples%2Ffunctions.cpp;fp=examples%2Ffunctions.cpp;h=df3ec471e10ccbfb96c2f1811832917ef21f6ec7;hp=0000000000000000000000000000000000000000;hb=5d3d8cfdf0048da0f821c055be2565f9c49e3107;hpb=6029eb4be9fbcda03387e9d013e3f026f08a8341 diff --git a/examples/functions.cpp b/examples/functions.cpp new file mode 100644 index 0000000..df3ec47 --- /dev/null +++ b/examples/functions.cpp @@ -0,0 +1,55 @@ +#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 functions to command-line parameters + */ + +void argument_function(bool a) { + std::cout << "Argument: " << a << std::endl; +} + +void option_function(std::string a) { + std::cout << "Help function " << a << std::endl; +} + +void void_function(void) { + std::cout << "Void function " << std::endl; +} + +int main(int argc, char **argv) +{ + try { + Command command(argc, argv, { + // function with bool parameter + new Argument("Input values", argument_function), + + // function with std::string parameter + new Option("f", "Optional file", option_function), + + // function without parameter + new Option("h", "Help", void_function) + }); + + /* Code is initialized. + * + * You can run your main program e.g. here + */ + } + catch(const std::exception & e) { + std::cout << e.what() << std::endl; + } + + return 0; +}