add a string parser

This commit is contained in:
Jarryd Beck 2014-10-01 17:52:34 +10:00
parent 6f51b4f218
commit 58b63617bd
3 changed files with 34 additions and 1 deletions

View File

@ -141,6 +141,7 @@ Options::parse(int& argc, char**& argv)
if (i + 1 == s.size())
{
checked_parse_arg(argc, argv, current+1, value, name);
++current;
}
else
{
@ -182,6 +183,8 @@ Options::parse(int& argc, char**& argv)
{
//parse the next argument
checked_parse_arg(argc, argv, current + 1, opt, name);
++current;
}
else
{

View File

@ -35,6 +35,21 @@ namespace cxxopts
return false;
}
};
class String : public Value
{
void
parse(const std::string& text, any& result) const
{
result = text;
}
bool
has_arg() const
{
return true;
}
};
}
extern std::basic_regex<char> option_matcher;

View File

@ -31,11 +31,26 @@ int main(int argc, char* argv[])
options.add_options()
("a,apple", "an apple")
("b,bob", "Bob")
("b,bob", "Bob")
("f,file", "File", std::make_shared<cxxopts::values::String>())
;
options.parse(argc, argv);
if (options.count("a"))
{
std::cout << "Saw option a" << std::endl;
}
if (options.count("b"))
{
std::cout << "Saw option b" << std::endl;
}
if (options.count("f"))
{
std::cout << "File = " << boost::any_cast<std::string>(options["f"]) << std::endl;
}
} catch (const std::regex_error& e)
{
std::cout << "regex_error: " << e.what() << std::endl;