-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexample.cpp
83 lines (65 loc) · 2.45 KB
/
example.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <string>
#include <vector>
#include <iostream>
#include "option_handler.h"
void add_options(OptionHandler::Handler & h) {
try {
h.add_option('e', "exists", OptionHandler::NONE, false); }
catch (const std::exception & e) {
std::cerr << e.what() << std::endl; }
try {
h.add_option('h', "help", OptionHandler::NONE, false); }
catch (const std::exception & e) {
std::cerr << e.what() << std::endl; }
// Add some required options
try {
h.add_option('m', "movie", OptionHandler::REQUIRED, false); }
catch (const std::exception & e) {
std::cerr << e.what() << std::endl; }
try {
h.add_option('f', "films", OptionHandler::REQUIRED, true); }
catch (const std::exception & e) {
std::cerr << e.what() << std::endl; }
}
int main(int argc, char *argv[])
{
OptionHandler::Handler h = OptionHandler::Handler(argc, argv);
// None or required options should be used within a try/catch block
add_options(h);
// Add some optional options
h.add_option('s', "song", OptionHandler::OPTIONAL, false);
h.add_option('a', "albums", OptionHandler::OPTIONAL, true);
// get_option returns whether an option was set
bool exists = h.get_option("exists"),
help = h.get_option("help");
// get_argument returns a string passed to an option
std::string song = h.get_argument("song"),
movie = h.get_argument("movie");
// get_arguments returns a vector of arguments (strings) passed to an option
std::vector<std::string> albums = h.get_arguments("albums"),
films = h.get_arguments("films");
// Check if a exists was set
if (exists)
std::cout << "'exists' was set!" << std::endl;
else
std::cout << "'exists' was not set." << std::endl;
// Check if help was set
if (help)
std::cout << "'help' was set!" << std::endl;
else
std::cout << "'help' was not set." << std::endl;
// Print out arguments passed to song and movie
std::cout << "Song:" << std::endl;
std::cout << " " << song << std::endl;
std::cout << "Movie:" << std::endl;
std::cout << " " << movie << std::endl;
// Print out passed albums arguments
std::cout << "Albums:" << std::endl;
for (unsigned int i = 0; i < albums.size(); ++i)
std::cout << " " << albums.at(i) << std::endl;
// Print out passed films arguments
std::cout << "Films:" << std::endl;
for (unsigned int f = 0; f < films.size(); ++f)
std::cout << " " << films.at(f) << std::endl;
return 0;
}