1 // Copyright Thomas Kent 2016
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE_1_0.txt
4 // or copy at http://www.boost.org/LICENSE_1_0.txt)
5 
6 #include <boost/program_options.hpp>
7 namespace po = boost::program_options;
8 #include <string>
9 #include <iostream>
10 
mapper(std::string env_var)11 std::string mapper(std::string env_var)
12 {
13    // ensure the env_var is all caps
14    std::transform(env_var.begin(), env_var.end(), env_var.begin(), ::toupper);
15 
16    if (env_var == "PATH") return "path";
17    if (env_var == "EXAMPLE_VERBOSE") return "verbosity";
18    return "";
19 }
20 
get_env_options()21 void get_env_options()
22 {
23    po::options_description config("Configuration");
24    config.add_options()
25       ("path", "the execution path")
26       ("verbosity", po::value<std::string>()->default_value("INFO"), "set verbosity: DEBUG, INFO, WARN, ERROR, FATAL")
27       ;
28 
29    po::variables_map vm;
30    store(po::parse_environment(config, boost::function1<std::string, std::string>(mapper)), vm);
31    notify(vm);
32 
33    if (vm.count("path"))
34    {
35       std::cout << "First 75 chars of the system path: \n";
36       std::cout << vm["path"].as<std::string>().substr(0, 75) << std::endl;
37    }
38 
39    std::cout << "Verbosity: " << vm["verbosity"].as<std::string>() << std::endl;
40 }
41 
main(int ac,char * av[])42 int main(int ac, char* av[])
43 {
44    get_env_options();
45 
46    return 0;
47 }
48