summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/program_options/example/env_options.cpp
blob: bab37e252f2f76a65504a0289ade88d2e4a63f01 (plain)
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
// Copyright Thomas Kent 2016
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt
// or copy at http://www.boost.org/LICENSE_1_0.txt)

#include <boost/program_options.hpp>
namespace po = boost::program_options;
#include <string>
#include <iostream>

std::string mapper(std::string env_var)
{
   // ensure the env_var is all caps
   std::transform(env_var.begin(), env_var.end(), env_var.begin(), ::toupper);

   if (env_var == "PATH") return "path";
   if (env_var == "EXAMPLE_VERBOSE") return "verbosity";
   return "";
}

void get_env_options()
{
   po::options_description config("Configuration");
   config.add_options()
      ("path", "the execution path")
      ("verbosity", po::value<std::string>()->default_value("INFO"), "set verbosity: DEBUG, INFO, WARN, ERROR, FATAL")
      ;

   po::variables_map vm;
   store(po::parse_environment(config, boost::function1<std::string, std::string>(mapper)), vm);
   notify(vm);

   if (vm.count("path"))
   {
      std::cout << "First 75 chars of the system path: \n";
      std::cout << vm["path"].as<std::string>().substr(0, 75) << std::endl;
   }

   std::cout << "Verbosity: " << vm["verbosity"].as<std::string>() << std::endl;
}

int main(int ac, char* av[])
{
   get_env_options();

   return 0;
}