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