Added examples for binding functions and methods
[command.git] / examples / functions.cpp
1 #include <iostream>
2 #include <string>
3 #include <functional>
4
5 #include "option.h"
6 #include "argument.h"
7 #include "required.h"
8 #include "multiValue.h"
9 #include "grouped.h"
10 #include "command.h"
11
12 using namespace command;
13
14 /**
15  * @file
16  * @brief Simple example explaining how to bind functions to command-line parameters
17  */
18
19 void argument_function(bool a) {
20     std::cout << "Argument: " << a << std::endl;
21 }
22
23 void option_function(std::string a) {
24     std::cout << "Help function " << a << std::endl;
25 }
26
27 void void_function(void) {
28     std::cout << "Void function " << std::endl;
29 }
30
31 int main(int argc, char **argv)
32 {
33     try {
34         Command command(argc, argv, {
35             // function with bool parameter
36             new Argument<bool>("Input values", argument_function),
37
38             // function with std::string parameter
39             new Option<std::string>("f", "Optional file", option_function),
40
41             // function without parameter
42             new Option<void>("h", "Help", void_function)
43         });
44
45         /* Code is initialized.
46          *
47          * You can run your main program e.g. here
48          */
49     }
50     catch(const std::exception & e) {
51         std::cout << e.what() << std::endl;
52     }
53
54     return 0;
55 }