-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
46 lines (40 loc) · 1.52 KB
/
main.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
#include <boost/program_options.hpp>
namespace po = boost::program_options;
#include <iostream>
#include <string>
int main(int argc, char *argv[]) {
try {
// Define and parse the command line arguments
po::options_description desc("Options");
desc.add_options() // clang-format off
("help", "Print usage information and exit")
("name", po::value<std::string>(), "The name of the person to greet")
; // clang-format on
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
// Print a usage message if the --help flag was specified
if (vm.count("help")) {
std::cout << "Usage: " << argv[0] << " [options]\n\n"
<< "Print a greeting message to the console.\n\n"
<< desc << "\n";
return 0;
}
// If the --name option was specified, use that name, otherwise ask the
// user for his name in the console
std::string name;
if (vm.count("name")) {
name = vm["name"].as<std::string>();
} else {
std::cout << "Please enter your name: ";
std::getline(std::cin, name);
}
// Print the greeting message
std::cout << "Hello, " << name << "!\n";
} catch (std::exception &e) {
// Print any errors we may run into (e.g. unrecognized options)
std::cerr << "error: " << e.what() << std::endl;
return 1;
}
return 0;
}