boost::program_options::parse_environment通过环境变量来设置options

这里需要注意的是需要注意的是:一定要将所需要的环境变量加入到options中。

 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
#include <iostream>
#include <string>
#include <boost/program_options.hpp>

using namespace std;
using namespace boost::program_options;

// compile: g++ boostprogramoption.cpp -o boostprogramoption -lboost_program_options

// envs
const string ENVS[] = {
    "HOME",
    "PKG_CONFIG_PATH",
    "PATH",
    "http_proxy",
};

const int ENVCOUNT = 4;

static std::string name_mapper(const std::string &var_name){
    for (int i = 0; i < ENVCOUNT; ++i) {
        if (var_name == ENVS[i])
            return var_name;
    }

    return "";
}

int main(int argc, char** argv)
{
    // need to pass the buffer size to constructor (e.g. 1024), otherwise:
    // undefined reference to 'boost::program_options::options_description::m_default_line_length'
    options_description desc("Allowed options", 1024);
    desc.add_options()
        ("help,h", "produce help message")
        ("compression,c", value<int>(), "set compression level")

	// need to use the env as option, otherwise exception throwed:
        ("HOME,m", value<string>()->default_value(""), "home dir")
        ("PATH,p", value<string>()->default_value(""), "path")
        ("PKG_CONFIG_PATH,k", value<string>()->default_value(""), "pkg config")
        ("http_proxy", value<string>()->default_value(""), "http proxy");
        ;

    variables_map vm;
    store(parse_command_line(argc, argv, desc), vm);
    store(parse_environment(desc, name_mapper), vm);
    notify(vm);

    if (vm.count("help")) {
        cout << desc << endl;
        return 1;
    }

    if (vm.count("compression")) {
        cout << "Compression level was set to "
             << vm["compression"].as<int>() << endl;
    } else {
        cout << "Compression level was not set." << endl;
    }

    for (int i = 0; i < ENVCOUNT; ++i) {
        if (vm.count(ENVS[i])) {
            cout << ENVS[i] << " is "
                 << vm[ENVS[i]].as<string>() << endl;
        }
    }

    return 0;
}