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