| |
|
|
| #include <iostream> |
| #include <filesystem> |
|
|
| #include <mcfp/mcfp.hpp> |
|
|
| int main(int argc, char * const argv[]) |
| { |
| auto &config = mcfp::config::instance(); |
|
|
| |
| |
| |
|
|
| config.init( |
| |
| "usage: example [options] file", |
|
|
| |
| mcfp::make_option("help,h", "Print this help text"), |
| mcfp::make_option("verbose,v", "Verbose level, can be specified more than once to increase level"), |
|
|
| |
| mcfp::make_option<std::string>("config", "Config file to use"), |
| mcfp::make_option<std::string>("text", "The text string to echo"), |
|
|
| |
| mcfp::make_option<int>("a", 1, "first parameter for multiplication"), |
| mcfp::make_option<float>("b", 2.0f, "second parameter for multiplication"), |
|
|
| |
| mcfp::make_option<std::vector<std::string>>("c", "Option c, can be specified more than once"), |
|
|
| |
| mcfp::make_hidden_option("d", "Debug mode") |
| ); |
|
|
| |
| |
| |
| |
|
|
| |
|
|
| std::error_code ec; |
| config.parse(argc, argv, ec); |
| if (ec) |
| { |
| std::cerr << "Error parsing arguments: " << ec.message() << std::endl; |
| exit(1); |
| } |
|
|
| |
|
|
| if (config.has("help") or config.operands().size() != 1) |
| { |
| |
| std::cerr << config << std::endl; |
| exit(config.has("help") ? 0 : 1); |
| } |
|
|
| |
| |
| |
|
|
| config.parse_config_file("config", "example.conf", { "." }, ec); |
| if (ec) |
| { |
| std::cerr << "Error parsing config file: " << ec.message() << std::endl; |
| exit(1); |
| } |
|
|
| |
|
|
| int VERBOSE = config.count("verbose"); |
|
|
| |
|
|
| std::cout << "The first operand is " << config.operands().front() << std::endl; |
|
|
| |
|
|
| auto text = config.get<std::string>("text", ec); |
| if (ec) |
| { |
| std::cerr << "Error getting option text: " << ec.message() << std::endl; |
| exit(1); |
| } |
|
|
| std::cout << "Text option is " << text << std::endl; |
|
|
| |
|
|
| int a = config.get<int>("a"); |
| float b = config.get<float>("b"); |
|
|
| std::cout << "a (" << a << ") * b (" << b << ") = " << a * b << std::endl; |
|
|
| |
|
|
| for (std::string s : config.get<std::vector<std::string>>("c")) |
| std::cout << "c: " << s << std::endl; |
|
|
| return 0; |
| } |