jarro2783 / cxxopts

Lightweight C++ command line option parser
MIT License
4.25k stars 590 forks source link

Add OptionValue::as_optional #442

Open TobyBrull opened 3 weeks ago

TobyBrull commented 3 weeks ago

Would be nice to have a function similar to "as" that returns an optional if no value is set. Something like

class OptionValue {
...
  template <typename T>
  std::optional<T>
  as_optional() const
  {
    if (m_value == nullptr) {
      return std::nullopt;
    }
    else {
      return CXXOPTS_RTTI_CAST<const values::standard_value<T>&>(*m_value).get();
    }
  }
...
};
nigels-com commented 3 weeks ago

Is Pull #421 what you're looking for?