parsing
This commit is contained in:
parent
5cd3fb221f
commit
a61dd78a57
@ -19,7 +19,8 @@ OptionAdder&
|
||||
OptionAdder::operator()
|
||||
(
|
||||
const std::string& opts,
|
||||
const std::string& desc
|
||||
const std::string& desc,
|
||||
std::shared_ptr<const Value> value
|
||||
)
|
||||
{
|
||||
std::match_results<const char*> result;
|
||||
@ -33,13 +34,13 @@ OptionAdder::operator()
|
||||
const auto& s = result[2];
|
||||
const auto& l = result[3];
|
||||
|
||||
auto option = std::make_shared<OptionDetails>(desc);
|
||||
auto option = std::make_shared<OptionDetails>(desc, value);
|
||||
|
||||
if (s.length() != 0)
|
||||
{
|
||||
auto result = m_options.m_short.insert(std::make_pair(s.str()[0], option));
|
||||
auto in = m_options.m_short.insert(std::make_pair(s.str(), option));
|
||||
|
||||
if (!result.second)
|
||||
if (!in.second)
|
||||
{
|
||||
throw option_exists_error(s.str());
|
||||
}
|
||||
@ -47,9 +48,9 @@ OptionAdder::operator()
|
||||
|
||||
if (l.length() != 0)
|
||||
{
|
||||
auto result = m_options.m_long.insert(std::make_pair(l, option));
|
||||
auto in = m_options.m_long.insert(std::make_pair(l, option));
|
||||
|
||||
if (!result.second)
|
||||
if (!in.second)
|
||||
{
|
||||
throw option_exists_error(l.str());
|
||||
}
|
||||
@ -58,4 +59,66 @@ OptionAdder::operator()
|
||||
return *this;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
auto iter = m_short.find(std::string(1, s[i]));
|
||||
|
||||
if (iter == m_short.end())
|
||||
{
|
||||
}
|
||||
|
||||
auto value = iter->second;
|
||||
|
||||
if (value->has_arg())
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (result[1].length() != 0)
|
||||
{
|
||||
if (result[3].length() != 0)
|
||||
{
|
||||
auto iter = m_long.find(result[3]);
|
||||
|
||||
if (iter == m_long.end())
|
||||
{
|
||||
}
|
||||
|
||||
auto value = iter->second;
|
||||
}
|
||||
|
||||
//equals provided for long option?
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
++current;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -2,9 +2,40 @@
|
||||
#include <set>
|
||||
#include <map>
|
||||
#include <exception>
|
||||
#include <boost/any.hpp>
|
||||
|
||||
namespace cxxopts
|
||||
{
|
||||
using boost::any;
|
||||
|
||||
class Value
|
||||
{
|
||||
public:
|
||||
|
||||
virtual void
|
||||
parse(const std::string& text, any& result) const = 0;
|
||||
|
||||
virtual bool
|
||||
has_arg() const = 0;
|
||||
};
|
||||
|
||||
namespace values
|
||||
{
|
||||
class Boolean : public Value
|
||||
{
|
||||
void
|
||||
parse(const std::string& text, any& result) const
|
||||
{
|
||||
}
|
||||
|
||||
bool
|
||||
has_arg() const
|
||||
{
|
||||
return false;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
extern std::basic_regex<char> option_matcher;
|
||||
|
||||
extern std::basic_regex<char> option_specifier;
|
||||
@ -54,13 +85,32 @@ namespace cxxopts
|
||||
class OptionDetails
|
||||
{
|
||||
public:
|
||||
OptionDetails(const std::string& description)
|
||||
OptionDetails
|
||||
(
|
||||
const std::string& description,
|
||||
std::shared_ptr<const Value> value
|
||||
)
|
||||
: m_desc(description)
|
||||
, m_parser(value)
|
||||
{
|
||||
}
|
||||
|
||||
const std::string&
|
||||
description() const
|
||||
{
|
||||
return m_desc;
|
||||
}
|
||||
|
||||
bool
|
||||
has_arg() const
|
||||
{
|
||||
return m_parser->has_arg();
|
||||
}
|
||||
|
||||
private:
|
||||
boost::any m_value;
|
||||
std::string m_desc;
|
||||
std::shared_ptr<const Value> m_parser;
|
||||
};
|
||||
|
||||
class Options
|
||||
@ -76,7 +126,7 @@ namespace cxxopts
|
||||
private:
|
||||
friend class OptionAdder;
|
||||
|
||||
std::map<char, std::shared_ptr<OptionDetails>> m_short;
|
||||
std::map<std::string, std::shared_ptr<OptionDetails>> m_short;
|
||||
std::map<std::string, std::shared_ptr<OptionDetails>> m_long;
|
||||
};
|
||||
|
||||
@ -93,7 +143,9 @@ namespace cxxopts
|
||||
operator()
|
||||
(
|
||||
const std::string& opts,
|
||||
const std::string& desc
|
||||
const std::string& desc,
|
||||
std::shared_ptr<const Value> value
|
||||
= std::make_shared<values::Boolean>()
|
||||
);
|
||||
|
||||
private:
|
||||
|
@ -33,6 +33,8 @@ int main(int argc, char* argv[])
|
||||
("b,bob", "Bob")
|
||||
;
|
||||
|
||||
options.parse(argc, argv);
|
||||
|
||||
} catch (const std::regex_error& e)
|
||||
{
|
||||
std::cout << "regex_error: " << e.what() << std::endl;
|
||||
|
Loading…
Reference in New Issue
Block a user