22 #include <boost/program_options.hpp> 28 namespace po = boost::program_options;
47 int argc,
const char *argv[],
const Filesystem &filesystem)
48 : continue_(true), loglevel_(0)
51 po::options_description options(
52 "usage: " + std::string(argv[0]));
54 (
"help,h",
"print this message")
55 (
"config", po::value<std::string>(),
"specify configuration file")
56 (
"ast",
"print AST of configuration file (do not run)")
57 (
"version",
"print version and license information")
58 (
"loglevel", po::value<unsigned int>(),
59 "level of logging, between 0 and 3");
61 po::store(po::parse_command_line(argc, argv, options), vm);
67 std::cout << options << std::endl;
73 if (vm.count(
"version"))
75 std::cout <<
"mavtables (SHAMU Project) ";
76 std::cout <<
"v" << std::to_string(VERSION_MAJOR);
77 std::cout <<
"." << std::to_string(VERSION_MINOR);
78 std::cout <<
"." << std::to_string(VERSION_PATCH) <<
"\n";
79 std::cout <<
"Copyright (C) 2018 Michael R. Shannon\n";
81 std::cout <<
"License: GPL v2.0 or any later version.\n";
82 std::cout <<
"This is free software; see the source for copying " 84 std::cout <<
"There is NO\nwarranty; not even for MERCHANTABILITY or " 86 std::cout <<
"FOR A PARTICULAR PURPOSE." << std::endl;
92 if (vm.count(
"config"))
94 if (filesystem.
exists(vm[
"config"].as<std::string>()))
96 config_file_ = vm[
"config"].as<std::string>();
101 throw std::runtime_error(
102 "mavtables could not locate a configuration file");
114 throw std::runtime_error(
115 "mavtables could not locate a configuration file");
119 if (vm.count(
"loglevel"))
121 loglevel_ = vm[
"loglevel"].as<
unsigned int>();
125 print_ast_ = vm.count(
"ast");
126 run_firewall_ = !print_ast_;
169 return run_firewall_;
179 Options::operator bool()
const 202 if (
auto config_path = std::getenv(
"MAVTABLES_CONFIG_PATH"))
211 if (filesystem.
exists(
".mavtablesrc"))
213 return ".mavtablesrc";
217 if (
auto home = std::getenv(
"HOME"))
220 config_path /=
".mavtablesrc";
222 if (filesystem.
exists(config_path))
224 return config_path.string();
229 if (filesystem.
exists(PREFIX
"/etc/mavtables.conf"))
231 return PREFIX
"/etc/mavtables.conf";
std::optional< std::string > find_config(const Filesystem &filesystem)
TEST_VIRTUAL bool exists(const path &p) const
boost::filesystem::path path
Options(int argc, const char *argv[], const Filesystem &filesystem=Filesystem())
std::string config_file()