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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
//-----------------------------------------------------------------------------
/** @file libboardgame_base/Options.cpp
@author Markus Enzenberger
@copyright GNU General Public License version 3 or later */
//-----------------------------------------------------------------------------
#include "Options.h"
namespace libboardgame_base {
//-----------------------------------------------------------------------------
Options::Options(int argc, const char** argv, const vector<string>& specs)
{
for (auto& s : specs)
{
auto pos = s.find('|');
if (pos == string::npos)
pos = s.find(':');
if (pos != string::npos)
m_names.insert(s.substr(0, pos));
else
m_names.insert(s);
}
bool end_of_options = false;
for (int n = 1; n < argc; ++n)
{
const string arg = argv[n];
if (! end_of_options && arg.compare(0, 1, "-") == 0 && arg != "-")
{
if (arg == "--")
{
end_of_options = true;
continue;
}
string name;
string value;
bool needs_arg = false;
if (arg.compare(0, 2, "--") == 0)
{
// Long option
name = arg.substr(2);
auto sz = name.size();
bool found = false;
for (auto& spec : specs)
if (spec.find(name) == 0
&& (spec.size() == sz || spec[sz] == '|'
|| spec[sz] == ':' ))
{
found = true;
needs_arg = (! spec.empty() && spec.back() == ':');
break;
}
if (! found)
throw OptionError("Unknown option " + arg);
}
else
{
// Short options
for (string::size_type i = 1; i < arg.size(); ++i)
{
auto c = arg[i];
bool found = false;
for (auto& spec : specs)
{
auto pos = spec.find("|" + string(1, c));
if (pos != string::npos)
{
name = spec.substr(0, pos);
found = true;
if (! spec.empty() && spec.back() == ':')
{
// If not last option, no space was used to
// append the value
if (i != arg.size() - 1)
value = arg.substr(i + 1);
else
needs_arg = true;
}
break;
}
}
if (! found)
throw OptionError("Unknown option -" + string(1, c));
if (needs_arg || ! value.empty())
break;
m_map.insert({name, ""});
}
}
if (needs_arg)
{
bool value_found = false;
++n;
if (n < argc)
{
value = argv[n];
if (value.empty() || value[0] != '-')
value_found = true;
}
if (! value_found)
throw OptionError("Option --" + name + " needs value");
}
m_map.insert({name, value});
}
else
m_args.push_back(arg);
}
}
Options::Options(int argc, char** argv, const vector<string>& specs)
: Options(argc, const_cast<const char**>(argv), specs)
{
}
void Options::check_name(const string& name) const
{
if (m_names.count(name) == 0)
throw OptionError("Internal error: invalid option name " + name);
}
bool Options::contains(const string& name) const
{
check_name(name);
return m_map.count(name) > 0;
}
string Options::get(const string& name) const
{
check_name(name);
auto pos = m_map.find(name);
if (pos == m_map.end())
throw OptionError("Missing option --" + name);
return pos->second;
}
string Options::get(const string& name, const string& default_value) const
{
check_name(name);
auto pos = m_map.find(name);
if (pos == m_map.end())
return default_value;
return pos->second;
}
//-----------------------------------------------------------------------------
} // namespace libboardgame_base
|