Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support mandatory values (must be set from command line) #7

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
164 changes: 101 additions & 63 deletions include/popl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ friend class OptionParser;
Visibility visibility() const;

virtual Argument argument_type() const = 0;
virtual bool is_set() const = 0;
virtual bool is_mandatory() const = 0;

protected:
virtual void parse(const std::string& what_option, const char* value) = 0;
Expand Down Expand Up @@ -100,7 +102,8 @@ class ValueTemplate : public Option
ValueTemplate(const std::string& short_option, const std::string& long_option, const std::string& description, T* assign_to = nullptr);

unsigned int count() const;
bool is_set() const;
bool is_set() const override;
bool is_mandatory() const override;

void assign_to(T* var);

Expand All @@ -118,15 +121,26 @@ class ValueTemplate : public Option
};


template<class T>
class MandatoryValue : public ValueTemplate<T>
{
public:
MandatoryValue(const std::string& short_option, const std::string& long_option, const std::string& description, T* assign_to = nullptr);

Argument argument_type() const override;
bool is_mandatory() const override;
protected:
void parse(const std::string& what_option, const char* value) override;
void update_reference() override;
};

/// Value option with optional default value
/**
* Value option with optional default value
* If set, it requires an argument
*/
template<class T>
class Value : public ValueTemplate<T>
class Value : public MandatoryValue<T>
{
public:
Value(const std::string& short_option, const std::string& long_option, const std::string& description);
Expand All @@ -137,11 +151,9 @@ class Value : public ValueTemplate<T>
T get_default() const;

T value(size_t idx = 0) const override;
Argument argument_type() const override;

bool is_mandatory() const override;
protected:
void parse(const std::string& what_option, const char* value) override;
void update_reference() override;
void update_reference() override;
std::string to_string() const override;
std::unique_ptr<T> default_;
};
Expand Down Expand Up @@ -333,6 +345,11 @@ inline bool ValueTemplate<T>::is_set() const
return (count() > 0);
}

template<class T>
inline bool ValueTemplate<T>::is_mandatory() const
{
return false;
}

template<class T>
inline void ValueTemplate<T>::assign_to(T* var)
Expand Down Expand Up @@ -398,21 +415,88 @@ inline T ValueTemplate<T>::value(size_t idx) const
return values_[idx];
}

/// MandatoryValue implementation /////////////////////////////////

template<class T>
inline MandatoryValue<T>::MandatoryValue(const std::string& short_option, const std::string& long_option, const std::string& description, T* assign_to) :
ValueTemplate<T>(short_option, long_option, description, assign_to)
{
}

template<class T>
inline bool MandatoryValue<T>::is_mandatory() const
{
return true;
}

template<class T>
inline void MandatoryValue<T>::parse(const std::string& what_option, const char* value)
{
T parsed_value;
std::string strValue;
if (value != nullptr)
strValue = value;

std::istringstream is(strValue);
int valuesRead = 0;
while (is.good())
{
if (is.peek() != EOF)
is >> parsed_value;
else
break;

valuesRead++;
}

if (is.fail())
throw std::invalid_argument("invalid argument for " + what_option + ": '" + strValue + "'");

if (valuesRead > 1)
throw std::invalid_argument("too many arguments for " + what_option + ": '" + strValue + "'");

if (strValue.empty())
throw std::invalid_argument("missing argument for " + what_option);

this->add_value(parsed_value);
}

template<>
inline void MandatoryValue<std::string>::parse(const std::string& what_option, const char* value)
{
if (strlen(value) == 0)
throw std::invalid_argument("missing argument for " + what_option);

add_value(value);
}

template<class T>
inline Argument MandatoryValue<T>::argument_type() const
{
return Argument::required;
}

template<class T>
inline void MandatoryValue<T>::update_reference()
{
if (this->assign_to_)
{
if (this->is_set())
*this->assign_to_ = this->value();
}
}

/// Value implementation /////////////////////////////////

template<class T>
inline Value<T>::Value(const std::string& short_option, const std::string& long_option, const std::string& description) :
ValueTemplate<T>(short_option, long_option, description, nullptr)
MandatoryValue<T>(short_option, long_option, description, nullptr)
{
}


template<class T>
inline Value<T>::Value(const std::string& short_option, const std::string& long_option, const std::string& description, const T& default_val, T* assign_to) :
ValueTemplate<T>(short_option, long_option, description, assign_to)
MandatoryValue<T>(short_option, long_option, description, assign_to)
{
set_default(default_val);
}
Expand Down Expand Up @@ -442,6 +526,11 @@ inline T Value<T>::get_default() const
return *this->default_;
}

template<class T>
inline bool Value<T>::is_mandatory() const
{
return false;
}

template<class T>
inline void Value<T>::update_reference()
Expand All @@ -462,57 +551,6 @@ inline T Value<T>::value(size_t idx) const
return ValueTemplate<T>::value(idx);
}


template<class T>
inline Argument Value<T>::argument_type() const
{
return Argument::required;
}


template<>
inline void Value<std::string>::parse(const std::string& what_option, const char* value)
{
if (strlen(value) == 0)
throw std::invalid_argument("missing argument for " + what_option);

add_value(value);
}


template<class T>
inline void Value<T>::parse(const std::string& what_option, const char* value)
{
T parsed_value;
std::string strValue;
if (value != nullptr)
strValue = value;

std::istringstream is(strValue);
int valuesRead = 0;
while (is.good())
{
if (is.peek() != EOF)
is >> parsed_value;
else
break;

valuesRead++;
}

if (is.fail())
throw std::invalid_argument("invalid argument for " + what_option + ": '" + strValue + "'");

if (valuesRead > 1)
throw std::invalid_argument("too many arguments for " + what_option + ": '" + strValue + "'");

if (strValue.empty())
throw std::invalid_argument("missing argument for " + what_option);

this->add_value(parsed_value);
}


template<class T>
inline std::string Value<T>::to_string() const
{
Expand All @@ -528,9 +566,6 @@ inline std::string Value<T>::to_string() const
return ss.str();
}




/// Implicit implementation /////////////////////////////////

template<class T>
Expand Down Expand Up @@ -797,6 +832,9 @@ inline void OptionParser::parse(int argc, const char * const * argv)
non_option_args_.push_back(arg);
}
}
for (auto& opt : options_)
if (opt->is_mandatory() && !opt->is_set())
throw std::invalid_argument("option \"" + opt->long_option() + "\" must have a value");
}


Expand Down