add a string parser
This commit is contained in:
parent
6f51b4f218
commit
58b63617bd
@ -141,6 +141,7 @@ Options::parse(int& argc, char**& argv)
|
|||||||
if (i + 1 == s.size())
|
if (i + 1 == s.size())
|
||||||
{
|
{
|
||||||
checked_parse_arg(argc, argv, current+1, value, name);
|
checked_parse_arg(argc, argv, current+1, value, name);
|
||||||
|
++current;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -182,6 +183,8 @@ Options::parse(int& argc, char**& argv)
|
|||||||
{
|
{
|
||||||
//parse the next argument
|
//parse the next argument
|
||||||
checked_parse_arg(argc, argv, current + 1, opt, name);
|
checked_parse_arg(argc, argv, current + 1, opt, name);
|
||||||
|
|
||||||
|
++current;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
@ -35,6 +35,21 @@ namespace cxxopts
|
|||||||
return false;
|
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;
|
extern std::basic_regex<char> option_matcher;
|
||||||
|
|||||||
17
src/main.cpp
17
src/main.cpp
@ -31,11 +31,26 @@ int main(int argc, char* argv[])
|
|||||||
options.add_options()
|
options.add_options()
|
||||||
("a,apple", "an apple")
|
("a,apple", "an apple")
|
||||||
("b,bob", "Bob")
|
("b,bob", "Bob")
|
||||||
("b,bob", "Bob")
|
("f,file", "File", std::make_shared<cxxopts::values::String>())
|
||||||
;
|
;
|
||||||
|
|
||||||
options.parse(argc, argv);
|
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)
|
} 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