Add basic support for default options
This commit is contained in:
parent
9b3d20d611
commit
22ab9836d4
@ -43,8 +43,14 @@ namespace cxxopts
|
||||
virtual void
|
||||
parse(const std::string& text) const = 0;
|
||||
|
||||
virtual void
|
||||
parse() const = 0;
|
||||
|
||||
virtual bool
|
||||
has_arg() const = 0;
|
||||
|
||||
virtual bool
|
||||
has_default() const = 0;
|
||||
};
|
||||
|
||||
class OptionException : public std::exception
|
||||
@ -237,12 +243,24 @@ namespace cxxopts
|
||||
parse_value(text, *m_store);
|
||||
}
|
||||
|
||||
void
|
||||
parse() const
|
||||
{
|
||||
parse_value("", *m_store);
|
||||
}
|
||||
|
||||
bool
|
||||
has_arg() const
|
||||
{
|
||||
return value_has_arg<T>::value;
|
||||
}
|
||||
|
||||
bool
|
||||
has_default() const
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
const T&
|
||||
get() const
|
||||
{
|
||||
@ -256,11 +274,50 @@ namespace cxxopts
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
protected:
|
||||
std::shared_ptr<T> m_result;
|
||||
T* m_store;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
class default_value_default : public default_value<T>
|
||||
{
|
||||
using base_type = default_value<T>;
|
||||
|
||||
public:
|
||||
default_value_default(T value)
|
||||
: base_type(), m_default(value)
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
parse(const std::string& text) const
|
||||
{
|
||||
parse_value(text, *base_type::m_store);
|
||||
}
|
||||
|
||||
void
|
||||
parse() const
|
||||
{
|
||||
*base_type::m_store = m_default;
|
||||
}
|
||||
|
||||
bool
|
||||
has_arg() const
|
||||
{
|
||||
return value_has_arg<T>::value;
|
||||
}
|
||||
|
||||
bool
|
||||
has_default() const
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
T m_default;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
@ -277,6 +334,13 @@ namespace cxxopts
|
||||
return std::make_shared<values::default_value<T>>(&t);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
std::shared_ptr<Value>
|
||||
value_default(const T& t)
|
||||
{
|
||||
return std::make_shared<values::default_value_default<T>>(t);
|
||||
}
|
||||
|
||||
class OptionAdder;
|
||||
|
||||
class OptionDetails
|
||||
@ -312,12 +376,23 @@ namespace cxxopts
|
||||
++m_count;
|
||||
}
|
||||
|
||||
void
|
||||
parse_default()
|
||||
{
|
||||
m_value->parse();
|
||||
++m_count;
|
||||
}
|
||||
|
||||
int
|
||||
count() const
|
||||
{
|
||||
return m_count;
|
||||
}
|
||||
|
||||
const Value& value() const {
|
||||
return *m_value;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
const T&
|
||||
as() const
|
||||
@ -799,6 +874,16 @@ Options::parse(int& argc, char**& argv)
|
||||
++current;
|
||||
}
|
||||
|
||||
for (auto& opt : m_options)
|
||||
{
|
||||
auto& detail = opt.second;
|
||||
auto& value = detail->value();
|
||||
|
||||
if(!detail->count() && value.has_default()){
|
||||
detail->parse_default();
|
||||
}
|
||||
}
|
||||
|
||||
argc = nextKeep;
|
||||
}
|
||||
|
||||
|
||||
@ -38,6 +38,7 @@ int main(int argc, char* argv[])
|
||||
("a,apple", "an apple", cxxopts::value<bool>(apple))
|
||||
("b,bob", "Bob")
|
||||
("f,file", "File", cxxopts::value<std::vector<std::string>>())
|
||||
("o,output", "Output file", cxxopts::value_default<std::string>("a.out"))
|
||||
("positional",
|
||||
"Positional arguments: these are the arguments that are entered "
|
||||
"without an option", cxxopts::value<std::string>())
|
||||
@ -88,6 +89,12 @@ int main(int argc, char* argv[])
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
if (options.count("output"))
|
||||
{
|
||||
std::cout << "Output = " << options["output"].as<std::string>()
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
if (options.count("int"))
|
||||
{
|
||||
std::cout << "int = " << options["int"].as<int>() << std::endl;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user