default_value
This commit is contained in:
parent
58b63617bd
commit
70d458e90b
@ -3,6 +3,7 @@
|
||||
#include <map>
|
||||
#include <exception>
|
||||
#include <boost/any.hpp>
|
||||
#include <sstream>
|
||||
|
||||
namespace cxxopts
|
||||
{
|
||||
@ -21,7 +22,27 @@ namespace cxxopts
|
||||
|
||||
namespace values
|
||||
{
|
||||
class Boolean : public Value
|
||||
template <typename T>
|
||||
class default_value : public Value
|
||||
{
|
||||
void
|
||||
parse(const std::string& text, any& result) const
|
||||
{
|
||||
T t;
|
||||
std::istringstream is(text);
|
||||
is >> t;
|
||||
result = t;
|
||||
}
|
||||
|
||||
bool
|
||||
has_arg() const
|
||||
{
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
class default_value<bool> : public Value
|
||||
{
|
||||
void
|
||||
parse(const std::string& text, any& result) const
|
||||
@ -36,7 +57,8 @@ namespace cxxopts
|
||||
}
|
||||
};
|
||||
|
||||
class String : public Value
|
||||
template <>
|
||||
class default_value<std::string> : public Value
|
||||
{
|
||||
void
|
||||
parse(const std::string& text, any& result) const
|
||||
@ -52,6 +74,13 @@ namespace cxxopts
|
||||
};
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
std::shared_ptr<Value>
|
||||
value()
|
||||
{
|
||||
return std::make_shared<values::default_value<T>>();
|
||||
}
|
||||
|
||||
extern std::basic_regex<char> option_matcher;
|
||||
|
||||
extern std::basic_regex<char> option_specifier;
|
||||
@ -293,10 +322,11 @@ namespace cxxopts
|
||||
const std::string& opts,
|
||||
const std::string& desc,
|
||||
std::shared_ptr<const Value> value
|
||||
= std::make_shared<values::Boolean>()
|
||||
= ::cxxopts::value<bool>()
|
||||
);
|
||||
|
||||
private:
|
||||
Options& m_options;
|
||||
};
|
||||
|
||||
}
|
||||
|
@ -31,7 +31,7 @@ int main(int argc, char* argv[])
|
||||
options.add_options()
|
||||
("a,apple", "an apple")
|
||||
("b,bob", "Bob")
|
||||
("f,file", "File", std::make_shared<cxxopts::values::String>())
|
||||
("f,file", "File", cxxopts::value<std::string>())
|
||||
;
|
||||
|
||||
options.parse(argc, argv);
|
||||
@ -48,7 +48,8 @@ int main(int argc, char* argv[])
|
||||
|
||||
if (options.count("f"))
|
||||
{
|
||||
std::cout << "File = " << boost::any_cast<std::string>(options["f"]) << std::endl;
|
||||
std::cout << "File = " << boost::any_cast<std::string>(options["f"])
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
} catch (const std::regex_error& e)
|
||||
|
Loading…
Reference in New Issue
Block a user