cxxopts/src/cxxopts.cpp

159 lines
3.0 KiB
C++
Raw Normal View History

2014-09-27 11:04:58 +04:00
#include "cxxopts.hpp"
namespace cxxopts
{
std::basic_regex<char> option_matcher
("--([[:alpha:]][-_[:alpha:]]+)(=(.*))?|-([a-zA-Z]+)");
std::basic_regex<char> option_specifier
("(([a-zA-Z]),)?([a-zA-Z][-_a-zA-Z]+)");
OptionAdder
Options::add_options()
{
return OptionAdder(*this);
}
OptionAdder&
OptionAdder::operator()
(
const std::string& opts,
2014-09-27 16:04:53 +04:00
const std::string& desc,
std::shared_ptr<const Value> value
2014-09-27 11:04:58 +04:00
)
{
std::match_results<const char*> result;
std::regex_match(opts.c_str(), result, option_specifier);
if (result.empty())
{
throw invalid_option_format_error(opts);
}
const auto& s = result[2];
const auto& l = result[3];
2014-09-27 16:04:53 +04:00
auto option = std::make_shared<OptionDetails>(desc, value);
2014-09-27 11:04:58 +04:00
if (s.length() != 0)
{
2014-09-27 16:04:53 +04:00
auto in = m_options.m_short.insert(std::make_pair(s.str(), option));
2014-09-27 11:04:58 +04:00
2014-09-27 16:04:53 +04:00
if (!in.second)
2014-09-27 11:04:58 +04:00
{
throw option_exists_error(s.str());
}
}
if (l.length() != 0)
{
2014-09-27 16:04:53 +04:00
auto in = m_options.m_long.insert(std::make_pair(l, option));
2014-09-27 11:04:58 +04:00
2014-09-27 16:04:53 +04:00
if (!in.second)
2014-09-27 11:04:58 +04:00
{
throw option_exists_error(l.str());
}
}
return *this;
}
2014-09-27 16:04:53 +04:00
void
Options::parse(int& argc, char**& argv)
{
int current = 1;
std::set<int> consumed;
while (current != argc)
{
std::match_results<const char*> result;
std::regex_match(argv[current], result, option_matcher);
if (result.empty())
{
//handle empty
//if we return from here then it was parsed successfully, so continue
}
else
{
//short or long option?
if (result[4].length() != 0)
{
std::string s = result[4];
for (int i = 0; i != s.size(); ++i)
{
2014-09-28 15:09:21 +04:00
std::string name(1, s[i]);
auto iter = m_short.find(name);
2014-09-27 16:04:53 +04:00
if (iter == m_short.end())
{
2014-09-28 15:41:19 +04:00
throw option_not_exists_exception(name);
2014-09-28 15:09:21 +04:00
//argument not found
2014-09-27 16:04:53 +04:00
}
auto value = iter->second;
2014-09-28 15:09:21 +04:00
//if no argument then just add it
if (!value->has_arg())
2014-09-27 16:04:53 +04:00
{
2014-09-28 15:09:21 +04:00
auto& v = m_parsed[name];
2014-09-29 12:13:21 +04:00
value->parse("", v.value);
++v.count;
2014-09-28 15:09:21 +04:00
}
else
{
//it must be the last argument
if (i + 1 == s.size())
{
}
else
{
//error
2014-09-28 15:41:19 +04:00
throw option_requires_argument_exception(name);
2014-09-28 15:09:21 +04:00
}
2014-09-27 16:04:53 +04:00
}
}
}
else if (result[1].length() != 0)
{
2014-09-28 15:09:21 +04:00
std::string name = result[1];
auto iter = m_long.find(name);
if (iter == m_long.end())
2014-09-27 16:04:53 +04:00
{
2014-09-28 15:41:19 +04:00
throw option_not_exists_exception(name);
2014-09-28 15:09:21 +04:00
}
2014-09-27 16:04:53 +04:00
2014-09-28 15:09:21 +04:00
auto opt = iter->second;
//equals provided for long option?
if (result[3].length() != 0)
{
//parse the option given
}
else
{
if (opt->has_arg())
2014-09-27 16:04:53 +04:00
{
2014-09-28 15:09:21 +04:00
//parse the next argument
}
else
{
//parse with empty argument
2014-09-27 16:04:53 +04:00
}
}
}
}
++current;
}
}
2014-09-27 11:04:58 +04:00
}