Added examples for binding functions and methods
[command.git] / examples / methods.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 class methods to command-line parameters
17  */
18
19 class ExampleClass {
20 public:
21     void _argument(bool a) {
22         std::cout << "Argument: " << a << std::endl;
23     }
24     void _option(std::string a) {
25         std::cout << "Help function " << a << std::endl;
26     }
27     void _void(void) {
28         std::cout << "Void function " << std::endl;
29     }
30 };
31
32 int main(int argc, char **argv)
33 {
34     ExampleClass c;
35
36     try {
37         Command command(argc, argv, {
38             // class method with bool parameter
39             new Argument<bool>("Input values", std::bind(&ExampleClass::_argument, &c, std::placeholders::_1)),
40
41             // class method std::string parameter
42             new Option<std::string>("f", "Optional file", std::bind(&ExampleClass::_option, &c, std::placeholders::_1)),
43
44             // class method without parameter
45             new Option<void>("h", "Help", std::bind(&ExampleClass::_void, &c))
46         });
47
48         /* Code is initialized.
49          *
50          * You can run your main program for example here
51          */
52     }
53     catch(const std::exception & e) {
54         std::cout << e.what() << std::endl;
55     }
56
57     return 0;
58 }